你好,我对pygame有点新,我写了这段代码:
import sys, pygame
pygame.init()
screen = pygame.display.set_mode((800, 368))
background = pygame.image.load("background.png")
background.convert()
screen.blit(background, (0, 0))
ball = pygame.image.load("ball.bmp")
ballrect = ball.get_rect()
player1 = pygame.image.load("player1.png")
player1rect = player1.get_rect()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.blit(ball, ballrect)
screen.blit(player1, player1rect)
pygame.display.update()
但是当我打开它时它只是说
File "C:\Users\User\Documents\Pong\pong.py", line 20, in <module>
ballrect = ballrect.move(speed)
NameError: name 'speed' is not defined
请帮帮我,我需要它。我无法解决这个问题。
答案 0 :(得分:2)
您没有定义一些变量:speed
,width
,height
。
在循环之前将代码放在以下某行:
speed = [1, 1]
width = 800
height = 368