|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random as r
def map_x(x):
x = 100
return map_x(x - r.randint(2,4))
def map_y(y):
y = 100
return map_Y(y - r.randint(2,4))
class Soldiers:
def __init__(self):
self.power = 5
self.x = r.randint(0,100)
self.y = r.randint(0,100)
def move(self):
new_x = self.x + r.choice([1,3,-1,-3])
new_y = self.y + r.choice([1,3,-1,-3])
if new_x < map_x(x):
self.x = 0 - (new_x - 0)
self.power -= 1
elif new_x > map_x(x):
self.x = map_x(x) - (new_x - map_x(x))
self.power -= 1
else:
self.x = new_x
if new_y < map_y(y):
self.y = 0 - (new_y - 0)
self.power -= 1
elif new_y > map_y(y):
self.y = map_y(y) - (new_y - map_y(y))
self.power -= 1
else:
self.y = new_y
return (self.x, self.y)
class Shooter(Soldiers):
def __init__(self):
self.power = 2
super().__init__()
def move(delf):
new_x = self.x + r.choice([2,-2])
new_y = self.y + r.choice([2,-2])
class Lord_Voldemort(Soldiers):
def __init__(self):
self.power = 5
super().__init__()
def move(self):
new_x = self.x + r.choice([1,-1])
new_y = self.y + r.choice([1,-1])
class Medic(Soldiers):
def __init__(self):
self.power = 3
if self.power > 5:
self.power = 5
super().__init__()
def move(self):
new_x = self.x + r.choice([2,-2])
new_y = self.y + r.choice([2,-2])
if new_x < map_x(x):
self.x = 0 - (new_x - 0)
self.power -= 1
elif new_x > map_x(x):
self.x = map_x(x) - (new_x - map_x(x))
self.power -= 1
else:
self.x = new_x
if new_y < map_y(y):
self.y = 0 - (new_y - 0)
self.power -= 1
elif new_y > map_y(y):
self.y = map_y(y) - (new_y - map_y(y))
self.power -= 1
else:
self.y = new_y
self.power += 1
return (self.x, self.y)
soldier = Soldiers()
shooter = Shooter()
lord_voldemort = Lord_Voldemort()
medic = Medic()
unit =[]
unit.append(soldier)
unit.append(shooter)
unit.append(lord_voldemort)
unit.append(medic)
while True:
if not soldier.power:
unit.remove(soldier)
print('普通士兵被淘汰了')
if not shooter.power:
unit.remove(shooter)
print('射手被淘汰了')
if not lord_voldemort.power:
unit.remove(lord_voldemort)
print('伏地魔被毒死了')
if not medic.power:
unit.remove(medic)
print('医疗兵被淘汰了')
soldier_pos = soldier.move()
shooter_pos = shooter.move()
lord_voldemort_pos = lord_voldemort.move()
medic_pos = medic.move()
if soldier_pos == shooter_pos:
print('射手被普通士兵击杀!')
shooter.power = 0
unit.remove(shooter)
if lord_voldemort_pos == soldier_pos:
print('普通士兵被伏地魔偷袭了!')
soldier.power = 0
unit.remove(soldier)
if lord_voldemort_pos == medic_pos:
print('医疗兵被伏地魔偷袭了!')
medic.power = 0
unit.remove(medic)
if lord_voldemort_pos == shooter_pos:
print('射手被伏地魔偷袭了!')
shooter.power = 0
unit.remove(shooter)
if abs(shooter_pos - medic_pos) >= 2 and abs(shooter_pos - medic_pos) <=4:
print('医疗兵被射手击杀了!!')
medic.power = 0
unit.remove(medic)
if abs(shooter_pos - soldier_pos) >= 2 and abs(shooter_pos - soldier_pos) <=4:
print('普通士兵被射手击杀了!!')
soldier.power = 0
unit.remove(soldier)
for each_unit in unit[:]:
if len(unit) == 1:
print('游戏结束,%s存活了下来!!!' % each_unit)
Traceback (most recent call last):
File "C:\Users\kakin\AppData\Local\Programs\Python\Python39\test\test38.1py.py", line 143, in <module>
soldier_pos = soldier.move()
File "C:\Users\kakin\AppData\Local\Programs\Python\Python39\test\test38.1py.py", line 22, in move
if new_x < map_x(x):
NameError: name 'x' is not defined
请问如何去解决这些问题呢!!!!求大神帮忙!!
包括后面的边界判断也是直接写的 0 而不是map_x【0】 要直接写也是-100 或者 你直接把边界改成0到100
|
|