0

我刚开始用 python 编程,我写了这段代码:

import sys, pygame
pygame.init()

size = width, height = 800, 600
speed = [2, 2]
black = 1, 1, 1

screen = pygame.display.set_mode(size)

ball = pygame.image.load("ball.bmp")
ballrect = ball.get_rect()
player1 = pygame.image.load("player1.png")
player1rect = player1.get_rect()

mod_x = mod_y = 0

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

        elif event.type == KEYDOWN:
            if event.key == K_W
                movex = 2
            if event.key == K_S
                movex = -2

    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.fill(black)
    screen.blit(ball, ballrect)
    screen.blit(player1, player1rect)
    pygame.display.flip()

但如果我试图运行它,它只会说:

     File "C:\Users\User\Documents\Pong\pong.py", line 22
    elif event.type == KEYDOWN:
    ^
IndentationError: unexpected indent

我需要帮助来解决这个错误。如果我的 pong 代码有更多失败,请写。

4

1 回答 1

1

ifelif不匹配的缩进(您混合了制表符和空格)。你之前拥有的:

if event.type == pygame.QUIT: sys.exit()

    elif event.type == KEYDOWN:

这应该可以解决您的问题:

if event.type == pygame.QUIT: 
    sys.exit()
elif event.type == KEYDOWN:
    ...
于 2013-07-09T18:22:10.633 回答