python控制台飞机大战

先上图:

 

 

 

 

 代码:

import consoleCanvas
import _thread
import time,random

def draw_cir (a,b,r=5):
    for x in range(a-r,a+r):
        y=int((((r**2)-(x-a)**2)**(1/2))+b)
        A.reviseCanvas([x,y],1)#绘制画布像素
    for x in range(a+r,a-r,-1):
        y=int(-1*(((r**2)-(x-a)**2)**(1/2))+b)
        A.reviseCanvas([x,y],1)#绘制画布像素

def drawline(p1,p2):
    x1=p1[0]
    y1=p1[1]
    x2=p2[0]
    y2=p2[1]
    try:
        k=(y1-y2)/(x1-x2)
    except:
        k=1
    b=y1-(k*x1)
    for x in range (x1,x2):
        y=int(k*x+b)
        A.reviseCanvas([x,y],1)

class aircraft:
    def __init__(self,name,x=50,y=50,health=100):
        self.x=x
        self.y=y
        self.health=health
        self.name=name
        self.FANG="默认"#设置初始化方向
    def up(self,l):
        self.y-=l
    def down(self,l):
        self.y+=l
    def lift(self,l):
        self.x-=l
    def right(self,l):    
        self.x+=l
    def draw(self):
        draw_cir(self.y,self.x,r=2)

    def fire(self):
        bullet_list.append(bullet(self.x,self.y))
        self.FANG="默认"
def drawboundary():
    A.map_c[0]=[1 for i in range(m_w)]#上边界
    A.map_c[len(A.map_c)-1]=[1 for i in range(m_w)]#下边界
    for i in A.map_c:
        i[0]=1#左边界
        i[len(i)-1]=1#右边界
def update(N):
    if (flyer.FANG=="w"):
        flyer.up(N)
    if (flyer.FANG=="s"):
        flyer.down(N)

    if (flyer.FANG=="a"):
        flyer.lift(N)
    if (flyer.FANG=="d"):
        flyer.right(N)
    if (flyer.FANG=="q"):
        flyer.fire()

# 为线程定义一个函数
def direction(n):
    while True:
        try:
            FANG=input("")
            flyer.FANG=FANG
            update(n)
        except:    
            pass

class  bullet :
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def draw(self):
        A.reviseCanvas([self.y,self.x],1)

def drawene():#绘制敌机
    for ene in enemy_flyer_list:#绘制敌机
        try:
            ene.draw()
        except:
            pass
def drawbu():#绘制子弹
    for i in range(0,len(bullet_list)):#绘制子弹
        for e in range(0,len(enemy_flyer_list)):
            ene=enemy_flyer_list[e]
            bu=bullet_list[i]
            l_l=((bu.x-ene.x)**2+(bu.y-ene.y)**2)**0.5
            print(l_l)
            if (l_l<5):
                #击中战机
                del enemy_flyer_list[e]
                del bullet_list[i]
            try:
                if(bu.x<0 or bu.y<0 ):
                    del bullet_list[i]
                else:
                    bu.y-=1
                    bu.draw()
            except:
                pass
def main():
    time.sleep(1)
    print("\033[1H\033[2J")
    A.ProduceCanvas(m_w,m_h)#清空画布
    # print(bullet_list)
    print(f"战机状态:运行方向:{flyer.FANG},位置:x {flyer.x},y {flyer.y},生命值{flyer.health}")
    update(step)
    drawboundary()#绘制边界
    flyer.draw()#绘制战机
    drawene()#绘制敌机
    try:
        drawbu()#绘制敌机
    except:
        pass 
    A.show()
    main()
def setFang(s):
    FANG=s

if __name__ =="__main__":
    m_w=100
    m_h=100
    step=1
    enemy_flyer_list=[]
    bullet_list=[]
    A=consoleCanvas.consoleCanvas()#初始化
    flyer=aircraft("一号战斗机",x=50,y=80)#创建一台飞机
    enemy_flyer_list.append(aircraft("二号战斗机",x=50,y=20))#创建一台飞机)
    enemy_flyer_list.append(aircraft("二号战斗机",x=10,y=20))#创建一台飞机)
    for i in range (5):
        enemy_flyer_list.append(aircraft("二号战斗机",x=random.randint(0,100),y=random.randint(0,30)))#创建一台飞机)
        
    try:
        _thread.start_new_thread( direction, (step,) )
    except:
        print ("Error: 无法启动线程")
    main()

 

posted @ 2021-05-07 16:57  sunny开始学坏  阅读(164)  评论(0编辑  收藏  举报