Python 2.7
Im trying to create a piano-like thing in pygame where when a key is pressed a note plays. My problem is that I can not figure out how to get the note to play forever while the key is held down. I tried to make a while loop but I couldnt figure out how to leave it.
import pygame
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
size = width, height = 800, 400
screen = pygame.display.set_mode((size))
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.type == pygame.KEYUP:
break
elif event.key == pygame.K_i:
pygame.mixer.music.load('file.wav') #A
pygame.mixer.music.play()
elif event.key == pygame.K_o:
pygame.mixer.Sound('file.wav').play() #B
elif event.key == pygame.K_e:
pygame.mixer.Sound('file.wav').play() #C
elif event.key == pygame.K_r:
pygame.mixer.Sound('file.wav').play() #D
elif event.key == pygame.K_t:
pygame.mixer.Sound('file.wav').play() #E
elif event.key == pygame.K_y:
pygame.mixer.Sound('file.wav').play() #F
elif event.key == pygame.K_u:
pygame.mixer.Sound('file.wav').play() #G
while running:
keys = pygame.key.get_pressed() #checking pressed keys
if keys[pygame.K_UP]:
pygame.mixer.Sound('file.wav').stop()
if keys[pygame.K_DOWN]:
pygame.mixer.Sound('file.wav').play()
While a key is pressed it will continue to play that key but when it isn't pressed anymore it will stop.
Related
This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
Closed 2 years ago.
I am learning Python in Python Crashcourse and am making the Alien Invasion game.
This is what I have in ship.py file to control the Spaceship's X position
# Movement flag
self.moving_right = False
self.moving_left = False
def update(self):
"""Update the ship''s position based on the movement flag"""
if self.moving_right:
self.rect.x += 1
if self.moving_left:
self.rect.x -= 1
And here is the main alien_invasion.py file using pygame.KEYLEFT and KEYRIGHT
def _check_events(self):
# Watch for keyboard and mouse movements.
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.KEYLEFT:
self.ship.moving_left = True
elif event.type == pygame.KEYUP:
if event.key == pygame.KEYRIGHT:
self.ship.move_right = False
elif event.key == pygame.KEYLEFT:
self.ship.moving_left = False
And I keep getting the error
AttributeError: module 'pygame' has no attribute 'KEYLEFT' when I run the game and press the left key. Sorry if this is an overload of stuff for a small problem, not sure how much info is needed to help fix. Thank you
There is a KEYUP and KEYDOWN, but no KEYRIGHT and KEYLEFT. You are looking for K_LEFT and K_RIGHT.
def _check_events(self):
# Watch for keyboard and mouse movements.
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
self.ship.move_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
This question already has an answer here:
Python Pygame press two direction key and another key to shoot there's no bullet
(1 answer)
Closed 2 years ago.
I'm making my first pygame project and it has been going smoothly. I wrote the following code to check for inputs and so far it was working ok.
def check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running, self.playing = False, False
self.curr_menu = False
self.curr_level.run_level = False
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
self.DOWN_KEY = True
if event.key == pygame.K_UP:
self.UP_KEY = True
if event.key == pygame.K_RETURN:
self.START_KEY = True
if event.key == pygame.K_BACKSPACE or event.key == pygame.K_ESCAPE:
self.BACK_KEY = True
if event.key == pygame.K_LEFT:
self.LEFT_KEY = True
if event.key == pygame.K_RIGHT:
self.RIGHT_KEY = True
if event.key == pygame.K_SPACE:
self.SPACE_KEY = True
if event.key == pygame.K_RSHIFT or event.key == pygame.K_LSHIFT:
self.SHIFT_KEY = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
self.DOWN_KEY = False
if event.key == pygame.K_UP:
self.UP_KEY = False
if event.key == pygame.K_RETURN:
self.START_KEY = False
if event.key == pygame.K_BACKSPACE or event.key == pygame.K_ESCAPE:
self.BACK_KEY = False
if event.key == pygame.K_LEFT:
self.LEFT_KEY = False
if event.key == pygame.K_RIGHT:
self.RIGHT_KEY = False
if event.key == pygame.K_SPACE:
self.SPACE_KEY = False
if event.key == pygame.K_RSHIFT or event.key == pygame.K_LSHIFT:
self.SHIFT_KEY = False
I wanted to add a sprinting for the x (SHIFT) and y (SPACE) axis it doesn't work properly. Whenever hold down SPACE and RIGHT, pygame doesn't register the UP key getting pressed and whenever I hold SPACE and LEFT, pygame doesn't register both UP and DOWN.
Somehow, holding shift doesn't mess with the registration of the arrow keys.
Because of the issues I was experiencing with the sprinting I started investigating further and realized that pygame doesn't recognize me pressing the UP key when LEFT and RIGHT are pressed simultaneously, but recognizes the DOWN key press.
Is the problem in my code?
If you want to deal with pressed events you need to use:
pygame.key.get_pressed()
It returns the state of the key is pressed or not (True, False)
Example:
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_LSHIFT]:
sprintX()
if key_pressed[pygame.K_SPACE]:
sprintY()
I created this program and when I run it the player is not moving. I checked and everything is right. Just in case you need it I am using python 3.8.6. Can you please help me? Here is my code.
# Game Loop
running = True
while running:
screen.fill((128, 20, 128))
for event in pygame.event.get():
player(playerX, playerY)
pygame.display.update()
# Movment
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
player_x_change -= 10
if event.key == pygame.K_d:
player_x_change += 10
if event.key == pygame.K_w:
player_y_change -= 10
if event.key == pygame.K_s:
player_y_change += 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
player_x_change = 0
if event.key == pygame.K_d:
player_x_change = 0
playerX += player_x_change
playerY += player_y_change
# Close the game
if event.type == pygame.QUIT:
running = False
It's probably just an indentation error (perhaps pasting to the question), but none of your events are being handled because of the if event.type clauses are not inside the for event loop.
Simply fixing this indentation alleviates this.
The next problem is that you calculate the player_x_change and player_y_change, but never apply the amounts to playerX and playerY.
Since these changes only happen when pygame.KEYDOWN is received, it's easy to simply add this inside the event handler:
# Movment
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
player_x_change -= 10
elif event.key == pygame.K_d:
player_x_change += 10
elif event.key == pygame.K_w:
player_y_change -= 10
elif event.key == pygame.K_s:
player_y_change += 10
# Move the player
playerX += player_x_change
playerY += player_y_change
Note the use of if .. elif. If the event is one particular type, it cannot possibly be another type too (at the same time). This means it's not necessary to keep re-checking the type. Using elif ("else if") helps with these checks, by stopping the check once a match is found. It is more efficient.
Final code:
import pygame
# Window size
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
WINDOW_SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF
BACKGROUND = (128, 20, 128)
### initialisation
pygame.init()
pygame.mixer.init()
pygame.display.set_caption("Not Moving")
# Game Loop
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
# Close the game
if event.type == pygame.QUIT:
running = False
# Movment
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
player_x_change -= 10
elif event.key == pygame.K_d:
player_x_change += 10
elif event.key == pygame.K_w:
player_y_change -= 10
elif event.key == pygame.K_s:
player_y_change += 10
# Move the player
playerX += player_x_change
playerY += player_y_change
# DON'T REALLY NEED THIS
#elif event.type == pygame.KEYUP:
# if event.key == pygame.K_a:
# player_x_change = 0
# elif event.key == pygame.K_d:
# player_x_change = 0
# Re-draw the screen
screen.fill( BACKGROUND )
player(playerX, playerY)
pygame.display.update()
clock.tick(60) # limit the re-fresh rate to save electricity
pygame.quit()
I also added a frame-rate limiter with a pygame.time.Clock() object, just to save some CPU.
I was able to fix the problem now, if anyone wants the code here it is. :) It turns out I put the "close program" code too early and it doesn't work until the close button is pressed. So my player did move, but only for a second.
# Game Loop
running = True
while running:
if event.type == pygame.QUIT:
running = False
# if keystroke is pressed check whether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -5
if event.key == pygame.K_RIGHT:
playerX_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
In my game the player can shoot different coloured bullets using the WASD keys. The player can shoot bullets as fast as they can press the keys right now, which means you can mash the WASD keys and shoot a stream of bullets. I've tried creating a USEREVENT to make a cooldown for when the player can shoot, but I'm not entirely sure if I'm doing this right because the player doesn't shoot at all when I run the game.
#cooldown userevent
shot_cool = pygame.USEREVENT + 2
pygame.time.set_timer(shot_cool, 10)
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False #breaks out of the while loop
if event.type == pygame.KEYDOWN and event.type == shot_cool:
if event.key == pygame.K_w:
pygame.time.set_timer(shot_cool, 10)
player.shoot('red')
elif event.key == pygame.K_a:
pygame.time.set_timer(shot_cool, 10)
player.shoot('green')
elif event.key == pygame.K_s:
pygame.time.set_timer(shot_cool, 10)
player.shoot('white')
elif event.key == pygame.K_d:
pygame.time.set_timer(shot_cool, 10)
player.shoot('blue')
Is there any way I can make it so the player has a short cooldown until he can fire another bullet?
As I said in my comment, you fixed your problem perfectly. But since I already had it in my editor I decided I'd share a little more. Since all the keys function the same way, you can handle them in the same function block, using a dictionary. It could end up looking like this:
#cooldown userevent
EVT_SHOT_COOLDOWN = pygame.USEREVENT + 2
# shot cool down time
COOLDOWN_TIME_MS = 100
SHOT_KEYS = {
pygame.K_w:'red',
pygame.K_a:'green',
pygame.K_s:'white',
pygame.K_d:'blue',
}
running = True
shot_delay = False
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False #breaks out of the while loop
elif event_type == shot_cool:
shot_delay = False
elif event.type == pygame.KEYDOWN:
# handle shooting keys
if (event.key in SHOT_KEYS and not shot_delay):
pygame.time.set_timer(shot_cool, COOLDOWN_TIME_MS)
shot_delay = True
player.shoot(SHOT_KEYS[event.key])
# handle other keys (if any)
Thanks to the comment from RufusVS, what you said has worked perfectly. This is the working code:
shoot_cooldown = pygame.USEREVENT +2
pygame.time.set_timer(shoot_cooldown, 100)
shot_delay = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == shoot_cooldown:
shot_delay = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w and shot_delay == False:
pygame.time.set_timer(shoot_cooldown, 100)
shot_delay = True
player.shoot('red')
elif event.key == pygame.K_a and shot_delay == False:
pygame.time.set_timer(shoot_cooldown, 100)
shot_delay = True
player.shoot('green')
elif event.key == pygame.K_s and shot_delay == False:
pygame.time.set_timer(shoot_cooldown, 100)
shot_delay = True
player.shoot('white')
elif event.key == pygame.K_d and shot_delay == False:
pygame.time.set_timer(shoot_cooldown, 100)
shot_delay = True
player.shoot('blue')
#enable pygame mode
import pygame
pygame.init()
#create screen
screen = pygame.display.set_mode((900,600))
#Title + Logo
pygame.display.set_caption("Space Invader")
icon = pygame.image.load("chicken.png")
pygame.display.set_icon(icon)
#Player icon
player_icon = pygame.image.load("spaceship.png")
playerX = 400
playerY = 500
def player(x, y):
screen.blit(player_icon, (x, y))
#game loop
running = True
while running:
# backround colour RGB
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#If key pressed check wether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print("key left pressed")
if event.key == pygame.K_RIGHT:
print("key right pressed")
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
print("key stroke has benn released")
#Player change in coordinates
playerX += 0
playerY += 0
player(playerX, playerY)
pygame.display.update()
I have been learning about pygame and game programing using python during this quarantine. I have been doing this by watching a tutorial on youtube. Please don't downgrade me I have tried harder to make my question better last time it was my first question and got 2 downgrades. Thankyou for your time.
It s a matter of Indentation. In your code the pygame.KEYDOWN event is only evaluated in case of event.type == pygame.QUIT. "Move" the pygame.KEYDOWN event handling:
running = True
while running:
# backround colour RGB
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# <--| INDENTATION
#If key pressed check wether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print("key left pressed")
if event.key == pygame.K_RIGHT:
print("key right pressed")
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
print("key stroke has benn released")