我正在尝试对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:UsersGustavDocumentsPongPong.py", line 58
screen.blit(player1, player1rect)
^
TabError: inconsistent use of tabs and spaces in indentation
我不认为它可以加载"Player1Rect"。 因为我不想让"球"和"Player1"碰撞,所以我确实为它们各自做了一个矩形,但它似乎不起作用。 所以总的来说,我不想让错误消失。
缩
进有问题 - 使用制表符或 4 个空格,而不是两者兼而有之。您在第 58 行附近混合了制表符和空格。
我检查了您的代码 - 它工作正常。因此,测试您的缩进或使用可以将制表符更改为 4 个空格(或将制表符更改为 4 个空格)的编辑器