简体   繁体   English

运行代码后如何禁用/限制一行代码? (蟒蛇)

[英]How to disable/restrict a line of code after it has been run? (Python)

I'm very new to coding and Python. 我是编码和Python的新手。 I'm making a simple "choose your adventure" type game in Python. 我正在用Python做一个简单的“选择冒险”类型的游戏。 The whole object of the game is to enter all 4 rooms/doors and acquire all 4 digits of the secret code to enter a locked door, which leads to the treasure. 游戏的整个目标是进入所有4个房间/门,并获取全部4位密码输入进入锁定的门,从而进入宝藏。 This is what im using to add a digit to the code: 这就是即时通讯用来在代码中添加数字的方式:

from random import choice

code = range(10)
my_code = []

def add_code():
    if len(my_code) < 4:
        code_digit = choice(code)
        my_code.append(code_digit)

So for every room, I have puzzles and challenges to conquer. 因此,对于每个房间,我都充满困惑和挑战。 If you complete the challenge, I have it run the add_code() function. 如果完成挑战,我可以运行add_code()函数。 What I want to avoid is having a user repeatedly go to the same door, complete the same challenge, and add a digit to the list, without even having to open any other door or complete any other challenge. 我要避免的是让用户反复去同一个门,完成相同的挑战,并将数字添加到列表中,而不必打开任何其他门或完成任何其他挑战。 Is there a way to make a certain line of code not run after it has already been ran once? 有没有办法使某行代码在已经运行一次之后不运行? Like, if door 1's challenge was completed and a digit was added to the code, is there a way to not let the user add another digit from door 1's add_code() function? 像是,如果完成了门1的挑战并将一个数字添加到代码中,是否有办法不让用户从门1的add_code()函数中添加另一个数字?

Associate each challenge with a boolean flag. 将每个挑战与一个布尔标志关联。 Set the flag to True when the player finishes the challenge, and check the flag before giving the player the option to do the challenge again. 当玩家完成挑战时,将标志设置为“ True”,并在允许玩家选择再次执行挑战之前检查该标志。

For example, if you had a "punch monkeys" quest, you might have the following flag: 例如,如果您执行“打孔猴子”任务,则可能具有以下标志:

monkeys_punched_yet = False

When the player punches the monkeys, you'd set 当玩家拳打猴子时,您将

monkeys_punched_yet = True

In the monkey-punching area, you'd have a check something like this: 在猴子打孔区域,您将进行如下检查:

if monkeys_punched_yet:
    description_text = ("You see a pile of bruised and battered monkeys "
                        "in a corner of the room.")
else:
    description_text = "You see a group of unsuspecting, punchable monkeys."
    options.append("punch monkeys")

You could check to see if the new code is not already in the list of completed codes. 您可以检查新代码是否尚未在完成的代码列表中。

def add_code():
    if len(my_code) < 4:
        code_digit = choice(code)
        if ( code_digit in my_code):
           print("Already visited that room")
        else:
            my_code.append(code_digit)

Use booleans: 使用布尔值:

door1 = False
door2 = False
door3 = False
door4 = False

After you have completed a challenge in one door, set the relevant door to True . 在一个门中完成挑战后,将相关门设置为True Then, you'll want to have some kind of a centre to display each door. 然后,您将需要某种中心来显示每个门。 Think in a game how you're in the centre room, and you are surrounded by four doors. 在游戏中思考您在中心房间的情况,而您被四扇门所包围。 Your code would be something like this: 您的代码将如下所示:

which_door = raw_input('Which door do you want to enter? ')
if which_door == "1":
    if door1: # This is short for "if door1 is True"
        print "You have already completed this challenge."
    else: # Otherwise, it is set to False. The challenge has not been started
        do_stuff()
        door1 = True
etc for each door...

By the way, there's a simpler way to make a random code: 顺便说一下,有一种更简单的方法来制作随机代码:

>>> random.sample(range(10), 4)
[3, 4, 0, 1]

What you could do is doing adding completed challenges to a list of completed challenges. 您可以做的是将已完成的挑战添加到已完成的挑战列表中。 Each challenge must have a unique identifier. 每个挑战必须具有唯一的标识符。

user.completed_challenges = set()

When opening a challenge, check if the challenge is in the list of completed challenge 发起挑战时,检查挑战是否在已完成挑战的列表中

if not challenge.uuid in user.completed_challenges:
   # do code

   # When everything is done add the uuid to challenges
   user.completed_challenges.add(challenge.uuid)

Also one thing you could do is to have for exemple two rooms that are the same challenge, if you do one of them. 另外,您可以做的一件事就是,例如,如果您选择其中一个,则拥有两个面临相同挑战的房间。 Both should be marked as "done". 两者都应标记为“完成”。 Then give both room the same unique identifier 然后给两个房间相同的唯一标识符

It is quite simple but should do the job in your case I guess. 这很简单,但我认为应该在您的情况下完成工作。

explanations 说明

Instead of using booleans, I'd rather use uuids . 与其使用布尔值,不如使用uuids It can be strings or whatever. 它可以是字符串或其他任何东西。 It has to be an identifier that is capable to identify single challenges.. For exemple ['room1', 'room2', 'room3', 'room4'] 它必须是能够识别单个挑战的标识符。例如: ['room1', 'room2', 'room3', 'room4']

The set prevent you from adding more than once elements. set可防止您添加多个元素。 The problem with booleans is that it is not quite flexible to use. 布尔值的问题在于使用起来不太灵活。 You'll have to create n variables for n challenges. 您必须为n挑战创建n变量。 It won't be very clean to add more challenge. 添加更多挑战并不是一件很干净的事情。 When you have less than 5 challenges, it should work fine but if you want to add more, having a set that keep tracks of the challenges you've done is much better. 当您的挑战少于5个时,它应该可以正常工作,但是如果您要添加更多挑战,那么拥有一套跟踪您已经完成的挑战的套件会更好。

The other good thing about the set, is that you'll have a good idea which challenges the user already completed. 集合的另一个好处是,您将有一个很好的主意,可以挑战已经完成的用户。

print "You completed challenges %s" % ', '.join(user.completed_challenges)

Whith booleans, you'll have to iterate over all hardcoded booleans which will get much much harder to handle with time. Whith布尔值,您必须遍历所有硬编码的布尔值,随着时间的推移,它们将变得更加难以处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在引发异常后,python 回溯如何运行? - How is the python traceback able to run after an exception has been raised? 如何在python中删除样式和元素后解析代码 - How to parse code after it has been stripped of styles and elements in python 在python中将代码运行了x次后,如何在代码中运行函数的特定部分? - How to run specific part of a function in a code when code has been run for x times in python? 如果python“一行一行”运行文件,它如何在定义之前使用函数? - If python run the file "line after line" how can it use a function before it's been defined? Python:限制代码运行一个小时 - Python: Restrict the code to be run for an hour 将新文件添加到桌面文件夹时如何运行python代码? - How to run python code when new file has been added to a folder in desktop? 如何让我的 Python 摩尔斯电码解码器在一个单词被翻译后分离? - How to get my Python Morse code-decoder to separate after a word has been translated? 如何打印最后一行 Python 一直卡在 - How to print the last line Python has been stuck on 创建python后如何使用python移动tkinter窗口? - How to move tkinter windows using python after it has been created? Python-找到匹配项后如何读取字符串的其余部分 - Python - how to read the remainder of a string after a match has been found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM