I want to close the game after was pressed the esc button. how can I do? And where should I place it?
I also found some problems that I can't solve, so if you solve them for me I would be happy
This is the code:
#Import Libraries
import pygame
import random
#PyGame Initialization
pygame.init()
#Images Variables
background = pygame.image.load('images/sfondo.png')
bird = pygame.image.load('images/uccello.png')
base = pygame.image.load('images/base.png')
gameover = pygame.image.load('images/gameover.png')
tube1 = pygame.image.load('images/tubo.png')
tube2 = pygame.transform.flip(tube1, False, True)
#Display Create
display = pygame.display.set_mode((288,512))
FPS = 60
#Define Functions
def draw_object():
display.blit(background, (0,0))
display.blit(bird, (birdx, birdy))
def display_update():
pygame.display.update()
pygame.time.Clock().tick(FPS)
def animations():
global birdx, birdy, bird_vely
birdx, birdy = 60, 150
bird_vely = 0
#Move Control
animations()
while True:
bird_vely += 1
birdy += bird_vely
for event in pygame.event.get():
if ( event.type == pygame.KEYDOWN
and event.key == pygame.K_UP):
bird_vely = -10
if event.type == pygame.QUIT:
pygame.quit()
draw_object()
display_update()
Well you do so by implementing below code snippet in your code:
running = True
while running:
# other code
event = pygame.event.wait ()
if event.type == pygame.QUIT:
running = False # Be interpreter friendly
pygame.quit()
Make sure that you call pygame.quit() before you exit your main function
You can also reference this thread
Pygame escape key to exit
You must terminate the application loop when the QUIT event occurs. You have implmented the QUIT event, but you don't terminate the loop. Add a variabel run = True and set run = False when the event occurs.
To terminate the game when ESC is pressed you have to implement the KEYDOWN event. Set run = False when the KEDOWN event occurs and event.key == pgame.K_ESC:
run = True
while run:
bird_vely += 1
birdy += bird_vely
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.key == pgame.K_ESC:
run = False
elif event.key == pygame.K_UP:
bird_vely = -10
draw_object()
display_update()
pygame.quit()
exit()
Related
I am new to pygame. Whenever I press the key "Delete". It prints "key" continuously. I only want it to print it only one time. How can I do this ?
import pygame
import sys
sprite = "sprite_1.jpg"
position = (20,0)
canvas = pygame.display.set_mode((952, 592))
color = (255,255,255)
exit = False
back = pygame.image.load("th.jpg").convert_alpha()
back = pygame.transform.scale(back, (952,592))
Sprite = pygame.image.load(sprite)
Sprite.set_colorkey((255,255,255))
Sprite = pygame.transform.scale(Sprite, (35,35))
pygame.display.set_caption("Game")
while not exit:
canvas.fill(color)
canvas.blit(back, dest = (0,0))
canvas.blit(Sprite, dest=position)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_DELETE):
print("hi")
pygame.display.update()
Please tell the solution. Thanks in advance
It is a matter of Indentation. The event has to be handled in the event loop instead of the applicaition loop:
while not exit:
canvas.fill(color)
canvas.blit(back, dest = (0,0))
canvas.blit(Sprite, dest=position)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
# INDENTATION
#-->|
if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_DELETE):
print("hi")
pygame.display.update()
count=0
while not exit:
enter code herecanvas.fill(color)
canvas.blit(back, dest = (0,0))
canvas.blit(Sprite, dest=position)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
if (event.type == pygame.KEYDOWN):
if count<=0:
if (event.key == pygame.K_DELETE):
print("hi")
else:
continue
pygame.display.update()
So I created a Python game Tetris based on Youtube tutorial:
https://www.youtube.com/watch?v=zfvxp7PgQ6c&t=2075s
But the pygame.error: display Surface quit occurs.
I have tried to add "break", "sys.exit()", "QUIT" after the pygame.quit but does not work.
Does anyone know how to solve it? Here is the code: (You can skip to the def main_menu)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run == False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_piece.x -= 1
if not (valid_space(current_piece, grid)):
current_piece.x += 1
if event.key == pygame.K_RIGHT:
current_piece.x += 1
if not (valid_space(current_piece, grid)):
current_piece.x -= 1
if event.key == pygame.K_DOWN:
current_piece.y += 1
if not (valid_space(current_piece, grid)):
current_piece.y -= 1
if event.key == pygame.K_UP:
current_piece.rotation += current_piece.rotation + 1 % len(current_piece.shape)
if not (valid_space(current_piece, grid)):
current_piece.rotation -= 1
shape_pos = convert_shape_format(current_piece)
for i in range(len(shape_pos)):
x, y = shape_pos[i]
if y > -1:
grid[y][x] = current_piece.color
if change_piece:
for pos in shape_pos:
p = (pos[0], pos[1])
locked_positions[p] = current_piece.color
current_piece = next_piece
next_piece = get_shape()
change_piece = False
score += clear_rows(grid, locked_positions) * 10
draw_window(win, grid, score, last_score)
draw_next_shape(next_piece, win)
pygame.display.update()
if check_lost(locked_positions):
draw_text_middle(win, "You Lost!", 80, (255,255,255))
pygame.display.update()
pygame.time.delay(1500)
run = False
update_score(score)
def main_menu(win):
run = True
while run:
win.fill((0,0,0))
draw_text_middle(win, 'Press any key to play', 60, (255,255,255))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
main(win)
pygame.display.QUIT()
win = pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption('Tetris')
main_menu(win)
Updated code:
def main_menu(win):
run = True
while run:
win.fill((0,0,0))
draw_text_middle(win, 'Press any key to play', 60, (255,255,255))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
main(win)
pygame.quit()
win = pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption('Tetris')
main_menu(win)
In your main_menu loop you are telling it to loop while local boolean run == True. This is okay, but you should as people mentioned in the comments do a pygame.quit() and optionally quit() (closes the window) instead of the pygame.display.quit() and sys.exit() that you have right now.
The second problem occurs if you start the game by going into the main loop. I assume that the main loop runs your events function shown at the top?
Depending on how you have written the code, the boolean run in the event function is
local. This means that it will not change the value of the run you are using in your
main loop (nor change it in the main_menu loop). I would suggest to transfer into OOP and create a self.run boolean instead,
or else you need to make the boolean run global.
And you should in the event function write this instead of what you have now at the
top:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
Hope this helps!
I have some python code that uses a pygame window to establish which key is being pressed. When a key is pressed, the code heads off and does things before coming back to see what the next key pressed might be.
The problem I have is that if the user presses a key repeatedly, even while the 'code heads off and does things', pygame seems to remember what has been pressed rather than waiting for the next keypress. What I want is for the code to ignore any keypresses while the 'go and do stuff' is done then once that's finished, get the next keypress. Hope this makes sense!
import pygame
import time
pygame.init()
screen = pygame.display.set_mode((450,282))
screen.fill((0,0,0))
pygame.display.flip()
clock = pygame.time.Clock()
done = False
def go_and_do_things():
print("doing things")
time.sleep(2)
print("things done")
# Loop as long as done == False
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.KEYDOWN:
keypressedcode = event.key # This is the ASCII code
print("keypressedcode is " + str(keypressedcode))
go_and_do_things()
elif event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
clock.tick(60)
time.sleep(4)
pygame.quit()
You could use pygame.event.clear. As written below, it will discard any keypresses during go_and_do_things().
while not done:
for event in pygame.event.get(): # User did something
# Any key down
if event.type == pygame.KEYDOWN:
keypressedcode = event.key # This is the ASCII code
print("keypressedcode is " + str(keypressedcode))
go_and_do_things()
pygame.event.clear(eventtype=[pygame.KEYDOWN,
pygame.KEYUP)
elif event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
clock.tick(60)
This is a fairly simple problem, you just need to add another
event.type for keyup. You also need to add a variable to make it stop
running which I will label as stopV if event.type == pygame.KEYUP:
stopV = True This should let you instantly stop it when you want to. edit because I want to reword this:
Re-doing this to make it more clear.
import pygame
import time
pygame.init()
screen = pygame.display.set_mode((450,282))
screen.fill((0,0,0))
pygame.display.flip()
clock = pygame.time.Clock()
done = False
unClick = False
def go_and_do_things():
if unClick == False
print("hello")
# do anything you want in the function here
else
return
while not done:
for event in pygame.event.get(): # User did something
# Any key down
if event.type == pygame.KEYDOWN:
keypressedcode = event.key # This is the ASCII code
print("keypressedcode is " + str(keypressedcode))
go_and_do_things()
if event.type == pygame.KEYUP:
unClick == True
elif event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
clock.tick(60)
I am coding with python on my raspberry pi. Python isn't my best language so bear with me.
I need a simple code that responds to key strokes on my keyboard. I'd doing this so I can set the Pulse Width Modulation, but I don't need that code, I already have it. My main concern is I am struggling to understand the pygame functionality required for my task.
I would like to be able to type a key, such as "up arrow" ↑ and have the program output "up pressed" for every millisecond the up arrow is pressed.
The pseudo-code would look like:
double x = 1
while x == 1:
if input.key == K_UP:
print("Up Arrow Pressed")
if input.key == K_q
x = 2
wait 1ms
pygame.quit()
Again I have no clue what to import or call due to not knowing the syntax.
Here's some code that will check if the ↑ key is pressed:
import pygame
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode([320,240])
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
print("Up Arrow Pressed")
elif keys[pygame.K_q]:
done = True
clock.tick(1000)
pygame.quit()
Note that clock.tick(1000) will limit the code to one-thousand frames per second, so won't exactly equate to your desired 1 millisecond delay. On my PC I only see a frame rate of around six-hundred.
Perhaps you should be looking at the key down and key up events and toggle your output then?
import pygame
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode([320,240])
done = False
output = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
output = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
output = False
elif event.key == pygame.K_q:
done = True
pygame.display.set_caption(f"Output Status {output}")
clock.tick(60)
pygame.quit()
If you run this, you'll see the title of the window change whilst the ↑ key is pressed.
what i am trying to achieve
I am attempting to get the enemy to do a set of movements with a picture swap that are timed and in a ordered sequence that repeats.
the timing is not doing milliseconds?
what is happening
After a long time the enemy does first movement then long pause images some times swap sometimes not.
I am not getting any exceptions or errors when I run it.
Also when I add the userevent into the game loop above the keydown events I break the player movement.
Github to images and full code
Below is the most successful of the things I tried so far
I created the desired numbered USEREVENT in ordered sequence
# Create a bunch of AI events
ai_uppercut_event = pygame.USEREVENT + 1
ai_l_punch_event = pygame.USEREVENT + 2
ai_r_punch_event = pygame.USEREVENT + 3
ai_r_dodge_event = pygame.USEREVENT + 4
ai_l_dodge_event = pygame.USEREVENT + 5
Gave the enemy ai character a start position
enemy_x = 235
enemy_y = 145
gave the enemy ai a movement amount
aimovex = 40
aimovey = 40
defined the images to swap out for the different moves
# Define ai images for different moves
enemy_default = pygame.image.load("enemy_default.png")
ai_uppercut_image = pygame.image.load("enemy_uppercut.png")
ai_l_punch_image = pygame.image.load("enemy_left.png")
ai_r_punch_image = pygame.image.load("enemy_right.png")
ai_r_dodge_image = pygame.image.load("enemy_dodge_right.png")
ai_l_dodge_image = pygame.image.load("enemy_dodge_left.png")
set up the clock and tick and timer for events
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
start_time = pygame.time.get_ticks()
# set timer for the ai movement events
pygame.time.set_timer(ai_uppercut_event, MOVE_UPPERCUT)
pygame.time.set_timer(ai_l_punch_event, MOVE_PUNCH_LEFT)
pygame.time.set_timer(ai_r_punch_event, MOVE_PUNCH_RIGHT)
pygame.time.set_timer(ai_r_dodge_event, MOVE_DODGE_RIGHT)
pygame.time.set_timer(ai_l_dodge_event, MOVE_DODGE_LEFT)
in the main event loop
# -------- Main Program Loop -----------
paused = False
running = True
while running:
# --- Main Event Loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for event in pygame.event.get():
if event.type == ai_uppercut_event:
aimovex =+ 80
activeEnemyImage = ai_uppercut_image
elif event.type == ai_l_punch_event:
aimovex =+ 80
activeEnemyImagee = ai_l_punch_image
elif event.type == ai_r_punch_event:
aimovex =- 80
activeEnemyImage = ai_r_punch_image
elif event.type == ai_r_dodge_event:
aimovey =+ 80
activeEnemyImage = ai_r_dodge_image
elif event.type == ai_l_dodge_event:
aimovey =- 80
activeEnemyImage = ai_l_dodge_image
enemy_x += aimovex
enemy_y += aimovey
#elif event.type == reloaded_event:
# when the reload timer runs out, reset it
#reloaded = True
#pygame.time.set_timer(reloaded_event, 0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if event.key == pygame.K_SPACE:
paused = not paused
# --- Event Processing ---
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
movex =- 40
activeCharacterImage = player_left
elif event.key == pygame.K_RIGHT:
movex =+ 40
activeCharacterImage = player_right
I suspect your issue has to do with your doubled event polling loops:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for event in pygame.event.get():
if event.type == ai_uppercut_event:
# ...
Because there are two loops, you'll often have your events lost to the first loop which only checks if they're QUIT events and ignores all other kinds of events.
To fix this, combine the two loops and use an elif for the first non-QUIT event check:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == ai_uppercut_event:
# ...