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

2024-04-19 23:26:05 发布

您现在位置:Python中文网/ 问答频道 /正文

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

在这里,准备一场新的比赛。在

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_计数每买一个外婆。示例:如果购买了2个grandma,self.cookie_count += 20个cookies/秒。然而,我现在有,每次我买奶奶,我只得到10个饼干。在

^{pr2}$

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


Tags: selfgame游戏newforcookiedefcount
3条回答

你需要使用时间模块。您可以使用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,这将是解决方法。上面的第一个代码行不通。在

^{pr2}$

在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()

与每秒递增一次cookies不同,您可以让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应该只在测试和简单的玩具中使用。

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

注:

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

  2. 以前从来没有玩过“饼干点击器”,我可能混淆了逻辑。如果有什么不明白的话,请纠正我。

  3. 如果玩家没有升级,我假设self.grandmaNone/错误的。

相关问题 更多 >