I found a tutorial on youtube in that video the guy was running this code:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Testing")
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if event.type == pygame.QUIT:
When I write "pygame.quit:" instead of "pygame.QUIT:" (In the for loop) the window doesn't close. I'm a complete beginner. Is it a command that we have to write it capitalized? Can someone explain why?
Because pygame.quit() makes the system exit and exit() closes that window.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
UPDATE
Event objects have a member variable (also called attributes or properties) named type which tells us what kind of event the object represents. Pygame has a constant variable for each of possible types in the pygame.locals modules.
if event.type == pygame.QUIT:
Checks if the Event object’s type is equal to the constant QUIT
This is why we check if the Event object is a quit event, then the pygame.quit() and exit() functions are called. The pygame.quit() function is sort of the opposite of the pygame.init() function: it runs code that deactivates the Pygame library.
TLDR; QUIT denotes a type of Event and pygame.quit() is a Function
I use this and its work
running = True
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
Related
The following works:
while True:
for event in pygame.event.get():
match (event.type):
case pygame.QUIT:
pygame.quit()
but if I write without "pygame." in the case expression
while True:
for event in pygame.event.get():
match (event.type):
case QUIT:
pygame.quit()
the case catches all the events and not only the QUIT one. Other potential cases are also marked unreachable by python.
Why is that? print(pygame.QUIT) and print(QUIT) both display "256".
You cant use the QUIT keyword if you only had imported pygame, to use only QUIT you need to also import pygame locals:
import pygame # Default
from pygame.locals import * # Optional
Then you can use either pygame.QUIT or QUIT.
Also instead of matching these events you can use:
for event in pygame.event.get(): # Handle events
if event.type == pygame.QUIT: # Check if the X button is clicked
pygame.quit() # Quit pygame
Then you just only need to import pygame
I am trying to display text in pygame. I have managed to display it fine in the main loop, but then in the die() function, it doesn't appear.
def die():
while True:
deadText = text.render("You Died",1,red)
gameDisplay.blit(deadText,(500,100))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
All functions draw in buffer and you have to send buffer to VideoCard which will display to screen.
You need pygame.display.flip() or pygame.display.update() for this.
Here it is, I dont know what is wrong, I looked at other answers but I still dont know what is wrong?
import pygame
pygame.init()
gameWindow = pygame.display.set_mode((1000,600));
pygame.display.set_caption("Practice")
#game starts
gameActive = True
while gameActive:
for event in pygame.event.get():
#print event
if event.type == pygame.QUIT:
gameActive = False
pygame.quit()
quit()
You have pygame.quit() in your main loop, so after one iteration through the loop you are calling pygame.quit(), which causes pygame to no longer be initialized which creates the error of not having a display surface.
Moving pygame.quit() out of the main while loop fixes the issue.
Im running a Pygame window using a GUI. When for example, a user clicks on a button on the GUI, the Pygame window appears. However, when I want to quit the Pygame window, my GUI quits too. Im sure this is beacuse of the following lines of code:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
sys.exit() exits everything, which is why the IDE closes with the Pygame window. But how do I only close the Pygae Window? Ive tried this:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
break
But this doesnt work. Any suggestions?
The GUI im using Pyqt4 with Python 3.
I guess that your code snippet
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
break
is part of your main loop, running in an while loop, like
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
break
The problem is that break will exit the for loop, not the while loop, eventually leading to an exception since you exit pygame but are probably trying to draw an the screen etc. etc.
A simple fix is to use a variable as condition for your while loop, like
quit = False
while not quit:
for event in pygame.event.get():
if event.type==pygame.QUIT:
quit = True
...
or make sure to exit the while loop
while True:
if pygame.event.get(pygame.QUIT): # only check for QUIT event
break
for event in pygame.event.get():
...
This program infinite loops. Does nothing. Won't take input or print anything. Ideas?
import pygame
pygame.init()
running = 1
while(running):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
print "hi"
running = 0
The problem with your script is solely that there's no window that could capture the events.
You have to create and initialize a window with pygame.display.set_mode first.
import pygame
pygame.init()
# create a window that will capture the events
pygame.display.set_mode((200, 200))
running = 1
while(running):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
print "hi"
running = 0
Try the following:
import pygame, sys
pygame.init()
pygame.mixer.init(22050, -16, 2, 3072)
pygame.mixer.music.load("Kundara_Lake-of-Dust-320.mp3")
pygame.mixer.music.play(1, 0.0)
running = 1
while(running):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.mixer.music.stop()
pygame.quit()
running = 0
From http://www.pygame.org/docs/ref/pygame.html
pygame.quit
Uninitialize all pygame modules that have previously been initialized. When the Python interpreter shuts down, this method is called regardless, so your program should not need it, except when it wants to terminate its pygame resources and continue. It is safe to call this function more than once: repeated calls have no effect.
Note, that pygame.quit will not exit your program. Consider letting your program end in the same way a normal python program will end.
You are looping infinitely calling pygame.quit() due to while(1).
You need to update the screen. Try it with
screen = pygame.display.set_mode((640,360),0,32)
and in the loop, write
pygame.dispay.flip()
to close the window completely, you can use
sys.exit()
just be sure to include sys in your imports
pygame does not recognize the term 'input' or 'print'. It would be a lot simpler if it did!! Instead, to get text onto the screen, you must use 'drawText('message',font,surface,(xpos,ypos) putting your own text in 'message', your own surface name in 'surface' and the x and y co-ordinates in 'xpos' and 'ypos'.