Pygame local variable referenced before assignment - python

I want to make a rectangle to run using keys but I take an error on line 34:
UnboundLocalError: local variable 'x' referenced before assignment
I can't fix that. Please help me.
Here is my code:
import pygame
import sys
from pygame.locals import *
fps = 30
fpsclock = pygame.time.Clock()
w = 640
h = 420
blue = (0, 0, 255)
white = (255, 255, 255)
x = w / 3
y = 350
boxa = 20
movex = 0
def drawwindow():
global screen
pygame.init()
screen = pygame.display.set_mode((w, h))
screen.fill(blue)
def drawbox(box):
if box.right > (w - boxa):
box.right = (w - boxa)
if box.left < 0:
box.left = 0
pygame.draw.rect(screen, white, box)
def main():
drawwindow()
box1 = pygame.Rect(x, y, boxa, boxa)
drawbox(box1)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_RIGHT:
movex = +4
if event.key == K_LEFT:
movex = -4
if event.type == KEYUP:
if event.key == K_RIGHT:
movex = 0
if event.key == K_LEFT:
movex = 0
x += movex
pygame.display.update()
fpsclock.tick(fps)
if __name__ == '__main__':
main()

The name x is in the global scope. Therefore, in order to modify its value inside the function main, you need to declare it as being global with global:
def main():
global x
...
x += movex
Note that you only need to do this if you modify a global. Accessing their values works just fine.

Related

How do I continuously loop KEYDOWN in pygame?

I'm new to python/pygame and I can't figure this out. Whenever I press and hold a key it won't loop the KEYDOWN. Also, if I hold the key on my keyboard down and move the mouse at the same time, it seems to move continuously.
Can someone tell me what I'm doing wrong?
import pygame
import random
pygame.init()
#Colors
white = 255, 255, 255
black = 0, 0, 0
back_color = 48, 255, 124
light_color = 34, 155, 78
#Display W/H
display_width = 800
display_height = 600
#X/Y
x_axis = 400
y_axis = 580
Block_size = 20
x_int = 0
y_int = 0
ON = True
Window = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Game')
#On Loop
while ON == True:
#Screen Event
for Screen in pygame.event.get():
#Quit Screen
if Screen.type == pygame.QUIT:
pygame.quit()
exit()
#Quit Full Screen
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_q:
pygame.quit()
exit()
#Full Screen !!!!!!!! EDIT THIS !!!!!!!!
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_1:
pygame.display.set_mode((display_width, display_height),pygame.FULLSCREEN)
if Screen.key == pygame.K_2:
pygame.display.set_mode((display_width, display_height))
#Player Movement K DOWN
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_d:
x_int = 20
if Screen.key == pygame.K_a:
x_int = -20
#Player Movement K UP
if Screen.type == pygame.KEYUP:
if Screen.key == pygame.K_d or Screen.key == pygame.K_a:
x_int = 0
x_axis += x_int
Window.fill((back_color))
Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])
pygame.display.update()
quit()
I have improved your code. You have placed the screen update (drawing the screen) portion in the events loop whereas it should be in the while loop. The code I have mode is a bit complex but works as expected. Why it is complex? When the key is held down, then the events list is empty (you can print the events). I have also made the block not to go out of the screen. The speed of the block was high so I decreased it to 10.
import pygame
import random
pygame.init()
#Colors
white = 255, 255, 255
black = 0, 0, 0
back_color = 48, 255, 124
light_color = 34, 155, 78
#Display W/H
display_width = 800
display_height = 600
#X/Y
x_axis = 400
y_axis = 580
global x_int
Block_size = 20
x_int = 0
y_int = 0
ON = True
Window = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Game')
global topass_a,topass_d
x_int,topass_a,topass_d = 0,0,0
#On Loop
while ON:
#Screen Event
events = pygame.event.get()
def on_a_press():
global topass_a,x_int
x_int = -10
topass_a = 1
def on_d_press():
global topass_d,x_int
x_int = 10
topass_d = 1
if len(events) == 0:
if topass_a == 1:on_a_press()
if topass_d == 1:on_d_press()
for Screen in events:
if Screen.type == pygame.QUIT:
pygame.quit()
exit()
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_q:
pygame.quit()
exit()
if Screen.key == pygame.K_1:
pygame.display.set_mode((display_width, display_height),pygame.FULLSCREEN)
if Screen.key == pygame.K_2:
pygame.display.set_mode((display_width, display_height))
if Screen.key == pygame.K_d or topass_d == 1:
on_d_press()
if Screen.key == pygame.K_a or topass_a == 1:
on_a_press()
if Screen.type == pygame.KEYUP:
if Screen.key == pygame.K_d or Screen.key == pygame.K_a:
x_int = 0
topass_a = 0
topass_d = 0
x_axis += x_int
x_int = 0
if x_axis < 0:x_axis=0
elif x_axis >= display_width-Block_size:x_axis = display_width-Block_size
Window.fill((back_color))
Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])
pygame.display.update()
You can further improve the code as you need.
Edit:
Why complex? Easy things come first. I have realized that there is no need to track the keys. pygame.key.get_pressed() returns the pressed key. Here is a smaller , better and improved code. I have also implemented the w and s (y_axis) keys.
import pygame
import random
pygame.init()
#Colors
white = 255, 255, 255
black = 0, 0, 0
back_color = 48, 255, 124
light_color = 34, 155, 78
#Display W/H
display_width = 800
display_height = 600
#X/Y
x_axis = 400
y_axis = 580
Block_size = 20
x_int = 0
y_int = 0
ON = True
Window = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Game')
while ON:
events = pygame.event.get()
for Screen in events:
if Screen.type == pygame.QUIT:
pygame.quit()
exit()
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_q:
pygame.quit()
exit()
if Screen.key == pygame.K_1:
pygame.display.set_mode((display_width, display_height),pygame.FULLSCREEN)
if Screen.key == pygame.K_2:
pygame.display.set_mode((display_width, display_height))
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
x_int = -10
if keys[pygame.K_d]:
x_int = 10
# keys controlling y axis, you can remove these lines
if keys[pygame.K_w]:
y_int = -10
if keys[pygame.K_s]:
y_int = 10
#x_axis......
x_axis += x_int
x_int = 0
if x_axis < 0:x_axis=0
elif x_axis >= display_width-Block_size:x_axis = display_width-Block_size
#y axis
y_axis += y_int
y_int = 0
if y_axis < 0:y_axis=0
elif y_axis >= display_height-Block_size:y_axis = display_height-Block_size
#updaing screen
Window.fill((back_color))
Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])
pygame.display.update()
You only receive pygame.KEYDOWN when the key is first pressed - not while it is held down. The simple solution is to only draw while the key is down (ie. when x_int != 0)
#On Loop
while ON == True:
#Screen Event
for Screen in pygame.event.get():
# <Removed not relevant code for brevity>
#Player Movement K DOWN
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_d:
x_int = 20
if Screen.key == pygame.K_a:
x_int = -20
#Player Movement K UP
if Screen.type == pygame.KEYUP:
if Screen.key == pygame.K_d or Screen.key == pygame.K_a:
x_int = 0
# Render only happens if x_int is not zero
# (Need to add code to force render first time)
if x_int:
x_axis += x_int
Window.fill((back_color))
Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])
pygame.display.update()
As the program grows and gets more complex, you'll need better logic for when to render, but this will get you started.

