小游戏
1、第一个pyton小程序,让DOTA两个英雄打架。
import time
class Hero():
'''
英雄类:
构造方法:name,atk,hp = 名字,物理攻击,血量
一般方法:fight(hero) 和其它英雄打架
'''
def __init__(self,name,atk,hp):
self.name = name
self.atk = atk
self.hp = hp
print(f'{self.name}的初始生命值为:{self.hp},初始攻击力为:{self.atk}\n')
def fight(self,hero):
rounding = 0
while 1:
rounding += 1
print(f'第{rounding}回合:')
hero.hp -= self.atk
print(f'\t{self.name}攻击了{hero.name},造成{self.atk}伤害,{hero.name}当前生命值{hero.hp}')
self.hp -= hero.atk
print(f'\t{hero.name}反击了{self.name},造成{hero.atk}伤害,{self.name}当前生命值{self.hp}')
if hero.hp <= 0:
print(f'\n经过{rounding}回合,{hero.name}被打死了,{self.name}取得了胜利')
break
if self.hp <= 0:
print(f'\n经过{rounding}回合,{self.name}被打死了,{hero.name}取得了胜利')
break
'''创建两个英雄,并让他们打架'''
hero_vip = Hero("viper", 70, 900)
hero_sf = Hero("sf", 50, 1000)
hero_vip.fight(hero_sf)
2、第二个pyton小程序,数字喝酒。
def red(str):
return "\033[1;31m"+str+"\033[0m"
def Fpass(i,max,row):
'''
一个输了喝酒小游戏,要求从1开始按顺序说出每个数字,到数字中包含i或能被i整除,
则说"过",其余说出数字,说到max停止.
:param i: 说"过"的数字
:param max: 最大值
:param row: 每行显示的数目
:return:
'''
print(f'输入值:i={i} max={max} row={row}')
print("结果:")
for num in range(1,max+1):
if num%row ==0:
a = '\n'
else:
a = ''
if num%i==0 :
print(red('过 '), end=a)
elif str(i) in str(num):
print(red('过 '), end=a)
else:
print(f'{num} ',end = a)
num+=1
help(Fpass)
Fpass(7,100,25)