Article directory
Counter-Strike Basic Edition
An elite versus a gangster.
analyze
- Define humans, describe public attributes
life:100
,name:姓名要传参
- Define hero and terrorist classes
- Define the main function to describe the gunfight process
main
and create two objects - Define the grab method, which is divided into two methods
Hero
,Is
both- The defined method requires passing in the object being shot.
- The health of the target being shot is reduced.
- Call the grab operation in the main program
- After the grab operation is started, everyone’s status information should be displayed in the main program
- Define the method
Person
in the class__str__
to display the status of each person - Set the opening and grabbing operation to be a repeated operation
- Set the conditions for stopping the gunfight: one party’s health value <=0
- Stop looping using break
Code
class Person:
def __init__(self, name):
self.name = name
self.life = 100
def __str__(self):
return "%s当前生命值为:[%d]" % (self.name, self.life)
class Hero(Person):
def fire(self, person):
damage = 40
print("%s向%s开抢,造成了[%d]伤害" % (self.name, person.name, damage))
person.life -= damage
class Is(Person):
def fire(self, person):
damage = 90
print("%s向%s开抢,造成了[%d]伤害" % (self.name, person.name, damage))
person.life -= damage
def main():
h = Hero("【英雄】")
is1 = Is("【不要命】")
while True:
h.fire(is1)
is1.fire(h)
print(h)
print(is1)
print()
# 结束枪战
if h.life <= 0:
print("%s死亡,枪战结束" % h.name)
break
# 结束枪战
if is1.life <= 0:
print("所有恐怖分子全部死亡,枪战结束")
break
# 执行主函数
main()
Counter-Strike Remastered Edition
analyze
- Fixed hero information display mode and status description
- 100: No damage
- 70-99: Minor injury
- 1-69: Serious injury
- 0: hung up
- Fix the problem of negative health value
- When shooting, if the health value is < the damage value, the health value = 0, otherwise the health value will be reduced normally.
- Ternary expression:
变量 = 成立值 if 条件 else 不成立值
Code
class Person:
def __init__(self, name):
self.name = name
self.life = 100
def __str__(self):
return "%s当前生命值为:[%d]" % (self.name, self.life)
class Hero(Person):
def fire(self, person):
damage = 40
print("%s向%s开抢,造成了[%d]伤害" % (self.name, person.name, damage))
# 三元表达式:变量 = 成立值 if 条件 else 不成立值
person.life = 0 if person.life < damage else (person.life - damage)
def __str__(self):
state = ""
if self.life == 100:
state = "无伤"
elif 70 <= self.life < 100:
state = "轻伤"
elif 1 <= self.life < 70:
state = "重伤"
elif self.life <= 0:
state = "挂了"
return "%s当前的状态为:%s" % (self.name, state)
class Is(Person):
def fire(self, person):
damage = 20
print("%s向%s开抢,造成了[%d]伤害" % (self.name, person.name, damage))
person.life = 0 if person.life < damage else (person.life - damage)
def main():
h = Hero("【英雄】")
is1 = Is("【不要命】")
while True:
h.fire(is1)
is1.fire(h)
print(h)
print(is1)
print()
# 结束枪战
if h.life <= 0:
print("%s死亡,枪战结束" % h.name)
break
# 结束枪战
if is1.life <= 0:
print("所有恐怖分子全部死亡,枪战结束")
break
# 执行主函数
main()
Counter-Strike Enhanced Edition
One elite versus three gangsters.
analyze
- Create three terrorist objects, all three objects must be robbed, and all three objects must print status.
- The end condition of the repair is that all three terrorists die, and all three terrorists die at the same time and
- Solving the problem of robbing three terrorists
- Random number: random
- Use random: add before all codes
import random
- Use
random.randint(1, 3)
to generate random numbers from 1 to 3
- Use random: add before all codes
- Generate a random number, judge it and shoot at that number of enemies
- Random number: random
Code
Counter-Strike Super Enhanced Edition
analyze
- Add gun shooting hit rate
- Added gun damage value fluctuation
- Added whip corpse text display effect
Code
import random
class Person:
def __init__(self, name):
self.name = name
self.life = 100
def __str__(self):
return "%s当前生命值为:[%d]" % (self.name, self.life)
class Hero(Person):
def fire(self, person):
# 增加命中率
hit = random.randint(1, 100)
# 80% 命中率
if hit > 20:
# 增加鞭尸判断
if person.life <= 0:
print("%s都死了,就不要鞭尸了" % person.name)
else:
# 加入伤害波动值
damage = random.randint(41, 60)
print("%s向%s开抢,造成了[%d]伤害" % (self.name, person.name, damage))
# 三元表达式:变量 = 成立值 if 条件 else 不成立值
person.life = 0 if person.life < damage else (person.life - damage)
else:
print("没有打到恐怖分子,这什么垃圾枪法~~~")
def __str__(self):
state = ""
if self.life == 100:
state = "无伤"
elif 70 <= self.life < 100:
state = "轻伤"
elif 1 <= self.life < 70:
state = "重伤"
elif self.life <= 0:
state = "挂了"
return "%s当前的状态为:%s" % (self.name, state)
class Is(Person):
def fire(self, person):
damage = random.randint(1, 20)
# 增加命中率
hit = random.randint(1, 100)
# 30% 命中率
if hit > 70:
print("%s向%s开抢,造成了[%d]伤害" % (self.name, person.name, damage))
person.life = 0 if person.life < damage else (person.life - damage)
else:
print("%s枪法不行呀,再去练练吧~~~" % self.name)
def main():
h = Hero("【英雄】")
is1 = Is("【不要命】")
is2 = Is("【不怕死】")
is3 = Is("【还有谁】")
while True:
# 产生一个随机数
x = random.randint(1, 3)
if x == 1:
h.fire(is1)
elif x == 2:
h.fire(is2)
else:
h.fire(is3)
# 结束枪战
if is1.life <= 0 and is2.life <= 0 and is3.life <= 0:
print("所有恐怖分子全部死亡,枪战结束")
break
is1.fire(h)
is2.fire(h)
is3.fire(h)
# 结束枪战
if h.life <= 0:
print("%s死亡,枪战结束" % h.name)
break
print(h)
print(is1)
print(is2)
print(is3)
print()
# 执行主函数
main()