IndentationError:意外缩进

时间:2013-07-09 18:17:50

标签: python-2.7 pygame

我刚开始用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

我需要帮助才能解决此错误。如果我的乒乓码有更多失败请写。

1 个答案:

答案 0 :(得分:1)

ifelif的缩进不匹配(您有标签和空格的混合)。你之前有什么:

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

    elif event.type == KEYDOWN:

这可以解决您的问题:

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