This is a screen grab of my code and the issue i am having
这是我第一次发布问题,并且从所有其他查询中我发现我似乎无法运行此代码。从我阅读的内容来看,我认为这是一个空白问题,因为此代码是为学校项目复制粘贴的。我似乎无法弄清楚,真的需要任何建议。谢谢。
import pygame #provides what we need to make a gameimport sys #gives us the
sys.exit function to close our programimport random # can generate random
positions for the pong ballfrom pygame.locals import*
pygame.init()
gameSurface = pygame.display.set_mode((450,450))# open a new window width =
440, height=480
pygame.display.set_caption('Gina Cooper')# set the title of the window
pygame.mouse.set_visible(0)
GREEN=(0,200,0)
BLUE=(0,0,128)
PURPLE=(102,0,102)
WHITE=(255,255,255)
rect1x=20
rect1y=100
rect2x=400
rect2y=100#Draw the game surface, two rectangles. One at position 20, 100, 30
pixes wide and 150 tall#The other rectangle and 400, 100, with the same
measurements
gameSurface.fill(WHITE)
pygame.draw.rect(gameSurface, GREEN,(rect1x, rect1y,30,150))
pygame.draw.rect(gameSurface, GREEN,(rect2x, rect2y,30,150))#put the ball on a
random place on the screen
ballx=random.randint(200,300)
bally=random.randint(100,150)
pygame.draw.circle(gameSurface, BLUE,(ballx, bally),20)
pygame.display.update()
FPS=20
fpsClock=pygame.time.Clock()
pygame.key.set_repeat(1,1)#This will allow you to press and hold keys on the
keyboard#game loopwhileTrue:foreventin pygame.event.get():
oldballx=ballx
ifevent.type==KEYDOWN:#pressing q will quit the programifevent.key==K_q:
pygame.quit()
sys.exit()ifevent.key==K_RIGHT:#The right arrow key
ballx=ballx+1#This moves the ball. Turn the old ball location white
then create a new ball at the new location simulating movement
pygame.draw.circle(gameSurface, WHITE,(oldballx, bally),20)
pygame.draw.circle(gameSurface, BLUE,(ballx,
bally),20)elifevent.key==K_LEFT:#the left arrow key
ballx=ballx-1
pygame.draw.circle(gameSurface, WHITE,(oldballx, bally),20)
pygame.draw.circle(gameSurface, BLUE,(ballx, bally),20)if
ballx==70:
pygame.draw.rect(gameSurface, PURPLE,(rect1x, rect1y,30,150))# If the
ball reaches the rectangle turn it purpleif ballx==380:
ball reaches the rectangle on the right side turn it purple
pygame.display.update()
fpsClock.tick(FPS)
pygame.display.update()
答案 0 :(得分:1)
您需要确保代码对空格的使用是一致的,Python要求每个代码块都必须一致地缩进。
错误似乎在屏幕快照中,该错误消息位于该位置。看来您可能粘贴了错误的代码。看来..foreventin pygame.event...
实际上是代码的一部分,而不是注释的一部分,这导致后面的行被错误地缩进。