繁体   English   中英

如果玩家在 x 秒内没有移动,请在 pygame 中执行一些操作

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

如果玩家在一定时间内没有移动,我会尝试执行一个动作。

我试过这样的事情:

        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

它不起作用,我明白为什么,但我不知道如何编写我想要的代码。 看看我的代码应该是什么样子的例子会很有帮助。 谢谢!

最小的工作示例。

当玩家不动时,它开始摇晃(上下随机移动)

我使用do_something = True/False来控制它是否应该摇晃。

当我按LEFTRIGH键时,我设置do_something = False并重置计时器time_since_last_action

当我不按键并且它什么都不做时,定时器改变值。

当计时器 > 1000 并且它不移动也不做某事时,我设置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()

在 Github 上放了更复杂的版本 它保存位置,更改图像并开始抖动。 当您按LEFTRIGHT它会恢复原始位置和图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM