老是左右x轴随机移动n个地方

**手淫文字版 

金沙网址,**

思路: 
1:主飞机从尾部y = 1生成,左右移动
,每一遍左右x轴随机移动n个地方,並且移交送达过的地点每叁个地点都会变动八个子弹,并发出。要是此职分Y轴适逢其会有小飞机,则击落后生可畏架且三次只可以击落,加一分。(击落多架除非下一遍随飞机地方置还在原地卡塔尔国

2:小飞机到底层时消失,即y = 0时候未有。(那些标准权且失效卡塔尔国

3:小飞机从顶层y = 10档案的次序地方变动,x地方随机,每一遍随机生成四个

4:主飞机本人尺寸y=1。当主飞机和小飞机下三个坐标都为(x,1卡塔 尔(英语:State of Qatar)时,碰撞。游戏结束。

 

事先版本运转实现会发觉如下三个bug:小飞机生成因为是随机的,所以或许Infiniti定生成n个,当中有
n=<1个飞机地点会另行,所以会现出以下场景 :

只发射了6个炮弹,却在档期的顺序方向打掉了十七个飞机,那是因为变化飞机重复了,以致有三八个飞机随机变化在四个y
= 10的品位地方上。

难题的BUG已经完美,加二个小飞机刷出条件不重叠。 

代码如下:

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(5)

开端分界面y值越大,游戏停止概率越小。 

 

想询问更加多Python知识或许想与越多的朋友合伙念书调换,可以加群705673780,群众文化艺术件内越来越多免费资料可供就学啊~

相关文章