【问题标题】:If player doesn't move for x seconds, do something in pygame如果玩家在 x 秒内没有移动,请在 pygame 中执行一些操作
【发布时间】:2020-02-15 11:10:34
【问题描述】:

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

我尝试过这样的事情:

        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

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

【问题讨论】:

  • 为什么它不起作用?解释一下。我们无法运行代码来查看它。 (PL: Wyjaśnij to)
  • 也许你应该在while True之前设置moved = False,然后在你更改player_position时设置moved = True。您还可以添加变量do_something = True/False 以查看它是否必须在其他循环中执行某些操作。
  • 动作每隔一秒执行一次,我是否移动并不重要。在我看来,这是因为 while 循环执行得非常快,而且从技术上讲,“移动”总是 False。我试图在while循环之前放置moved = False,并在我改变位置时设置为True,但它只工作一次。第一次移动后,它不再起作用(移动 = True,因此不会执行操作)。
  • 当您按下按钮时,只会创建一个事件KEYDOWN - 当您按住按钮时它不会创建事件。您必须设置速度,而不是更改位置,否则您必须检查 pygame.pygame.key.get_pressed()[K_LEFT] 而不是事件才能在每个循环中获取此值。

标签: python time pygame


【解决方案1】:

最小的工作示例。

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

我使用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()

我输入了more complex version on Github。它保存位置,更改图像并开始摇晃。当你按下LEFTRIGHT时,它会恢复原来的位置和图像。

【讨论】:

    猜你喜欢
    • 2020-01-20
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    相关资源
    最近更新 更多