![](/img/trans.png)
[英]import error but in the program init, can someone help me? (Python 2.7.14)
[英]Can someone help me find the error in my python program?
嗨,这是我的错误和我的 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,cropHeight,
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,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,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,(250,270))
goalMiddleWidth=goalMiddle.get_rect().width
goalMiddleHeight=goalMiddle.get_rect().height
goalMiddle=cropSurface(goalMiddleWidth,
goalMiddleHeight/2+adjust,
0,
goalMiddleHeight/2-adjust,
goalMiddle)
goalRight=pygame.image.load("26.3_-_goalRight.png").convert_alpha()
goalRight=pygame.transform.scale(goalRight,(250,270))
goalRightWidth=goalRight.get_rect().width
goalRightHeight=goalRight.get_rect().height
goalRight=cropSurface(goalRightWidth/2+adjust,goalRightHeight/2+adjust,0,goalRightHeight/2-adjust,goalRight)
goalStart=(width-goalLeft.get_rect().width-
goalMiddle.get_rect().width-
goalRight.get_rect().width)/2
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))
playerX=width/2
playerY=530
playerXOriginal=playerX
playerYOriginal=playerY
#screen.blit(foot,(0,0))
screen.blit(player,(playerX-player.get_rect().width/2,
playerY-player.get_rect().height/2))
ballX=width/2
ballY=450
radius=playerY-ballY
screen.blit(ball,(ballX-ball.get_rect().width/2,
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",radius,currentRotation)
player=pygame.transform.rotate(playerStart,currentRotation)
playerX=playerXOriginal+changeX
playerY=playerYOriginal-changeY
elif pressedKeys[pygame.K_RIGHT]==1:
if currentRotation<90:
changeX,changeY,currentRotation=movePlayer("Right",radius,currentRotation)
player=pygame.transform.rotate(playerStart,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,(playerX-player.get_rect().width/2,
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,(footX-foot.get_rect().width/2,
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 块中声明,并且稍后调用使用此变量的 function。
因此,可能是特定的elif
块(创建footX
变量)未执行,这会在UpdateFrameImages
中产生错误。
UpdateFrameImages
仅在showFoot=True
时使用 footX。 因此,最初,当footX
时,可以将showFoot
参数设置为 False。
如果在 elif 块中定义了 footX,则在调用UpdateFrameImages
时设置showFoot = True
。
注意:我也将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,changeY,currentRotation=movePlayer("Right",radius,currentRotation)
player=pygame.transform.rotate(playerStart,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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.