[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Capturing Multiple Keyboard Inputs
- To: <pygame-users@xxxxxxxx>
- Subject: [pygame] Capturing Multiple Keyboard Inputs
- From: "Ryan Strunk" <ryan.strunk@xxxxxxxxx>
- Date: Sun, 15 Jan 2012 21:46:58 -0600
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Sun, 15 Jan 2012 22:46:53 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:subject:date:message-id:mime-version:content-type :content-transfer-encoding:x-mailer:thread-index:content-language; bh=N4aS67+vOM+BI9u8+0EPUVCOmjd00aRZ0XMPhgG8lHI=; b=PiIrkiXMfNqgEgVn5qfaa8I10upIQDtXzrsE89sRqPgc7+zv22ByCrvxHqyq/VqiJ0 FjrrFkkB+oNB+v1us9UX6bAfPRdjrbPGtiH2gm1Bf7xFQ0LsvEUf9ir2srQpM1Nx0xjS bD3DLM+vM2oRVD2HxJCpumoD81eVjsUCrWh58=
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
- Thread-index: AczUAXoDfxgJFZ8JQbeI49mLSQD0Uw==
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