2022寒假在家练 - 基于Python实现小游戏
- 自我介绍
中央民族大学 2018级 信息工程学院 电子信息工程 李晓航
- 项目描述
2.1项目介绍
设计或移植一款经典的游戏。
在Raspberry Pi上运行DOS软件Microsoft的第一个基于PC的操作系统MS-DOS,于1981年发布,并于2000年停产。在此期间,超过2000款游戏发行了。此外,Windows 95和98可以运行DOS软件。通常,您会离开桌面操作系统来运行MS-DOS游戏和应用程序。
该过程非常简单:
安装DOSBox
配置DOSBox
查找MS-DOS游戏
安装MS-DOS游戏
享受!
2.2设计思路(框图)
2.3简单的硬件介绍
本次试验使用的是树莓派RP2040板。简单来说,树莓派代表了一种微型电脑。树莓派是尺寸仅有信用卡大小的一个小型电脑。树莓派能替代日常桌面计算机的多种用途,包括文字处理、电子表格、媒体中心甚至是游戏。并且树莓派还可以播放高至1080p的高清视频。
本次使用树莓派RP2040板的目的是,为了实现游戏功能的代码,从而通过LCD屏显示,通过按键和四向摇杆控制游戏的动作:1. 经典的Pong2. 挖地雷3. 打飞机等。
2.4功能
2.4.1主要的代码片段级说明
步骤1:在Raspberry Pi上安装DOSBox
已经有一个操作开始之前,请在Raspberry Pi上安装系统。在打开Pi的电源下,打开终端,然后检查更新:完成后,安装DOSBox:
步骤2:在Raspberry Pi上配置DOSBox
需要一个目录来运行DOSBox并从以下位置运行软件:
mkdir dos
下一步,在nano中编辑DOSBox配置文件:
sudo nano .dosbox/dosbox-0.74.conf
下一步,滚动到文件末尾,然后在[autoexec]标题下添加安装指令。
DOSBox运行时,它将dos目录用作C:驱动器。
mount c ~/dos
使用 Ctrl + X 保存并退出文本编辑器,并按 Y 进行确认。
现在可以从Raspberry Pi上的“游戏”菜单运行DOSBox!
第3步:选择MS- Raspberry Pi的DOS游戏
好吧,旧货店和eBay是一个不错的起点。老式游戏通常容易设计,因此,需要确保已将正确类型的磁盘驱动器连接到Raspberry Pi。
如果不是这样,可能的话,将需要依靠在线找到的磁盘映像。可以用ROM文件,将其解压缩并安装在DOSBox中,就像在正版MS-DOS PC上一样。
一旦有了游戏,您该怎么办?如何将它们加载到DOSBox中?
步骤4:在Raspberry Pi上安装MS-DOS游戏
要在Raspberry Pi上运行DOS游戏,需要DOSBox来注意游戏。最好的方法是在dos目录中为游戏创建一个新目录:
mkdir dos/games
然后,使用mv(move)命令从中复制任何游戏下载到此新文件夹中:
mv Downloads/[GAME_TITLE] dos/games/
现在就可以开始安装和玩游戏了。
启动DOSBox,并使用命令行来导航到/games/目录。请记住,现在处于模拟MS-DOS的环境中,因此需要不同的命令。当cd仍更改目录时,目录用dir列出(可以使用dir/p一次显示目录列表中的目录)。键入help以获得更多帮助。
将游戏复制到游戏目录后,打开一个特定目录,然后找到install.bat文件并运行。有时,它可能以相关游戏的名字命名。例如,在这次设计中,我使用cm2.bat开始安装Championship Manager 97-98。
在安装游戏时等待。完成后,通常可以使用缩写形式的标题来运行游戏。通常,它与BAT文件相同,但没有后缀。
例如,要运行Championship Manager 2,我只需输入cm2。但是,这因游戏而异。
2.4.2图片展示
2.4.3实现的功能
一进入游戏,主人公就会随机获取一个游戏已经设定的道具。最开始,主人公站在丛林的边缘,只有左右可选,左边是个洞穴,右边是沙滩。
选择左边,则开始可以获得手电筒,但是在洞穴中会碰到蛇hp-20,继续向下会到一个未知空间,游戏结束。
选择右边,会到沙滩 获得凉爽的水hp+70,但是由于没有游泳装备,如果继续选择游泳,会遇到海啸而死亡。
3.过程
3.1遇到的主要难题
在游戏图形编程的环节,我遇到了困难。游戏图形编程在设计过程中至关重要。教学上也有整整两堂课,对该部分进行了讲解。但身为女生的我,面对繁琐的编程语言,显得愈发吃力。在大学期间,编程的专业选修课,也学得不够扎实,没有取得较高的分数。
3.2解决方法
针对编程薄弱的问题,我反复观看课程,不懂的地方询问老师,有时也会拿出大学编程课本仔细翻阅,温故而知新。最终问题都得到妥善的解决。
3.3未来的计划
本人认为,游戏的运行和样式固然重要,但是游戏音乐,作为玩家玩游戏时的BGM也至关重要。它往往在设计过程中被轻视或者忽视。实际上,音乐可以给玩家带来更好的游戏体验,游戏内容和音乐种类的搭配也是至关重要。
3.4建议
树莓派的教学活动对我的帮助莫大,意义非凡,让我原本枯燥的大学生活变得多姿多彩。在学校专业课学习的编程技能,也在实际中运用了起来。老师讲课生动有趣,讲解清晰,面对同学们的提问耐心解答,回复积极并且及时。相信一定会有越来越多的人从中受益。
4.代码
#Python Adventure Game
import time
import random
#define variable health point
hp = 30
#define variable object_ to show what the adventurer has
object_=[]
#define variable what the game sets
tools = ["Torch","Rope","Spanner","50HP","10HP"]
#set two acceptable answers ,select any one to end loop
def get_input(prompt,accepted):
while True:
value = input(prompt).lower()
if value in accepted:
return value
else :
print("That is not a recognisedanswer,must be one of",accepted)
def handle_room(location) :
global hp
if location== "start":
print("You are stading on a path at the edge of a jungle.There is a cave to your left and a beach to your right.")
object_.append(random.choice(tools))
print("Lucky,you have gained",object_[-1])
use_tools(object_)
direction = get_input("Do you want to go left or right?",["left","right"])
if direction == "left":
return "cave"
elif direction == "right":
return "beach"
elif location == "cave":
print("On the entrance of cave,you find a torch!!!")
object_.append("Torch")
print("You walk to the cave and notice there is an opening.")
print("A small snake bites you,and you lose 20 health points.")
hp-=20
answer = get_input("Do you want to go deeper?(y/n)",["y","n"])
if answer == "y":
return "deep_cave"
elif answer == "n":
return "start"
elif location == "beach":
print("You walk to the beach but remember you do not have any swimwming equipment.")
print("The cool water revitalizes you.You have never felt more alive,gain 70 health points.")
hp+=70
answer = get_input("Do you want to go swimming?(y/n)",["y","n"])
if answer == "y":
return "sea"
elif answer == "n":
return "start"
elif location == "sea":
print("Suddenly there was a tsunami,you can't escape.")
hp=0
return "end"
else :
print("Programmer error,room",location,"is unknown")
return "end"
#define the function to use the tools of HP
def use_tools(tool):
global hp
length = len(tool)
for i in range(0,length):
if tool[i]=="50HP":
hp+=50
tool.pop(i)
print("You have use the tool of 50HP")
j+=1
elif tool[i]=="10HP":
hp+=10
tool.pop(i)
print("You have use the tool of 10HP")
j+=1
#The begin of the program.
location = "start"
#Loop until we reach the special "end" location
while location!="end":
location = handle_room(location)
#Check we are not dead each return
print("You now have",hp,"health points.")
if hp<=0:
print("You are dead.\nGame Over!!!")
break
print("Your adventure has ended,bye~")
5.可编译下载的代码解释(用于验证)
#define variable health point
hp = 30(声明变量)
if value in accepted:
return value
else :
print("That is not a recognised answer,must be one of",accepted)
if语句,给玩家打出字。
if location== "start":
print("You are stading on a path at the edge of a jungle.There is a cave to your left and a beach to your right.")
object_.append(random.choice(tools))
print("Lucky,you have gained",object_[-1])
use_tools(object_)
direction = get_input("Do you want to go left or right?",["left","right"])
左右方向确立+规定
if direction == "left":
return "cave"
elif direction == "right":
return "beach"
IF语句,左右方向选择
answer = get_input("Do you want to go deeper?(y/n)",["y","n"])
if answer == "y":
return "deep_cave"
elif answer == "n":
return "start"
第一种方向的玩家选择
answer = get_input("Do you want to go swimming?(y/n)",["y","n"])
if answer == "y":
return "sea"
elif answer == "n":
return "start"
第二种玩家选择
#define the function to use the tools of HP
def use_tools(tool):
global hp
length = len(tool)
for i in range(0,length):
if tool[i]=="50HP":
hp+=50
tool.pop(i)
print("You have use the tool of 50HP")
j+=1
elif tool[i]=="10HP":
hp+=10
tool.pop(i)
print("You have use the tool of 10HP")
j+=1
开始循环
#The begin of the program.
location = "start"
#Loop until we reach the special "end" location
while location!="end":
location = handle_room(location)
#Check we are not dead each return
print("You now have",hp,"health points.")
if hp<=0:
print("You are dead.\nGame Over!!!")
break
print("Your adventure has ended,bye~")
用{;}插入的代码在四,五是代码解释!!!
我的树莓派板子好像有一些问题,但是班里面并没有同学有寄换的经历,所以我用了自己之前的板子。代码是树莓派适用的Python代码