Python Forum
Not able to make a specific thing pause in pygame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not able to make a specific thing pause in pygame
#1
when i use time.sleep() or pygame.time.wait() it always freezes the whole game instead of one specific thing.

lets say i want to make a door open and close every few seconds. when i do that instead of just pausing the door it pauses the whole game every few seconds:

elif self.flag == 'door_v':
self.x -= 3
if abs(self.x - self.door_prev_pos) > TILE:
self.door_open_trigger = False
pygame.time.wait(2000)
self.door_close_trigger = True
Reply
#2
Yes these two commands will freeze the whole thing. What you can do is to set a variable to record how long the time has passed and freeze only the part you want to freeze, something like the following. I have developed a game using this method, you can check out..
t0 = time.time()
if time.time() - t0 > timestep:
   freeze the part
   t0 = time.time()
Reply
#3
Those functions pause the whole script. Do you have a game loop? What you need are timed updates for your doors, where they measure how much time has passed and open and close how you want.

class Door():
    def __init__(self):
        self.last_update = pygame.time.get_ticks()
        self.interval = 1000  #    1 second
        self.state = "closed"
        

    def update(self):
        now = pygame.time.get_ticks()
        if now - self.last_update > self.interval:
            self.last_update = now
            if self.state == "closed":
                self.state = "open"
            else:
               self.state = "closed"
Reply
#4
How about setting a flag that tells the door handler code that it should open/close the door, leave it closed or leave it open? It is very rare that you use anything other than really short delays in a game (or any interactive program for that matter).

Hopefully this code conveys what I am trying to say.
from tkinter import *

root = Tk()
door_command = 'TOGGLE'
door = StringVar()

def update_door():
   state = door.get()
   if state != door_command:
        if door_command == 'TOGGLE':
            door.set('OPEN' if state == 'CLOSE' else 'CLOSE')
        else:
            door.set(door_command)
   root.after(1000, update_door)

def set_door(command):
    global door_command
    door_command = command
 
Label(root, textvariable=door, width=10).pack()
Button(root, text='Open Door', command=lambda:set_door('OPEN')).pack()
Button(root, text='Close Door', command=lambda:set_door('CLOSE')).pack()
Button(root, text='Toggle', command=lambda:set_door('TOGGLE')).pack()
update_door()
mainloop()
I have two functions, set_door() and update_door(). set_door() tells the program what it is supposed to do; close the door (CLOSE), open the door (OPEN), or open and close the door over and over (TOGGLE).

update_door() actually controls the door. Here it just updates a label, but it could just as easily control a sprite. This function is called every second but only does something when the door state is different than the door command. And if the door command is 'TOGGLE' it opens or closes the door based on the current state.
Reply
#5
(Dec-13-2020, 09:43 PM)michael1789 Wrote: Those functions pause the whole script. Do you have a game loop? What you need are timed updates for your doors, where they measure how much time has passed and open and close how you want.

class Door():
    def __init__(self):
        self.last_update = pygame.time.get_ticks()
        self.interval = 1000  #    1 second
        self.state = "closed"
        

    def update(self):
        now = pygame.time.get_ticks()
        if now - self.last_update > self.interval:
            self.last_update = now
            if self.state == "closed":
                self.state = "open"
            else:
               self.state = "closed"

sorry i need to put it more clear:
def door_open(self):
if self.flag == 'door_h':
self.y -= 3
if abs(self.y - self.door_prev_pos) > TILE:
self.door_open_trigger = False
self.door_close_trigger = True

elif self.flag == 'door_v':
self.x -= 3
if abs(self.x - self.door_prev_pos) > TILE:
self.door_open_trigger = False
now = pygame.time.get_ticks()
if now - self.last_update > self.interval:
self.last_update = now
self.door_close_trigger = True



def door_close(self):
if self.flag == 'door_v':
for i in range(self.amount):
self.times += 0.2
waited = self.wait
if self.count < self.speed:
self.count += 1
else:
self.x += 3
self.count = 0
if self.times < self.amount + 6:
return waited
if self.flag == 'door_h':
for i in range(self.amount):
self.times += 0.2
waited = self.wait
if self.count < self.speed:
self.count += 1
else:
self.y += 3
self.count = 0
if self.times < self.amount + 6:
return waited

this is the whole code. i wanted to make it so when i open the door it automatically closes after 1 or 2 seconds.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I pause only one object? actualpy 1 365 Feb-01-2024, 07:43 PM
Last Post: deanhystad
  How to pause execution (on windows)? pstein 1 565 Jun-23-2023, 06:58 PM
Last Post: rob101
  Is it Possible to pause a script? tester_V 6 1,525 Apr-05-2023, 06:11 AM
Last Post: tester_V
  I am confused with the key and value thing james1019 3 975 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Need help i just started the whole thing gabriel789 16 3,230 Sep-12-2022, 08:04 PM
Last Post: snippsat
  how to mouse click a specific item in pygame? Frankduc 5 1,735 May-03-2022, 06:22 PM
Last Post: Frankduc
  i making a terminal sign up website thing Kenrichppython 1 1,711 Nov-04-2021, 03:57 AM
Last Post: bowlofred
  use thing before self sylvanas 3 2,351 Jul-21-2021, 04:54 PM
Last Post: sylvanas
  Did subprocess.Popen() causes main routine to pause stdout? liudr 4 3,658 May-04-2021, 08:58 PM
Last Post: liudr
  How to make a telegram bot respond to the specific word in a sentence? Metodolog 2 6,375 Dec-22-2020, 07:30 AM
Last Post: martabassof

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020