[pygame] Accounting for Pressed Keys
Hi everyone, I'm closer to solving my arrow key problem with regard to getting pyHook to snag the arrow keys first. I've blocked the four arrow keys, and I've used the block to put a KEYDOWN event on the Pygame queue. The problem, though, is that now Pygame receives a key down event several times a second. My goal in all of this is to try to figure out how Pygame accounts for pressed keys. Does Pygame have a special event for pressed keys that I could generate, or does it get its data elsewhere. Thanks for any help you can provide. All the best, Ryan
[pygame] pyHooking the Arrow Keys
Hi everyone, I wrote a little while back about how I wanted to tie the arrow keys specifically to my program so that they could be used exclusively for the program instead of being intercepted by a screen reader. I attempted to modify the code from the tutorial I found, but it is producing some rather strange results. In addition to not allowing the arrow keys to work inside of the program, the program also blocks the arrow keys in other windows. Can anyone tell me, please, what is going wrong and how I can fix it? Here is the code I have pasted at the bottom of my main file: def OnKeyboardEvent(event): # return True to pass the event to other handlers return (event.Key not in ['Up', 'Down', 'Right', 'Left']) # create a hook manager hm = pyHook.HookManager() hm.KeyDown = OnKeyboardEvent hm.HookKeyboard() pythoncom.PumpMessages() Thanks for your help. Best, Ryan
[pygame] RE: Intercepting the Keyboard
One more thing about this situation: Just after writing my previous email, I discovered pygame.event.set_grab(True). However, even when I set this to true outside of the main loop, I did not steal keyboard input from the screen reading software. In addition, I was still able to alt+tab out of the window to close the pygame program. This suggests that either set_grab isn't that powerful or I'm using it wrong (a distinct possibility). Thanks for any help you can provide. Best, Ryan
[pygame] Intercepting the Keyboard
Hello everyone, In creating audio-based games, I'm trying to make my programs work with major screen readers. The problem is that the industry leader, JAWS, likes to intercept keystrokes sent to the system. This means that if you build arrow key use into a pygame program, JAWS gets to your arrow keys first and processes them before Pygame can. Unfortunately, cutting out the screen reader isn't an option, as it's used to read any game text that might be written to the screen. I'm curious if anyone has recommendations as to how I could have Pygame or Python get to keyboard events before the rest of the system. I have a friend who was able to make this happen using C#.net, but I can't even begin to ask how or if it's possible to do this using Python. Any help you all can provide would be greatly appreciated. All the best, Ryan
RE: [pygame] Substantial Lag
This is all great info. Thank you. -Original Message- From: owner-pygame-us...@seul.org [mailto:owner-pygame-us...@seul.org] On Behalf Of Sam Bull Sent: Friday, February 17, 2012 3:17 PM To: pygame-users@seul.org Subject: RE: [pygame] Substantial Lag On Mon, 2012-02-13 at 02:37 -0600, Ryan Strunk wrote: > If clock can force my program to run at a desired speed, how do I > program it to do so? Do I use clock.tick at a certain framerate, for > instance? [Sent from wrong address, so re-posting] Clock.tick(fps) effectively sleeps your program to keep it running at a desired framerate. So, if you run clock.tick(10), it would try to maintain 10 fps, by sleeping your program for upto 100ms. If the last call to clock.tick() was 10ms earlier, meaning it's taken your code 10ms to run a frame, then it will sleep your program for 90ms, in order to regulate it at 10 fps. So, this is more flexible than simply using time.sleep(100). If your code took 10ms to run, then you run sleep(100) on top of that, it's 110ms per frame, and you're dropping to 9 fps. So clock.tick(fps) is preferred. If you don't pass a framerate, then it simply returns the time passed since the last tick, and does not sleep at all, and so will max out the CPU usage. The reason tapping a key wasn't moving your character: when the game is sleeping for upto 200ms, as in your example, if a player presses the key down and releases before it finishes sleeping, then when the code runs get_pressed(), the key is no longer being held down, and your code doesn't move the character. Increasing the framerate will likely improve the responsiveness, but if you want to be certain you catch this, then you should use the pygame events. Here's a change to your code: from pygame.locals import * clock = pygame.time.Clock() o = Output() guy = Player() screen = pygame.display.set_mode((640, 400)) move_left = move_right = False while(True): if move_left: guy.move(-1) if move_right: guy.move(1) for event in pygame.event.get(): if event.type == KEYDOWN: if event.key == K_LEFT: move_left = True guy.move(-1) elif event.key == K_RIGHT: move_right = True guy.move(1) elif event.type == KEYUP: if event.key == K_LEFT: move_left = False elif event.key == K_RIGHT: move_right = False clock.tick(30) Something like that would ensure you always catch the key press. If the user taps the key quickly in-between frames, we would get both a KEYDOWN and a KEYUP event. This is why we call guy.move() when we catch the KEYDOWN event, it will move the character even if the move_[left/right] variable is set back to False in the same frame. If you set this to something like 2 fps, you can test that it does work. It should always move the character after the user taps the key, though at 2 fps, there would obviously be a delay before you see the character respond. -- Sam Bull PGP: 9626CE2B
RE: [pygame] Substantial Lag
From: owner-pygame-us...@seul.org [mailto:owner-pygame-us...@seul.org] On Behalf Of Santiago Romero Sent: Monday, February 13, 2012 2:16 AM To: pygame-users@seul.org Subject: Re: [pygame] Substantial Lag > > In order to save CPU power, do I need to put in a pygame.time.delay(10) at the end of my event loop? > No. > clock() does already that, forcing your program to run at N fps by "idle-ing" it enough time to run it at the desired speed. I really apologize for all these posts, but I'm trying to get a handle on this, and I'm not finding what I need on Google. What sort of syntax do I need to use to get the clock to handle all of this for me? I tried using pygame.clock.tick with no parameters, and my CPU useage jumped 30%. I tried just instantiating the clock and got the same result. The only thing that kept the CPU useage minimal was to throw in a pygame.time.wait(10) at the end of each iteration of the event loop. If clock can force my program to run at a desired speed, how do I program it to do so? Do I use clock.tick at a certain framerate, for instance? Thanks, really, for all the help. Best, Ryan
RE: [pygame] Substantial Lag
From: owner-pygame-us...@seul.org [mailto:owner-pygame-us...@seul.org] On Behalf Of Vovk Donets Sent: Monday, February 13, 2012 1:48 AM To: pygame-users@seul.org Subject: Re: [pygame] Substantial Lag > I would use builtin pygame clock, coz' no need for manual control of the frame rate. I looked at the documentation, and it said that tick was used to manually slow down the game. I don't need to do that, so I can leave that out. Thanks for setting me straight on that. In order to save CPU power, do I need to put in a pygame.time.delay(10) at the end of my event loop?
RE: [pygame] Substantial Lag
From: owner-pygame-us...@seul.org [mailto:owner-pygame-us...@seul.org] On Behalf Of Vovk Donets Sent: Sunday, February 12, 2012 10:12 PM To: pygame-users@seul.org Subject: Re: [pygame] Substantial Lag > If you, by any chance, need this then for C++ like delay in miliseconds you can use .sleep() function from time module. Is there a need for me to build that in, or is Pygame good at giving up the processor from time to time?
RE: [pygame] Substantial Lag
From: owner-pygame-us...@seul.org [mailto:owner-pygame-us...@seul.org] On Behalf Of Christopher Night Sent: Sunday, February 12, 2012 7:07 PM To: pygame-users@seul.org Subject: Re: [pygame] Substantial Lag > You're only updating 5 frames a second, so yu should expect delays of up to 0.2s, and if you tap the key fast enough it won't register. Why not change the 5 to 50 or something? That's good to know. I was thinking it was like the wait function in C++. I didn't realize that was how many frames it updates; I thought that was how many milliseconds it paused. Thanks a ton, Ryan
[pygame] Substantial Lag
Hi everyone, I apologize in advance for posting 43 lines of code, but I can't figure out where on Earth the trouble is coming from. When I run this code, the keys do exactly what I'd like, but I'm noticing a delay of a few tenths of a second between when I press the key and when the sound of the step is played. Further, sometimes when I tap an arrow key quickly, the player won't even take a step. Can anyone please tell me what I'm doing wrong? This looks sound to me. import time import pygame from sound_lib.stream import FileStream from sound_lib.output import Output class Player(object): def __init__(self): self.x = 10 self.step = FileStream(file="sounds/step.ogg") self.step.pan = 0 self.step_time = 0.25 self.last_step_time = 0.0 def move(self, dir): if time.time() - self.last_step_time <= self.step_time: return if self.x + dir < 1 or self.x + dir > 20: return self.x += dir if dir > 0: self.step.pan += 0.1 else: self.step.pan -= 0.1 self.step.play(True) self.last_step_time = time.time() def main(): clock = pygame.time.Clock() o = Output() guy = Player() screen = pygame.display.set_mode((640, 400)) while(True): keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: guy.move(-1) if keys[pygame.K_RIGHT]: guy.move(1) for event in pygame.event.get(): pass clock.tick(5) if __name__ == '__main__': main() Thanks much, Ryan
RE: [pygame] Capturing Multiple Keyboard Inputs
> On 1/15/2012 8:16 PM, Ian Mallett wrote: >>> >> You should use pygame.key.get_pressed() to check whether the left/up >> keys are pressed. Something like: >> >> while pygame.event.get(): pass >> key = pygame.key.get_pressed() >> if key[K_LEFT]: #whatever >> if key[K_UP]: #whatever I understand the reasoning behind get_pressed. What's the significance of while pygame.event.get(): pass Is that what you use instead of the for loop to step through the events in the queue, or is that the main event loop? Should I be putting that in instead of for key in pygame.event.get(): in order to look through all of the generated events? Thanks, Ryan
[pygame] Capturing Multiple Keyboard Inputs
Hello everyone, I am testing my understanding of the pygame.key module by creating a program that pans the sound of a car engine and raises/lowers its frequency. While the individual keys do exactly what they're supposed to, I run into a big problem when I try to do two things at once. For example, if I hold the up arrow, the frequency of the sound rises with no problem. If I then hold down the left arrow while still holding up, however, the frequency stops rising and the pan begins to adjust itself. How can I make both keys carry out their assigned task at the same time? As a side note, aside from exporting the redundant code below into its own methods, are there any other ways to check for multiple keys without giving each its own if check? Ugly code is below: import pygame from sound_lib.stream import FileStream from sound_lib.output import Output def main(): clock = pygame.time.Clock() o = Output() sound = FileStream(file="sounds/car.wav") screen = pygame.display.set_mode((640, 400)) sound.looping = True sound.play() pygame.key.set_repeat(50, 50) while(True): for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: sound.frequency += 200 if event.key == pygame.K_DOWN: sound.frequency -= 200 if event.key == pygame.K_LEFT: if sound.pan <= -0.9: sound.pan = -0.9 else: sound.pan -= 0.1 if event.key == pygame.K_RIGHT: if sound.pan >= 0.9: sound.pan = 0.9 else: sound.pan += 0.1 if event.key == pygame.K_ESCAPE: exit() clock.tick(10) if __name__ == '__main__': main() Thanks, Ryan
RE: [pygame] Capabilities of Pygame
--- On Fri, 1/13/12, Lenard Lindstrom wrote: > > Also, > > though SDL does support streaming, Pygame does not. > > Everything must be loaded before played. > Um... that's not true. pygame.mixer.music is Pygame's streaming module. So if you were to stream audio, would that eliminate the potential delay. The only audio-only game I've seen written in Pygame, Sound RTS, has a bit of noticeable lag when playing sounds. Is there a way to program such that sounds would play instantly when told to do so? Thanks for everyone's help thus far. Ryan
RE: [pygame] Capabilities of Pygame
From: owner-pygame-us...@seul.org [mailto:owner-pygame-us...@seul.org] On Behalf Of Christopher Night Sent: Thursday, January 12, 2012 7:54 PM To: pygame-users@seul.org Subject: Re: [pygame] Capabilities of Pygame > Seriously, what kind of game do you want to make? I have a couple in mind: an internet multi-player side scroller based on the rules to Sparkle, a sandbox-type world combining missions and social situations, various sports titles. All of the games will take place solely in an audio medium. > If you're working on a game that you could conceivably write by yourself or with a small team, python will probably be up for the job. In neither case is performance going to be the main consideration of you personally. That's good to know. With as much as critics of Python harp on the speed, I was worried that resulting software was going to crawl along at a snail's pace. Are there any situations that come to mind where Python wouldn't work? Thanks a lot for all your help. Best, Ryan
[pygame] Capabilities of Pygame
Hello everyone, As I embark on this journey of learning Pygame and game design, I have one last burning question I haven't been able to find an answer to. I've heard that Python, as an interpreted language, isn't as fast as languages like C++. It follows, then, that Pygame would suffer the same drawback in terms of speed. What I don't know, though, is how much this potential limitation would affect game play. Using Pygame, is it possible to create games that would rival the scope and complexity of mainstream titles out there. Could you build a World the size of World of Warcraft and still have it be responsive to players? Could you build a game as fast-moving as Mortal Kombat, play it over the internet with a good connection, and still have it be as smooth as the Xbox? I want to make sure I don't get deep into a project only to realize that the language was better suited to a different style of game. Any help anyone can provide would be greatly appreciated. All the best, Ryan
RE: [pygame] Beginner Question
Good to know. Thank you. From: owner-pygame-us...@seul.org [mailto:owner-pygame-us...@seul.org] On Behalf Of Nick Arnoeyts Sent: Monday, January 09, 2012 1:55 PM To: pygame-users@seul.org Subject: Re: [pygame] Beginner Question As far as I know, Pygame is still being actively developed. You should take a look on bitbucket.com to see when the last commit was, and it wasn't that long ago.
[pygame] Beginner Question
Hello everyone, I've been learning Python for about a year now, and I wanted to get more seriously into developing audio games. I was told that Pygame is a good package to look at, so I installed it on my machine and joined this list. On looking at python.org, however, it appears as though there have been no major updates since 2009. I've heard tell of a new version of Pygame that runs with later versions of Python, but I can't seem to find much information on it. So I'm curious if: 1. Pygame is still actively being maintained/expanded, and 2. if not, what libraries are people using to develop games. Thank you much for any help you can provide. Best, Ryan