This question already has answers here:
pygame.event.get() not returning any events when inside a thread
(1 answer)
Is it possible to make the keyboard module work with pygame and threading
(1 answer)
Closed 1 year ago.
I was writing a small program in python 2.7.10 with pygame. Im using multithreading to play sounds (with winsound) and still draw things fast. I was trying to replicate a similar idea of some videos i saw on youtube of sorting algorithms. Unfortunately it runs for about 10 seconds then randomly crashes, the sounds keep going but the pygame window goes not responding and the drawing does not change. Even if i put a print "hi" in the code somewhere it will still be printing in the console but pygame will still have gone not responding. Cant think of whats wrong but it may be something with the multithreading (im very new to it!)
from random import randint
import pygame,winsound,threading,time
from pygame.locals import *
screen=pygame.display.set_mode([1000,500])
def bubbleSort(listName):
sorting=True
while sorting:
listChanged=False
for i in range(len(listName)-1):
draw(listName,1000/len(listName),500/11,i)
if listName[i] < listName[i+1]:
listName[i],listName[i+1]=listName[i+1],listName[i]
threading.Thread(target=playsound,args=([listName[i]])).start()
listChanged=True
if not listChanged:
sorting=False
time.sleep(10)
pygame.quit()
def playsound(frequency):
winsound.Beep(frequency*100,100)
def draw(listName,width,height,comparing):
print "hi"
screen.fill([0,0,0])
for i in range(len(listName)):
if i == comparing or i == comparing+1:
pygame.draw.rect(screen,[0,255,0],[width*(len(listName)-i),500-(height*listName[i]),width,500])
else:
pygame.draw.rect(screen,[255,255,255],[width*(len(listName)-i),500-(height*listName[i]),width,500])
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
pygame.display.update()
list1=[]
for i in range(100):
list1.append(randint(1,10))
threading.Thread(target=bubbleSort,args=([list1])).start()
Related
This question already has answers here:
Pygame window not responding after a few seconds
(3 answers)
Pygame unresponsive display
(1 answer)
How does the for event in pygame.event.get() in python work?
(1 answer)
Closed 3 months ago.
So, a while true loop that only passes, freezes the window. But a while true loop that checks the pool of events for a Quit event won't freeze. Why is that? checking the events take enough time to prevent the program from overloading? What is going on inside the hood?
Loop that freezes the Pygame window:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
while True:
pass
Loop that won't freeze the Pygame window:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
running = True
while running:
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
So if both are running infinitely (The second one virtually, supposed that the QUIT event is never called), why the first one consumes so many resources that it overloads it an freezes the window, but the second one "behaves" and doesn't overload it, even if the exit condition of the loop is not called? Is there some waiting time in the pygame.event.get() function?
I tried looking for info on Google but I got lost in subprocesses, etc.
This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 2 years ago.
i have no problem of syntax or any erorr its just a problems when i run the program
import pygame
pygame.init()
# generate window
pygame.display.set_caption("shooter Game")
pygame.display.set_mode((1080, 720))
running = True
while running:
# if the player close the window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
print("game closed")
sceenshot of the code and the 🚀 of python that not stoping to jump
You don't update the display, which you have to do with
pygame.display.update()
You can add it right after pygame.display.set_mode((1080, 720))
Try like this and tell me if it works :)
This question already has an answer here:
Pygame error : video system not initialized [duplicate]
(1 answer)
Closed 2 years ago.
Why this error is coming I am not understanding can anyone please tell why is it coming and how to correct it?
For every event in the event queue, you call pygame.quit(), which will deinitialize all pygame modules:
pygame.quit()
uninitialize all pygame modules
quit() -> None
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 as repeated calls have no effect.
Note
Calling pygame.quit()uninitialize all pygame modules will not exit your program. Consider letting your program end in the same way a normal Python program will end.
So in the next iteration of the while loop, the call to pygame.event.get() will fail because the video system is no longer initialzed.
You should check the type attribute of the event object against the pygame.QUIT constant, and not call the pygame.quit() function:
...
if event.type == pygame.QUIT:
...
Usually there's no need to ever call pygame.quit().
So I just finished up with a good amount of learning python and working on learning pygame. So I am following along with this free online book, and the first thing of code in the book of making a window with hello world on the top is giving me an error:
Heres the code:
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
while True: # main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Heres the error:
File "pygame.py", line 9
if event.type == QUIT:
^
IndentationError: expected an indented block
Thanks in advance!
As presented, this code works fine at this end. If you are still having a problem I suggest you look at tabs vs spaces in your editor.
Be aware that Python is a space sensitive language. The compiler actually cares whether there is a mix of spaces and tabs throughout the program.
Stick to one of either kind throughout your code and this will resolve the issue.
This question already has answers here:
Problems getting pygame to show anything but a blank screen on Macos
(10 answers)
pygame installation issue in mac os
(5 answers)
Closed 2 years ago.
I wanted to blit an image in python, a png image. But for some reason about half the image has the wrong pixel colours, it looks all messy, having random coloured pixels in it. I have made a few games on my Macbook Pro (OSX) using pygame with python 3.4, and I had no trouble when it came to blitting images. Here's the code:
import pygame
import os
w = pygame.display.set_mode((300,300))
bug = os.path.join("/Users/Snyman/Desktop/images/double-block_spm_R.png")
bug = pygame.image.load(bug).convert_alpha()
bug = pygame.transform.scale(bug,(64,64))
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
w.fill((255,255,255))
w.blit(bug,(10,10))
pygame.display.update()
pygame.quit()
"bug" is my image