Python: 每日一题 50
本帖最后由 ooxx7788 于 2017-5-21 09:05 编辑在学习python的过程中,对于我这种完完全全的零基础门外汉而言,我觉得类是比较难理解的一个概念。小甲鱼给了我们一个乌龟和鱼的游戏。
虽然我也知道大佬都比较偏爱算法一类以思考为主的题目,但是毕竟这个系列总体是偏向比较基础的题库。
所以今天我也给各位出个游戏题,这样可以给各位和我一样的新手更多的参考。如果你熟悉类,这题不会很难,如果你不熟悉类,可以通过这题来熟悉。Let's go.
写一个小游戏的程序:两名武士决斗,返回出胜利者的名字。
要求:两名武士轮流攻击对方,先杀死对方的胜者。而死亡取决于其health, health<=0时死亡。
每个武士为一个Fighter实例。(需要有名字(name),生命值(health),伤害(damage_per_attack))
主函数为:declare_winner(武士1,武士2,先攻击者)
返回值为:获胜者。
示例:
declare_winner(Fighter("Lew", 10, 2), Fighter("Harry", 5, 4), "Lew") => "Lew" # 此处函数中最后一个参数为先攻击者,而返回值为胜利者。
Lew attacks Harry; Harry now has 3 health. # 这里是整个游戏的攻击过程,这也需要体现出来。
Harry attacks Lew; Lew now has 6 health.
Lew attacks Harry; Harry now has 1 health.
Harry attacks Lew; Lew now has 2 health.
Lew attacks Harry: Harry now has -1 health and is dead. Lew wins.
好了,如果有说的不清楚的地方,请留言。
**** Hidden Message ***** @jerryxjr1220 @lumber2388779 @冬雪雪冬 {:10_248:} class Fighter:
def __init__(self,name,hp,att):
self.name = name
self.hp = hp
self.att = att
def declare_winner(f1,f2,first):
if first == f1.name:
while True:
f2.hp -= f1.att
print '%s attack %s, %s\'s health %d left' % (f1.name,f2.name,f2.name,f2.hp)
if f2.hp <= 0:
return f1.name
f1.hp -= f2.att
print '%s attack %s, %s\'s health %d left' % (f2.name,f1.name,f1.name,f1.hp)
if f1.hp <= 0:
return f2.name
else:
while True:
f1.hp -= f2.att
print '%s attack %s, %s\'s health %d left' % (f2.name,f1.name,f1.name,f1.hp)
if f1.hp <= 0:
return f2.name
f2.hp -= f1.att
print '%s attack %s, %s\'s health %d left' % (f1.name,f2.name,f2.name,f2.hp)
if f2.hp <= 0:
return f1.name
print '%s is the winner!' % declare_winner(Fighter('Lee',10,2),Fighter('Blu',5,4),'Blu') 看看 看看 class fighter():
def __init__(self, name, health, attack):
self.name = name
self.health = health
self.attack = attack
def declare_winner(f1,f2,f):
if f1.name == f.name:
while True:
f2.health -= f1.attack
print '%s-->%s,%s now has %d health' %(f1.name, f2.name, f2.name, f2.health)
if f2.health<=0:
return f1.name
f1.health -= f2.attack
print '%s-->%s, %s now has %d health' %(f2.name, f1.name, f1.name, f1.health)
if f1.health<=0:
return f2.name
else:
while True:
f1.health -= f2.attack
print '%s-->%s, %s now has %d health' %(f2.name, f1.name, f1.name,f1.health)
if f1.health<=0:
return f2.name
f2.health -= f1.attack
print '%s-->%s, %s now has %d health' %(f1.name, f2.name, f2.name, f2.health)
if f2.health<=0:
return f1.name
A = fighter('len', 10, 2)
B = fighter('he', 5, 5)
print declare_winner(A,B,B) 都好厉害呀! 尊炎再世 发表于 2017-5-21 11:22
看看
你好 方便加一下QQ1547298104交流点事情吗 looklook upup
游戏要有一定的随机性才好玩,用random加入 强力格挡和 会心一击{:10_256:}
import random
class Fighter:
def __init__(self,name,health,damage):
self.name = name
self.health = health
self.damage = damage
def get_name(self):
return self.name
def be_injured(self,num):
self.health -= num
print("%s 受到 %s 点攻击,剩余 %s health" %(self.name,num,self.health))
if self.health <= 0:
print("gg. %s is dead"%self.name)
return 0
else:
return self.health
def attack(self,enemy):
print("%s attacking %s" %(self.name,enemy.name))
odds = random.randint(0,11)
if odds<5:
print("%s 强力格挡!"%enemy.name)
elif odds>10:
print("%s 发动会心一击!!效果拔群!!"%self.name)
odds = 20
return enemy.be_injured(odds*self.damage//10)
def declare(a,b,first_pick):
if first_pick == a.get_name():
former,latter = a,b
elif first_pick ==b.get_name():
former,latter = b,a
else:
return ("nobody name %s"%first_pick)
result = 1
while result:
result = former.attack(latter)
former,latter = latter,former
print("%s wins" %latter.get_name())
效果如下
declare(Fighter("Lew", 10, 2), Fighter("Harry", 5, 4), "Lew")
##Lew attacking Harry
##Harry 受到 1 点攻击,剩余 4 health
##Harry attacking Lew
##Harry 发动会心一击!!效果拔群!!
##Lew 受到 8 点攻击,剩余 2 health
##Lew attacking Harry
##Harry 强力格挡!
##Harry 受到 0 点攻击,剩余 4 health
##Harry attacking Lew
##Lew 受到 3 点攻击,剩余 -1 health
##gg. Lew is dead
##Harry wins
class Fighter(object):
def __init__(self, name, health, damage_per_attack):
self.name = name
self.health = health
self.damage_per_attack = damage_per_attack
def declare_winner(f1, f2, first_pick):
if f1.name == first_pick:
while True:
f2.health -= f1.damage_per_attack
if f2.health < 0:
print('{} attack {},{} now has {} health and is dead,{} win'.format(f1.name, f2.name, f2.name, f2.health, f1.name))
return f1.name
else:
print('{} attack {};{} now has {} health'.format(f1.name, f2.name, f2.name, f2.health))
f1.health -= f2.damage_per_attack
if f1.health < 0:
print('{} attack {},{} now has {} health and is dead,{} win'.format(f2.name, f1.name, f1.name, f1.health, f2.name))
return f2.name
else:
print('{} attack {};{} now has {} health'.format(f2.name, f1.name, f1.name, f1.health))
else:
while True:
f1.health -= f2.damage_per_attack
if f1.health < 0:
print(
'{} attack {},{} now has {} health and is dead,{} win'.format(f2.name, f1.name, f1.name, f1.health,f2.name))
return f2.name
else:
print('{} attack {};{} now has {} health'.format(f2.name, f1.name, f1.name, f1.health))
f2.health -= f1.damage_per_attack
if f2.health < 0:
return f1.name
else:
print('{} attack {};{} now has {} health'.format(f1.name, f2.name, f2.name, f2.health))
if __name__ == '__main__':
f1 = Fighter('johny j', 10, 2)
f2 = Fighter('jin', 15, 3)
winner_name = declare_winner(f1, f2, 'jin')
感觉比较臃肿,想看看大佬怎么写的 本帖最后由 shigure_takimi 于 2017-12-6 10:34 编辑
class Samurai:
def __init__(self, name, health, damage_per_attack):
self.name = name
self.health = health
self.damage_per_attack = damage_per_attack
samurai1 = Samurai('服部半藏',150,100)
samurai2 = Samurai('柳生十兵衛',200,60)
def declare_winner(samurai1, samurai2, first_attack):
while not (samurai1.health <= 0 or samurai2.health <= 0):
if first_attack == samurai1:
if samurai1.health > 0:
samurai2.health -= samurai1.damage_per_attack
print('{0}攻擊{1}; {2}還有{3}滴血。'.format(samurai1.name,samurai2.name,samurai2.name,samurai2.health))
if samurai2.health > 0:
samurai1.health -= samurai2.damage_per_attack
print('{0}攻擊{1}; {2}還有{3}滴血。'.format(samurai2.name,samurai1.name,samurai1.name,samurai1.health))
elif first_attack == samurai2:
if samurai2.health > 0:
samurai1.health -= samurai2.damage_per_attack
print('{0}攻擊{1}; {2}還有{3}滴血。'.format(samurai2.name,samurai1.name,samurai1.name,samurai1.health))
if samurai1.health > 0:
samurai2.health -= samurai1.damage_per_attack
print('{0}攻擊{1}; {2}還有{3}滴血。'.format(samurai1.name,samurai2.name,samurai2.name,samurai2.health))
winner = samurai1.name if samurai2.health<=0 else samurai2.name
if winner == samurai1.name:
print('{}有{}滴血,掛掉。{}勝利。'.format(samurai2.name, samurai2.health, samurai1.name))
else:
print('{}有{}滴血,掛掉。{}勝利。'.format(samurai1.name, samurai1.health, samurai2.name))
declare_winner(samurai1, samurai2, samurai1)
## 服部半藏攻擊柳生十兵衛; 柳生十兵衛還有100滴血。
## 柳生十兵衛攻擊服部半藏; 服部半藏還有90滴血。
## 服部半藏攻擊柳生十兵衛; 柳生十兵衛還有0滴血。
## 柳生十兵衛有0滴血,掛掉。服部半藏勝利。 class Fighter():
def __init__(self,name,health,damage):
self.name = name
self.health = health
self.damage = damage
def declare_winner(p1_obj, p2_obj, first_act_name):
p1 = p1_obj
p2 = p2_obj
users =
for i in range(len(users)):
if users.name == first_act_name:
users.insert(0,users.pop(i))
while True:
users.health -= users.damage
if users.health <= 0:
print('{first.name} attacks {second.name}: {second.name} now has {second.health} health and is dead. {first.name} wins.'.format(first=users,second=users))
break
else:
print('{first.name} attacks {second.name}; {second.name} now has {second.health} health.'.format(first=users,second=users))
users = users[::-1]
declare_winner(Fighter("Lew", 10, 2), Fighter("Harry", 5, 4), "Lew")
#结果
Lew attacks Harry; Harry now has 3 health.
Harry attacks Lew; Lew now has 6 health.
Lew attacks Harry; Harry now has 1 health.
Harry attacks Lew; Lew now has 2 health.
Lew attacks Harry: Harry now has -1 health and is dead. Lew wins. '''
两名武士决斗,返回出胜利者的名字。
要求:两名武士轮流攻击对方,先杀死对方的胜者。而死亡取决于其health, health<=0时死亡。
每个武士为一个Fighter实例。(需要有名字(name),生命值(health),伤害(damage_per_attack))
主函数为:declare_winner(武士1,武士2,先攻击者)
返回值为:获胜者。
'''
import random
class Fighter(object):
def __init__(self,name,health,damage):
self.name = name
self.health = health
self.damage = damage
def attack(f1,f2):
counter = 1
while f1.health > 0 and f2.health > 0:
print('****************第%d回合开始****************' % counter)
print('%s当前血量为%d,%s当前血量为%d。' %(f1.name,f1.health,f2.name,f2.health))
print('%s开始攻击' % f1.name,end=' ')
f2.health -= f1.damage
print('%s血量降低到%d' %(f2.name,f2.health))
if f2.health < 0:
print('%s 战败!' % f2.name)
winner = f1.name
break
print('%s开始攻击' % f2.name,end=' ')
f1.health -= f2.damage
print('%s血量降低到%d' %(f1.name,f1.health))
if f1.health < 0:
print('%s 战败! ' % f1.name)
winner = f2.name
break
counter += 1
return winner
def declare_winner(fighter1,fighter2,names):
if fighter1.name == names:
return attack(fighter1,fighter2)
elif fighter2.name == names:
return attack(fighter2,fighter1)
else:
print('names not exists!')
return 0
bob = Fighter('Bob',1200,99)
stam = Fighter('Stam',1360,88)
declare_winner(bob,stam,'Stam')
正在学习类 看看 zan 看看
本帖最后由 咕咕鸡鸽鸽 于 2019-1-9 19:28 编辑
我把攻击变成随机的攻击力
from random import *
class Fighter():
def __init__(self,name):
self.name = name
self.health = 10
self.damage = randint(1,3)
self.alive = True
def attack(self):
self.damage = randint(1,3)
def main():
man_a = Fighter("A")
man_b = Fighter("B")
man_list =
gaming = True
while gaming:
for each in man_list:
if each.health <= 0:
each.alive = False
print("%s is dead." % each.name)
man_list.remove(each)
print("winner is %s." % man_list.name)
gaming = False
break
index = man_list.index(each)
man_list.remove(each)
each.attack()
man_list.health -= each.damage
print("%s attacks %s;%s now has %d health" % (each.name,man_list.name,man_list.name,man_list.health))
man_list.insert(index,each)
print("---------------------------------")
if __name__ == "__main__":
main() 本帖最后由 永恒的蓝色梦想 于 2019-8-18 16:23 编辑
class Fighter:
def __init__(self,name,health,dpa):self.name=name;self.health=health;self.dpa=dpa
def fight(self,other):other.health-=self.dpa
declare_winner(Fighter("Lew", 10, 2), Fighter("Harry", 5, 4), "Lew")
def declare_winner(F1, F2, F):
if F!=F1.name:F1,F2=F2,F,1
while 1:
F1.fight(F2)
if F2.heath>0:print(f'{F1.name} attacks {F2.name}; {F2.name} now has {F2.health} health.')
else:print(f'{F1.name} attacks {F2.name}; {F2.name} now has {F2.health} health and is dead. {F1.name} wins.');winner=F1.name;break
F2.fight(F1)
if F1.heath>0:print(f'{F2.name} attacks {F1.name}; {F1.name} now has {F1.health} health.')
else:print(f'{F2.name} attacks {F1.name}; {F1.name} now has {F1.health} health and is dead. {F2.name} wins.');winner=F2.name;break
return winner
页:
[1]
2