Python study diary – KillAliens realizes continuous reading and writing and computing

foreword

It has been half a year since I put down Python. After the mid-term exam, the preliminary algorithm will be involved soon. I think that through the thinking of Python, it is very helpful to the learning of the algorithm, so I spent a few hours writing this KillAliens game.

The two most important parts are the continuous reading and writing of scores and the generation of random events. Next, I will describe in detail how to implement the entire KillAliens game and the implementation process of these two parts.

based on

This KillAilens small game is inspired by the simple killing alien.py I wrote a long time ago. This simple text expression game has less than 30 lines. In KillAilens, the scoring system is not available in killing aliens.py , while using a dictionary, using key-value pairs to achieve each type of Ailen corresponding to its own score.

Kill Alien.py can be found in my Python learning repository.

output preview

欢迎来到KillAliens 小游戏,字典已经初始化完成接下来请输入内容你的名字:Magneto你好Magneto ,接下来让我为你介绍游戏中有的Alien 以及其对应的分数!击杀Big Alien 可以获得10 分 击杀Middle Alien 可以获得5 分 击杀A Alien 可以获得2 分 击杀Small Alien 可以获得1 分如果你想退出游戏,请输入'q' 键,退出游戏,或输入其它任意值开始(继续)游戏。当前你的分数是0 分请输入对应的分数以选择你要击杀的Alien 击杀成功后,可获得指定分数 名称:Big Alien对应分数:10名称:Middle Alien对应分数:5名称:A Alien对应分数:2名称:Small Alien对应分数:1请注意,你有概率无法击杀Alien 并且会因此结束游戏,并且对应分数越高,击杀几率越小。请输入对应分数:1你选择了Small Alien恭喜你Magneto ,击杀成功,获得1 分当前总分:1.0 分如果你想退出游戏,请输入'q' 键,退出游戏,或输入其它任意值开始(继续)游戏。当前你的分数是1.0 分请输入对应的分数以选择你要击杀的Alien 击杀成功后,可获得指定分数 名称:Big Alien对应分数:10名称:Middle Alien对应分数:5名称:A Alien对应分数:2名称:Small Alien对应分数:1请注意,你有概率无法击杀Alien 并且会因此结束游戏,并且对应分数越高,击杀几率越小。请输入对应分数:10你选择了Big Alien恭喜你Magneto ,击杀成功,获得10 分当前总分:11.0 分如果你想退出游戏,请输入'q' 键,退出游戏,或输入其它任意值开始(继续)游戏。当前你的分数是11.0 分请输入对应的分数以选择你要击杀的Alien 击杀成功后,可获得指定分数 名称:Big Alien对应分数:10名称:Middle Alien对应分数:5名称:A Alien对应分数:2名称:Small Alien对应分数:1请注意,你有概率无法击杀Alien 并且会因此结束游戏,并且对应分数越高,击杀几率越小。请输入对应分数:10你选择了Big Alien很遗憾Magneto ,你尝试击杀Big Alien 失败了,分数清零,退出游戏。进程已结束,退出代码0

Code preview

 ############################ ### Date 2022 May 14 ### ### Author Magneto ### ### Name KillAliens <——> ### Facility Windows 11 ### ### Language Python ### ############################ import random aliens_name_and_mark = { 'Big Alien': '10', 'Middle Alien': '5', 'A Alien': '2', 'Small Alien': '1', } state_one = ['1', '2', '3', '4', '5'] The_mark = ['0'] print("欢迎来到KillAliens 小游戏,字典已经初始化完成\n接下来请输入内容\n") Your_name = input("你的名字:") print(f"你好{Your_name} ,接下来让我为你介绍游戏中有的Alien 以及其对应的分数!") for name, marks in aliens_name_and_mark.items(): print(f"\t击杀{name} 可以获得{marks} 分") while True: exit_the_game = input("如果你想退出游戏,请输入'q' 键,退出游戏,或输入其它任意值开始(继续)游戏。") if exit_the_game == 'q': break print(f"当前你的分数是{The_mark[-1]} 分") print("请输入对应的分数以选择你要击杀的Alien 击杀成功后,可获得指定分数") for name_2, makrs_2 in aliens_name_and_mark.items(): print(f"\t名称:{name_2}\n\t\t对应分数:{makrs_2}") print("请注意,你有概率无法击杀Alien 并且会因此结束游戏,并且对应分数越高,击杀几率越小。") ChoiseAliens = input("请输入对应分数:") if ChoiseAliens == '1': state_two = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] The_Random = random.choice(state_two) print("你选择了Small Alien") if The_Random in state_one: print(f"恭喜你{Your_name} ,击杀成功,获得1 分") value = The_mark[-1] float_value = float(value) end_value = float_value + 1 The_mark.append(end_value) print(f"当前总分:{The_mark[-1]} 分\n\n\n") del The_mark[-2] else: print(f"很遗憾{Your_name} ,你尝试击杀Small Alien 失败了,分数清零,退出游戏。") break if ChoiseAliens == '2': state_two = ['1', '2', '3', '6', '7', '8'] The_Random = random.choice(state_two) print("你选择了A Alien") if The_Random in state_one: print(f"恭喜你{Your_name} ,击杀成功,获得2 分") value = The_mark[-1] float_value = float(value) end_value = float_value + 2 The_mark.append(end_value) print(f"当前总分:{The_mark[-1]} 分\n\n\n") del The_mark[-2] else: print(f"很遗憾{Your_name} ,你尝试击杀A Alien 失败了,分数清零,退出游戏。") break if ChoiseAliens == '5': state_two = ['1', '2', '6', '7', '8'] The_Random = random.choice(state_two) print("你选择了Middle Alien") if The_Random in state_one: print(f"恭喜你{Your_name} ,击杀成功,获得5 分") value = The_mark[-1] float_value = float(value) end_value = float_value + 5 The_mark.append(end_value) print(f"当前总分:{The_mark[-1]} 分\n\n\n") del The_mark[-2] else: print(f"很遗憾{Your_name} ,你尝试击杀Middle Alien 失败了,分数清零,退出游戏。") break if ChoiseAliens == '10': state_two = ['1', '6', '7', '8'] The_Random = random.choice(state_two) print("你选择了Big Alien") if The_Random in state_one: print(f"恭喜你{Your_name} ,击杀成功,获得10 分") value = The_mark[-1] float_value = float(value) end_value = float_value + 10 The_mark.append(end_value) print(f"当前总分:{The_mark[-1]} 分\n\n\n") del The_mark[-2] else: print(f"很遗憾{Your_name} ,你尝试击杀Big Alien 失败了,分数清零,退出游戏。") break

code analysis

To be continued

This article is reproduced from: https://fmcf.cc/technology/763/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment