Bartosz Panek
Bartosz Panek

Reputation: 35

If player doesn't move for x seconds, do something in pygame

I'm trying to execute an action if player is not moved in certain amount of time.

I tried something like this:

        while True:
        dt = self.clock.tick(30)
        time_since_last_action += dt
        moved = False

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                moved = True
                if event.key == pygame.K_LEFT:
                    player_position -= player_size
                elif event.key == pygame.K_RIGHT:
                    player_position += player_size

        if time_since_last_action > 1000 and not moved:
            #----action----
            time_since_last_action = 0

It doesn't work and I understand why but I have no other idea how to code what I want. It would be very helpful to see an example of what my code should look like. Thank you!

Upvotes: 0

Views: 136

Answers (1)

furas
furas

Reputation: 143000

Minimal working example.

When player doesn't move then it start shaking (make random moves up and down)

I use do_something = True/False to control if it should shake or not.

When I press key LEFT or RIGH then I set do_something = False and reset timer time_since_last_action.

When I don't press keys and it does nothing then timer change value.

When timer > 1000 and it doesn't move and doesn't do something then I set do_something = True

# https://github.com/furas/python-examples/

import pygame
import random

# --- constants --- (UPPER_CASE_NAMES)

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

FPS = 25 # for more than 220 it has no time to update screen

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# --- classes --- (CamelCaseNames)

class Player(pygame.sprite.Sprite):

    def __init__(self, x=SCREEN_WIDTH//2, y=SCREEN_HEIGHT//2):
        super().__init__()
        self.image = pygame.Surface((100,100))
        self.image.fill(WHITE)
        self.rect = self.image.get_rect(centerx=x, centery=y)

    def update(self):
        #move_x = random.randint(-5, 5)
        #move_y = random.randint(-5, 5)
        #self.rect.move_ip(move_x,move_y)
        pass

    def draw(self, surface):
        surface.blit(self.image, self.rect)

# --- functions --- (lower_case_names)

# --- main ---

pygame.init()

screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )

player = Player()

# --- mainloop ---

clock = pygame.time.Clock()

# default values at start
do_something = False
time_since_last_action = 0

running = True
while running:
    # --- FPS ---

    dt = clock.tick(30)
    time_since_last_action += dt

    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                running = False

    keys = pygame.key.get_pressed()

    moved = False

    if keys[pygame.K_LEFT]:
        player.rect.x -= 10 # player.rect.width
        moved = True
        # reset other values
        do_something = False
        time_since_last_action = 0
    elif keys[pygame.K_RIGHT]:
        player.rect.x += 10 # player.rect.width
        moved = True
        # reset other values
        do_something = False
        time_since_last_action = 0

    if not do_something and not moved and time_since_last_action > 1000:
        do_something = True
        # reset other values
        time_since_last_action = 0

    if do_something:
        # - action -
        # shaking
        player.rect.y -= random.randint(-5, 5)

    # --- changes/moves/updates ---

    # empty

    # --- draws ---

    screen.fill(BLACK)

    player.draw(screen)

    pygame.display.flip()

# --- end ---

pygame.quit()

I put more complex version on Github. It saves position, changes image and start shaking. When you press LEFT or RIGHT then it restore original position and image.

Upvotes: 2

Related Questions