[英]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!
谢谢!
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.我使用
do_something = True/False
来控制它是否应该摇晃。
When I press key LEFT
or RIGH
then I set do_something = False
and reset timer time_since_last_action
.当我按
LEFT
或RIGH
键时,我设置do_something = False
并重置计时器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
当计时器 > 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()
I put more complex version on Github .我在 Github 上放了更复杂的版本。 It saves position, changes image and start shaking.
它保存位置,更改图像并开始抖动。 When you press
LEFT
or RIGHT
then it restore original position and image.当您按
LEFT
或RIGHT
它会恢复原始位置和图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.