20214201 刘嘉铭 实验四 Python综合实践 实验报告
目录
- 实验分析
- 实验设计
- 实现过程
- 实验结果
- 课程感想体会、意见建议
一、实验分析
根据综合实践要求:Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。选择了文字游戏这一内容开展事件,有如下几个考虑方面:
1. 个性化程度高
- 自我设计的游戏依托于自己原创设计的游戏内容,依托于基本的2G时代文字游戏大致运行逻辑,用小1000行代码进行的简单实现,不会与其他同学重复。
2. 拓展性强
- 由于实验规定时间相对较短,最终我只完成了一个场景的冒险内容的制作,并没有插入剧情和任务环节。但我们可以看到的是,文字游戏的实践方式能够提供很多拓展发挥的路径,便于长时间的开发应用。
3. 贴合个人喜好与情怀
- 本人闲暇时间还是比较喜欢打游戏的,对于游戏的一些基本逻辑运作方式较为熟悉,结合自身编程基础能够进行一些简单实现,唯一美中不足的就是个人的绘画水平实在是太差,做不了含图、动画的游戏出来,未来还应提高自身综合能力实现自己游戏的开发设计。
二、实验设计
本次实验设计的文字游戏基本实现了如下几个游戏功能:玩家数据存储、职业选择、属性面板、死亡惩罚、战斗系统(普攻、技能、随机事件)、商店、打工等。以下是对各个系统的介绍:
-
玩家数据存储
由于未能解决程序在Pycharm中能顺利运行而至ECS云中无法存储的问题,故最终代码将存储功能注释化了。该功能是对玩家的数据进行储存,以便于继续游戏。 -
职业选择
目前游戏中有战士和法师两个职业,前者高HP、DEF低MP、ATK;后者高MP、ATK低HP、DEF。这些基础属性将会影响玩家的游戏策略。 -
属性面板
玩家可以输入“I”来查看当前自己的属性值,了解自身角色的状态。 -
死亡惩罚
当玩家被怪物攻击死亡后,将会有降低一半等级的惩罚,玩家需要重新练级。 -
战斗系统
当玩家选择“外出冒险”后,当即进入战斗系统,目前有3种怪物随机与玩家战斗,并跟随玩家等级提高遭遇概率。
在真正进入与怪物的作战之前,会进行判定是否遭遇随机事件,随机事件将给予玩家属性一定的影响。
当玩家与怪物作战时,会有攻击、技能、逃跑三个选项。攻击会根据玩家自身ATK+身上装备提供的额外ATK综合怪物的DEF与自身HIT的情况计算伤害量,其中怪物也会对玩家反击。
计算伤害量的同时也会对玩家/怪物是否MISS进行判定,基本上实现了一般回合制战斗的游戏功能。
玩家的技能将会跟随自身等级提升而解锁,并且不同职业的技能略有不同。
当玩家选择逃跑的时候,会根据玩家自身SPD值与怪物做对比,若高于则成功,低于则失败,等于则五五开。
当玩家等级等于10级时将会遭遇BOSS,击败BOSS后该场景结束(因为目前就制作了一个场景,即击败BOSS后游戏将会结束) -
商店系统
玩家在外出冒险前可以进入商店系统购买药水和装备,购买后当即生效。其物品属性值在一定范围内随机生成。 -
打工系统
玩家除了可以在外出冒险的时候获取金币以外,也可以根据打工来赚取金币和经验值提升自己装备和等级,以面对更强大的怪物。
三、实现过程
整个游戏包含六个.py文件,共同支撑起整个简单文字游戏的运作。
主程序 Main_System.py
点击查看代码
# encoding: utf-8
import random
import time
import Create_Character as cc
import Game_Body
import os
print("欢迎来到20214201制作的MINI RPG GAME")
time.sleep(1)
c_work = 0
N_O = 'Not Opened'
g_difficulty = 0
SelectCharacter_Choice = 0
"""
wj = open('Player_Data.txt','a')
wj.close()
wj = open('Player_Data.txt','r')
print('当前存档' + wj.read() + '\n')
wj.close()
"""
while True:
print("————*菜单*————")
time.sleep(1)
print("-1.开始游戏")
print("-2.继续游戏")
print("-3.退出游戏")
f_choice = input('->')
try:
if int(f_choice) == 3:
exit()
elif int(f_choice) == 2:
print(N_O)
exit()
elif int(f_choice) == 1:
while True:
print("*请选择你的职业:")
print("-1.战士")
print("-2.法师")
print("-0.返回上一级")
s_choice = input('->')
try:
if int(s_choice) == 1:
SelectCharacter_Choice = cc.Warrior()
if SelectCharacter_Choice == 1:
c_work = 1
break
elif int(SelectCharacter_Choice) == 0:
continue
elif int(s_choice) == 2:
SelectCharacter_Choice = cc.Mage()
if int(SelectCharacter_Choice) == 1:
c_work = 1
break
elif int(SelectCharacter_Choice) == 0:
continue
elif int(s_choice) == 0:
break
if c_work == 1:
break
except ValueError:
print("-错误的选择,请重新选择~")
print("test")
time.sleep(0.5)
continue
if c_work == 1:
while True:
print("*请选择难度:")
print("-1.简单")
print("-2.普通")
print("-3.困难")
difficulty = input('->')
try:
if int(difficulty) == 1:
print("-已选择简单难度")
g_difficulty = 0.5
break
elif int(difficulty) == 2:
print("-已选择中等难度")
g_difficulty = 1
break
elif int(difficulty) == 3:
print("-已选择困难难度")
g_difficulty = 2
break
else:
print("-错误的选择,请重新选择~")
time.sleep(0.5)
continue
except ValueError:
print("-错误的选择,请重新选择~")
time.sleep(0.5)
continue
if c_work == 1 and g_difficulty != 0:
print("*游戏即将开始......")
time.sleep(3)
print("————————————————")
print("---GAME*START---")
print("————————————————")
time.sleep(1)
Game_Body.Game_Start()
break
else:
print("-错误的选择,请重新选择~")
time.sleep(0.5)
continue
except ValueError:
print("-错误的选择,请重新选择~")
time.sleep(0.5)
continue
游戏本体 Game_Body.py
点击查看代码
# encoding: utf-8
import time
import random
import Create_Character as cc
import Create_Items as ci
import Create_Events as ce
import Create_Skills as cs
BOSS = 0
def Self_Look():
print("————————人物属性————————")
print("LVL = ", cc.Player.LVL)
print("HP = ", cc.Player.HP)
print("MP = ", cc.Player.MP)
print('ATK = {0} + {1} = {2}'.format(cc.Player.ATK,cc.Player.WEAPON[1]+
cc.Player.GUARD[1]+cc.Player.SHOES[1],
cc.Player.ATK+cc.Player.WEAPON[1]+
cc.Player.GUARD[1]+cc.Player.SHOES[1]))
print('DEF = {0} + {1} = {2} '.format(cc.Player.DEF,cc.Player.WEAPON[2]+
cc.Player.GUARD[2]+cc.Player.SHOES[2],
cc.Player.DEF+cc.Player.WEAPON[2]+
cc.Player.GUARD[2]+cc.Player.SHOES[2]))
print("HIT = ", cc.Player.HIT)
print('SPD = {0} + {1} = {2} '.format(cc.Player.SPD,cc.Player.WEAPON[3]+
cc.Player.GUARD[3]+cc.Player.SHOES[3],
round(cc.Player.SPD+cc.Player.WEAPON[3]+
cc.Player.GUARD[3]+cc.Player.SHOES[3],2)))
print("COIN = ", cc.Player.COIN)
print("——————————————————————")
def Deadth_Punishment():
lost_lvl = cc.Player.LVL //2
cc.Player.LVL = lost_lvl
cc.Player.MAX_HP -= 10*(lost_lvl+1)
cc.Player.MAX_MP -= 10*(lost_lvl+1)
cc.Player.ATK -= 5*(lost_lvl+1)
cc.Player.DEF -= 1*(lost_lvl+1)
cc.Player.HP = cc.Player.MAX_HP
cc.Player.MP = cc.Player.MAX_MP
time.sleep(1)
print("*死亡惩罚已结算~玩家数据更新完毕~")
def Refresh_Information():
CNT = 0
Early_LVL = cc.Player.LVL+1
while cc.Player.EXP - (50+cc.Player.LVL)*Early_LVL >= 0:
cc.Player.LVL = cc.Player.LVL + 1
print("*恭喜你提升了1级,当前等级为:",cc.Player.LVL)
time.sleep(1)
print("*你的基础属性有所提升~")
time.sleep(1)
cc.Player.MAX_HP += 10
cc.Player.MAX_MP += 10
cc.Player.ATK += 5
cc.Player.DEF += 1
cc.Player.EXP = cc.Player.EXP - 100
CNT += 1
cc.Player.SATK = cc.Player.ATK + cc.Player.WEAPON[1] + cc.Player.GUARD[1] + cc.Player.SHOES[1]
cc.Player.SDEF = cc.Player.DEF + cc.Player.WEAPON[2] + cc.Player.GUARD[2] + cc.Player.SHOES[2]
cc.Player.SSPD = cc.Player.SPD + cc.Player.WEAPON[3] + cc.Player.GUARD[3] + cc.Player.SHOES[3]
time.sleep(1)
if cc.Player.LVL == 3:
print("*恭喜解锁新的打工~")
if cc.Player.LVL == 5:
print("*恭喜解锁新的技能~")
if cc.Player.LVL == 7:
print("*恭喜解锁新的技能~")
if cc.Player.LVL == 10:
print("*注意!外出冒险将会碰到【BOSS】")
print('距离下次升级还需要{0}EXP'.format( (50+cc.Player.LVL)*(Early_LVL+CNT) - cc.Player.EXP))
time.sleep(1)
print("*玩家数据更新完毕~")
#cc.Player.list_all_data()
def Refresh_HM():
if cc.Player.HP > cc.Player.MAX_HP:
cc.Player.HP = cc.Player.MAX_HP
if cc.Player.MP > cc.Player.MAX_MP:
cc.Player.MP = cc.Player.MAX_MP
print("当前血量:",cc.Player.HP)
print("当前蓝量:",cc.Player.MP)
time.sleep(1)
def IF_Critical_A(IF_C_HIT):
IFC = random.uniform(0,1)
if abs(IF_C_HIT-1) > IFC:
return 1
else:
return 0
def IF_Blocking():
IFB = random.uniform(0,1)
if IFB > 0.7:
return 1
else:
return 0
def Attack(A_ATK,B_DEF,A_HIT,A_NAME):
IFMISS = random.uniform(0,1)
if IFMISS > A_HIT:
print('*{0}的攻击MISS!'.format(A_NAME))
time.sleep(0.5)
DPS = 0
return DPS
else:
IFC = IF_Critical_A(A_HIT)
IFB = IF_Blocking()
if IFC:
print('{0}的攻击暴击!'.format(A_NAME))
time.sleep(0.5)
A_ATK = A_ATK*2
if IFB:
print('{0}的攻击遭到格挡!'.format(A_NAME))
time.sleep(0.5)
B_DEF = B_DEF*2
DPS = A_ATK*A_HIT - B_DEF
if DPS < 0:
DPS = 0
print('*{0}的攻击总共造成 {1} 点伤害!'.format(A_NAME,round(DPS,2)))
print("————————————————")
time.sleep(1)
return DPS
def RUN(A_SPD,B_SPD):
if A_SPD > B_SPD:
return 1
elif A_SPD == B_SPD:
return random.randint(0,1)
elif A_SPD < B_SPD:
return 0
def Use_Skills(ID,A_ATK,B_DEF):
S_ATK = 0
S_MP = 0
S_NAME = 0
S_HIT = 0
S_HP = 0
if ID == 1:
S_ATK = cs.Heavy_Hit.ATK
S_MP = cs.Heavy_Hit.MP
S_NAME = cs.Heavy_Hit.NAME
S_HIT = cs.Heavy_Hit.HIT
print("*【重击】技能就绪")
elif ID == 2:
S_ATK = cs.Magic_Hit.ATK
S_MP = cs.Magic_Hit.MP
S_NAME = cs.Magic_Hit.NAME
S_HIT = cs.Magic_Hit.HIT
print("*【魔力一击】技能就绪")
elif ID == 3:
S_NAME = cs.HP_Healing.NAME
S_MP = cs.HP_Healing.MP
S_HP = cs.HP_Healing.HP
S_ATK = cs.HP_Healing.ATK
S_HIT = cs.HP_Healing.HIT
print("*【生命恢复】准备就绪")
elif ID == 4:
S_NAME = cs.Self_Attack.NAME
S_HP = cs.Self_Attack.HP
S_MP = cs.Self_Attack.MP
S_ATK = cs.Self_Attack.ATK
S_HIT = cs.Self_Attack.HIT
elif ID == 5:
S_NAME = cs.Magic_Attack.NAME
S_HP = cs.Magic_Attack.HP
S_MP = cs.Magic_Attack.MP
S_ATK = cs.Magic_Attack.ATK
S_HIT = cs.Magic_Attack.HIT
IFMISS = random.uniform(0, 1)
if IFMISS > S_HIT:
print('*{0}的技能MISS!'.format(S_NAME))
if ID == 4:
cc.Player.HP -= S_HP
DPS = 0
return DPS
if ID == 3 and cc.Player.MP >= S_MP:
DPS = S_HP
cc.Player.MP -=S_MP
Refresh_HM()
print('*{0}技能回复 {1} 点HP!'.format(S_NAME, DPS))
print("————————————————")
return DPS
if cc.Player.MP >= S_MP:
if ID == 4:
cc.Player.HP -= S_HP
DPS = A_ATK * S_ATK - B_DEF
cc.Player.MP -= S_MP
print('*{0}技能造成 {1} 点伤害!'.format(S_NAME, DPS))
print("————————————————")
return DPS
else:
print("MP不足!")
return 0
def Battle(ID):
print("————————*战斗开始*————————")
print('\n')
time.sleep(1)
N_NAME = 0
N_HP = 0
N_ATK = 0
N_DEF = 0
N_HIT = 0
N_EXP = 0
N_COIN = 0
N_SPD = 0
N_HOR = 0
if ID == 1:
N_NAME = cc.SLM.NAME
N_HP = cc.SLM.HP
N_ATK = cc.SLM.ATK
N_DEF = cc.SLM.DEF
N_HIT = cc.SLM.HIT
N_EXP = cc.SLM.EXP
N_COIN = cc.SLM.COIN
N_SPD = cc.SLM.SPD
if ID == 2:
N_NAME = cc.BAT.NAME
N_HP = cc.BAT.HP
N_ATK = cc.BAT.ATK
N_DEF = cc.BAT.DEF
N_HIT = cc.BAT.HIT
N_EXP = cc.BAT.EXP
N_COIN = cc.BAT.COIN
N_SPD = cc.BAT.SPD
if ID == 3:
N_NAME = cc.BOSS_SLM.NAME
N_HP = cc.BOSS_SLM.HP
N_ATK = cc.BOSS_SLM.ATK
N_DEF = cc.BOSS_SLM.DEF
N_HIT = cc.BOSS_SLM.HIT
N_EXP = cc.BOSS_SLM.EXP
N_COIN = cc.BOSS_SLM.COIN
N_SPD = cc.BOSS_SLM.SPD
N_HOR +=1
if ID == 4:
N_NAME = cc.GBL.NAME
N_HP = cc.GBL.HP
N_ATK = cc.GBL.ATK
N_DEF = cc.GBL.DEF
N_HIT = cc.GBL.HIT
N_EXP = cc.GBL.EXP
N_COIN = cc.GBL.COIN
N_SPD = cc.GBL.SPD
print("*怪物血量:", N_HP)
while True:
time.sleep(1)
if N_HP <= 0:
cc.Player.EXP += N_EXP
print("获得经验:",N_EXP)
time.sleep(1)
cc.Player.COIN += N_COIN
print("获得金币:",N_COIN)
time.sleep(1)
if N_HOR == 1:
cc.Player.HOR += 1
break
elif cc.Player.HP <= 0:
print("你已被击败!即将回城~")
Deadth_Punishment()
time.sleep(1)
break
print("————*请选择*————")
print("0.逃跑")
print("1.攻击")
print("2.技能")
command = input('->')
if command == 'I':
Self_Look()
continue
try:
if int(command) == 1:
print('{0} 对 {1} 攻击!并遭到回击!'.format(cc.Player.NAME,N_NAME))
N_HP = N_HP - Attack(cc.Player.SATK,N_DEF,cc.Player.HIT,cc.Player.NAME)
cc.Player.HP = cc.Player.HP - Attack(N_ATK,cc.Player.DEF,N_HIT,N_NAME)
print("敌人还剩血量:",round(N_HP,2))
elif int(command) == 0:
YN = RUN(cc.Player.SSPD,N_SPD)
if YN == 1:
print("逃跑成功!")
break
elif YN == 0:
print("逃跑失败!")
time.sleep(0.5)
print('{0} 对 {1} 攻击!'.format(N_NAME, cc.Player.NAME))
cc.Player.HP = cc.Player.HP - Attack(N_ATK, cc.Player.SDEF, N_HIT,N_NAME)
print("己方还剩血量:", cc.Player.HP)
elif int(command) == 2:
print("——[技能栏]——")
print("-0.返回上一级")
if cc.Player.LVL >=5 and cc.Player.CAREER == 1:
print("-1.【重击】:MP:-50;ATK:*150%;HIT:80%")
print("-2.【生命恢复】:MP:-50;HP:+50;HIT:100%")
if cc.Player.LVL >= 7:
print("-3.【冲撞】:HP:-30;ATK:*200%;HIT:80%")
if cc.Player.LVL >=5 and cc.Player.CAREER == 2:
print("-1.【魔力一击】:MP:50;ATK:150%;HIT:80%")
print("-2.【生命恢复】:MP:-50;HP:+50;HIT:100%")
if cc.Player.LVL >= 7:
print("-3.【魔法攻击】:MP:-25;ATK:*100%;HIT:90%")
command = input('->')
if command == 'I':
Self_Look()
continue
try:
if int(command) == 1 and cc.Player.LVL >= 5 and cc.Player.CAREER == 1:
print('{0} 对 {1} 发动技能 {2} !'.format(cc.Player.NAME, N_NAME, cs.Heavy_Hit.NAME))
N_HP = N_HP - Use_Skills(1, cc.Player.SATK, N_DEF)
cc.Player.HP = cc.Player.HP - Attack(N_ATK, cc.Player.DEF, N_HIT, N_NAME)
print("敌人还剩血量:", round(N_HP, 2))
elif int(command) == 1 and cc.Player.LVL >= 5 and cc.Player.CAREER == 2:
print('{0} 对 {1} 发动技能 {2} !'.format(cc.Player.NAME, N_NAME, cs.Magic_Hit.NAME))
N_HP = N_HP - Use_Skills(2, cc.Player.SATK, N_DEF)
cc.Player.HP = cc.Player.HP - Attack(N_ATK, cc.Player.DEF, N_HIT, N_NAME)
print("敌人还剩血量:", round(N_HP, 2))
elif int(command) == 2 and cc.Player.LVL >= 5:
print('{0} 对 {1} 发动技能 {2} !'.format(cc.Player.NAME, N_NAME, cs.HP_Healing.NAME))
cc.Player.HP = cc.Player.HP + Use_Skills(3,cc.Player.SATK, N_DEF)
print("己方当前血量:", cc.Player.HP)
elif int(command) == 3 and cc.Player.LVL >= 7 and cc.Player.CAREER == 1:
print('{0} 对 {1} 发动技能 {2} !'.format(cc.Player.NAME, N_NAME, cs.Self_Attack.NAME))
N_HP = N_HP - Use_Skills(4, cc.Player.SATK, N_DEF)
cc.Player.HP = cc.Player.HP - Attack(N_ATK, cc.Player.DEF, N_HIT, N_NAME)
print("敌人还剩血量:", round(N_HP, 2))
elif int(command) == 3 and cc.Player.LVL >= 7 and cc.Player.CAREER == 2:
print('{0} 对 {1} 发动技能 {2} !'.format(cc.Player.NAME, N_NAME, cs.Magic_Attack.NAME))
N_HP = N_HP - Use_Skills(5, cc.Player.SATK, N_DEF)
print("敌人还剩血量:", round(N_HP, 2))
elif int(command) == 0:
pass
except ValueError:
print("-错误的选择,请重新选择~")
time.sleep(0.5)
continue
except ValueError:
print("-错误的选择,请重新选择~")
time.sleep(0.5)
continue
Refresh_Information()
print("————————*战斗结束*————————")
print('\n')
def Adventure_Noob():
print("*新手村可能碰到的敌人:")
time.sleep(1)
print("【史莱姆】:等级1\n"
"【蝙蝠】:等级3\n"
"【哥布林】:等级5")
print("————————————————")
time.sleep(0.5)
ce.Random_Events()
Refresh_Information()
time.sleep(0.5)
N_LVL = cc.Player.LVL + 4 // 4
while True:
if cc.Player.LVL == 10 and BOSS == 0:
print("——————【WARNING】——————")
print("^即将与【BOSS】对战")
time.sleep(3)
Battle(3)
break
VS = random.randint(1,N_LVL)
if VS == 1:
print("^即将与【史莱姆】对战")
time.sleep(1)
Battle(1)
break
if VS == 2:
print("^即将与【蝙蝠】对战")
time.sleep(1)
Battle(2)
break
if VS == 3:
print("^即将与【哥布林】对战")
time.sleep(1)
Battle(4)
break
else:
pass
if cc.Player.HP <= 0:
print("血量不足!")
Deadth_Punishment()
break
# exit()
def Adventure_Shop():
print("*欢迎来到商店~")
time.sleep(1)
print("-0.返回上一级")
time.sleep(1)
while True:
print("————*药水*————")
print("-1.【红药水】可恢复50~100HP。 售价:", ci.Red_Water.COIN)
print("-2.【蓝药水】可恢复50~100HP。 售价:", ci.Blue_Water.COIN)
print("-9.下一页:【装备】")
ysbuy = 0
command = input('->')
if command == 'I':
Self_Look()
continue
try:
if int(command) == 9:
print("-0.返回上一级")
time.sleep(1)
print("————*装备*————")
print("-1.【新手武器】。 售价:", ci.NOOB_WEAPON.COIN)
print("-2.【新手防具】。 售价:", ci.NOOB_GUARD.COIN)
print("-3.【新手鞋子】。 售价:", ci.NOOB_SHOES.COIN)
while True:
command = input('->')
if command == 'I':
Self_Look()
continue
try:
if int(command) == 1:
buy1 = ci.NOOB_WEAPON
elif int(command) == 2:
buy1 = ci.NOOB_GUARD
elif int(command) == 3:
buy1 = ci.NOOB_SHOES
elif int(command) == 0:
break
else:
print("NOT OPENED")
if cc.Player.COIN >= buy1.COIN:
print('*成功购买【{0}】,花费金币【{1}】,剩余金币【{2}】'.format(buy1.NAME,buy1.COIN,cc.Player.COIN-buy1.COIN))
cc.Player.equip(buy1.POS,buy1.ATK,buy1.DEF,buy1.SPD)
cc.Player.COIN -= buy1.COIN
Refresh_Information()
print("————————————————")
else:
print("*金币不足!")
continue
except ValueError:
print("-错误的选择,请重新选择~")
time.sleep(0.5)
continue
elif int(command) == 1:
ysbuy = 1
buy = ci.Red_Water
elif int(command) == 2:
ysbuy = 1
buy = ci.Blue_Water
elif int(command) == 0:
break
else:
print("NOT OPENED")
if ysbuy == 1 and cc.Player.COIN >= buy.COIN:
print('*成功购买【{0}】,花费金币【{1}】,剩余金币【{2}】'.format(buy.NAME,buy.COIN,cc.Player.COIN))
cc.Player.HP += buy.HP
cc.Player.MP += buy.MP
cc.Player.COIN -= buy.COIN
Refresh_HM()
print("————————————————")
elif ysbuy == 0:
continue
else:
print("*金币不足!")
continue
except ValueError:
print("-错误的选择,请重新选择~")
time.sleep(0.5)
continue
def Tempoary_Work():
print("*欢迎来到打工界面~")
print("-0.返回上一级")
print('-1.【扶老太太过马路】:MP = - 5~10; COIN = + 3~5; EXP = + 10~15')
if cc.Player.LVL >=3:
print('-2.【扫地】:MP = - 20~30; COIN = + 5~10; EXP = + 15~25')
while True:
command = input('->')
if command == 'I':
Self_Look()
continue
try:
if int(command) == 1:
if cc.Player.MP >= ce.FLTTGML.MP:
print("*成功打工!")
cc.Player.MP -= ce.FLTTGML.MP
cc.Player.COIN += ce.FLTTGML.COIN
cc.Player.EXP += ce.FLTTGML.EXP
Refresh_Information()
else:
print("*MP不足!")
continue
if int(command) == 2 and cc.Player.LVL >=3:
if cc.Player.MP >= ce.SD.MP:
print("*成功打工!")
cc.Player.MP -= ce.SD.MP
cc.Player.COIN += ce.SD.COIN
cc.Player.EXP += ce.SD.EXP
Refresh_Information()
else:
print("*MP不足!")
continue
elif int(command) == 0:
break
except ValueError:
print("-错误的选择,请重新选择~")
time.sleep(0.5)
continue
def Game_Start():
command = 0
print("-你即将开始你的冒险~")
time.sleep(1)
print("*你可以输入“I”来查看自己的属性值")
time.sleep(1)
print("*你可以输入“ESC”来结束游戏")
time.sleep(1)
while True:
if cc.Player.HOR == 0:
print("————————*新手村*————————")
time.sleep(1)
print("-1.外出冒险")
print("-2.冒险商店")
print("-3.打工赚钱")
command = input('->')
if command == 'I':
Self_Look()
continue
if command == 'ESC':
print("——————*游戏结束*——————")
break
try:
if int(command) == 1:
time.sleep(1)
Adventure_Noob()
elif int(command) == 2:
time.sleep(1)
Adventure_Shop()
elif int(command) == 3:
time.sleep(1)
Tempoary_Work()
elif int(command) == 99:
cc.Player.EXP +=100
Refresh_Information()
else:
print("NOT OPENED")
pass
except ValueError:
print("-错误的选择,请重新选择~")
time.sleep(0.5)
continue
else:
print("*恭喜通关!感谢游玩!")
角色(玩家、怪物)Create_Character.py
点击查看代码
# encoding: utf-8
import time
import random
import Create_Items as ci
#from Main_System import Player
class Player1(object):
def __init__(self):
self.NAME = 'Player'
self.MAX_HP = 0
self.MAX_MP = 0
self.HP = 0
self.MP = 0
self.ATK = 0
self.DEF = 0
self.HIT = 0
self.SPD = 0
self.SATK = 0
self.SDEF = 0
self.SSPD = 0
self.HOR = 0
self.LVL = 0
self.EXP = 0
self.COIN = 300
self.CAREER = 0
self.WEAPON =[0, 0,0,0] # 1 position
self.GUARD = [0, 0,0,0] #2 position
self.SHOES = [0, 0,0,0] #3 position
"""
[0]为是否装备[1]为ATK[2]为DEF[3]为SPD
"""
def init_data(self,name,maxhp,maxmp,atk,DEF,hit,spd,hor,career):
self.NAME = name
self.MAX_HP = int(maxhp)
self.HP = int(maxhp)
self.MAX_MP = int(maxmp)
self.MP = int(maxmp)
self.ATK = int(atk)
self.SATK = int(atk)
self.DEF = int(DEF)
self.SDEF = int(DEF)
self.HIT = float(hit)
self.SPD = float(spd)
self.SSPD = float(spd)
self.HOR = int(hor)
self.CAREER = int(career)
def list_all_data(self):
wj = open('Player_Data.txt', 'w')
for name, value in vars(self).items():
wj.write('%s=%s\n' % (name, value))
wj.close()
def equip(self,position,EATK,EDEF,ESPD):
if position == 1:
self.WEAPON[0] = 1
self.WEAPON[1] = EATK
self.WEAPON[2] = EDEF
self.WEAPON[3] = ESPD
elif position == 2:
self.GUARD[0] = 1
self.GUARD[1] = EATK
self.GUARD[2] = EDEF
self.GUARD[3] = ESPD
elif position == 3:
self.SHOES[0] = 1
self.SHOES[1] = EATK
self.SHOES[2] = EDEF
self.SHOES[3] = ESPD
else:
pass
class SLM1: # ID = 1
def __init__(self):
self.NAME = '史莱姆'
self.HP = random.randint(50, 70)
self.MP = 50
self.ATK = random.randint(20, 30)
self.DEF = random.randint(1, 5)
self.HIT = round(random.randint(7, 8) / 10)
self.SPD = random.randint(7, 8) / 10
self.LVL = 1
self.COIN = random.randint(1, 5)
self.EXP = random.randint(15, 30)
class BAT2: # ID = 2
def __init__(self):
self.NAME = '蝙蝠'
self. HP = random.randint(130,150)
self.MP = 50
self.ATK = random.randint(30,50)
self.DEF = random.randint(1,5)
self.HIT = round(random.randint(7,9)/10)
self.SPD = random.randint(9,10)/10
self.LVL = 1
self.COIN = random.randint(3,7)
self.EXP = random.randint(50,70)
class BOSS_SLM: #ID = 3
NAME = '【BOSS】'
HP = 1000
MP = 50
ATK = 80
DEF = 40
HIT = 0.85
SPD = 1
LVL = 10
COIN = 100
EXP = 200
class GBL4: # ID = 4
def __init__(self):
self.NAME = '哥布林'
self. HP = random.randint(150,230)
self.MP = 50
self.ATK = random.randint(40,60)
self.DEF = random.randint(5,10)
self.HIT = round(random.randint(6,8)/10)
self.SPD = random.randint(5,7)/10
self.LVL = 5
self.COIN = random.randint(10,15)
self.EXP = random.randint(50,80)
Player = Player1()
SLM = SLM1()
BAT = BAT2()
GBL = GBL4()
def Warrior(): # ID = 1
print("*已选择战士!")
print("*以下为战士的初始数值:")
print("HP = 500")
print("MP = 300")
print("ATK = 10")
print("DEF = 10")
print("HIT = 0.95")
print("SPD = 0.9")
print("*是否选择?1/0")
while True:
Choice = input('->')
if Choice == '1':
names = 'Player'
Player.init_data(names,500,300,10,10,0.95,0.9,0,1)
# Player.list_all_data()
return 1
if Choice == '0':
return 0
def Mage():
# ID = 2
print("*已选择法师!")
print("*以下为法师的初始数值:")
print("HP = 300")
print("MP = 500")
print("ATK = 15")
print("DEF = 5")
print("HIT = 0.95")
print("SPD = 0.95")
print("*是否选择?y/n")
while True:
Choice = input('->')
try:
if Choice == 'y':
names = 'Player'
Player.init_data(names, 300, 500, 15, 5, 0.95, 0.95, 0, 2)
# Player.list_all_data()
return 1
if Choice == 'n':
return 0
except ValueError:
print("-错误的选择,请重新选择~")
time.sleep(0.5)
游戏实践 Create_Events.py
点击查看代码
# encoding: utf-8
import time
import Create_Character as cc
import random
class FLTTGML: #扶老太太过马路
MP = random.randint(5,10)
COIN = random.randint(3,5)
EXP = random.randint(10,15)
class SD: #LVL >= 3
MP = random.randint(20,30)
EXP = random.randint(15, 25)
COIN = random.randint(5,10)
class Random_Events_Details1():
def __init__(self):
self.NAME = ['精灵的馈赠','误入陷阱','道德绑架']
self.ATK = [0,random.randint(25,35),random.randint(5,15)]
self.HP = [random.randint(30,70),0,0]
self.MP = [0,0,random.randint(20,40)]
self.EXP = [30,30,30]
Random_Events_Details = Random_Events_Details1()
def Random_Events():
if cc.Player.LVL >= 1:
RPT = random.uniform(0.000,1.000)
if RPT <=0.1:
CE = random.randint(0,2)
print('!!!触发随机事件【{0}】'.format(Random_Events_Details.NAME[CE]))
cc.Player.HP -=Random_Events_Details.ATK[CE]
cc.Player.HP +=Random_Events_Details.HP[CE]
cc.Player.MP -= Random_Events_Details.MP[CE]
cc.Player.EXP +=Random_Events_Details.EXP[CE]
time.sleep(1)
if Random_Events_Details.ATK[CE] != 0:
print('受到【{0}】事件伤害{1}'.format(Random_Events_Details.NAME[CE],Random_Events_Details.ATK[CE]))
if Random_Events_Details.HP[CE] != 0:
print('受到【{0}】事件恢复HP{1}'.format(Random_Events_Details.NAME[CE], Random_Events_Details.HP[CE]))
if Random_Events_Details.MP[CE] != 0:
print('受到【{0}】事件损失MP{1}'.format(Random_Events_Details.NAME[CE], Random_Events_Details.MP[CE]))
time.sleep(1)
print('获得【{0}】经验值'.format(Random_Events_Details.EXP[CE]))
print("————————————————")
else:
pass
else:
pass
游戏物品 Create_Items.py
点击查看代码
# encoding: utf-8
import random
import Create_Character as cc
class Red_Water:
NAME = '红药水'
HP = random.randint(50,100)
MP = 0
COIN = 10
class Blue_Water:
NAME = '蓝药水'
HP = 0
MP = random.randint(50,100)
COIN = 10
class NOOB_WEAPON1:
def __init__(self):
self.NAME = '新手武器'
self.POS = 1
self.ATK = random.randint(5,15)
self.DEF = random.randint(1,5)
self.SPD = 0.01
self.COIN = 75
class NOOB_GUARD1:
def __init__(self):
self.NAME = '新手防具'
self.POS = 2
self.ATK = random.randint(1,5)
self.DEF = random.randint(5,10)
self.SPD = 0.03
self.COIN = 75
class NOOB_SHOES1:
def __init__(self):
self.NAME = '新手鞋子'
self.POS = 3
self.ATK = random.randint(5,10)
self.DEF = random.randint(5,10)
self.SPD = 0.05
self.COIN = 75
NOOB_SHOES = NOOB_SHOES1()
NOOB_WEAPON = NOOB_WEAPON1()
NOOB_GUARD = NOOB_GUARD1()
玩家技能 Create_Skills.py
点击查看代码
# encoding: utf-8
import Create_Character
import random
class Heavy_Hit: #ID = 1
NAME = '重击'
HP = 0
MP = 50
ATK = 1.5
HIT = 0.8
class Magic_Hit: #ID = 2
NAME = '魔力一击'
HP = 0
MP = 50
ATK = 1.5
HIT = 0.8
class HP_Healing: #ID = 3
NAME = '生命恢复'
MP = 50
HP = 50
ATK = 0
HIT = 1
class Self_Attack: #ID = 4
NAME = '冲撞'
HP = 30
MP = 10
ATK = 2
HIT = 0.8
class Magic_Attack: #ID = 5
NAME = '魔法攻击'
HP = 0
MP = 25
ATK = 1
HIT = 0.9
四、实验结果
本次实验代码可以成功在华为云ECS服务器上运行,本地通过XShell链接华为云ECS服务器上传实验文件并运行。
成功运行!
五、课程感想体会、意见建议
-
感想体会
通过这一个学期来学习的Python知识,我对这门新兴的潜力极高的语言有了基本的了解,并且能够应用它来实现一些相对实用的功能,例如爬虫、数据处理、可视化等。从最开始认识语言到理解语言到应用语言,经历了一个长期努力的过程,在这里十分感谢王志强老师每次课详细的教学,以及课后完备地指导。
由于自己在高中时接触过C++,对于基本的编程逻辑、算法有一定的理解基础,从而能够在老师基本的讲解之下有一定的拓展学习,丰富了自己对编程的理解。 -
意见建议
由于我是和舍友一起报的这门课,在课下有过很多交流,可能对于编程0基础的同学而言,授课速度略快了一些,但是若减缓授课速度又可能会导致整体的内容讲不完的情况,这也只是我自己观察到的一个现象,未能想出来一个确切的建议办法。
其次对于期末综合实践作业希望能够延长提交时间吧,一周多的时间可能难以对一个项目进行更加完整的创作和优化(可能也是我码代码太慢的缘故?)