0

Am trying to block mouse at some time after button click, so I have created a function:

import pygame
import game_cache
import time

def set_mouse_blocked(boolean, timer=False):
    if boolean:
        game_cache.is_mouse_active = False

        if timer:
            ...

    else:
        game_cache.is_mouse_active = True

How to complete this function without a cycle?

P.S. There is no opportunity to use cycle, because this function called in cycle

martineau
  • 119,623
  • 25
  • 170
  • 301
  • I used pygame module in main script - this file is just a part of all project. timer argument is time of blocking in seconds – Deimos Sound Feb 15 '21 at 23:08

1 Answers1

0

Assuming you have a main loop function you can create a timestamp with the moment the mouse was blocked and check how far in the past that timestamp is:

import time

def init():
    game_cache.is_mouse_active = True
    game_cache.last_blocked = 0
    game_cache.mouse_block_timer = 0

def set_mouse_blocked(timeout = 0):
    game_cache.is_mouse_active = False
    game_cache.last_blocked = time.time()
    game_cache.mouse_block_timer = timeout

def main_loop():
    if game_cache.mouse_block_timer:
        if time.time() - game_cache.last_blocked > game_cache.mouse_block_timer:
            game_cache.is_mouse_active = True
            game_cache.mouse_block_timer = 0
skysphr
  • 26
  • 2