getting an int from a list (pygame)

i am trying to make a 'snake'-like game in python.
My problem right now is that I can't use the defined values from my 'coinlist' in my for-loop. This is the error that i receive if i execute it: TypeError: 'int' object is not subscriptable. Thanks for your help.
import pygame
import random
pygame.init()
rot = (255,0,0)
grün = (0,255,0)
blau = (0,0,255)
gelb = (255,255,0)
schwarz = (0,0,0)
weiß = (255,255,255)
uhr = pygame.time.Clock()
display = pygame.display.set_mode((800, 600))
pygame.display.set_mode((800, 600))
pygame.display.set_caption('Snake')
display.fill(weiß)
def arialmsg(msg, color, x, y, s):
header = pygame.font.SysFont("Arial", s)
text = header.render(msg, True, color)
display.blit(text, [x, y])
def mainloop():
gameExit = False
start = False
movex = 400
movey = 300
changex = 0
changey = -2
rx = random.randrange(10, 790)
ry = random.randrange(10, 590)
snakelist = []
snakelenght = 20
#coinlist defined here:
coinlist = []
while start == False:
display.fill(schwarz)
arialmsg('Snake', grün, 350, 200, 25)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
start = True
pygame.display.flip()
#gameloop:
while gameExit == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
changey = 2
changex = 0
elif event.key == pygame.K_UP:
changey = -2
changex = 0
elif event.key == pygame.K_RIGHT:
changex = 2
changey = 0
elif event.key == pygame.K_LEFT:
changex = -2
changey = 0
movex += changex
movey += changey
snakehead = []
snakehead.append(movex)
snakehead.append(movey)
snakelist.append(snakehead)
display.fill(schwarz)
if len(coinlist) < 1:
rx = random.randrange(10, 790)
ry = random.randrange(10, 590)
coinlist.append(rx)
coinlist.append(ry)
for XY in snakelist:
pygame.draw.circle(display, grün, (XY[0], XY[1]), 10, 10)
for-loop for the coinlist:
for coin in coinlist:
pygame.draw.rect(display, grün, (coin[0], coin[1], 10, 10))
pygame.display.flip()
if snakelenght < len(snakelist):
del snakelist[:1]
if movex >= rx - 19 and movex <= rx + 19 and movey >= ry - 19 and movey <= ry + 19:
del coinlist[:1]
snakelenght += 10
uhr.tick(15)
mainloop()
pygame.quit()
quit()
You're appending ints to coinlist (rx and ry are random ints from 10 to 790/590) and later on you're trying to access elements within coinlist as if they are arrays.
Consider doing something like replacing
coinlist.append(rx)
coinlist.append(ry)
with
coinlist.append([rx,ry])

