gpt4 book ai didi

python - 在 Pygame 中每隔几秒移动一个对象

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:56 25 4
gpt4 key购买 nike

我正在使用 pygame 制作一款街机游戏,我试图让 Sprite 每隔几秒改变一次位置。

我试过使用 time.sleep(1) 并将帧速率更改为 .5 (clock.tick(.5)).

两者都使对象仅在时间间隔过后才改变位置,但它们也使 Sprite 以相同的速率跟随我的鼠标更新坐标。

我一直在研究并且似乎无法找到另一种方法来让 Sprite 移动而不会使我的程序每次运行时刷新速度变慢或“休眠”。

最佳答案

您可以使用 Event为此与pygame.time.set_timer()一起:

pygame.time.set_timer()
repeatedly create an event on the event queue
set_timer(eventid, milliseconds) -> None

Set an event type to appear on the event queue every given number of milliseconds


这是一个简单、完整的示例。请注意敌人是如何每 1000 毫秒横向移动一次,每 3500 毫秒向下移动一次,而您可以每 450 毫秒射击一次(全部使用事件)。

enter image description here


import pygame

# you'll be able to shoot every 450ms
RELOAD_SPEED = 450

# the foes move every 1000ms sideways and every 3500ms down
MOVE_SIDE = 1000
MOVE_DOWN = 3500

screen = pygame.display.set_mode((300, 200))
clock = pygame.time.Clock()

pygame.display.set_caption("Micro Invader")

# create a bunch of events
move_side_event = pygame.USEREVENT + 1
move_down_event = pygame.USEREVENT + 2
reloaded_event = pygame.USEREVENT + 3

move_left, reloaded = True, True

invaders, colors, shots = [], [] ,[]
for x in range(15, 300, 15):
for y in range(10, 100, 15):
invaders.append(pygame.Rect(x, y, 7, 7))
colors.append(((x * 0.7) % 256, (y * 2.4) % 256))

# set timer for the movement events
pygame.time.set_timer(move_side_event, MOVE_SIDE)
pygame.time.set_timer(move_down_event, MOVE_DOWN)

player = pygame.Rect(150, 180, 10, 7)

while True:
clock.tick(40)
if pygame.event.get(pygame.QUIT): break
for e in pygame.event.get():
if e.type == move_side_event:
for invader in invaders:
invader.move_ip((-10 if move_left else 10, 0))
move_left = not move_left
elif e.type == move_down_event:
for invader in invaders:
invader.move_ip(0, 10)
elif e.type == reloaded_event:
# when the reload timer runs out, reset it
reloaded = True
pygame.time.set_timer(reloaded_event, 0)

for shot in shots[:]:
shot.move_ip((0, -4))
if not screen.get_rect().contains(shot):
shots.remove(shot)
else:
hit = False
for invader in invaders[:]:
if invader.colliderect(shot):
hit = True
i = invaders.index(invader)
del colors[i]
del invaders[i]
if hit:
shots.remove(shot)

pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT]: player.move_ip((-4, 0))
if pressed[pygame.K_RIGHT]: player.move_ip((4, 0))

if pressed[pygame.K_SPACE]:
if reloaded:
shots.append(player.copy())
reloaded = False
# when shooting, create a timeout of RELOAD_SPEED
pygame.time.set_timer(reloaded_event, RELOAD_SPEED)

player.clamp_ip(screen.get_rect())

screen.fill((0, 0, 0))

for invader, (a, b) in zip(invaders, colors):
pygame.draw.rect(screen, (150, a, b), invader)

for shot in shots:
pygame.draw.rect(screen, (255, 180, 0), shot)

pygame.draw.rect(screen, (180, 180, 180), player)
pygame.display.flip()

关于python - 在 Pygame 中每隔几秒移动一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23368999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com