GuardGoose
GuardGoose

Reputation: 57

Disable Keys for 2 Seconds Pygame

I'm writing a game in pygame and I need to try and disable one of the three input keys for 2 seconds, every so often. Now I am having a lot of trouble with this and have spent a lot of time looking around the internet for a method. Here is the 'key_disable' code so far:

def key_disable(self):
    disabled_period = 2000
    timer = 0
    alarm = alert_font.render("ALERT", False, (255, 0, 0))
    x = randint(0, 2500)
    if x < 25:
        timer += 1
        if timer <= disabled_period:
            screen.blit(alarm, (74, 100))
            return False
    elif self.damage > 100:
        return False
    elif x > 26:
        return True

And this is used in the main event loop, like so:

keys_pressed = pg.key.get_pressed()
if rover.fuel > 0:
    MarsRover.key_disable(rover)
    if keys_pressed[pg.K_LEFT] and MarsRover.key_disable(rover) is True:
        rover.move_left()
    if keys_pressed[pg.K_RIGHT] and MarsRover.key_disable(rover) is True:
        rover.move_right()
    if keys_pressed[pg.K_SPACE] and MarsRover.key_disable(rover) is True:
        thrust = Thrust('thrust.png', rover.rect.left + 31, rover.rect.bottom - 12)
        thrust.rotated()
        rover.Start_Engine()
        screen.blit(thrust.image_rotated, thrust.rect)
        pg.display.update()

The idea was that if the function returns False then it would disable the controls for 2 seconds. But it only works if I disable all the controls for what seems like a few milliseconds, which isn't what I want it to do. I have tried various methods of time keeping such as module (sorry for spelling) time keep, like: if (current_time - start_time) % 2 == 0. None of the methods I try seem to work, and I always end up with the same result. Can I get any help? Thanks

Note: This is for a First Year project, asking here was a last resort.

Upvotes: 1

Views: 1385

Answers (1)

Martijn
Martijn

Reputation: 417

You're currently defining timer inside of the function. Thus it'll be forgotten and set back to 0 every time the function is called. The value of timer also seems to be independent of the actual time: it's randomly updated 0 to 4 times a loop, where a loop might take more than 1 millisecond in a busy period. A better implementation might be:

class MarsRover(object):
    time_left_disabled = 0

    def disable(self):
        self.time_left_disabled = 2000

    def update_disabled(self, time_taken):
        # There are several ways to implement this, including if-statements
        self.time_left_disabled = max(self.time_left_disabled - time_taken, 0)

    def is_enabled(self):
        return (self.time_left_disabled <= 0)

    # your other code here

With a part of your main loop as follows:

# Check if the rover should be disabled again
if randint(0, 2500)  < 25:
    rover.disable()
else:
    rover.update_disabled(dt)

# Do some other stuff

# Check the keyboard input
if keys_pressed[pg.K_LEFT] and rover.is_enabled():
    rover.move_left()
if keys_pressed[pg.K_RIGHT] and rover.is_enabled():
    rover.move_right()
if keys_pressed[pg.K_SPACE] and rover.is_enabled():
    rover.thrust()

# some other stuff

# Ensure the FPS is not going crazy
dt = clock.tick(framerate)

Upvotes: 3

Related Questions