pygame continuous and simultaneous key inputs - python

I am stuck again and cannot find any valid solutions online. I am trying to use pygame and its key inputs to control various things. Now I need to use several keys simultaneously. My code is as follows:
pygame.key.set_reapeat(50,50)
bProgramLoop = True
while (bProgramLoop == True):
for event in pygame.event.get():
if (event.type == pygame.QUIT):
bProgramLoop = False
if (pygame.key.get_pressed()[pygame.K_LEFT]):
EXECUTE_FUNCTION1()
print "left"
if (pygame.key.get_pressed()[pygame.K_RIGHT]):
EXECUTE_FUNCTION2()
print "right"
Now the problem that I have is:
When I hold down "LEFT of RIGHT" it correctly and continuously registers that I pressed left/right. BUT when I hold in "LEFT" and just tap "RIGHT", it registers that left and right were pressed but it then stops to register that "LEFT" is still being pressed.
Any ideas anyone?
Any help would be greatly appreciated.
Misha

In my code the "repeat" is correctly spelt.
I found the work around for my problem. The above code needs to be modified.
pygame.key.set_repeat(50,50)
bProgramLoop = True
while (bProgramLoop == True):
for event in pygame.event.get():
if (event.type == pygame.QUIT):
bProgramLoop = False
if (event.type == pyame.KEYDOWN):
if (event.key == pygame.K_a) # if A is pressed
bKeyA = True # set the Boolean True
if (event.key == pygame.K_s)
bKeyS = True
if (event.type == pyame.KEYDOWN):
if (event.key == pygame.K_a) # if A is released
bKeyA = False# set the Boolean False
if (event.key == pygame.K_s)
bKeyS = False
if (bKeyA == True):
Execute_function1()
if (bKeyB == True):
Execute_function2()
I double checked, the repeat is correctly spelt and it would not continue a keyboard input once another one was tapped. The problem is, as far as I can figure it out, and even occurs once at the point when a key is pressed. When another key is simultaneously pressed the event is lost.
Thus the solution is to set a variable true until the key is lifted up, and thus the variable is set false.

You have misspelled repeat in pygame.key.repeat(). I corrected this and it worked for me.
def main():
while Running:
check_events()
update()
clock.tick(FPS)
def check_events():
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
sys.exit()
if key == pygame.K_q:
Running = False
return
if (pygame.key.get_pressed()[pygame.K_LEFT]):
#EXECUTE_FUNCTION1()
print "left"
if (pygame.key.get_pressed()[pygame.K_RIGHT]):
#EXECUTE_FUNCTION2()
print "right"

If you want to use continuous inputs than try this, it is my code.
import pygame as py
import time
sc = py.display.set_mode((800, 600))
x = 350
y = 300
blue = (0, 0, 255)
last = 0
while True:
key = py.key.get_pressed()
for event in py.event.get():
if event.type == py.KEYDOWN:
last = event.key
else:
last = 0
if last == py.K_UP:
y -= 0.1
if last == py.K_DOWN:
y += 0.1
if last == py.K_LEFT:
x -= 0.1
if last == py.K_RIGHT:
x += 0.1
sc.fill((0,255,0))
py.draw.rect(sc, blue, (x,y,50,50))
py.display.flip()
If you want to use simultaneous input, then here:
import pygame as py
import time
sc = py.display.set_mode((800, 600))
x = 350
y = 300
blue = (0, 0, 255)
last = 0
def move(times, yspeed, xspeed):
for i in range(times):
global x, y
x += (xspeed / times)
y += (yspeed / times)
time.sleep((xspeed / times / 10) + (yspeed / times / 10))
while True:
key = py.key.get_pressed()
for event in py.event.get():
if event.type == py.KEYDOWN:
last = event.key
else:
last = 0
if event.key == py.K_UP and event.key == py.K_l:
y -= 0.1
sc.fill((0,255,0))
py.draw.rect(sc, blue, (x,y,50,50))
py.display.flip()

Related

pygame selection menu navigation using arrow keys and return

