boren -飞机大战7

谁说胖子不能爱 提交于 2020-02-06 17:02:25
import pygame
import sys
import random # 产生随机数

# back  返回,后面
# ground 地面
# 哪一行代码确定战机的位置,27行,用坐标确定战机位置
# 飞机的类
# 飞机名字, 飞机的位置,窗口,图片
class feiji():
    def __init__(self, ck):
        self.name = "hero"
        self.x = 200
        self.y = 400
        self.chuangkou = ck
        self.picture = pygame.image.load("图片/hero1.png")
        self.zidan_ku = []

    # 把飞机放到屏幕上
    def show(self):
        self.chuangkou.blit(self.picture, (self.x, self.y))
        # 如果有子弹,就放上窗口
        # print(len(self.zidan_ku))
        for dan_ge_zidan in self.zidan_ku:
            dan_ge_zidan.show()
            # 飞机的子弹移动
            dan_ge_zidan.move__fd()
            if dan_ge_zidan.yuejie():
                self.zidan_ku.remove(dan_ge_zidan)

    # move 向左移动
    def move_left(self):
        self.x -= 20
        if self.x < -25:
            self.x = 5

    # 向右移动
    def move_right(self):
        self.x += 20
        if self.x >= 330:
            self.x = 330

    # 发射子弹的函数
    def fashe(self):
        # 新建子弹对象保存到zidan1变量到列表里去
        self.zidan_ku.append(zidan(self.chuangkou, self.x + 48, self.y - 10, "图片/zidan1.gif"))
        self.zidan_ku.append(zidan(self.chuangkou, self.x + 14, self.y + 25, "图片/zidan2.png"))
        self.zidan_ku.append(zidan(self.chuangkou, self.x + 80, self.y + 25, "图片/zidan3.gif"))


# 子弹的类
# 属性:子弹的位置 ,子弹窗口,子弹图片,伤害,子弹名字,子弹数量,
# 方法:子弹的show展示,move移动
class zidan():
    def __init__(self, ck, x, y, zidan_tupian):
        self.x = x
        self.y = y
        self.window = ck
        self.picture = pygame.image.load(zidan_tupian)

    def show(self):
        # print("子弹的y坐标是%d"%self.y)
        self.window.blit(self.picture, (self.x, self.y))

    def move__fd(self):
        self.y = self.y - 1
    def move__djfd(self):
        self.y = self.y + 1
    def yuejie(self):
        if self.y < -100:
            return True
        else:
            return False
    def yuejie2(self):
        if self.y > 700:
            return True
        else:
            return False

"""
敌机类:
属性:飞机位置 ,飞机图片,飞机名字,飞机窗口
方法:移动,开火
"""
class diji():
    def __init__(self,ck):
        self.name="diji"
        self.x = 0
        self.y =0
        self.picture = pygame.image.load("图片/diji.gif")
        self.zidan_ku=[]
        self.chuangkou = ck
        self.fang_xiang = "right"
    def show(self):
        self.chuangkou.blit(self.picture,(self.x,self.y))
        for dan_ge_zidan in self.zidan_ku:
            dan_ge_zidan.show()
            # 飞机的子弹移动
            dan_ge_zidan.move__djfd()
            if dan_ge_zidan.yuejie2():
                self.zidan_ku.remove(dan_ge_zidan)

    def move(self):
        if self.fang_xiang == "right":
            self.x += random.randint(1,2)
        elif self.fang_xiang=="left":
            self.x -= 1

        if self.x>=330:
            self.fang_xiang = "left"
        if self.x==0:
            self.fang_xiang="right"

    def fashe(self):
        a = random.randint(1,100)
        if a==2:
            self.zidan_ku.append(zidan(self.chuangkou, self.x + 0, self.y +0, "图片/zidan1.gif"))

# 控制飞机
def key_kong(hero):
    for shi_jian in pygame.event.get():
        # type 类型   QUIT 退出
        if shi_jian.type == pygame.QUIT:
            pygame.quit()  # 退出游戏
            sys.exit()  # 系统文件的退出
        # 判断是否按了键 KEY
        elif shi_jian.type == pygame.KEYDOWN:
            # 检测电脑是否按了a键,向左移动
            if shi_jian.key == pygame.K_a :
                hero.move_left()
            elif shi_jian.key == pygame.K_d :
                hero.move_right()
            elif shi_jian.key == pygame.K_j:
                hero.fashe()


# 1。初始化
def zhu():
    pygame.init()
    # 2。设置一个窗口
    chuangkou = pygame.display.set_mode((400, 700))
    # 3。设置一个标题
    pygame.display.set_caption("飞机大战")
    # 6. 导入图片 image图像   load 加载
    bei_jing = pygame.image.load("图片/background.png")
    # 创建一个飞机对象
    feiji1 = feiji(chuangkou)
    diji1 =diji(chuangkou)
    # 5。循环
    while True:

        # 把背景放上去
        chuangkou.blit(bei_jing, (0, 0))
        # 把飞机和相关元素放上窗口去
        feiji1.show()
        diji1.show()
        diji1.move()
        diji1.fashe()
        # 控制飞机jj
        key_kong(feiji1)
        # key_kong2(diji1 , 2)
        # 4.刷新
        pygame.display.update()


# main主要的函数
if __name__ == '__main__':
    zhu()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!