【问题标题】:IndentationError: unexpected indentIndentationError:意外缩进
【发布时间】:2013-07-10 02:22:10
【问题描述】:

我刚开始用 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 代码有更多失败,请写。

【问题讨论】:

    标签: python-2.7 pygame


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-04
      • 2022-08-20
      • 2011-04-24
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 2018-11-16
      相关资源
      最近更新 更多