如何在 Pygame 中每秒增加一个变量?



我正在创建一个点击游戏,非常类似于cookie点击器。我的问题是,如何每秒增加一个变量的数量?

在这里,为新游戏做准备。

def new(self):
# set cookies/multipliers for a new game
self.cookie_count = 0
self.grandma = 10 # grandma bakes 10 cookies/second

然后,如果购买了奶奶,则每购买一个奶奶,每秒添加10 个饼干self.cookie_count。示例:如果购买了 2 个奶奶,则每秒self.cookie_count += 20个饼干。然而,就像我现在一样,每次我买奶奶,我只会得到10块饼干。

if self.rect2.collidepoint(self.mouse_pos) and self.pressed1:
self.cookie_count += self.grandma

我知道这与时间有关,但除此之外,我不太确定从哪里开始。

您可以让 cookie 计算自开始以来经过的秒数,而不是每秒递增一次 Cookie。在某些情况下,这可能会导致问题(例如,这将使暂停复杂化(,但适用于简单的游戏。

我的 Python 有点生疏,所以很抱歉,如果这不完全是惯用语:

import time
self.start_time = time.time()
# When you need to know how many cookies you have, subtract the current time
#  from the start time, which gives you how much time has passed
# If you get 1 cookie a second, the elapsed time will be your number of cookies
# "raw" because this is the number cookies before Grandma's boost 
self.raw_cookies = time.time() - self.start_time
if self.grandma:
self.cookies += self.raw_cookies * self.grandma
else:
self.cookies += raw.cookies
self.raw_cookies = 0

这可能看起来比仅使用time.sleep更复杂,但它有两个优点:

  1. 在游戏中使用sleep很少是一个好主意。如果你sleep动画线程,你会在睡眠期间冻结整个程序,这显然不是一件好事。即使这在简单的游戏中不是问题,为了习惯,也应该限制sleep的使用。sleep实际上应该只用于测试和简单的玩具。

  2. sleep不是100%准确的。随着时间的推移,sleep时间的误差会累积。但这是否是一个问题完全取决于应用程序。只需减去时间,您就可以确切地(或至少高精度地(知道过去了多少时间。

注意:

  1. 使用上面的代码,cookies将是一个浮点数,而不是整数。这会更准确,但显示时可能看起来不太好。在显示之前将其转换为整数/四舍五入。

  2. 以前从未玩过"饼干点击器",我可能混淆了逻辑。如果有什么不合理的地方,请纠正我。

  3. 我假设如果播放器没有升级self.grandmaNone/falsey。

在pygame中执行此操作的方法是使用pygame.time.set_timer(),并每隔给定的毫秒数生成一个事件。这将允许事件像任何其他事件一样在脚本的主循环中处理。

这里有一个有点无聊但可以运行的示例,可以做这样的事情:

import pygame
pygame.init()
SIZE = WIDTH, HEIGHT = 720, 480
FPS = 60
BLACK = (0,0,0)
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
BACKGROUND_COLOR = pygame.Color('white')
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()
font = pygame.font.SysFont('', 30)
COOKIE_EVENT = pygame.USEREVENT
pygame.time.set_timer(COOKIE_EVENT, 1000)  # periodically create COOKIE_EVENT
class Player(pygame.sprite.Sprite):
def __init__(self, position):
super(Player, self).__init__()
self.cookie_count = 0
self.grandma = 10 # grandma bakes 10 cookies/second
text = font.render(str(self.cookie_count), True, RED, BLACK)
self.image = text
self.rect = self.image.get_rect(topleft=position)
self.position = pygame.math.Vector2(position)
self.velocity = pygame.math.Vector2(0, 0)
self.speed = 3
def update_cookies(self):
self.cookie_count += self.grandma  # 10 cookies per grandma
if self.cookie_count > 499:
self.cookie_count = 0
text = font.render(str(self.cookie_count), True, RED, BLACK)
self.image = text
player = Player(position=(350, 220))
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == COOKIE_EVENT:
player.update_cookies()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.velocity.x = -player.speed
elif keys[pygame.K_RIGHT]:
player.velocity.x = player.speed
else:
player.velocity.x = 0
if keys[pygame.K_UP]:
player.velocity.y = -player.speed
elif keys[pygame.K_DOWN]:
player.velocity.y = player.speed
else:
player.velocity.y = 0
player.position += player.velocity
player.rect.topleft = player.position
screen.fill(BACKGROUND_COLOR)
screen.blit(player.image, player.rect)
pygame.display.update()

你需要使用时间模块。您可以使用time.time()捕获时间段。

import time
grandma = 3
cookie_count = 0
timeout = 1
while True:
cookie_count += grandma * 10
print 'cookie count: {}'.format(cookie_count)
time.sleep(timeout)

另一种选择是验证表达式now - start > timeout。它们都会做同样的事情,但如果您的超时大于 1,这将是解决方案。上面的第一个代码不起作用。

import time
grandma = 3
cookie_count = 0
timeout = 1
start = time.time()
while True:
if time.time() - start > timeout:    
cookie_count += grandma * 10
print 'cookie count: {}'.format(cookie_count)
time.sleep(timeout)

最新更新