首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在游戏中每秒钟增加一个变量?

如何在游戏中每秒钟增加一个变量?
EN

Stack Overflow用户
提问于 2017-08-03 22:11:21
回答 3查看 2.7K关注 0票数 0

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

来,准备一场新游戏。

代码语言:javascript
复制
def new(self):
    # set cookies/multipliers for a new game
    self.cookie_count = 0
    self.grandma = 10 # grandma bakes 10 cookies/second

然后,如果要购买奶奶,则将10 cookie/秒添加到每购买一次奶奶的self.cookie_count中。例:如果买了两个奶奶,self.cookie_count += 20 cookie/秒。然而,就像我现在拥有的一样,每次我买奶奶的时候,我只会得到10块饼干。

代码语言:javascript
复制
if self.rect2.collidepoint(self.mouse_pos) and self.pressed1:
   self.cookie_count += self.grandma

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-08-03 23:16:31

与每秒递增一次cookie不同,您可以让cookie计算从开始到现在已经过去了多少秒。这可能会在某些场景中引起问题(例如,这会使暂停变得复杂),但是对于简单的游戏来说是有效的。

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

代码语言:javascript
复制
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。
票数 1
EN

Stack Overflow用户

发布于 2017-08-04 00:32:42

在游戏中这样做的方法是使用pygame.time.set_timer()并在给定的毫秒数内生成一个事件。这将允许像其他脚本一样在脚本的主循环中处理事件。

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

代码语言:javascript
复制
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()
票数 1
EN

Stack Overflow用户

发布于 2017-08-03 22:29:22

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

代码语言:javascript
复制
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,这将是解决方案。上面的第一段代码不起作用。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45495344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档