其他分享
首页 > 其他分享> > boren -飞机大战8

boren -飞机大战8

作者:互联网

import pygame
import sys
import random # 产生随机数
import time
# 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 = []
        # 爆炸代码
        self.boom = False    # 判断战机是否被撞击
        self.boom_picture = []   # 爆炸图库
        self.jiaru_boom_picture()  # 爆炸图库加入图片的函数
        self.boom_picture_num = -1 # 第几张爆炸图片
    def jiaru_boom_picture(self):
        self.boom_picture.append(pygame.image.load("图片/hero_blowup_n1.png"))
        self.boom_picture.append(pygame.image.load("图片/hero_blowup_n2.png"))
        self.boom_picture.append(pygame.image.load("图片/hero_blowup_n3.png"))
        self.boom_picture.append(pygame.image.load("图片/hero_blowup_n4.png"))

    # 把飞机放到屏幕上
    def show(self):
        if self.boom == True:
            self.boom_picture_num += 1
            print(self.boom_picture_num )
            self.chuangkou.blit(self.boom_picture[self.boom_picture_num],(self.x,self.y))
            time.sleep(2)

            if self.boom_picture_num>3:
                time.sleep(2)
                pygame.quit()  # 退出游戏
                sys.exit()  # 系统文件的退出
        else:
            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"))
    def zhanji_boom(self):
        self.boom = True

# 子弹的类
# 属性:子弹的位置 ,子弹窗口,子弹图片,伤害,子弹名字,子弹数量,
# 方法:子弹的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 + 2

    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 = random.randint(0,400)
        self.y = 0
        self.picture = pygame.image.load("图片/diji.png")
        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"
        self.y += 1
        # self.x += 1

    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()
            elif shi_jian.key == pygame.K_b:
                hero.zhanji_boom()


# 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)
    diji_ku = [] # 列表
    # 5。循环
    while True:
        # 把背景放上去
        chuangkou.blit(bei_jing, (0, 0))
        # 把飞机和相关元素放上窗口去
        feiji1.show()
        b = random.randint(1,100)
        if b ==5:
            diji_ku.append(diji(chuangkou))

        for diji1 in diji_ku:
            diji1.show()
            diji1.move()
            diji1.fashe()
        # 控制飞机jj
        key_kong(feiji1)
        # key_kong2(diji1 , 2)
        # 4.刷新
        pygame.display.update()


# main主要的函数
if __name__ == '__main__':
    zhu()
少儿编程侯老师 发布了293 篇原创文章 · 获赞 18 · 访问量 1万+ 私信 关注

标签:picture,飞机,self,大战,boren,pygame,zidan,def,boom
来源: https://blog.csdn.net/houlaos/article/details/104206956