gpt4 book ai didi

python - 尝试在 PYGAME 中随机进行随机图像 block 传输

转载 作者:行者123 更新时间:2023-12-03 19:12:02 24 4
gpt4 key购买 nike

我有一个有趣的问题。我需要使图像必须随机出现在随机位置,如果它与我的 GAMER 碰撞,它必须影响 GAMER(某些功能)与 GAMER 恰好 3 秒,然后消失并在随机时间后再次出现。
.我有一个想法,但它不起作用。(注意:这是我已经初始化所有代码的一部分)

clock = pygame.time.Clock()  
FPS = 30
playtime = 0
newtime=random.randint(3,10)
while mainloop:
milliseconds = clock.tick(FPS)
seconds = milliseconds / 1000.0
playtime += seconds
if playtime>=newtime:
image.draw()
newtime=random.randint(2,6) #because next appear time must change
if collision(GAMER,image):
GAMER.do_something() #how to do it exactly for 3 sec?
image.far_away() #just dissapear for some time

最佳答案

当您谈论图像时,您谈论的是 pygame.Surface .一个 pygame.Surface可以 blit()到显示(分别是与窗口关联的 Surface)。例如

screen.blit(image, image_rect)

您不能将表面移到很远的地方。您必须在每一帧中绘制整个场景,并且必须在每一帧的显示表面上绘制 ( blit) 图像。
您可以更改图像的位置。当时间跨度超过时创建一个随机位置:

position = random.randint(0, window_height), random.randint(0, window_width)
image_rect = image.get_rect(center = position)

当玩家与图像碰撞时,则设置计算 future 3 秒的时间点。只要不超过 3 秒,就会对玩家产生影响:

if image_rect.colliderect(GAMER.rect):
affecttime = playtime + 3

if playtime < affecttime:
GAMER.do_something()

一般流程:

clock = pygame.time.Clock()  
FPS = 30
newtime=random.randint(3,10)
playtime = 0
affecttime = 0

position = random.randint(0, window_height), random.randint(0, window_width)
image_rect = image.get_rect(center = position)

while mainloop:
milliseconds = clock.tick(FPS)
seconds = milliseconds / 1000.0
playtime += seconds

# handle events (event loop)
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainloop = False

if playtime >= newtime:
newtime = playtime + random.randint(2,6)
position = random.randint(0, window_height), random.randint(0, window_width)
image_rect = image.get_rect(center = position)

if image_rect.colliderect(GAMER.rect):
affecttime = playtime + 3
position = .... # set position far away
image_rect = image.get_rect(center = position)

if playtime < affecttime:
GAMER.do_something()

# clear dispaly
scree.fill(0)

#draw image at the current position
screen.blit(image, image_rect)

# update disaply
pygame.display.update()

关于python - 尝试在 PYGAME 中随机进行随机图像 block 传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61755222/

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