首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用comand提示符的视频游戏的python倒计时器

使用comand提示符的视频游戏的python倒计时器
EN

Stack Overflow用户
提问于 2021-01-07 15:47:51
回答 1查看 86关注 0票数 0

我目前正在设计一个在python中选择你自己的冒险游戏,我试图设置一个计时器,在每3-4秒你的氧气点消耗3秒之后,我会发布游戏的全部代码,我想要帮助它的定时器方法,以及把定时器放在哪里。谢谢你抽出时间来帮我

代码语言:javascript
复制
__Author__ = 'Mangle200/Tristan Lauzon'
import tkinter
from time import *
from random import *
import os,sys

def clear_screen():
    os.system('cls' if os.name=='nt' else 'clear')
def title():
    print("___  ___                                        \n|  \/  |                                        \n| .  . | ___   ___  _ __                        \n| |\/| |/ _ \ / _ \| '_ \                       \n| |  | | (_) | (_) | | | |                      \n\_|  |_/\___/ \___/|_| |_|                      \n                                                \n                                                \n _                     _ _                      \n| |                   | (_)                     \n| |     __ _ _ __   __| |_ _ __   __ _          \n| |    / _` | '_ \ / _` | | '_ \ / _` |         \n| |___| (_| | | | | (_| | | | | | (_| |         \n\_____/\__,_|_| |_|\__,_|_|_| |_|\__, |         \n                                  __/ |         \n                                 |___/          \n _____                    _ _ _   _             \n|  ___|                  | (_) | (_)            \n| |____  ___ __   ___  __| |_| |_ _  ___  _ __  \n|  __\ \/ / '_ \ / _ \/ _` | | __| |/ _ \| '_ \ \n| |___>  <| |_) |  __/ (_| | | |_| | (_) | | | |\n\____/_/\_\ .__/ \___|\__,_|_|\__|_|\___/|_| |_|\n          | |                                   \n          |_|                                   \n")
def setup():
    global name
    global HP
    global SP
    global OP
    while True:
        name = input('What is your name --->   ')
        isnum = False
        for i in name:
            if i.isnumeric():
                isnum = True
                break
    
        if isnum:
            print('Please type your name.  ')
            continue
    
        break
    
    HP = randint(17,20)
    SP = randint(17,20)
    OP = randint(90,100)
    print('welcome ['+name+':]:  to the moon landing expedition')

def villager():
    global npcname
    global response
    responses = ['hello', 'who are you?', 'why are you here?', 'what are your intentions?']
    npcnamechoice = ['Sarah', 'Bobby', 'Chris', 'Trent', 'Susan', 'Michelle', 'Mara']
    shuffle(npcnamechoice)
    print('\n['+npcname+':]Hello my name is'+npcname+'would you like to talk to me?\n')
    shuffle(responses)
    print('press y to talk to the villager')
    if input().upper== 'Y':
        print('['+npcname+':]  '+responses[0])
    else:
        print('['+npcname+':] Goodbye')
def enemy():
    global enemyHP
    global enemyMP
    global enemyname
    enemyHP = randint(5,20)
    enemyMP = randint(5,20)
    enemyname = ['Space ogre', 'Space witch', 'Space bandit', 'Rick Astley in Space', 'Emminem In space']
    shuffle(enemyname)
    print('\nSuddenly you hear somthing in the woods and a'+enemyname[0]+'appears')
    print('your enemy has'+' '+ str(enemyHP)+' '+'Health points')
    print('your enemy has'+' '+ str(enemyMP)+' '+'magic points')
def north():
    global gonorth
    gonorth = 'you have decided to head north'
def east():
    global goeast
    goeast='you have decided to go east'
def west():
    global gowest
    gowest = 'you have decided to go west'
clear_screen()
title()
setup()
print('hello this game is created by ',__Author__)
print('your health is at ', HP, 'your stamina points are at', SP, 'your Oxygen levels at the start of Chapter one wil be ', OP)
print('<suspencful music activates>, Robotic voice:"hello and welcome to the',)
title()
input('press enter to continue')
print('you have been choosen out of fifty-million canidates for this once in a lifetime adventure your mission...\nis reconasance of planets near and far, my superiors have trust in you\nplease do your be..."<electrical static cuts out the automated voice>\n<emergency sirens are sounded>\nMan on Intercoms:"this is not a drill i repeat this is not a drill everyone you must evacuate imediat..."')
print('<the sound of gunshots can be heard all around you> *in mind:even though i fear for my life, i feel like aslong as i stay put i am safe*<a loud buzzer is heard>\nAutomated voice:"Initiating emergancy launch in T-minus ten')
print('nine')
print('eight')
print('seven')
print('six')
print('five')
print('four')
print('t-h-r-e-e-e-e-e')
print('t-t-t-t-w-o-o-o')
print('o-o-o-o-n-n-n-e-e')
print('i-i-i-g-n-i-i-i-s-i-o-n"')
print('<the sound of rocket thrusters are all you are able to hear as you watch the ground getting fartehr way>')
print('\n\n\n\n\n\n\n<five years have passed since the attack on the military space center>\n<your space craft has almost completly run out of fuel>\n<you are forced to make an emergancy landing on one of the moons of the planet nexus>')
input('press enter to start the first chapter of the game')
print(" _____ _                 _            \n/  __ \ |               | |           \n| /  \/ |__   __ _ _ __ | |_ ___ _ __ \n| |   | '_ \ / _` | '_ \| __/ _ \ '__|\n| \__/\ | | | (_| | |_) | ||  __/ |   \n \____/_| |_|\__,_| .__/ \__\___|_|   \n                  | |                 \n                  |_|                 \n _____                                \n|  _  |                               \n| | | |_ __   ___                     \n| | | | '_ \ / _ \                    \n\ \_/ / | | |  __/                    \n \___/|_| |_|\___|                    \n                                      \n                                      \n _____ _      _   _                   \n/  ___(_)    | | | |                  \n\ `--. ___  _| |_| |__                \n `--. \ \ \/ / __| '_ \               \n/\__/ / |>  <| |_| | | |              \n\____/|_/_/\_\\__|_| |_|              \n                                      \n                                      \n___  ___                              \n|  \/  |                              \n| .  . | ___   ___  _ __              \n| |\/| |/ _ \ / _ \| '_ \             \n| |  | | (_) | (_) | | | |            \n\_|  |_/\___/ \___/|_| |_|            \n                                      \n                                      \n _____  __                            \n|  _  |/ _|                           \n| | | | |_                            \n| | | |  _|                           \n\ \_/ / |                             \n \___/|_|                             \n                                      \n                                      \n _   _                                \n| \ | |                               \n|  \| | _____  ___   _ ___            \n| . ` |/ _ \ \/ / | | / __|              \n| |\  |  __/>  <| |_| \__ \              \n\_| \_/\___/_/\_\\__,_|___/              \n                                         \n                                 ")