Python sprite not moving when key is held down

I'm working on the basics of a game right now, but for some reason I cannot get my sprite to move. I know that it's registering when I'm pressing the key down, but the sprite for some reason just stays still.
import pygame
import sys
from pygame.locals import *
import time
pygame.init()
black = (0, 0, 0)
white = (255, 255, 255)
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("RADICAL")
screen.fill(black)
imga = pygame.image.load('coina.png')
imgb = pygame.image.load('coinb.png')
sound = pygame.mixer.Sound('coin.mp3')
FPS = 80
imgx = 10
imgy = 10
pixMove = 10
steps = 0
x1 = 0
y1 = 0
keystate = pygame.key.get_pressed()
GameOver = False
while not GameOver:
screen.fill(white)
points = (steps)
font = pygame.font.SysFont(None, 30)
text = font.render('Score: '+str(points), True, black)
screen.blit(text, (0,0))
if steps % 2 == 0:
screen.blit(imga, (imgx, imgy))
else:
screen.blit(imgb, (imgx, imgy))
for event in pygame.event.get():
print event
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if keystate[pygame.K_UP]:
y1 -= pixMove
elif keystate[pygame.K_DOWN]:
y1 += pixMove
elif keystate[pygame.K_LEFT]:
x1 -= pixMove
elif keystate[pygame.K_RIGHT]:
x1 += pixMove
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x1 = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y1 = 0
steps +=1
if event.type == K_SPACE:
sound.play()
time.sleep(1)
sound.stop()
pygame.display.update()
fpsTime.tick(FPS)
Generally you blit() images on to the screen (pygame.display.set_mode()), for the changes to to reflect on the screen we call pygame.display.update(), In your case the game never comes the statement, which is out of while loop.

Python Sprites/Images are blinking

