时隔半个月的学习报告。
首先是对前两周学习及作业的总结。
第一份,字符串、列表、字典之间的相互转换。
test_list = [{"name": "老王", "age": "60"}, {"name": "老李", "age": "25"}, {"name": "二狗", "age": "33"}]
test_list.sort(key=lambda x: x["age"])
print(test_list)
str1 = "你 是 不 是 我 最 疼 爱 的 人"
list1 = str1.split(" ")
print(list1)
print("".join(list1))
str2 = '{"name":"老王","age":18,"addr":"长沙"}'
#eval可以将字符串转换成python的表达式
dict1 = eval(str2)
print("dict1的类型是{},{}".format(type(dict1), dict1))
str3 = str(dict1)
print("str3的类型是{},{}".format(type(str3),str3))
list3 = ["姓名", "年龄", "地址"]
list4 = ["老王", 18, "北京"]
data = dict(zip(list3, list4))
print(data)
dict2 = {"name": "老王", "age": 18, "addr": "长沙"}
print(dict2.keys())
print(dict2.values())
这份难点主要在一些没有被老师讲过的函数的运用,代码本身的难度并不大,主要学到了自己如何单独学习代码。
第二份,写一份射击代码。
讲真的,这份作业我还挺感兴趣的,如果给我足够多的时间我可能还会写下去,奈何现实不允许(学校里作业太难了,花了太多时间)。上代码!
import random
import time
class Gun:
def __init__(self, model, full_bullet, damage):
self.model = model
"""枪的型号"""
self.full_bullet = full_bullet
"""弹匣子弹的数量"""
self.damage = damage
"""单发子弹造成的伤害"""
self.bullet = full_bullet
def __str__(self):
return "这把枪是{},可填装{}发子弹".format(self.model, self.full_bullet)
class Soldier:
def __init__(self, name, health):
self.name = name
"""士兵名字"""
self.health = health
"""生命值"""
self.health_bag = 0
"""存储血包"""
self.most_health_bag = 2
"""最大血包数量"""
self.gun = 0
def add_gun(self, gun_get):
"""添加枪械方法"""
print("已装备【{}】,{}".format(gun_get.model, gun_get))
self.gun = gun_get
def enemy_find():
fight = input("发现敌人,输入1进攻,输入任意字符离开")
if fight == "1":
fight_with()
def enemy_found():
enemy_health = 100
your_infor.health -= 20
fight = input("你被敌人发现了,扣除20生命值,输入1进攻,输入任意字符逃跑")
if fight == "1":
fight_with()
def fight_with():
enemy_health = 100
while True:
if enemy_health >= 0:
if your_infor.health >= 0:
if your_infor.gun.bullet > 0:
enemy_health -= your_infor.gun.damage*random.randint(4, 6)
your_infor.gun.bullet -= 6
your_infor.health -= 20
print("你目前的血量:{}".format(your_infor.health))
else:
print("没子弹了正在装弹")
your_infor.gun.bullet = your_infor.gun.full_bullet
your_infor.health -= 20
print("你目前的血量:{}".format(your_infor.health))
else:
print("你输了")
break
else:
if your_infor.health > 0:
print("你击倒了敌人")
global enemy_number
enemy_number -= 1
break
else:
print("但是你重伤倒地")
break
# 创建枪型
sub = Gun("冲锋枪", 24, 14)
rifle = Gun("步枪", 32, 15)
shot = Gun("霰弹枪", 4, 15)
sniper = Gun("狙击枪", 2, 120)
pistol = Gun("手枪", 6, 10)
print("欢迎来到虚拟靶场")
your_name = input("请输入你的昵称:")
your_infor = Soldier(your_name, 100)
luck_a = 20
luck_b = 80
enemy_number = 60
if __name__ == '__main__':
while True:
a = random.randint(0, 10)
if 1 < a < 7:
print("你没捡到枪!")
time.sleep(1)
continue
elif 0 <= a <= 1:
print("受到敌人攻击,受到20点伤害")
your_infor.health = your_infor.health - 20
print("目前剩余血量:{}".format(your_infor.health))
time.sleep(1)
continue
else:
print("你捡到了手枪")
your_infor.add_gun(pistol)
your_buttles = pistol.full_bullet
your_whole_buttles = 0
time.sleep(1)
break
print("战术指令列表:")
print("1.搜索物资")
print("2.搜索敌人")
print("3.躲藏")
print("4.换子弹")
print("5.打血包")
while True:
if your_infor.health <= 0:
print("你的生命值归零,游戏结束")
break
command = input("请输入你的战术:")
if command == "1":
luck = random.randint(0, 100)
if luck_a <= luck <= luck_b:
print("你找到了物资")
new_gun_number = random.randint(1, 14)
if 1 <= new_gun_number <= 3:
print("你找到了{}".format(sub.model))
change = input("输入1,更换枪械,或输入其他任意内容跳过")
if change == "1":
your_infor.add_gun(sub)
your_buttles = pistol.full_bullet
your_whole_buttles = 0
if 3 < new_gun_number <= 5:
print("你找到了{}".format(rifle.model))
change = input("输入1,更换枪械,或输入其他任意内容跳过")
if change == "1":
your_infor.add_gun(rifle)
your_buttles = pistol.full_bullet
your_whole_buttles = 0
if 5 < new_gun_number <= 8:
print("你找到了{}".format(shot.model))
change = input("输入1,更换枪械,或输入其他任意内容跳过")
if change == "1":
your_infor.add_gun(shot)
your_buttles = pistol.full_bullet
your_whole_buttles = 0
if 8 < new_gun_number <= 9:
print("你找到了{}".format(sniper.model))
change = input("输入1,更换枪械,或输入其他任意内容跳过")
if change == "1":
your_infor.add_gun(sniper)
your_buttles = pistol.full_bullet
your_whole_buttles = 0
if 10 < new_gun_number <= 14:
print("你找到了{}".format(pistol.model))
change = input("输入1,更换枪械,或输入其他任意内容跳过")
if change == "1":
your_infor.add_gun(pistol)
your_buttles = pistol.full_bullet
your_whole_buttles = 0
health_up = random.randint(0, 15)
if health_up <= 4:
print("你捡到了血包")
if your_infor.health_bag < your_infor.most_health_bag:
your_infor.health_bag += 1
print("你现在拥有{}个血包".format(your_infor.health_bag))
else:
print("你的血包已到达最大值")
elif 0 <= luck <= luck_a:
print("你没有找到物资")
else:
print("受到敌人攻击,受到20点伤害")
your_infor.health = your_infor.health - 20
print("目前剩余血量:{}".format(your_infor.health))
if luck_a <= 30:
luck_a += 0.5
if luck_b >= 70:
luck_b -= 0.5
if command == "2":
find = random.randint(0, 100)
if find <= luck_a:
enemy_found()
else:
enemy_find()
if command == "3":
if luck_b <= 95:
luck_b += 5
if command == "4":
your_infor.gun.bullet = your_infor.gun.full_bullet
print("子弹已填装")
if command == "5":
if your_infor.health_bag > 0:
if 80 <= your_infor.health <= 100:
your_infor.health_bag -= 1
your_infor.health = 100
print("你目前的血量:{}".format(your_infor.health))
elif your_infor.health < 80:
your_infor.health_bag -= 1
your_infor.health += 20
print("你目前的血量:{}".format(your_infor.health))
else:
print("你的生命值已满")
else:
print("你的血包用完了")
这已经是我来到蓝桥老师班后写过最长的代码了,我居然还意犹未尽。。。
这份作业可以说综合性比较强了,不只有Class的运用及各种技巧,还加入了自定义函数,并在保证逻辑通畅的前提下用def简化代码。说实话,写这份代码已经让我感觉顶不住了,时常跳个红跳个黄,实在让我这个代码小白掉头发。
代码一长问题就容易显现出来,比如全局变量的使用,虽然蓝桥老师拼命将,我也只能一知半解。另外就是组的用法实在让人头秃,只是浅学过c的我对组中代码的格式实在难以理解,光是写作业前的准备就画上了有一小时。
到此为止,已经可以说正真迈出代码路上的第一步了吧。与自己写的代码互动,真的很有趣啊。