Hello, today I wondered if it would be possible to make a delay in the background or something so that the program does not stop. it's possible?
I would like to make every 2 seconds a rectangle move to a position x and after another 2 seconds it disappears. All this without stopping the other processes.
Edition:
After doing the code I have a problem. By putting that it disappears in exactly twice the time in which it appears, there is a problem and that is that it does not disappear because the appearance and disappearance contradict each other.
Code to better explain:
here is the class:
class Laser1(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("sprites/laserH.png")
self.rect = self.image.get_rect()
self.rect.left = 176
self.rect.top = 400
def update(self, dt):
pass
def move_1(self, dt):
self.rect.left = 176
self.rect.top = 400
def move_2(self, dt):
self.rect.left = 1000
self.rect.top = 700
here are the timers:
pygame.time.set_timer(pygame.USEREVENT + 1, 700)
pygame.time.set_timer(pygame.USEREVENT + 2, 1400)
here is the event detection:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.USEREVENT + 1:
laser1.move_1(dt)
elif event.type == pygame.USEREVENT + 2:
laser1.move_2(dt)
it's as if USEREVENT + 2 never happened. Some help? Thanks.
In PyGame there are two methods that can and usually are used for this type of task:
pygame.time.get_ticks
: This method gives us the milliseconds passed since itpygame.init()
was called.pygame.time.set_timer
: Allows launching an event repeatedly every x milliseconds and that we can capture in the event queue and launch a response when said event is present.In principle, with both methods we can play enough to adapt them to a multitude of situations that require timing or programming certain tasks without, of course, blocking the mainloop.
To illustrate its use a little we will see two examples:
Sprite changes position every x time repetitively.
An example is the pieces of the tetrix. For this we can use
pygame.time.set_timer
. Of course, the possibilities are not limited to moving the object, we can perform any task repetitively every x milliseconds.We can make multiple calls to
pygame.time.set_timer
, each one will have its own timer and will place the assigned event when it plays in the event queue.To stop a timer for a given event, simply call the method again, passing 0 to the second argument (milliseconds).
Make a sprite move to a certain position two seconds after it was created and after another two seconds it destroys itself.
This is something similar in principle to what you want, in this case we are going to use
pygame.time.get_ticks
. Pressing space creates a new sprite (limited to 5 simultaneous sprites), when 2 seconds have passed since it was created it moves to the center of the screen, after another two seconds it disappears:They are just two simple examples to try to illustrate a general idea, depending on the particular situation we can adapt the code to cover specific needs.
If someone wants to reproduce the examples, this is the sprite used (
square.png
):