I am trying to make it so I can generate a group of options to select from and using the up and down arrows the user can navigate between them the current option being highlighted and if you press enter it calls the function that option contains.
Excuse the mess, I had it cleaner, but was trying several things to try to get the intended result.
The selection function works fine by itself
def selection(text, selected_status, x, y, function):
if selected_status:
fill_rect(black, x, y, display_width - x, font_size + 2)
fill_rect( settings[3], x, y, display_width - x, font_size +2)
message_to_screen("["+ text + "]", settings[3], black, x, y)
if event_handler() == pygame.K_RETURN:
function()
return True
elif not selected_status:
fill_rect(black, x, y, display_width - x, font_size + 2)
message_to_screen("["+ text + "]", black, settings[3], x, y)
return False
The selection handler is the issue. it seems I have to hit the arrow keys at certain times in order for them to work. the event_handler just grabs the currently pressed key and returns it. overall it's just a broken mess.
def selection_handler(selection_param_array, x, y):
text_array = selection_param_array[0]
selected_status_array = selection_param_array[1]
function_array = selection_param_array[2]
index = 0
chosen = False
original_y = y
while not chosen:
y = original_y
for i in range(len(selected_status_array)):
if selected_status_array[i] == selected_status_array[index]:
selected_status_array[i] = True
else:
selected_status_array[i] = False
selection(text_array[i], selected_status_array[i], x, y, function_array[i])
y += font_size
if event_handler() == pygame.K_UP and index > 0:
index -= 1
elif event_handler() == pygame.K_DOWN and index < len(selection_param_array):
index += 1
elif event_handler() == pygame.K_RETURN:
chosen = True
function_array[index]()
pygame.display.update()
Instead of just checking whether a key is pressed down right now, you need to use pygame.event.get() to obtain all events that have occurred since the last pygame.event.get() call. The usual syntax is this:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
index += 1
elif event.key == pygame.K_DOWN:
index -= 1
elif event.key == pygame.K_RETURN:
chosen = True
function_array[index]()
break
index %= len(selection_param_array)
Also, if you call from pygame.locals import * at the start of your program, you can omit pygame. from all of the keys and event types, so you get more readable code:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_UP:
index += 1
elif event.key == K_DOWN:
index -= 1
elif event.key == K_RETURN:
chosen = True
function_array[index]()
break
index %= len(selection_param_array)
This replaces your if event_handler() == pygame.K_UP and index > 0: ... function_array[index]() code.
Note that the code above will loop from the top to the bottom of the list when you are selecting things, I don't know if you want this. You can stop it from doing this by reintroducing your index comparisons on the event.key == lines and removing index %= len(selection_param_array).

How do I make a game object move continuously as long as the key is pressed?

I am trying to make a game object (here, the welcome text) move as long as the key is pressed on the keyboard. But in this code of mine,
import pygame , sys
from pygame.locals import *
pygame.init()
WHITE = (255 , 255 , 255)
RED = (255 , 0 , 0)
DISPLAYSURF = pygame.display.set_mode((800 , 400))
pygame.display.set_caption('Welcome Tanks')
#render(text, antialias, color, background=None)
fontObj = pygame.font.SysFont('serif' , 40)
text = fontObj.render('Welcome Folks' , True , RED )
x = 150
y = 29
while True:
DISPLAYSURF.fill(WHITE)
DISPLAYSURF.blit(text ,(x , y))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit(0)
elif event.type == KEYDOWN or event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit(0)
elif event.key == K_DOWN:
y += 15
elif event.key == K_UP:
y -= 15
elif event.key == K_RIGHT:
x += 14
elif event.key == K_LEFT:
x -= 15
else:
x = 150
y = 29
pygame.display.update()
The object moves only once, even though the key is continuously pressed for a long time. In other words, the object changes its position only once when the keyboard button is pressed. I want it to move continuously while I hold the key.
Which event should I look for instead of event.KEYDOWN?
I suggest you to use the key.get_pressed(), e.g.
while True:
DISPLAYSURF.fill(WHITE)
DISPLAYSURF.blit(text ,(x , y))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit(0)
elif event.type == KEYDOWN or event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit(0)
if pygame.key.get_pressed()[K_LEFT]:
x -= 15
if pygame.key.get_pressed()[K_RIGHT]:
x += 14
if pygame.key.get_pressed()[K_UP]:
y -= 15
if pygame.key.get_pressed()[K_DOWN]:
y += 15
pygame.display.update()
pygame.key.get_pressed()
Returns a sequence of boolean values representing the state of every
key on the keyboard. Use the key constant values to index the array. A
True value means the that button is pressed.
Getting the list of pushed buttons with this function is not the
proper way to handle text entry from the user. You have no way to know
the order of keys pressed, and rapidly pushed keys can be completely
unnoticed between two calls to pygame.key.get_pressed(). There is also
no way to translate these pushed keys into a fully translated
character value. See the pygame.KEYDOWN events on the event queue for
this functionality.
Also you can take a look at doc
keyState = pygame.key.get_pressed()
Declare that, and then you use keystate:
if KeyState()[K_DOWN]:
y += 15
You could try:
if key.get_pressed()
down = True
while down = True:
y+= 15
Then get FPS clock so it does not just flow over the screen!

