Hi here is my error and my python program I have been stuck on this for a week and desperately need some help If anyone knows what the error is and how to solve it, please comment down below! I will be grateful for any help! This is copied from a python tutorial and it is not working
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)
and here is my error
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
According to the given code and error message, the variable "footX" has been referred as global variable but it has not been defined anywhere globally in your code.
This piece of code might make it clear why you are getting this error
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()
Also I have noticed that the footX
variable in your code is getting declared in a if else block and the function using this variable is called later.
So it might be the case that the particular elif
block ( which create footX
variable ) is not executed and this gives error in UpdateFrameImages
function.
The UpdateFrameImages
function uses footX only when showFoot=True
. So initially, showFoot
argument can be set to False when footX
is not defined.
When footX is defined ie in the elif block, then set showFoot = True
when calling UpdateFrameImages
.
NOTE: Also I have changed currentrotation
to currentRotation
since currentrotation
wasn't defined anywhere.
I have made these changes in the while loop as follows, just replace it with your while loop:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.