Pygame cant get events inside nested while loop - python

I've looked at a couple sources so far and i can't seem to figure out why pygame wont grab events to unpause in my pause function.
def pause():
paused = True
while paused:
for event in pygame.event.get():
if event == pygame.QUIT:
pygame.quit()
quit()
elif event == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
paused = False
Keep in mind that this is nested in another while loop that's running the game, the function is called, gets stuck into an infinite loop, but can't detect key press to escape even with get keys function. Anybody got answers?

pygame.event.get() returns a list of pygame.event.Event() objects. You have to test if the type attribute of the event is pygame.KEYDOWN:
elif event == pygame.KEYDOWN:
elif event.type == pygame.KEYDOWN:
def pause():
paused = True
while paused:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
paused = False

Related

pygame KEYUP registers only KEYDOWNs

When I press the spacebar "up" and "down" is printed simultaneous, however when I release, nothing is happening.
while True:
pygame.display.set_mode((600,600))
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
print('down')
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
print('up')
if event.type == pygame.QUIT:
pygame.quit()
exit()
You're creating a new display every single frame of your game.
You need to create the display only once, on the outside of your game loop. It should also be assigned to a variable, as you'll need it if you want to draw (blit) items to it, for example.
display = pygame.display.set_mode((600,600))
# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
# ...

Replacing keyboard command with commands from a list

My group is attempting to create an AI that plays Tetris and is struggling to figure out the right way to replace the part of the code in the original file that executes the keyboard command with one that executes a list of commands that will be passed to it. Below is the original code for this part.
for event in pygame.event.get():
if event.type == pygame.USEREVENT+1:
self.drop(False)
elif event.type == pygame.QUIT:
self.quit()
elif event.type == pygame.KEYDOWN:
for key in key_actions:
if event.key == eval("pygame.K_"
+key):
key_actions[key]()
We have tried a slew of different approaches but none get any reaction from the game and instead cause it to break. Is there a simple way we are missing to do this?
Edit: should add, "key_actions" is a switch statement that takes a string equivalent to the name of a keystroke and executes the proper command. I've tried changing this to take strings from a list with the same names but it has no effect other than causing the game to break if a keystroke is entered.
Using a dictionary pattern is much more interesting in this case (and avoids evil eval):
import pygame
def action1():
print('up')
def action2():
print('down')
key_actions = {pygame.K_UP : action1,
pygame.K_DOWN : action2,
}
pygame.init()
screen = pygame.display.set_mode((200,200))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
elif event.type == pygame.KEYDOWN:
if event.key in key_actions:
key_actions[event.key]()
EDIT :
This should match the pattern you already have for key_actions :
kb_keys = {pygame.K_UP : key_actions['UP'],
pygame.K_DOWN : key_actions['DOWN'],
}
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
elif event.type == pygame.KEYDOWN:
if event.key in kb_keys:
kb_keys[event.key]()

Key press is not being recognised by python's 'pygame.key.get_pressed()' module

I am trying to create a function where if a key is pressed, variables are changed. This will be put into a class that moves a sprite on a screen. The while loop is broken when the key is released. However, nothing happens. The while loop is running and I get no error. Does anyone know why this might be happening? I am not sure if I am using the function pygame.key.get_pressed() correctly.
import pygame
from pygame.locals import *
def move():
for event in pygame.event.get():
if event.type == QUIT:
pygame.QUIT()
sys.exit()
while True:
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]:
print("something happens")
#runs this, but when up arrow key is pressed does nothing
if event.type == KEYUP:
False
screen = pygame.display.set_mode((512,384))
move()
pygame.display.update()
You can do something like this:
def move():
for event in pygame.event.get():
if event.type == QUIT:
pygame.QUIT()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
print("up pressed")
# etc.
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
print("up released")
# etc.
For more key names you can go to the Pygame Key Docs
get_pressed() returns list with information about pressed keys but this list is updated by pygame.event.get() and other event functions so you have to execute pygame.event.get() all the time. And this is why we do it in while-loop
import pygame
pygame.init()
screen = pygame.display.set_mode((512,384))
while True:
# get events and (somewhere in background) update list
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# get current list.
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]:
print("UP")

PyGame wont register keydowns

I have installed pygame on my macbook and it seems to be working however it doesnt seem to be registering specific keys
For example
RunGame = True
while RunGame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
RunGame = False
if event.type == pygame.K_LEFT:
Direction = 'West'
The quit code runs just fine however the left key is not registered. When i print the event the only event that is printed when i press any key is the key up and key down event so i cant do anything with specific keys. Am i using pygame wrong?
RunGame = True
while RunGame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
RunGame = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
Direction = 'West'
You forgot the event.type and remember it is event.key == pygame.K_LEFT:. you had event.type == pygame.K_LEFT:

Pygame doesn't CLOSE

Can someone PLEASE tell me what I'm doing wrong. I can't get my stupid program to close. It has a spinning circle(windows) and it doesn't close upon quit or keypress on the space bar.
def visualize(self):
pygame.init()
main_surface = pygame.display.set_mode((1024, 768))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
break
pygame.display.flip()
pygame.quit()
I'm not sure if the inside of my program matters much, but if it does, I'll add it. I would REALLY appreciate your help!
The break will just exit the innermost loop, which is the for loop. You should have a condition variable for your while loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
break
pygame.display.flip()

Categories

Resources