Not able to make a specific thing pause in pygame - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Not able to make a specific thing pause in pygame (/thread-31468.html) |
Not able to make a specific thing pause in pygame - cooImanreebro - Dec-13-2020 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 RE: Not able to make a specific thing pause in pygame - MK_CodingSpace - Dec-13-2020 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() RE: Not able to make a specific thing pause in pygame - michael1789 - Dec-13-2020 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" RE: Not able to make a specific thing pause in pygame - deanhystad - Dec-13-2020 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. RE: Not able to make a specific thing pause in pygame - cooImanreebro - Dec-13-2020 (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. 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. |