【发布时间】:2018-06-24 10:47:52
【问题描述】:
我正在尝试让我的播放器在程序中移动,但我不断收到这个我无法识别的错误。 这是错误:
Traceback(最近一次调用最后一次): 文件“C:\Users\1234\AppData\Local\Programs\Python\Python36-32\My First game ERROR.py”,第 39 行,在 游戏()。主(屏幕) 文件“C:\Users\1234\AppData\Local\Programs\Python\Python36-32\My First game ERROR.py”,第 19 行,在 main 图像_x += 1 UnboundLocalError:赋值前引用了局部变量“image_x”
这是代码:
# This just imports all the Pygame modules
import pygame
class Game(object):
def main(self, screen):
clock = pygame.time.Clock()
image = pygame.image.load('Sprite-01.png')
while 1:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return
image_x += 1
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
image_x -= 10
if key[pygame.K_RIGHT]:
image_x += 10
if key[pygame.K_UP]:
image_y -= 10
if key[pygame.K_DOWN]:
image_y += 10
screen.fill((200, 200, 200))
screen.blit(image, (320, 240))
pygame.display.flip()
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((640, 480))
Game().main(screen)
【问题讨论】: