有人可以帮我找到 python 程序中的错误吗? 可能的解决方案:

问题描述

嗨,这是我的错误和我的 python 程序 我已经坚持了一个星期,迫切需要一些帮助 如果有人知道错误是什么以及如何解决它,请在下面评论! 我将不胜感激任何帮助! 这是从python教程复制的,它不起作用

import pygame
import sys
import math
pygame.init()


def cropSurface(newWidth,newHeight,cropWidth,cropHeight,image):
    newSurf=pygame.Surface((newWidth,newHeight),pygame.SRCALPHA,32)
    newSurf.blit(image,(0,0),(cropWidth,newWidth,newHeight))
    return newSurf

def movePlayer(direction,radius,absRot):
    yChange=5
    deltaTheta=int(90/(radius/yChange))
    if direction=="Left":
        deltaTheta*=-1

    finalRot=(absRot+deltaTheta)*math.pi/180

    Hypotenuse=(radius*math.sin(finalRot)/
                (math.sin((math.pi-finalRot)/2)))

    newX=Hypotenuse*math.cos(math.pi/2-(math.pi-finalRot)/2)
    newY=Hypotenuse*math.sin(math.pi/2-(math.pi-finalRot)/2)

    return newX,newY,absRot+deltaTheta
def updateFrameImages(showFoot=False):
    global screen,grassImage,goalLeft,goalMiddle,goalRight
    global ball,player,goalStart,ballX,ballY
    global playerX,playerY
    screen.blit(grassImage,0))
    screen.blit(goalLeft,(goalStart,0))
    screen.blit(goalMiddle,(goalStart+
                        goalLeft.get_rect().width,0))
    screen.blit(goalRight,(goalStart+
                       goalLeft.get_rect().width+
                       goalMiddle.get_rect().width,0))
    if showFoot:
        global foot,footX,footY
        screen.blit(foot,(footX-foot.get_rect().width/2,footY-foot.get_rect().height/2))
        
        screen.blit(ball,(ballX-ball.get_rect().width/2,ballY-ball.get_rect().height/2))
            
        screen.blit(player,(playerX-player.get_rect().width/2,playerY-player.get_rect().height/2))
width=900
height=700
screenDim=(width,height)

screen=pygame.display.set_mode(screenDim)

pygame.display.set_caption("my second game")

grassImage=pygame.image.load("23.2_-_grass.png").convert()
grassImage=pygame.transform.scale(grassImage,(width,height))
screen.blit(grassImage,0))


rescale=3
player=pygame.image.load("24.4_-_characterBody.png").convert_alpha()
playerWidth=player.get_rect().width
playerHeight=player.get_rect().height
player=pygame.transform.scale(player,(playerWidth*rescale,playerHeight*rescale))
player=pygame.transform.rotate(player,90)
playerStart=player
currentRotation=0
playerStart=player
currentRotation=0
foot=pygame.image.load("24.3_-_characterFoot.png").convert_alpha()
footWidth=foot.get_rect().width
footHeight=foot.get_rect().height
foot=pygame.transform.scale(foot,(footWidth*rescale,footHeight*rescale))
foot=pygame.transform.rotate(foot,90)
footStart=foot

rescaleBall=2
ball=pygame.image.load("24.2_-_ball.png").convert_alpha()
ballWidth=ball.get_rect().width
ballHeight=ball.get_rect().height
ball=pygame.transform.scale(ball,(ballWidth*rescaleBall,ballHeight*rescaleBall))



goalLeft=pygame.image.load("25.2_-_goalLeft.png").convert_alpha()
goalLeft=pygame.transform.scale(goalLeft,(250,270))
goalLeftWidth=goalLeft.get_rect().width
goalLeftHeight=goalLeft.get_rect().height
adjust=12
goalLeft=cropSurface(goalLeftWidth/2+adjust,goalLeftHeight/2+adjust,goalLeftWidth/2-adjust,goalLeftHeight/2-adjust,goalLeft)


goalMiddle=pygame.image.load("26.2_-_goalMiddle.png").convert()
goalMiddle=pygame.transform.scale(goalMiddle,270))
goalMiddleWidth=goalMiddle.get_rect().width
goalMiddleHeight=goalMiddle.get_rect().height
goalMiddle=cropSurface(goalMiddleWidth,goalMiddleHeight/2+adjust,goalMiddleHeight/2-adjust,goalMiddle)


goalRight=pygame.image.load("26.3_-_goalRight.png").convert_alpha()
goalRight=pygame.transform.scale(goalRight,270))
goalRightWidth=goalRight.get_rect().width
goalRightHeight=goalRight.get_rect().height

goalRight=cropSurface(goalRightWidth/2+adjust,goalRightHeight/2+adjust,goalRightHeight/2-adjust,goalRight)


goalStart=(width-goalLeft.get_rect().width-
           goalMiddle.get_rect().width-
           goalRight.get_rect().width)/2

screen.blit(goalLeft,0))
screen.blit(goalMiddle,(goalStart+goalLeft.get_rect().width,0))
screen.blit(goalRight,(goalStart+goalLeft.get_rect().width+goalMiddle.get_rect().width,0))


playerX=width/2
playerY=530

playerXOriginal=playerX
playerYOriginal=playerY
#screen.blit(foot,0))
screen.blit(player,playerY-player.get_rect().height/2))
ballX=width/2
ballY=450

radius=playerY-ballY
screen.blit(ball,ballY-ball.get_rect().height/2))

