如何创建一个分数计数器,其中分数每秒增加?
How to create a score counter where scores add for every second?
我想在我的游戏中添加特殊的分数计数器,当玩家获得分数时,例如 1000 分,玩家将获胜,但是,我完全不知道如何实现它。我需要做什么?可能,我必须使用 pygame.USEREVENT + 1
;
import time
total_score = 1000
scores = 0
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
pygame.sprite.Sprite.__init__(self)
#[...]
self.score = scores
self.plus = 1
def update(self):
#[...]
self.score += self.plus
def game():
global health
Game = True
while Game:
#[...]
print_text('Total score ' + str(scores), 725, 25)
使用定时器事件。在 pygame 中存在一个定时器事件。在事件队列中使用 pygame.time.set_timer()
to repeatedly create a USEREVENT
。时间必须以毫秒为单位设置。例如:
timer_interval = 1000 # 1 seconds
timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event, timer_interval)
每个计时器事件都需要一个唯一的 ID。用户事件的 ID 必须介于 pygame.USEREVENT
(24) 和 pygame.NUMEVENTS
(32) 之间。在这种情况下,pygame.USEREVENT+1
是定时器事件的事件 ID。
计时器事件发生时增加分数:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == timer_event:
player.score += player.plus
我想在我的游戏中添加特殊的分数计数器,当玩家获得分数时,例如 1000 分,玩家将获胜,但是,我完全不知道如何实现它。我需要做什么?可能,我必须使用 pygame.USEREVENT + 1
;
import time
total_score = 1000
scores = 0
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
pygame.sprite.Sprite.__init__(self)
#[...]
self.score = scores
self.plus = 1
def update(self):
#[...]
self.score += self.plus
def game():
global health
Game = True
while Game:
#[...]
print_text('Total score ' + str(scores), 725, 25)
使用定时器事件。在 pygame 中存在一个定时器事件。在事件队列中使用 pygame.time.set_timer()
to repeatedly create a USEREVENT
。时间必须以毫秒为单位设置。例如:
timer_interval = 1000 # 1 seconds
timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event, timer_interval)
每个计时器事件都需要一个唯一的 ID。用户事件的 ID 必须介于 pygame.USEREVENT
(24) 和 pygame.NUMEVENTS
(32) 之间。在这种情况下,pygame.USEREVENT+1
是定时器事件的事件 ID。
计时器事件发生时增加分数:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == timer_event:
player.score += player.plus