Python 游戏代码错误:NameError: name 'start_game_button_rect' is not defined
我在使用你提供的代码时,当我点击“开始游戏”按钮时,出现了以下错误:
Traceback (most recent call last):
File "D:/Python/Testcode/pythonProject/pnovel.py", line 108, in <module>
handle_event()
File "D:/Python/Testcode/pythonProject/pnovel.py", line 91, in handle_event
if start_game_button_rect.collidepoint(event.pos):
NameError: name 'start_game_button_rect' is not defined
以下是我的代码:
import pygame
import json
pygame.init()
# 设置窗口
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('My Game')
# 加载图片
background_img = pygame.image.load('background.jpg').convert()
character_img = pygame.image.load('character.png').convert_alpha()
dialogue_box_img = pygame.image.load('dialogue_box.png').convert_alpha()
save_button_img = pygame.image.load('save_button.png').convert_alpha()
load_button_img = pygame.image.load('load_button.png').convert_alpha()
# 设置字体
pygame.font.init()
font = pygame.font.SysFont('SimHei', 24)
# 设置状态
game_state = 'menu'
# 设置对话
dialogues = [
{'speaker': '小明', 'content': '欢迎来到游戏世界!'},
{'speaker': '小红', 'content': '这个游戏很好玩!'},
{'speaker': '小刚', 'content': '来试试吧!'}
]
dialogue_index = 0
# 设置进度
progress = {
'dialogue_index': 0
}
# 加载进度
try:
with open('progress.json', 'r') as f:
progress = json.load(f)
except FileNotFoundError:
pass
# 保存进度
def save_progress():
with open('progress.json', 'w') as f:
json.dump(progress, f)
# 绘制菜单界面
def draw_menu():
screen.blit(background_img, (0, 0))
global start_game_button_rect, continue_game_button_rect, cg_gallery_button_rect
start_game_button_rect = pygame.Rect(300, 200, 200, 50)
pygame.draw.rect(screen, (255, 255, 255), start_game_button_rect)
start_game_button_text = font.render('开始游戏', True, (0, 0, 0))
screen.blit(start_game_button_text, (320, 210))
continue_game_button_rect = pygame.Rect(300, 270, 200, 50)
pygame.draw.rect(screen, (255, 255, 255), continue_game_button_rect)
continue_game_button_text = font.render('继续游戏', True, (0, 0, 0))
screen.blit(continue_game_button_text, (320, 280))
cg_gallery_button_rect = pygame.Rect(300, 340, 200, 50)
pygame.draw.rect(screen, (255, 255, 255), cg_gallery_button_rect)
cg_gallery_button_text = font.render('CG画廊', True, (0, 0, 0))
screen.blit(cg_gallery_button_text, (330, 350))
# 绘制对话框
def draw_dialogue():
screen.blit(background_img, (0, 0))
screen.blit(character_img, (50, 50))
screen.blit(dialogue_box_img, (150, 350))
speaker_text = font.render(dialogues[dialogue_index]['speaker'], True, (255, 255, 255))
screen.blit(speaker_text, (180, 370))
content_text = font.render(dialogues[dialogue_index]['content'], True, (255, 255, 255))
screen.blit(content_text, (180, 400))
global save_button_rect, load_button_rect
save_button_rect = pygame.Rect(700, 50, 50, 50)
pygame.draw.rect(screen, (255, 255, 255), save_button_rect)
screen.blit(save_button_img, (700, 50))
load_button_rect = pygame.Rect(700, 120, 50, 50)
pygame.draw.rect(screen, (255, 255, 255), load_button_rect)
screen.blit(load_button_img, (700, 120))
# 处理事件
def handle_event():
global game_state, dialogue_index
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONUP:
if game_state == 'menu':
if start_game_button_rect.collidepoint(event.pos):
game_state = 'game'
dialogue_index = progress['dialogue_index']
elif continue_game_button_rect.collidepoint(event.pos):
game_state = 'game'
dialogue_index = progress['dialogue_index']
elif cg_gallery_button_rect.collidepoint(event.pos):
pass
elif game_state == 'game':
if save_button_rect.collidepoint(event.pos):
progress['dialogue_index'] = dialogue_index
save_progress()
elif load_button_rect.collidepoint(event.pos):
game_state = 'menu'
# 游戏循环
while True:
handle_event()
if game_state == 'menu':
draw_menu()
elif game_state == 'game':
draw_dialogue()
pygame.display.update()
错误分析:
- 变量作用域: 在 Python 中,变量的作用域限制在定义它的函数内部。你在
draw_menu()函数中定义了start_game_button_rect变量,但在handle_event()函数中试图访问它,导致错误。 - 访问全局变量: 要在其他函数中访问变量,需要将其定义为全局变量。
- 正确访问全局变量: 使用
global关键字来声明你要访问的全局变量。例如:
global start_game_button_rect, continue_game_button_rect, cg_gallery_button_rect
解决方案:
- 在
draw_menu()函数中,使用global关键字声明start_game_button_rect,continue_game_button_rect和cg_gallery_button_rect,将它们定义为全局变量。 - 在
draw_dialogue()函数中,使用global关键字声明save_button_rect和load_button_rect,将它们定义为全局变量。
代码修改:
请在代码中添加我提到的 global 关键字声明。
注意:
- 使用全局变量应该谨慎,因为它们可能会导致程序难以维护。如果可能的话,尽量使用函数参数传递和返回值。
- 在代码中,你使用了一些变量,例如
dialogue_index,它们在多个函数中使用,但没有定义为全局变量。为了保证代码的正确性,请根据需要将这些变量也定义为全局变量。
总结:
通过正确地定义全局变量和访问它们,可以解决代码中的“NameError: name 'start_game_button_rect' is not defined”错误。记住,理解变量的作用域和正确使用全局变量是编写 Python 代码的关键。
原文地址: https://www.cveoy.top/t/topic/oSlI 著作权归作者所有。请勿转载和采集!