Stopping a delay in pygame/python [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
I am using pygame to create something and I need to have a delay for about 10 seconds that is interrupted when a button is pushed. When I tried to do this, it paused on the delay and it was unable to detect for button pushes. I was wondering if this is even possible or if there are any alternatives. I have tried:
pygame.time.delay(10000)
if hit1: #hit1 is a bool representing keydown
Blue1() #my function to draw a shape

I happened to have come across the same problem 2 years ago (when I was 12) so I think I understand what you mean.
First import sleep from time
from time import sleep
Then you can try this
#code before 10 sec count
interrupt = false
millisecondPassed = 0
while true:
sleep(0.01)
millisecondPassed += 1
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_b:
interrupt = true
if millisecondPassed >= 10000 or interrupt == true:
break
#code after interruption
As for reference:
10 seconds = 10000 milliseconds

Related

Why do key presses sometimes work and sometimes not? [duplicate]

This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?
(1 answer)
Closed last month.
I have some code to check if "q" is pressed and then I make an instance of a class. The problem is that sometimes it recognizes a press of the button and sometimes it doesn't. For example if I press "q" 10 times, sometimes it will recognize the press 3 times, sometimes 6 times, sometimes 7 times. So how can I fix it so it will recognize the press every single time? Some people said that it's probably because I call pygame.event.get() multiple times. Because of that I have tried getting events once in the whole script, but it's still the same result.
Code:
pygame.init()
fpsClock = pygame.time.Clock()
screen = pygame.display.set_mode((1920, 1080)
while True:
#Input
self.events = self.pygame.event.get()
for event in self.events:
if event.type == self.QUIT:
self.pygame.quit()
self.sys.exit()
if event.type == self.pygame.KEYDOWN:
if event.key == self.pygame.K_q:
#makes an instance
pygame.display.flip()
fpsClock.tick(60)

Hi. Just getting started with Pygame. This code if not running i don't know why [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
import pygame
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
running = True
while running:
for event pygame.event.get():
if event.type == pygame.QUIT:
running = False
you are missing an "in" in your for loop
import pygame
pygame.init()
pygame.display.set_mode((800, 600))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
also, check out the Stackoverflow guide in order to use proper font for your code, have a great day

Pygame window is not responding after just making it

I wanted to make a game using pygame and I started my code making the pygame window
import pygame
pygame.init()
pygame.display.set_mode((1500, 1000))
But every time I run the program, my pygame window does not respond.
Does anyone know what is happening, this did happen to me on previous codes too but it wasn't all the time like it is now
you need a game loop to start the game and you need events to work on.
for python code i suggest:
import pygame
pygame.init()
pygame.display.set_mode((1500, 1000))
start_game = True
while start_game:
print("Game Started!")
and for the events you can use this code in the while loop:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
print("mouse button up pressed")
the source of this code is : RipTutorial
I assume that by 'my pygame window does not respond' you mean that you believe the window is not displaying. I think that you are just missing it because it is closing too quickly.
The window does display, but because your program ends right after you create the window, the window is immediately closed again. This is likely just happening so quickly that you are not noticing it. If you want to see this, then just put a call to sleep after opening the window and you will see the window sit there until the sleep expires and then it will close.
If you do not mean that and you are seeing the window but expecting it to respond, then I do not understand what you expect it to respond to. You do not have any code that tries to make it respond to anything.
EDIT:
I just noticed that #AnassABEA suggests that in a comment under his answer, though not in his answer. #AnassABEA you should update your answer to include this since I believe that is the actual answer to his question, not the missing event loop.
Add this below to your code:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
Lines 4 to 5 of the code are for closing pygame when the window is closed.

How should I properly program an immediate exit procedure in pygame? [duplicate]

This question already has answers here:
How to wait some time in pygame?
(3 answers)
Move an object every few seconds in Pygame
(2 answers)
Closed 1 year ago.
I have this piece of code in the end of my main function in a python pygame script. This seems to be the correct way to handle an exit with a simple keypush.
running = True
while running == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
elif event.type == pygame.KEYDOWN:
running = False
break
if running == False:
break
draw(screen, background)
pygame.quit() # quits pygame
sys.exit()
However in this draw function I also have the following line right before the closing accolade:
pygame.time.delay(randint(2,5) * 1000)
This line of code makes the game wait anywhere from 2 till 5 seconds. Apparently this delay code creates an extra delay of about 3 loops in detecting the key pushes.
My question is: how do I properly execute an immediate exit with a key push without waiting for the delay?
Edit: Solution (credit to HÃ¥ken Lid)
animate_event = pygame.USEREVENT
pygame.time.set_timer(animate_event, 1000)
while True:
if pygame.event.get(pygame.QUIT): break
if pygame.event.get(pygame.KEYDOWN): break
for event in pygame.event.get():
if event.type == animate_event:
animate(screen, background)
# reset event
pygame.time.set_timer(animate_event, randint(2,5)*1000)
pygame.quit()
sys.exit()

Pygame Music Queue not functioning [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I've been trying to figure this out all day without success:
How do you set up a playlist of songs to play in pygame? The queue command doesn't seem to work for me. All that happens is that the first song I load will play all the way through and then the second won't start. The pygame documentation says this is all I need. Am I making an obvious mistake?
import pygame
pygame.init()
pygame.mixer.init
screen=pygame.display.set_mode((800,450))
pygame.mixer.music.load('mimages\\sounds\\droll.wav')
pygame.mixer.music.play(0,0.0)
pygame.mixer.music.queue('mimages\\sounds\\hip2014.wav')
running=True
clock=pygame.time.Clock()
while running:
clock.tick(2)
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
holdon=False
if event.type==pygame.KEYDOWN and event.key==pygame.K_ESCAPE:
running=False
holdon=False
print pygame.mixer.music.get_busy()
The queue () function in the Pygame module does not work correctly.
I created a function that works without queue () function.
I don't know why it doesn't work. :)
But luckily there's a code that works.
try it:
import pygame
import time
from tkinter import *
root = Tk()
c = ["music1.mp3","music2.mp3","music3.mp3"] #you_can_add_more
x= 0
def music():
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(c[x])
pygame.mixer.music.play(0)
que()
def que():
global x, c
pos = pygame.mixer.music.get_pos()
if int(pos) == -1:
x += 1
pygame.mixer.music.load(c[x])
pygame.mixer.music.play(0)
root.after(1, que)
music()
root.mainloop()
Try this instead:
pygame.mixer.music.load('mimages\\sounds\\droll.wav')
pygame.mixer.music.queue('mimages\\sounds\\hip2014.wav')
pygame.mixer.music.play()
This allows you to build up the queue, then play through it all at once. If you want to play different files later, try running pygame.mixer.music.stop() (if the queue hasn't finished yet) then use load() (optionally followed by one or more queue() statements) then play() again.

Categories

Resources