Python 使图像随机出现并在一定时间后消失
我非常困惑如何处理这个问题,我无法让它发挥作用。所以,如果你之前玩过蛇,你会记得在某些时候会出现一个图像,如果你吃了蛇,你会得到更多的分数,但如果你错过了它,它会消失(也就是说,在你抓住它之前不会永远停留在那里) 我已经编写了与时间相关的代码(允许您激活一个盾牌),如下所示:Python 使图像随机出现并在一定时间后消失,python,pygame,Python,Pygame,我非常困惑如何处理这个问题,我无法让它发挥作用。所以,如果你之前玩过蛇,你会记得在某些时候会出现一个图像,如果你吃了蛇,你会得到更多的分数,但如果你错过了它,它会消失(也就是说,在你抓住它之前不会永远停留在那里) 我已经编写了与时间相关的代码(允许您激活一个盾牌),如下所示: 如何在pygame中实现消失的图像? 我知道这是非常规的,但如果你能帮忙,我将不胜感激,因为我在过去的一个小时里一直无法做到这一点 最好的, Seyed使用时间为开始显示和结束显示的变量 在游戏中控制不同的元素是很流行
如何在pygame中实现消失的图像? 我知道这是非常规的,但如果你能帮忙,我将不胜感激,因为我在过去的一个小时里一直无法做到这一点 最好的,
Seyed使用时间为
开始显示
和结束显示
的变量
在游戏中控制不同的元素是很流行的方法
import pygame
import random
# - init -
pygame.init()
screen = pygame.display.set_mode((800, 600))
# - objects -
display_apple = False
start_showing = None
end_showing = None
current_time = pygame.time.get_ticks()
# show first time
start_showing = current_time + random.randint(1,5)*1000
# - mainloop -
clock = pygame.time.Clock()
running = True
while running:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# - updates -
current_time = pygame.time.get_ticks()
if display_apple:
# is it time to hide ?
if end_showing and current_time >= end_showing:
# hide it
display_apple = False
end_showinge = False
# set time when to show
start_showing = current_time + random.randint(1,5)*1000
else:
# is it time to show ?
if start_showing and current_time >= start_showing:
# show it
display_apple = True
start_showing = False
# set time when to hide
end_showing = current_time + random.randint(1,5)*1000
# - draws -
screen.fill((0,0,0))
if display_apple:
pygame.draw.rect(screen, (255,0,0), (0,0,100,100))
pygame.display.flip()
# - FPS -
clock.tick(30)
# - end -
pygame.quit()
使用主循环中的
hide_time=curr_time+random_time
并选中hide_time
以显示或不显示图像。但我有一个小问题,您的代码在固定位置的随机时间生成粒子,我想要固定时间的随机位置。我该怎么做?阅读部分代码#-更新-
-使用固定时间而不是随机时间,并添加随机位置。
import pygame
import random
# - init -
pygame.init()
screen = pygame.display.set_mode((800, 600))
# - objects -
display_apple = False
start_showing = None
end_showing = None
current_time = pygame.time.get_ticks()
# show first time
start_showing = current_time + random.randint(1,5)*1000
# - mainloop -
clock = pygame.time.Clock()
running = True
while running:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# - updates -
current_time = pygame.time.get_ticks()
if display_apple:
# is it time to hide ?
if end_showing and current_time >= end_showing:
# hide it
display_apple = False
end_showinge = False
# set time when to show
start_showing = current_time + random.randint(1,5)*1000
else:
# is it time to show ?
if start_showing and current_time >= start_showing:
# show it
display_apple = True
start_showing = False
# set time when to hide
end_showing = current_time + random.randint(1,5)*1000
# - draws -
screen.fill((0,0,0))
if display_apple:
pygame.draw.rect(screen, (255,0,0), (0,0,100,100))
pygame.display.flip()
# - FPS -
clock.tick(30)
# - end -
pygame.quit()