python - 如果玩家在 x 秒内没有移动,请在 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
用来控制它是否应该摇晃。
当我按下键LEFT
或RIGH
然后我设置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 上放了更复杂的版本。它保存位置,更改图像并开始摇晃。当您按LEFT
或RIGHT
然后它恢复原始位置和图像。
推荐阅读
- python - 如何散列十万条记录作为 CSV 文件的输入?
- javascript - 如何在猫鼬中使用分组和求和?
- npm - 我应该如何在 Svelte 中导入 Paper.js?
- python - 如何从 2 个列表创建字典,一个代表键,另一个代表值?
- listview - 在颤动中加载项目后将Listview滚动到底部
- flutter - 我应该总是在 Dart 中使用“this”吗?
- c++ - 我可以让编译器忽略特定于操作系统的方法吗
- javascript - ReactJS 中 componentDidUpdate() 之后的无限循环
- java - 在 Google Sheet API 中使用我的 Google 帐户登录后出现错误
- python-3.x - 从图像列表创建图像列表