How do i quit and print the events in one loop here - python

import pygame
pygame.init()
x = height,width = (800,600)
Display = pygame.display.set_mode(x)
pygame.display.set_caption("Blocky")
red = (157, 139, 215)
black = (0,0,0)
Display.fill(red)
pygame.draw.rect(Display,black,(120,450,600,50))
#It updates every frame
pygame.display.update()
excape = False
while not excape:
for dork in pygame.event.get():
print(dork)
if pygame.event == pygame.QUIT:
pygame.quit()
quit()
Here the print(dork) is working but when i click the exit button of the window it doesn't quit at all..
So how do i both print events and quit the application in 1 while loop?

First of all, you should update the screen in the while not excape loop.
Secondly, set the excape to True if pygame.event is equal to pygame.QUIT.
So your code will look like this:
import pygame, sys
pygame.init()
x = height,width = (800,600)
Display = pygame.display.set_mode(x)
pygame.display.set_caption("Blocky")
red = (157, 139, 215)
black = (0,0,0)
Display.fill(red)
pygame.draw.rect(Display,black,(120,450,600,50))
#It updates every frame
excape = False
while not excape:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
excape = True
pygame.quit()
sys.exit()
pygame.display.update()

You need to cycle through EVERY pygame event and check if the event is a quit.
while not excape:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
excape = True

Related

Is there a way to close the pygame window with a MOUSEBUTTONDOWN event?

I am trying to make a clickable image that exits pygame, but im not sure how to make it close. I have tried using the pygame.quit() and sys.exit() but that loads for a second but doesn't do anyhting. I will show the code I have here(the only relevant code is the x and y variables nad the exit button down the bottom):
import pygame, sys
clock = pygame.time.Clock()
from pygame.locals import *
pygame.init() # inititates Pygame
pygame.display.set_caption('Lightmind')
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) # initiates the window
logo = pygame.image.load("GameLogo.png").convert()
logo = pygame.transform.scale(logo, (256, 256))
start_button = pygame.image.load("StartButton.png").convert()
start_button = pygame.transform.scale(start_button, (256, 256))
exit_button = pygame.image.load("ExitButton.png").convert()
exit_button = pygame.transform.scale(exit_button, (256, 100))
x_2 = 560
y_2 = 400
fade_in = True
fade_out = True
fade_in_ball = True
fade_in_start = True
fade_in_exit = True
running = True
while running: # game loop
for event in pygame.event.get():
if event.type == QUIT:
running = False
pygame.quit()
sys.exit()
# fade in the logo
if fade_in == True:
for i in range(255):
screen.fill((0,0,0))
logo.set_alpha(i)
screen.blit(logo, (560,260))
pygame.display.flip()
clock.tick(60)
fade_in = False
# fade out the logo
if fade_out == True:
for i in range(255):
screen.fill((0,0,0))
logo.set_alpha(255-i)
screen.blit(logo, (560,260))
pygame.display.flip()
clock.tick(60)
fade_out = False
# fade in the start button
if fade_in_start == True:
for i in range(255):
start_button.set_alpha(i)
screen.blit(start_button, (560, 240))
pygame.display.flip()
clock.tick(60)
fade_in_start = False
# fade in the exit button
if fade_in_exit == True:
for i in range(255):
exit_button.set_alpha(i)
screen.blit(exit_button, (x_2, y_2))
pygame.display.flip()
clock.tick(60)
fade_in_exit = False
# make exit button exit game
if event.type == pygame.MOUSEBUTTONDOWN:
x_2, y_2 = event.pos
if exit_button.get_rect().collidepoint(x_2, y_2):
pygame.quit()
sys.exit()
pygame.display.update()
Any help is appreciated!
You're checking the event outside of your event loop. Move it up instead:
for event in pygame.event.get():
if event.type == QUIT:
running = False
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
x_2, y_2 = event.pos
if exit_button.get_rect().collidepoint(x_2, y_2):
pygame.quit()
pygame.event.get() get all the messages and remove them from the queue. See the documentation:
This will get all the messages and remove them from the queue. [...]
If pygame.event.get() is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.
You must handle the click detection in the event loop.
pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, but it returns a rectangle that always starts at (0, 0) since a Surface object has no position.
The Surface is placed at a position on the display with the blit function.
You've to set the location of the rectangle, either by a keyword argument, e.g:
running = True
while running: # game loop
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
extit_button_rect = exit_button.get_rect(topleft = (x_2, y_2))
if extit_button_rect.collidepoint(event.pos):
running = False
# [...]
pygame.quit()
sys.exit()