frame=pygame.time.Clock()
finished = False
while finished ==False:
    #a;slkdjf;alskdjf;aslkdjf;a;dlkfj
    for event in pygame.event.get():
        #a;fjallkdjfaa;lakjdf;ala;sdkfja;
        if event.type==pygame.QUIT:
            finished=True
            pygame.quit()
            sys.exit()

    pressedKeys=pygame.key.get_pressed()
    print(pygame.K_LEFT)
    
    if pressedKeys[pygame.K_LEFT]==1:
        if currentRotation>-90:
            changeX,changeY,currentRotation=movePlayer("Left",currentRotation)
            player=pygame.transform.rotate(playerStart,currentRotation)
            playerX=playerXOriginal+changeX
            playerY=playerYOriginal-changeY
        
    elif pressedKeys[pygame.K_RIGHT]==1:
        if currentRotation<90:
            changeX,currentRotation=movePlayer("Right",currentRotation)
            playerX=playerXOriginal+changeX
            playerY=playerYOriginal-changeY
        
    elif pressedKeys[pygame.K_SPACE]==1:
        xMove=(playerX-ballX)/10
        yMove=(playerY-ballY)/10
        normMove=1/math.sqrt(xMove**2+yMove**2)
        distancetoShoulder=20
        shoulderAngle=currentRotation*math.pi/180
        for i in range(3):
            playerX-=xMove
            playerY-=yMove
            updateFrameImages()
            pygame.display.flip()
            frame.tick(30)
        footX=(playerX+distancetoShoulder*math.cos(shoulderAngle)-25*xMove*normMove)
        footY=(playerY-distancetoShoulder*math.sin(shoulderAngle)-25*yMove*normMove)
        foot=pygame.transform.rotate(footStart,currentrotation)
        updateFrameImages()
    #wwwwwwwwww
    updateFrameImages(True)
    pygame.display.flip()
            
    screen.blit(player,playerY-player.get_rect().height/2))
    pygame.display.flip()
    frame.tick(30)

这是我的错误

Traceback (most recent call last):
  File "C:\Users\Divija\Downloads\Udemy Master Python Interactively With PyGame Ultimate Bootcamp_git.ir\5_-_Files_And_Images\py.py",line 191,in <module>
    updateFrameImages(True)
  File "C:\Users\Divija\Downloads\Udemy Master Python Interactively With PyGame Ultimate Bootcamp_git.ir\5_-_Files_And_Images\py.py",line 45,in updateFrameImages
    screen.blit(foot,NameError: name 'footX' is not defined

        

        

解决方法

根据给定的代码和错误消息,变量“footX”已被称为全局变量,但尚未在您的代码中全局定义。

这段代码可能会清楚地说明为什么会出现此错误

footX = "some value" # you get error if this line is commented out


def my_func():
    global footX
    print("here footX is accessed as a global variable with value {}".format(footX))


my_func()

我还注意到您代码中的 footX 变量是在 if else 块中声明的,并且稍后会调用使用此变量的函数。

因此可能是特定的 elif 块(创建 footX 变量)没有执行,这会导致 UpdateFrameImages 函数出错。

可能的解决方案:

UpdateFrameImages 函数仅在 showFoot=True 时使用 footX。因此,最初,当 showFoot 未定义时,footX 参数可以设置为 False。

当定义了 footX 时,即在 elif 块中,然后在调用 showFoot = True 时设置 UpdateFrameImages

注意:此外,我已将 currentrotation 更改为 currentRotation,因为 currentrotation 未在任何地方定义。

我在 while 循环中进行了如下更改,只需将其替换为您的 while 循环:

while finished ==False:
    #a;slkdjf;alskdjf;aslkdjf;a;dlkfj
    for event in pygame.event.get():
        #a;fjallkdjfaa;lakjdf;ala;sdkfja;
        if event.type==pygame.QUIT:
            finished=True
            pygame.quit()
            sys.exit()

    pressedKeys=pygame.key.get_pressed()
    print(pygame.K_LEFT)
    
    if pressedKeys[pygame.K_LEFT]==1:
        if currentRotation>-90:
            changeX,changeY,currentRotation=movePlayer("Left",radius,currentRotation)
            player=pygame.transform.rotate(playerStart,currentRotation)
            playerX=playerXOriginal+changeX
            playerY=playerYOriginal-changeY
        
    elif pressedKeys[pygame.K_RIGHT]==1:
        if currentRotation<90:
            changeX,currentRotation=movePlayer("Right",currentRotation)
            playerX=playerXOriginal+changeX
            playerY=playerYOriginal-changeY
        
    elif pressedKeys[pygame.K_SPACE]==1:
        xMove=(playerX-ballX)/10
        yMove=(playerY-ballY)/10
        normMove=1/math.sqrt(xMove**2+yMove**2)
        distanceToShoulder=20
        shoulderAngle=currentRotation*math.pi/180
        for i in range(3):
            playerX-=xMove
            playerY-=yMove
            updateFrameImages()
            pygame.display.flip()
            frame.tick(30)
        footX=(playerX+distanceToShoulder*math.cos(shoulderAngle)-25*xMove*normMove)
        footY=(playerY-distanceToShoulder*math.sin(shoulderAngle)-25*yMove*normMove)
        foot=pygame.transform.rotate(footStart,currentRotation) #changed 'currentotation' to 'currentRotation'
        updateFrameImages(True) # true beacuse now footX is defined
    #wwwwwwwwww
    updateFrameImages(False) #false since footX not defined initially
    pygame.display.flip()
            
    screen.blit(player,(playerX-player.get_rect().width/2,playerY-player.get_rect().height/2))
    pygame.display.flip()
    frame.tick(30)