input('press enter to continue')


print(name+']:"Ok so i am here on one of the moons on an unknown planet with absolutly no way of contacting for help i have no fuel and my power reserves are failing,\n soon i will have no oxygen left,\n fortunatly there are solar panels on the side of the rocket so once the sun hits the panels the oxygen production systems will kick in,\ni just hope i can survive long enough for a search party comes to find me."')
print('the sixth moon of nexus is the biggest source of metal in the universe, (dont wory about oxygen for the time being ')
EN

回答 1

Stack Overflow用户

发布于 2021-01-07 16:01:10

我会把所有的投入集中在一起,并在那里完成。本质上,在每次操作之后,都要将数据与输入一起打印出来。

另外,作为一个类,这将更好地工作。这样,您就可以不使用全局值来共享数据。

如果你想得到真正的花哨,你可以使用状态栏进度条的播放器级别从启发

编辑:下面是一个使用类的可能实现。在这里,氧是一个属性,所以它是每次被调用时计算出来的。你可能想推迟设定氧气的开始时间,直到介绍完成,或者通过增加5-10秒来填充这个数字。它也是整数的四舍五入,但是您可能想要使用浮点数,这样您就可以将3.6v舍入到4。

你也可以把它分开,为主玩家、村民、敌人等设置单独的等级。

代码语言:javascript
复制
__Author__ = 'Mangle200/Tristan Lauzon'
import os
import random
import time


class MoonLandingExpedition:

    def __init__(self):

        self.health = 0
        self.stamina = 0
        self._oxygen_start = (0, 0.0)
        self.name = None
        self.direction = None

    @property
    def oxygen(self):

        oxygen_per_sec = 0.33
        initial_level, initial_time = self._oxygen_start
        time_delta = time.time() - initial_time
        current_level = max(0, initial_level - time_delta * oxygen_per_sec)
        return round(current_level)

    def clear_screen(self):
        os.system('cls' if os.name == 'nt' else 'clear')

    def encounter_enemy(self):

        e_name = random.choice(('Space ogre', 'Space witch', 'Space bandit',
                              'Rick Astley in Space', 'Emminem In space'))
        e_health = random.randint(5, 20)
        e_magic = random.randint(5, 20)

        print('\nSuddenly you hear somthing in the woods and a {e_name} appears')
        print('your enemy has {e_health} Health points')
        print('your enemy has {e_magic} magic points')

    def encounter_villager(self):

        v_name = random.choice(('Sarah', 'Bobby', 'Chris', 'Trent', 'Susan', 'Michelle', 'Mara'))
        responses = ('Hello!', 'Who are you?', 'Why are you here?', 'What are your intentions?')

        print(f'\n[{v_name}:] Hello my name is {v_name} would you like to talk to me?\n')
        if self.get_input('Press y to talk to the villager').upper == 'Y':
            print(f'[{v_name}:] {random.choice(responses)}')
        else:
            print(f'[{v_name}:] Goodbye')

    def get_name(self):

        while True:
            name = input('What is your name? --->   ')
            if any(char.isdigit() for char in name):
                print('Please type your name.  ')
            else:
                break

        return name

    def get_input(self, message):
        self.print_status()
        return input(message)

    def intro(self):

        print("You have been chosen...")
        print("Moon of Nexus")
        self.get_input('press enter to continue')

    def print_status(self):
        print(f'Health: {self.health}  Stamina: {self.stamina}  Oxygen: {self.oxygen}')

    def run(self):

        self.clear_screen()
        self.title()
        self.setup()
        print('hello this game is created by ',__Author__)
        self.print_status()
        print('<suspenseful music activates>, Robotic voice:"hello and welcome to the',)
        self.title()
        input('press enter to continue')
        self.intro()

    def setup(self):

        self.health = random.randint(17, 20)
        self.stamina = random.randint(17, 20)
        self._oxygen_start = (random.randint(17, 20), time.time())

        self.name = self.get_name()

        print(f'Welcome [{self.name}:]:  to the moon landing expedition')

    def title(self):
        print("Moon Landing Expedition")


if __name__ == "__main__":
    game = MoonLandingExpedition()
    game.run()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65615552

复制
相关文章

相似问题

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