请看一下我从makegameswithpython.2-raspberrypi第33页复制的以下代码好吗?我在Visual Studio上运行了它,得到的第一个错误是(第73行):
if (playerVX > 0.0 and playerVX < maxSpeed) or
^
SyntaxError: invalid syntax
代码如下:
import pygame, sys
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS
# pygame variables
pygame.init()
windowWidth = 800
windowHeight = 800
surface = pygame.display.set_mode((windowWidth, windowHeight))
pygame.display.set_caption('Pygame Keyboard!')
# square variables
playerSize = 20
playerX = (windowWidth / 2) - (playSize / 2)
playerY = windowHeight - playerSize
playerVX = 1.0
playerVY = 0.0
jumpHeight = 25.0
moveSpeed = 1.0
maxSpeed = 10.0
gravity = 1.0
#keybpard variables
leftDown = False
rightDown = False
haveJumped = False
def move():
global playerX, playerY, playerVX, playerVY, haveJumped, gravity
#move left
if leftDown:
# if we are already moving to the right,reset
#the moving speed and invert the direction
if playerVX > 0.0:
playerVX = moveSpeed
playerVX = -playerVX
# make sure our square does not leave our
#window to the left
if playerX > 0:
playerX += playerVX
#move right
if rightDown:
#if we are already moving to the left,reset
#the moving speed again
if playerVX < 0.0:
playerVX = moveSpeed
#make sure our square does not leave our
#window to the right
if playerX + playerSize < windowWidth:
playerX += playerVX
if playerVY > 1.0:
playerVY = player * 0.9
else:
playerVY = 0.0
haveJumped - False
# is our square in the air?
# better add some gravity to bring it back down
if playerY < windowHeight - playerSize:
playerY += gravity
gravity = gravity * 1.1
else:
playerY = windowHeight - playerSize
gravity = 1.0
playerY -= playerVY
if (playerVX > 0.0 and playerVX < maxSpeed) or
(playerVX < 0.0 and playerVX > -maxSpeed):
if not haveJumped and (leftDown or rightDown)
playerVX = playerVX * 1.1
# how to quit our program
def quitGame():
pygame.quit()
sys.exit()
while True:
surface.fill((0,0,0))
pygame.draw.rect(surface, (255,0,0),
(playerX, playerY, playerSize, playerSize))
#get a list of all enets that happened since
#the last redraw
for event in GAME_EVENTS.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.k_LEFT:
leftDown = True
if event.key == pygame.K_RIGHT:
rightDown = True
if event.key == pygame.K_UP:
if not haveJumped:
haveJumped = True
playerVY += jumpHeight
if event.key == pygame.K_ESCAPE:
quitGame()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
leftDown = False
playerVX = moveSpeed
if event.key == pygame.K_RIGHT:
rightDown = False
playerVX = moveSpeed
if event.type == GAME_GLOBALS.QUIT:
quitGame()
move()
pygame.display.update()
请问我的代码有什么问题吗?我该怎么改变呢?我试图删除表示错误的行并再次键入,而错误仍然存在。你知道吗
你的台词
不是完整的语句,因此需要向Python指示它继续到下一行。可以使用更多的圆括号:
或者用反斜杠
随后,该行
缺少冒号
:
我纠正了你的代码,是的,代码本身是错误的
我把你所有的代码都放在本地,我修改了它,这样它就不会抛出任何错误。我使用的是python3.6和pygame的最新版本。你知道吗
我还留下了注释,上面写着
if playerVY > 1.0
,因为有一个var在实际上下文中不存在,我将它改为另一个var。方块移动到fast,所以我可能会出错,这是我的代码:如果需要,可以使用apschedular(必须使用pip安装的模块)每0.04秒移动一次。你得加上
from apscheduler.schedulers.background import BackgroundScheduler
在顶端和函数move()之后
编辑:
另外,变量没有遵循PEP-8的命名约定。你知道吗
相关问题 更多 >
编程相关推荐