哈喽,这位同学,大家好!今天我们将从头开始制作一款有趣的基于文本的冒险游戏。首先,让我们了解什么是基于文本的游戏,然后我们将用 python 编程语言实现同样的游戏。
什么是基于文字的游戏?
文本游戏是一种完全基于文本输入输出的简单游戏。在这种类型的游戏中,当用户以输入的形式做出选择时,用户可以选择处理各种情况。
我们游戏的故事情节
下图显示了我们将在本教程中用 python 构建的小故事。您可以根据自己的喜好扩展或更改故事。
基于文本的冒险游戏的Python实现
让我们首先通过打印初始场景以及故事如何发展来开始故事。只需使用打印功能即可完成此操作。为了让它更有趣,我们还可以添加表情符号和表情符号!
1
2
3
4
5
6
7
8
|
print ( """WELCOME TO THE ADVENTURE GAME! Let's start the action! ☆-🎬-☆ Lily wakes up in her bedroom in the middle of the night. She heard a loud BAN outside the house. Now she has two choices she can either stay in the room or check what the sound might be about. Type your choice: Stay or Evaluate? """ ) |
干得好!现在我们已经设置好了场景,结果也很有趣,看这里是你的首选!现在让我们获取用户的输入并为每个选择输入条件语句。
我们需要确保我们的游戏能够回答用户所做的所有类型的输入,并且不会导致任何选择错误。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 号
18
19
20
21
22
23
24
25
|
def scene1(): import time print ( """WELCOME TO THE ADVENTURE GAME! Let's start the action! ☆-🎬-☆ Lily wakes up in her bedroom in the middle of the night. She heard a loud BAN outside the house. Now she has two choices she can either stay in the room or check what the sound might be about. Type your choice: Stay or Evaluate? """ ) c1 = input () time.sleep( 2 ) ans = 'incorrect' while (ans = = 'incorrect' ): if (c1.upper() = = "STAY" ): print ( "\nLily decides to stay in the room and ends up staying inside forever as noone seems to come to help her." ) ans = 'correct' elif (c1.upper() = = "EVALUATE" ): print ( "Lily exits the room silently and reaches the main hall." ) ans = 'correct' scene2() else : print ( "ENTER THE CORRECT CHOICE! Stay or Evaluate?" ) c1 = input () |
我们选择第一个选择input
,然后创建一个变量来确认我们的答案是否正确。然后我们创建条件循环和 if-else 语句。游戏会不断询问选择,直到给出的答案有效。
现在第一个场景已经完成,我们可以进入下一个场景并以相同的方式构建整个游戏。下面是第二个场景的代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 号
18
19
20
21
22
23
24
25
26
27
28
29
|
def scene2(): import time print ( """ In the main hall, she finds a strange but cute teddy bear on the floor. She wanted to pick the teddy up. But should she? It doesn't belong to her. (•˳̂•̆) Type your choice: Pick or Ignore? """ ) time.sleep( 2 ) c1 = input () ans = 'incorrect' while (ans = = 'incorrect' ): if (c1.upper() = = "PICK" ): print ( """\nThe moment Lily picked up the the teddy bear. The Teddy bear starts TALKING!The bear tells Lily that she is in grave danger as there is a monster in the house.And the monster has captured her PARENTS as well!But he hugged her and told her not to get scared as he knows how to beat the moster!""" ) time.sleep( 2 ) print ( """\nThe bear handed lily a magical potion which can weaken the moster and make him run away!He handed her the potion and then DISAPPEARED!Lily moved forward.""" ) ans = 'correct' pick = "True" elif (c1.upper() = = 'IGNORE' ): print ( """\nLily decided not to pick up the bear and walked forward.""" ) ans = 'correct' pick = "False" else : print ( "Wrong Input! Enter pick or ignore?" ) c1 = input () time.sleep( 2 ) scene3(pick) |
第三个场景的代码如下。现在第三个场景的结果取决于场景2中所做的选择,即泰迪熊是否被选中或忽略,以及主角是否收到药水。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
def scene3(pick_value): import time print ( """\n\nAfter walking for a while, Lily saw the MONSTOR in front of her! It had red eyes and evil looks. She got very scared! """ ) time.sleep( 2 ) if (pick_value = = "True" ): time.sleep( 2 ) print ( """But then she remembered! She had the magic portion and she threw it on the moster! Well she had nothing to lose!""" ) time.sleep( 2 ) print ( "\n The monster SCREAMED in pain but he managed to make a portal and pushed Lily to a new world!" ) elif (pick_value = = "False" ): print ( "The monster attacked Lily and hurt her! She was then thrown to the new world by the monster!" ) |
我们将在三个场景之后结束故事的第一章。您可以根据自己的喜好扩展甚至更改整个故事。
要开始故事,只需开始故事的场景1。
1
2
3
|
scene1() print ( "\n\n" ) print ( "=================================END OF CHAPTER 1=================================" ) |
上面故事的结果如下所示。而且非常棒!
结论
现在您知道如何自己构建简单易用的基于文本的冒险游戏了!您也可以尝试自己独特的故事!快乐编码!感谢您的阅读!