您是否会看一下我使用Python.2-Raspberry Pi,第33页从Make Games复制的以下代码?我在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()
请问我的代码有什么问题吗?我怎么改变它?我试图删除指示错误的行并再次输入,但错误仍然存在。
答案 0 :(得分:3)
你的行
if (playerVX > 0.0 and playerVX < maxSpeed) or
不是一个完整的语句,所以你需要向Python表明它继续到下一行。您可以使用更多括号来执行此操作:
if ((playerVX > 0.0 and playerVX < maxSpeed) or
(playerVX < 0.0 and playerVX > -maxSpeed)):
或使用反斜杠
if (playerVX > 0.0 and playerVX < maxSpeed) or \
(playerVX < 0.0 and playerVX > -maxSpeed):
随后,该行
if not haveJumped and (leftDown or rightDown)
缺少冒号:
答案 1 :(得分:0)
我在本地拥有所有代码,并且我对其进行了更改,以便它不会抛出任何错误。我正在使用python 3.6和最新版本的pygame。
我还在if playerVY > 1.0
处留下了评论,因为在实际上下文中存在var并且我将其更改为另一个var。广场移动到快,所以我可能会犯一个错误,这是我的代码:
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) - (playerSize / 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:
# You tried to find the variable "player" so I guessed you meant playerVY
# You had "playerVY = player * 0.9"
# I don't refactor the code because I'm not sure you wanted what I made.
playerVY = playerVY * 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 *= 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 *= 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
if event.key == pygame.K_RIGHT:
rightDown = False
if event.key in {pygame.K_LEFT, pygame.K_RIGHT}:
playerVX = moveSpeed
if event.type == GAME_GLOBALS.QUIT:
quitGame()
move()
pygame.display.update()
如果需要,可以使用apschedular(一个必须使用pip安装的模块)每隔0.04秒移动一次。你必须补充一下
from apscheduler.schedulers.background import BackgroundScheduler
在顶部和
sched = BackgroundScheduler()
sched.start()
sched.add_job(move, "interval", seconds = 0.04)
在函数move()之后
另一方面,变量不符合PEP-8的命名约定。