文字版打飞机1.0
1:炮台从底层y = 1生成,左右移动 ,每次左右x轴随机移动n个位置,并且移送过的地方每一个位置都会生成一个炮弹,并发射。如果此位置Y轴正好有小飞机,则击落一架且一次只能击落,加一分。(击落多架除非下一次随机位置还在原地)2:小飞机到底层时消失,即y = 0时候消失。
3:小飞机从顶层y = 10水平位置生成,x位置随机,每次随机生成n个
4:主飞机本身长度y=1。当炮台和小飞机下一个坐标都为(x,1)时,碰撞。游戏结束。
5:加载到核武器清屏,加到目前所有分。
6:所有操作都是电脑随机操作。
代码如下:
import random as r
import sys
legal_x =
legal_y =
list_boom = []
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,10)
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:
pos_x = legal_x - (new_x - legal_x)
pos_y = mew_y
elif new_x < legal_x:
pos_x = legal_x - 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=10随机生成小飞机位置"""
self.x = r.randint(0,10)
self.y = 10
def move(self):
"""固定移动,每次向下一步"""
new_x = self.x
mew_y = self.y - 1
"""判断是否越界"""
if mew_y <= legal_y:
self.x = r.randint(0,10)
self.y = 10
else:
self.x = new_x
self.y = mew_y
return (new_x , mew_y)
class Boom:
"""核武器"""
def __init__(self):
self.x = r.randint(0,10)
self.y = 1
def DAFEIJI(n):
Scorer = 0
list_s = []
big_air = Big()
"""炮台出场"""
i = r.randint(3,10)
while n:
boom = Boom()
"""核武器生成"""
for each_air in range(i):
small_air = Small()#小飞机出场数量随机生成,位置也随机,所以可能与之前重叠
list_s.append(small_air)
pos = big_air.move()
n = n - 1
if pos != (boom.x ,boom.y):
for each in list_s[:]:
if each.move() == pos:
print(">>>>>>>很不幸! 您的炮台撞小飞机了....GG!!")#这个几率.....
print("本次打飞机的分数是:%d" % Scorer)
sys.exit(0)
if(each.move()) in (big_air.Interval):
print("一架小飞机被打掉..")
Scorer += 1
list_s.remove(each)
else:
print("炮台加载了核武器...======================清屏!=======================..")
Scorer += len(list_s)
list_s.clear()
print("本次打飞机的分数是:%d" % Scorer)
#==============================主程序==================================
DAFEIJI(200)#n为移动步数
目前的BUG:
1:小飞机生成地方随机,量大会重叠
2:炮台时不时原地放一炮(哈哈,这个不算)
求各位大神给思路,怎么限制小飞机出生坐标和目前所有坐标不重复
==== 目前电脑随机最多打了800分{:10_279:},有兴趣的可以测试下~~哈哈
改进了已知bug
1:小飞机随机生生成地方不会重叠
2:增加了y长度可增大炮台存活时间
代码如下:
import random as r
import sys
legal_x =
legal_y =
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)
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:
pos_x = legal_x - (new_x - legal_x)
pos_y = mew_y
elif new_x < legal_x:
pos_x = legal_x - 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)
self.y = legal_y
def move(self):
"""固定移动,每次向下一步"""
new_x = self.x
mew_y = self.y - 1
"""判断是否越界"""
if mew_y <= legal_y:
self.x = r.randint(0,legal_x)
self.y = legal_y
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)
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 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基础的成果,求指导一个问题:
这样的炮台其实是激光炮,会扫射一条直线所有的飞机。
请问怎么修改,才可以让炮台只打距离他第一个小飞机,而不是一条直线是全扫了~{:10_261:}
厉害了,学习学习!! 还真是厉害,我还没有研究过这个小程序呢,恐怕现在好多东西要向你学习呢 小茗同学 发表于 2016-12-9 09:34
还真是厉害,我还没有研究过这个小程序呢,恐怕现在好多东西要向你学习呢
没有没有 多指教。 这个好啊,留着学习学习 向楼主大大学习!{:5_92:} 好厉害!向楼主学习~ 为何如此污 我喜欢{:5_98:} 天呐!!大神!!你是都学完之后做出来的吗!!啊。打飞机是我现在的目标{:10_281:}{:10_281:}{:10_281:}{:10_281:} wdyx491 发表于 2016-12-18 20:35
天呐!!大神!!你是都学完之后做出来的吗!!啊。打飞机是我现在的目标{: ...
现在还没学完呢 这是第38课的时候写的 zua 发表于 2016-12-18 20:39
现在还没学完呢 这是第38课的时候写的
噢噢噢噢!!好激动!!{:10_266:} 赞一下,感觉很不错 学习学习 膜拜大神 111 太强大了,只能仰视..... so nb
页:
[1]