或者表示为无效语法,我的代码有什么问题?

2024-04-26 00:13:47 发布

您现在位置:Python中文网/ 问答频道 /正文

请看一下我从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()    

请问我的代码有什么问题吗?我该怎么改变呢?我试图删除表示错误的行并再次键入,而错误仍然存在。你知道吗


Tags: thetokeyeventfalseifourpygame
2条回答

你的台词

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)

缺少冒号:

我纠正了你的代码,是的,代码本身是错误的

我把你所有的代码都放在本地,我修改了它,这样它就不会抛出任何错误。我使用的是python3.6和pygame的最新版本。你知道吗

我还留下了注释,上面写着if playerVY > 1.0,因为有一个var在实际上下文中不存在,我将它改为另一个var。方块移动到fast,所以我可能会出错,这是我的代码:

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的命名约定。你知道吗

相关问题 更多 >