can you help me it wont display my rectangle

import pygame,sys
pygame.init()
screen = pygame.display.set_mode((800,600))
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.draw.rect(screen, (255,0,0),(400,300,50,50))
pygame.display.flip()
As #Furas writes, the screen update code is not being called from within the loop. Python uses indentation to designate code blocks, so if the function call (or other code section) is not indented to the correct column, it is literally a completely different set of operations.
Since a piece of sample code is worth a thousand words:
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((800,600))
game_over = False
while not game_over:
# Handle user-events
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# Re-draw the screen
pygame.draw.rect(screen, (255,0,0), (400,300,50,50))
pygame.display.flip()
pygame.quit()
sys.exit()

Pygame window freezes on quit

I'm relatively new to python and very new to pygame. I'm trying to use pygame. All programs seem to work fine, except when I try to quit. The window freezes ("application not responding") and I have to force quit it. I'm using OSX, python 3.6, and running it through sublime text if that matters. Code is below:
import pygame
done = False
size = (400,400)
screen = pygame.display.set_mode(size)
while done==False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
done = True
pygame.display.quit()
pygame.quit()
Thanks for your help!
Try this one, it works for me:
import sys
import pygame
size = (400,400)
screen = pygame.display.set_mode(size)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
you can just do this:
import pygame
done = False
size = (400,400)
screen = pygame.display.set_mode(size)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
this is what i do
or you can use this:
import pygame
pygame.init()
running = True
width, heigth = 800, 600
size = (width, heigth)
screen = pygame.display.set_mode(size)
while running:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
don't use pygame.display.quit()

Pygame not restoring from pause

I'm trying to pause my game by pressing the 'p' key, but after it pauses it does not unpause when p is pressed again. Here are the relevant segments of my code, I'd like to know how to fix this and/or if there are better alternative methods of implementing a game pause.
pause = False
while True:
if not pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#game code
keys = pygame.key.get_pressed()
#if key up
elif keys[pygame.K_p]:
pause = True #appears to work correctly, screen freezes and
#prints "PAUSED" every tick.
#more game code
pygame.display.update()
fpsClock.tick(FPS)
else:
print("PAUSED")
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.type == pygame.K_p:
pause = False
pygame.display.update()
fpsClock.tick(FPS)
The code below does the trick.
I have added in if not pause: ... else: section also a handler for a keypress because without it there will be no chance to come out of the pause (the part if not pause won't never ever be run without this keypress check if once paused).
import pygame
pygame.init() # start PyGame (necessary because 'import pygame' doesn't start PyGame)
colorWhite = (255, 255, 255) # RGB color for later use in PyGame commands (valueRed=255, valueGreen=255, valueBlue=255)
colorWhite = (255, 255, 255) # RGB color for later use in PyGame commands (valueRed=255, valueGreen=255, valueBlue=255)
winDisplay = pygame.display.set_mode((1024, 768)) # set PyGame window size to 1024x768 pixel
pygame.display.set_caption("Minimal PyGame Test Script")
winDisplay.fill(colorWhite)
fpsClock = pygame.time.Clock()
FPS = 15
pause = False
pauseCounter = 0
while True:
if not pause:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_p:
pause = not pause
#game code
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#more game code
pygame.display.update()
fpsClock.tick(FPS)
else:
pauseCounter += 1
print(pauseCounter, "PAUSED")
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_p:
pause = not pause
#more game code
pygame.display.update()
fpsClock.tick(FPS)

Background image does not appear on screen

I'm trying to run code from a tutorial I found online.
But when I run this code, my background image does not appear, even if it loaded correctly.
Why?
bif = "castle.jpg"
import pygame, sys, pygame.mixer
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1280,960),0, 32)
background = pygame.image.load(bif).convert()
pygame.display.set_caption("castlevania ultimate")
hit_sound = pygame.mixer.Sound("02.wav")
hit = False
if hit is True:
hit_sound.play()
sound = pygame.mixer.Sound("castlevania_1.wav")
sound.play()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
pygame.display.update()
You never call pygame.display.update() in your while loop.
Python is indentation sensitive, so the loop should look like this:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
pygame.display.update()
Note that pygame.display.update() is now inside the while loop.

Categories