python2.7,与小卡特一起学python,在18章写python乒乓球的程序时,为什么键盘、鼠标都没有动作,球就不会运动,感觉是主循环没有走完,卡在那里,请大神来解答。
主循环代码:
pygame.init()
screen = pygame.display.set_mode([640,480])
background = pygame.Surface(screen.get_size() )
background.fill([255, 255, 255])
clock = pygame.time.Clock()
myBall = MyBallClass('wackyball.png', [10, 5], [50, 50])
ballGroup = pygame.sprite.Group(myBall)
paddle = MyPaddleClass([270, 400])
lives = 3
score = 0
score_font = pygame.font.Font(None, 50)
score_surf = score_font.render(str(score),1,[0,0,0])
score_pos = [10, 10]
pygame.key.set_repeat(100, 50)
done = False
running = True
while running:
screen.blit(background, (0,0))
for event in pygame.event.get():
if not done:
screen.blit(myBall.image, myBall.rect)
screen.blit(paddle.image, paddle.rect)
screen.blit(score_surf, score_pos)
for i in range (lives):
width = screen.get_rect().width
screen.blit(myBall.image, [width - 40 * i, 20])
pygame.display.flip()
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
paddle.move()
if pygame.sprite.spritecollide(paddle, ballGroup, False):
myBall.speed[1] = -myBall.speed[1]
myBall.move()
if myBall.rect.top >= screen.get_rect().bottom:
lives = lives - 1
if lives == 0:
final_text1 = "Game Over"
final_text2 = "Your final score is: " + str(score)
ft1_font = pygame.font.Font(None, 70)
ft1_surf = ft1_font.render(final_text1, 1, (0,0,0))
ft2_font = pygame.font.Font(None, 50)
ft2_surf = ft2_font.render(final_text2, 1, (0,0,0))
screen.blit(ft1_surf, [screen.get_width()/2 - \
ft1_surf.get_width()/2, 100])
screen.blit(ft2_surf, [screen.get_width()/2 - \
ft1_surf.get_width()/2, 200])
pygame.display.flip()
done = True
else:
pygame.time.delay(2000)
myBall.rect.topleft = [(screen.get_rect().width) - 40 * lives, 20]
clock.tick(30)
pygame.quit()