Detecting duration of a keypress python/pygame

In my program I'd like users to be able to hold down a button. Upon releasing the button I wish to print the duration that they held down the key. I have been trying to use the pygame clock function but have run into some trouble. The program works fine for the first keypress, but on later keypresses records any downtime between keypresses. Any help would be appreciated, here is my code:
import pygame
from pygame.locals import *
def main():
key = 0
pygame.init()
self = pygame.time.Clock()
surface_sz = 480
main_surface = pygame.display.set_mode((surface_sz, surface_sz))
small_rect = (300, 200, 150, 90)
some_color = (255, 0, 0)
while True:
ev = pygame.event.poll()
if ev.type == pygame.QUIT:
break;
elif ev.type == KEYUP:
if ev.key == K_SPACE: #Sets the key to be used
key += 1 #Updates counter for number of keypresses
while ev.type == KEYUP:
self.tick_busy_loop()
test = (self.get_time()/1000.0)
print "number: ", key, "duration: ", test
ev = pygame.event.poll()
main()
You can use keyboard library for this.
Here is a sample code that I made:
import keyboard, time
while True:
a = keyboard.read_event() #Reading the key
if a.name == "esc":break #Loop will break on pressing esc, you can remove that
elif a.event_type == "down": #If any button is pressed (Not talking about released) then wait for it to be released
t = time.time() #Getting time in sec
b = keyboard.read_event()
while not b.event_type == "up" and b.name == a.name: #Loop till the key event doesn't matches the old one
b = keyboard.read_event()
print('Pressed Key "'+ b.name + '" for ' + str(time.time()-t))
If you are looking for more solutions (for Pygame or Pynput), then you can find them on my answer on other related question.
I recommend using get_ticks() instead of get_time(). You should read about the differences, but I feel it may not work as desired since you are not explicitly calling self.tick().
The problem is your code is outputting the time between each KEYUP event. There is another way you make the code work by going over the events once per loop and continuing onward without having the nested while loop.
time_down = 0.0
time_elapsed = 0.0
while True:
for ev in pygame.event.get():
if ev.type == QUIT:
break # you had a semicolon here, there is no need for it
elif ev.type == KEYDOWN:
if ev.key == K_SPACE:
time_down = pygame.time.get_ticks()
elif ev.type == KEYUP:
if ev.key == K_SPACE:
key += 1
time_elapsed = (pygame.time.get_ticks() - time_down)/1000.0
print "number: ", key, "duration: ", time_elapsed
self.tick()
pygame.display.update()

Character only maintains movement while mouse is moving on screen?

My while loops only maintains the movement for the sprite while the cursor is moving inside of the screen. I've tried reorganizing some of the screen.blits and display.update() and display.flip(). I can't seem to figure out why the character stops after a change in one pixel instead of continuing like it I intended.
background_image = 'Terrain_Grass_First.png'
import pygame, sys
from pygame.locals import *
pygame.init()
pygame.display.set_caption('Hans')
screen_width = 600
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height),0,32)
pygame.mouse.set_visible(False)
sprite = pygame.image.load('Hans_front_still.png').convert_alpha()
x,y = (0,0)
movex, movey = (0,0)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_w:
y = -1
elif event.key == K_a:
x = -1
elif event.key == K_s:
y = +1
elif event.key == K_d:
x = +1
elif event.type == KEYUP:
if event.key == K_w:
y = 0
elif event.key == K_a:
x = 0
elif event.key == K_s:
y = 0
elif event.key == K_d:
x = 0
movex += x
movey += y
screen.fill((0,0,0))
screen.blit(sprite,(movex,movey))
pygame.display.flip()
Your loop blocks on pygame.event.get.
You don't schedule any events of your own.
So, you do nothing until the OS has an event (mouse move, redraw, etc.) to give you.
And, even if you fixed that, you're calling movex += x once per event. So, when the OS is throwing a lot of events at you, your sprite will go zipping madly across the screen, but when the events are coming more slowly, it will crawl along. That's almost never what you want.
An easy fix for both problems is to just schedule your own events. For example, with pygame.time.set_timer(), you can make sure you get an event every, say, 250 milliseconds, and you can only move the sprite on those events. For example:
timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event, 250)
while True:
for event in pygame.event.get():
# ...
elif event.type == timer_event:
movex += x
movey += y
Another alternative is to design your game with a fixed framerate.
A trivial example looks like this:
FRAME_TIME = 50 # 50ms = 20fps
next_frame_time = pygame.time.get_ticks() + FRAMES
while True:
while True:
event = pygame.event.poll()
if event.type == pygame.NOEVENT:
break
elif # ...
pygame.display.flip()
now = pygame.time.get_ticks()
if now < next_frame_time:
pygame.time.wait(next_frame_time - now)
next_frame_time += FRAMES
Now you can just move every 5th frame.
A realistic example has to deal with missed frames, too many events in the queue, choose between wait and delay appropriately, etc.
But I'd go with the event-driven version with a timer instead of a fixed-framerate version in most cases, especially if you're already going down that line.
The only problem is your Indentation!
The fifth and sixth lines from the bottom have wrong indentation, and you need to delete them.
These two lines:
movex += x
movey += y
should be:
movex += x
movey += y
And it works

