改进了已知bug
1:小飞机随机生生成地方不会重叠
2:增加了y长度可增大炮台存活时间
代码如下:import random as r
import sys
legal_x = [0,10]
legal_y = [0,25]
class Big:
def __init__(self):
"""在y=1随机生成炮台位置"""
self.x = r.randint(0,10)
self.y = 1
def move(self):
self.Interval = []
"""随机移动左右方向并移动到新的位置(x,1)"""
self.step = r.randint(0,legal_x[1])
self.direction = r.randint(-1,1)#方向,-1为左,0不移动,1为右
new_x = self.x + self.direction * self.step
mew_y = self.y
"""判断是否越界"""
if new_x > legal_x[1]:
pos_x = legal_x[1] - (new_x - legal_x[1])
pos_y = mew_y
elif new_x < legal_x[0]:
pos_x = legal_x[0] - new_x
pos_y = mew_y
else:
pos_x = new_x
pos_y = mew_y
"""炮台移动前后对应坐标"""
if self.x > pos_x:
for i in range(pos_x,self.x + 1 ):
self.Interval.append(i)
print("炮台从坐标x=%d移动到x=%d,沿途轰了%d炮"%(self.x,pos_x,self.x + 1 -pos_x ))
print(">>>轰出%d个炮的位置是x ="% (self.x + 1 -pos_x),end = "")
print(self.Interval)
elif self.x < pos_x:
for i in range(self.x,pos_x + 1):
self.Interval.append(i)
print("炮台从坐标x=%d移动到x=%d,沿途轰了%d炮"%(self.x,pos_x,pos_x + 1 -self.x ))
print(">>>轰出%d个炮的位置是x ="% (pos_x + 1 -self.x),end = "")
print(self.Interval)
else:
self.Interval.append(pos_x)
print(">>>炮台原地轰了一炮")
print(">>>轰炮的坐标是x = %s"% str(self.Interval))
"""初始化炮台到移动的目标"""
self.x = pos_x
self.y = pos_y
return (pos_x,pos_y)
class Small:
def __init__(self):
"""在y=25随机生成小飞机位置"""
self.x = r.randint(0,legal_x[1])
self.y = legal_y[1]
def move(self):
"""固定移动,每次向下一步"""
new_x = self.x
mew_y = self.y - 1
"""判断是否越界"""
if mew_y <= legal_y[0]:
self.x = r.randint(0,legal_x[1])
self.y = legal_y[1]
else:
self.x = new_x
self.y = mew_y
return (new_x , mew_y)
class Boom:
"""核武器"""
def __init__(self):
self.x = r.randint(0,legal_x[1])
self.y = 1
def DAFEIJI(n):
Scorer = 0
list_s = []
big_air = Big()
"""激光炮台出场"""
i = r.randint(9,10)
while n:
list_pos = []
boom = Boom()
"""核武器生成"""
for numbers in range(1 ,i + 1):
small_air = Small()#小飞机出场数量位置随机,设置不重叠
if small_air.x not in list_pos:
list_pos.append(small_air.x)
list_s.append(small_air)
else:
continue
pos = big_air.move()
n = n - 1
if pos != (boom.x ,boom.y):
for each in list_s[:]:
pos_small = each.move()
if pos == pos_small:
print(">>>>>>>很不幸! 您的炮台撞小飞机了....GG!!")#这个几率.....
print("本次打飞机的分数是:%d" % Scorer)
sys.exit(0)
elif pos_small[0] in (big_air.Interval):
"""一条直线打的,其实是激光炮"""
print("一架小飞机被打掉..")
Scorer += 1
list_s.remove(each)
else:
print("炮台加载了核武器...======================清屏!=======================..")
Scorer += len(list_s)
list_s.clear()
print("本次打飞机的分数是:%d" % Scorer)
#==============================主程序==================================
DAFEIJI(50)
@小茗同学 @小甲鱼 0基础的成果,求指导一个问题:
这样的炮台其实是激光炮,会扫射一条直线所有的飞机。
请问怎么修改,才可以让炮台只打距离他第一个小飞机,而不是一条直线是全扫了~
|