在 Python 点击​​游戏中添加每秒硬币数

我对Python很陌生。我正在尝试构建一款闲置点击游戏。如果你点击你会得到一个硬币。您应该能够购买“自动点击器”,这意味着您每秒自动获得硬币。我目前的代码处于 while 循环中,当我写“EXIT”时该循环结束。我不知道如何在发生其他事情时实现时间并添加汽车硬币。总而言之:想要实现每秒硬币数


我的代码:


print("Welcome to the game")


coins = 1

a = 0

shop = 0

coinsperclick = 1

nothing = 999

coinspersec = 1



def help():

    print("'help' für Hilfe", '\n', "Enter für coins", '\n', "'shop' für den Shop")



help()



a = input()

while a != "EXIT":

    a = input()

    if a == "shop":

        print("Number 0:     EXIT Shop      Cost: 0")

        print("Number 1:     Clicker +1     Cost: 50")

        print("Number 2:     CPS +1         Cost: 100")

        shop = eval(input("What do you want to buy?"))

        if shop == 0:

            nothing = nothing

        if shop == 1:

            kaufmenge = eval(input("How many do you want to buy?"))

            coinsperclick = coinsperclick+kaufmenge

            coins = coins-(kaufmenge*50)

        if shop == 2:

            kaufmenge = eval(input("How many do you want do buy?"))

            coinspersec = coinspersec+kaufmenge

            coins = coins-(kaufmenge*100)

    if a == "help":

        help()

    if a == "":

        coins = coins+coinsperclick

        print(coins)

        a = 1


森林海
浏览 71回答 2
2回答

慕尼黑5688855

import timecoins = 0while True:     coins += 1     time.sleep(1)每秒添加一个

HUWWW

您可以使用 2 个属性:1 个用于硬币计数器,1 个用于每秒硬币数。每当您使用硬币属性时,您都会计算自上次使用硬币计数器以来您赚取的新硬币,这有点“假装”就像硬币一直在计数一样,而只是“具体化”了硬币,无论何时都很重要。import timeclass Game:  def __init__(self):    self._coins = 0    self._last_time = time.time()    self._cps = 1    @property  def coins(self):    self._collect_coins()    return self._coins  def _collect_coins(self):    new_time = time.time()    self._coins += self._cps * (new_time - self._last_time)    self._last_time = new_time  @property  def coins_per_second(self):    return self._cps  @coins_per_second.setter  def coins_per_second(self, value):    self._collect_coins()    self._cps = value每当您使用新硬币时,此代码都会计算它的价值:>>> g = Game()>>> g.coins0>>> time.sleep(2)>>> g.coins2并且它确保每当游戏coins_per_second发生变化时,无论每秒更改硬币之前您赚取多少硬币,您都会从那时开始以新的比率开始赚取:>>> g = Game()>>> time.sleep(1)>>> g.coins_per_second = 2>>> time.sleep(1)>>> g.coins3如果您在每秒金币发生变化时没有收集金币,那么您就会以每秒 2 个金币的变化率错误地计算这两个秒。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python