My sprite wont move right pygame

earlier my sprite wouldn't move at all so i posted the code and i got it fixed for the most part but now my up/down arrows work fine but my right key doesn't work. (Also, when you press two keys, and then let go of one, the walk animation doesn't work, but I'm not desperate right now to get that fixed.) Also i would prefer not to use user made classes. thanks in advance.
Here is the code:
from pygame.locals import *
import pygame._view
pygame.init()
clock = pygame.time.Clock()
height = 500
width = 500
screen = pygame.display.set_mode((width, height), 0, 32)
pygame.display.set_caption('placeholder text')
photo = 'grassbackground.png'
background = pygame.image.load(photo).convert()
rectexist = False
photo1 = 1
user = pygame.sprite.Sprite()
change = False
up = False
down = False
left = False
right = False
speed = 5
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_UP:
up = True
change = True
if event.key == K_DOWN:
down = True
change = True
if event.key == K_LEFT:
left = True
change = True
if event.type == K_RIGHT:
right = True
change = True
if event.type == KEYUP:
if event.key == K_UP:
up = False
change = False
if event.key == K_DOWN:
down = False
change = False
if event.key == K_LEFT:
left = False
change = False
if event.key == K_RIGHT:
right = False
change = False
if down and user.rect.bottom < height:
user.rect.top += speed
if up and user.rect.top > 0:
user.rect.top -= speed
if left and user.rect.left > 0:
user.rect.left -= speed
if right and user.rect.right < width:
user.rect.right += speed
if change == True:
pygame.time.wait(110)
photo1 += 1
if change == False:
photo1 = 1
if photo1 == 1:
user.image = pygame.image.load("1.png").convert()
if rectexist == False:
user.rect = user.image.get_rect()
rectexist = True
screen.blit(user.image, user.rect)
if photo1 == 2:
user.image = pygame.image.load("2.png").convert()
screen.blit(user.image, user.rect)
if photo1 == 3:
user.image = pygame.image.load("3.png").convert()
screen.blit(user.image, user.rect)
if photo1 >= 4:
photo1 = 1
thesprites = pygame.sprite.RenderPlain((user))
thesprites.update()
screen.blit(background, (0, 0))
thesprites.draw(screen)
pygame.display.update()
clock.tick(60)
In your code it says:
if event.type == K_RIGHT:
it should be:
if event.key == K_RIGHT:
To keep animating you'd need to change the code a little more, add a:
key_pressed = []
in the beginning. Then for each key-press block do:
key_pressed.append(event.key)
and key-release do:
key_pressed = [k for k in key_pressed if k != event.key]
instead of the change=True and change=False respectively. Then after the two segments of checking what was pressed and released have add these lines:
if len(key_pressed) > 0:
change = True
else:
change = False
That should fix most of your issues...
Use pressed = pygame.key.get_pressed() to get a dictionary (a tuple, actually, but the way things are structured you can think of it as a dictionary) of all keys that are currently pressed. You'd use it like this:
pressed = pygame.key.get_pressed()
if pressed[K_LEFT]:
# move left
elif pressed[K_RIGHT]:
# move right
# etc

Categories