-1

你好我对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

请帮助我,我需要它。我无法自己解决这个问题。

4

1 回答 1

2

您没有定义一些变量:speed, width, height.

在 while 循环之前的某处放置如下代码:

speed = [1, 1]
width = 800
height = 368
于 2013-07-10T09:23:25.280 回答