-1

我正在尝试对 Pong 进行编程,到目前为止:

import pygame, sys, time
from pygame.locals import *
pygame.init()

FPS=30
fpsClock=pygame.time.Clock()

size = width, height = 1280, 648
speed = [8, 8]
DISPLAYSURF=pygame.display.set_mode((size),0,32)
background=pygame.image.load('bg.png')
screen = pygame.display.set_mode(size)

UP='up'
DOWN='down'

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

player1 = pygame.image.load('player1.png')
player1rect = player1.get_rect()
player1x=14
player1y=323
direction=None

def move(direction, player1, player1x, player1y):
    if direction:
        if direction == K_UP:
            player1y-=5
        elif direction == K_DOWN:
            player1y+=5
    return player1, player1x, player1y

while True:
    DISPLAYSURF.blit(background,(0,0))

    DISPLAYSURF.blit(player1,(player1x,player1y))

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

        if event.type == KEYDOWN:
            direction = event.key
        if event.type == KEYUP:
            if (event.key == direction):
                direction = None
    player1, player1x, player1y = move(direction, player1, player1x, player1y)

    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()
    fpsClock.tick(FPS)

但是当我试图在 CMD 中打开它时,会出现此错误

  File "C:\Users\Gustav\Documents\Pong\Pong.py", line 58
    screen.blit(player1, player1rect)
                                    ^
TabError: inconsistent use of tabs and spaces in indentation

我不认为它可以加载“player1rect”。因为我不想让 'ball' 和 'player1' 发生碰撞,所以我确实为它们分别做了一个矩形,但它似乎不起作用。所以总而言之,我不想让错误消失。

4

1 回答 1

0

这是缩进的问题 - 使用制表符或 4 个空格,而不是两者。您在第 58 行附近混合了制表符和空格。

我检查了你的代码 - 它工作正常。所以测试你的缩进或使用可以将制表符更改为 4 个空格(或 4 个空格为制表符)的编辑器

于 2013-11-04T20:47:41.533 回答