Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 或者被指示为无效语法,我的代码有什么问题?_Python_Pygame - Fatal编程技术网

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

Python 或者被指示为无效语法,我的代码有什么问题?,python,pygame,Python,Pygame,请看一下下面的代码,这些代码是我从makegameswithpython.2-raspberrypi第33页复制的?我在Visual Studio上运行了它,得到的第一个错误是(第73行): if(playerVX>0.0和playerVX0.0: playerVX=移动速度 playerVX=-playerVX #确保我们的广场不会离开我们的广场 #左边的窗户 如果playerX>0: playerX+=playerVX #右移 如果右下: #如果我们已经向左移动,请重置 #移动速度又变快了

请看一下下面的代码,这些代码是我从makegameswithpython.2-raspberrypi第33页复制的?我在Visual Studio上运行了它,得到的第一个错误是(第73行):

if(playerVX>0.0和playerVX
代码如下:

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()    
import pygame,sys
将pygame.locals导入为GAME\u GLOBALS
将pygame.event导入为游戏\事件
#pygame变量
pygame.init()
窗宽=800
窗高=800
surface=pygame.display.set_模式((窗口宽度、窗口高度))
pygame.display.set_标题('pygame键盘!')
#平方变量
playerSize=20
playerX=(窗口宽度/2)-(播放大小/2)
playerY=窗口高度-playerSize
playerVX=1.0
playerVY=0.0
跳跃高度=25.0
移动速度=1.0
最大速度=10.0
重力=1.0
#键盘变量
leftDown=False
右下=假
havejump=False
def move():
全球playerX,playerY,playerVX,playerVY,haveJumped,重力
#向左移动
如果左下:
#如果我们已经向右移动,请重置
#移动速度和反转方向
如果playerVX>0.0:
playerVX=移动速度
playerVX=-playerVX
#确保我们的广场不会离开我们的广场
#左边的窗户
如果playerX>0:
playerX+=playerVX
#右移
如果右下:
#如果我们已经向左移动,请重置
#移动速度又变快了
如果playerVX<0.0:
playerVX=移动速度
#确保我们的广场不会离开我们的广场
#右边的窗户
如果playerX+playerSize<窗口宽度:
playerX+=playerVX
如果playerVY>1.0:
playerVY=player*0.9
其他:
playerVY=0.0
哈维-错
#我们的广场在空中吗?
#最好加点重力把它降下来
如果playerY0.0且playerVX-maxSpeed):
如果没有跳转和(左下或右下)
playerVX=playerVX*1.1
#如何退出我们的计划
def quitGame():
pygame.quit()
sys.exit()
尽管如此:
表面填充((0,0,0))
pygame.draw.rect(曲面,(255,0,0),
(playerX,playerY,playerSize,playerSize))
#获取自
#最后一次重画
对于GAME_EVENTS.get()中的事件:
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.k_左:
leftDown=True
如果event.key==pygame.K_RIGHT:
右下=真
如果event.key==pygame.K_UP:
如果没有:
正确的
playerVY+=跳跃高度
如果event.key==pygame.K_退出:
quitGame()
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_左:
leftDown=False
playerVX=移动速度
如果event.key==pygame.K_RIGHT:
右下=假
playerVX=移动速度
如果event.type==GAME\u GLOBALS.QUIT:
quitGame()
移动()
pygame.display.update()
请问我的代码有什么问题吗?我该如何改变它?我已尝试删除指示错误的行并再次键入,而错误仍然存在。

您的行

if (playerVX > 0.0 and playerVX < maxSpeed) or
缺少冒号:

我更正了您的代码,是的,代码本身是错误的。 我将您的所有代码都放在本地,并对其进行了更改,这样它就不会抛出任何错误。我正在使用Python3.6和pygame的最新版本

我还留下了注释,上面写着
if playerVY>1.0
,因为有一个变量在实际上下文中不存在,我将其更改为另一个变量。方块移动到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()
函数move()之后

编辑:
另外,变量没有遵循PEP-8的命名约定。

非常感谢!我只是添加了更多的括号,新的错误是:第75行如果没有跳转和(leftDown或rightDown)语法错误:无效的语法,我应该如何处理它呢?我是一个初学者,刚刚从书中复制了代码,很抱歉这些低水平questions@ruihuiliu
如果没有跳转和(leftDown或rightDown)
行缺少冒号。非常感谢!你的建议对我帮助很大!我只是检查了代码,并根据错误指示重新编写了代码,但它显示了一个白色屏幕窗口并通知了新的错误,我认为代码本身可能有一些问题,我正在考虑更改一本新书
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)
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()
sched = BackgroundScheduler()
sched.start()
sched.add_job(move, "interval", seconds = 0.04)