This question already has answers here:
Pygame unresponsive display
(1 answer)
Pygame window not responding after a few seconds
(3 answers)
Closed 2 years ago.
I am trying to understand why my program is loosing respond when I trying exit from game. I make some test and here is code:
import pygame
import time
pygame.init()
run=True
pygame.display.set_caption("test")
win=pygame.display.set_mode((200,200))
while run==True:
pygame.time.delay(16)
keys=pygame.key.get_pressed()
#for event in pygame.event.get():
#if event.type == pygame.QUIT:
#run=False
win.fill((125,125,125))
if keys[pygame.K_q]:
run=False
pygame.display.update()
pygame.quit()
If execute this code - windows can't properly exit from program when user pressed key "q".
If remove comments symbols # form 14,15,16 strings, all will work properly. Hitting "q" key will exit from program normally.
Only one question - why???
You have to handle the events in the application loop. See pygame.event.get() respectively pygame.event.pump():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
If you don't handle the events, pygame.key.get_pressed() will stop working. The states of the keys returned by pygame.key.get_pressed() are evaluated internally when the events are handled.
At least you've to call pygame.event.pump():
while run==True:
pygame.time.delay(16)
pygame.event.pump()
keys=pygame.key.get_pressed()
if keys[pygame.K_q]:
run=False
win.fill((125,125,125))
pygame.display.update()
This question already has answers here:
python: sys is not defined
(4 answers)
Closed 3 years ago.
I have some Pygame code that is working fine up until I try to close the program. The program keeps telling me that sys is not defined. I am using the more python programming for the absolute beginner to get the hang of Pygame.
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
My expected result is that the window will close but instead the window stays open and this error is spat out: NameError: name 'sys' is not defined
sys is a module that you need to import, preferably at the beginning.
import sys
This question already has answers here:
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Is there any other way to load a resource like an image, sound, or font into Pygame? [closed]
(1 answer)
Closed 2 years ago.
I recently started to learn to use the pygame module and I've run into my first blocker. I an trying to paste an image of a circle in a grid that I created on top of my solid background. For some reason when I blit the image and update the screen nothing appears and I am not sure why as I am pretty sure that my code is correct. I've pasted my code below. Maybe you guys will see what I am missing.
from pygame import *
def main():
init()
display.set_caption("Othello")
screen = display.set_mode((480,480))
screen.fill((0,128,0))
draw.line(screen,(0,0,0),(60,0),(60,480))
draw.line(screen,(0,0,0),(120,0),(120,480))
draw.line(screen,(0,0,0),(180,0),(180,480))
draw.line(screen,(0,0,0),(240,0),(240,480))
draw.line(screen,(0,0,0),(300,0),(300,480))
draw.line(screen,(0,0,0),(360,0),(360,480))
draw.line(screen,(0,0,0),(420,0),(420,480))
draw.line(screen,(0,0,0),(0,60),(480,60))
draw.line(screen,(0,0,0),(0,120),(480,120))
draw.line(screen,(0,0,0),(0,180),(480,180))
draw.line(screen,(0,0,0),(0,240),(480,240))
draw.line(screen,(0,0,0),(0,300),(480,300))
draw.line(screen,(0,0,0),(0,360),(480,360))
draw.line(screen,(0,0,0),(0,420),(480,420))
display.flip()
image_white = image.load("White_Side.jpg")
image_black = image.load("Black_Side.jif")
screen.blit(image_white,(0,0))
screen.blit(image_black,(60,0))
display.update()
running = True
while running:
for pygame_event in event.get():
if pygame_event.type == QUIT:
running = False
if __name__=="__main__":
main()
This question already has answers here:
Trying to display a png file in pygame using pygame.display.update, and it shows for less than a second then disappears.
(2 answers)
Closed 7 years ago.
We have recently moved our school lab to Mac, and when I run some code to display an image from a file, the display appears then disappears immediately. I am using the 3.4 interpreter in Pycharm and Pygame version 1.9.2. Can someone please help?
Here is my code:
# displays a hard-coded filename in a window
import pygame
pygame.init()
picture = pygame.image.load("cards/S01.png")
pygame.display.set_mode(picture.get_size())
main_surface = pygame.display.get_surface()
main_surface.blit(picture, (0, 0))
pygame.display.update()
If the code you posted is the complete code, then that is the reason why its exiting immediately, because immediately after calling pygame.display.update() , the program exits. I do not think this should have been working before.
One thing you can do for this , add a loop to run till the user presses close, after your code -
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Put a
time.sleep(10)
after pygame.display.update() see if it stays up longer.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Pygame programs hanging on exit
Ran this simple program:
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((800,600))
while True:
#process input
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
Got this simple message in IDLE:
Traceback (most recent call last):
File "C:/Users/Aerovistae/Desktop/GD_in_class", line 12, in <module>
sys.exit()
SystemExit
And the program stops responding and has to be Ctrl+Alt+Del'd. I can't see where I'm going wrong here, this is as basic as I can get. I was following what a professor did in lecture, I don't see any difference between my code and his. Can anyone suggest what might cause the problem?
Proper way to quit pygame is to call
pygame.quit()
after the main run loop.
Read From the pygame documentation: http://www.pygame.org/wiki/FrequentlyAskedQuestions#In IDLE why does the Pygame window not close correctly?
Just exit the main run loop instead of sys.exit() and end the program.