I have been coding a program, a survival game, on Python. I seem to have an issue with adding Images/Sprites. I am noticing the images blink. Any way to fix this?
import os
import pygame
import time
import random
from pygame.locals import *
launchLog = pygame.init()
print(launchLog)
white = (255,255,255)
black = (0,0,0)
red=(255,0,0)
blue=(0,0,255)
green=(0,255,0)
skin = (236,227,100)
size = 10
dependant = (green)
rate = 0.0018
weight = 100
bound_x = 600
bound_x2 = bound_x-size
bound_y = 600
bound_y2 = bound_y-size
screen = pygame.display.set_mode((bound_x,bound_y))
pygame.display.set_caption("Survival: EfEs Edition")
clock = pygame.time.Clock()
font = pygame.font.SysFont(None,25)
x = 300
y = 300
roundX = x
roundY = y
img=pygame.image.load("pewds.png")
def hmn(x,y,size):
clothes = pygame.draw.rect(screen,skin,[x,y,size,size-size*2])
snake = pygame.draw.rect(screen, red, [x,y,size,size])
def mesg(msg,color):
txt = font.render(msg, True, color)
screen.blit(txt, [x,y-30])
display.flip(img)
def gameLoop():
global x
global y
jump = 1
quitted = False
starved = 100
eaten = False
restart = False
lead_change = 0
lead_y = 0
randF_x = random.randrange(0,bound_x)
randF_y = random.randrange(0,bound_y)
didX2=round(randF_x/10.0)*10.0
didY2=round(randF_y/10.0)*10.0
while not quitted:
screen.blit(img,(x,y -15))
screen.blit(img,(x,y -15))
pygame.display.update()
hmn(x,y,size)
starved=starved-rate
#print(starved)
if starved<0:
screen.fill(white)
mesg("You died of hunger! Press R to restart.",red)
pygame.display.flip()
time.sleep(0.3)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
starved = 100
pygame.display.update()
rate = 0.0018
x=300
y=300
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.display.update()
quitted = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
lead_change = -.2
elif event.key == pygame.K_d:
lead_change = .2
elif event.key == pygame.K_w:
lead_y = .2
elif event.key == pygame.K_s:
lead_y = -0.2
if event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_a:
lead_change = 0
#print(x)
#print(y)
elif event.key == pygame.K_ESCAPE:
#print("Quitting")
quitted = True
elif event.key == pygame.K_w or event.key == pygame.K_s:
#print(y)
lead_y=0
x += lead_change
y -= lead_y
roundX = x
roundY = y
global roundX
global roundY
didX=round(roundX/10)*10
didY=round(roundY/10)*10
screen.fill(white)
s = pygame.draw.rect(screen, red, [bound_x2/15,bound_y2/20, starved * 2, 50])
pygame.draw.rect(screen, dependant, [didX2,didY2,10,10])
pygame.display.update()
#boundary script
if x>bound_x2:
x=bound_x2
elif x<-1:
x=-1
elif y<2:
y=2
elif y>590:
y=590
hmn(x,y,size)
pygame.display.flip()
if didX == didX2 and didY==didY2:
didX2 = round(random.randrange(0,bound_x)/10)*10
didY2 = round(random.randrange(0,bound_y)/10)*10
global dependant
dependant = green
global starved
starved = starved +0.01
global weight
weight = weight + 3
elif weight>150:
mesg("Weight increased! You now get hungry faster.",black)
pygame.display.update()
global rate
rate = rate+0.0008
pygame.display.update()
clock.tick(55)
mesg("Leaving game", black)
screen.blit(img,(x,y-15))
pygame.display.update()
time.sleep(2)
pygame.quit()
quit()
gameLoop()`
Please excuse the bad coding, the game is only in it's earliest state.
The correct sequence is to:
draw everything you want to show on a frame (all the blit()s, draw()s), etc.
do only one of display.update() with a list of all the changed regions OR display.flip() to update the whole screen once you've drawn everything
tl;dr - don't mix draw()s with update()s.

python pygame doesn't recognize keyboard event

I want to make a program where I move a rectangle via the keyboard, but it doesn't move like it doesn't understand the event commands. I can't find what's wrong. I think the problem is the sequence of commands, but as a beginner, I can't find it. Can anyone help me? Thanks!
import pygame
import sys
from pygame.locals import *
fps = 30
fpsclock = pygame.time.Clock()
w = 640
h = 420
blue = (0, 0, 255)
white = (255, 255, 255)
x = w / 3
y = 350
boxa = 20
movex = 0
def drawwindow():
global screen
pygame.init()
screen = pygame.display.set_mode((w, h))
screen.fill(blue)
def drawbox(box):
if box.right > (w - boxa):
box.right = (w - boxa)
if box.left < 0:
box.left = 0
pygame.draw.rect(screen, white, box)
def main():
global x
global movex
drawwindow()
box1 = pygame.Rect(x, y, boxa, boxa)
drawbox(box1)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_RIGHT:
movex = +4
if event.key == K_LEFT:
movex = -4
if event.type == KEYUP:
if event.key == K_RIGHT:
movex = 0
if event.key == K_LEFT:
movex = 0
x += movex
pygame.display.update()
fpsclock.tick(fps)
if __name__ == '__main__':
main()
Keyboard events are being accepted properly. That can be verified by sticking a print statement inside one of the if event.key == ... blocks.
One of the problems is you are never redrawing the box after initially drawing it. Every iteration of the game loop should redraw the background (ideally only the area that changes, but that's for later) and the box in its new position. Something like this:
while True:
# [event handling code omitted for brevity]
x += movex
drawwindow()
drawbox(box1)
pygame.display.update()
fpsclock.tick(fps)
However there's another problem. Changing x or movex has no effect on anything, because they are not used anywhere once the main loop is entered. Rather than x += movex, the box would move if its x attribute was changed, as in the following code:
while True:
# [event handling code omitted for brevity]
box1.x += movex # this line changed
drawwindow() # this line added
drawbox(box1) # this line added
pygame.display.update()
fpsclock.tick(fps)
Running your code with the changes above, the box now moves.

Categories

Resources