qiyuan -飞机大战最终版


import pygame
import random
import time
# 类
"""
类名:feiji
属性:位置x,y;皮肤;血量
方法: show, move,发射子弹
"""


class feiji:
    def __init__(self, CK):
        self.x = 250
        self.y = 500
        self.picture = "hero1.png"
        self.pifu = pygame.image.load(self.picture)
        self.blood = 3
        self.chuangkou = CK
        self.bullets = []  # 列表
        # 是否爆炸
        self.boom = False
        self.boom_picture=[] # 爆炸图库
        self.join_boom_picture()
        self.boom_picture_num = 0
        self.image_index = 0

    def join_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:
            self.chuangkou.blit(self.boom_picture[self.boom_picture_num], (self.x, self.y))
            self.image_index += 1
            if self.image_index == 7:
                self.image_index = 0
                self.boom_picture_num += 1
            if self.boom_picture_num>3:
                time.sleep(2)
                pygame.quit()
        else:
            self.chuangkou.blit(self.pifu, (self.x, self.y))

        zidanx = []
        zidany = []
        for dangezidan in self.bullets:
            zidanx.append(dangezidan.x)
            zidany.append(dangezidan.y)
            # print(dangezidan.x,dangezidan.y)
            dangezidan.y = dangezidan.y - 10
            dangezidan.show()
            if dangezidan.yuejie():
                self. bullets.remove(dangezidan)
        return zidanx,zidany
    # 向左
    def move_left(self):
        self.x = self.x - 10

    def move_right(self):
        self.x = self.x + 10

    # 化身为敌机
    def youli(self):
        self.picture = "enemy-1.gif"
        self.pifu = pygame.image.load(self.picture)

    # 发射子弹:
    def xiuxiuxiu(self):
        self.bullets.append(zidan(self.chuangkou, self.x, self.y))
        self.bullets.append(zidan(self.chuangkou, self.x + 70, self.y))

    def isboom(self):
        # self.picture = "hero_blowup_n4.png"
        # self.pifu = pygame.image.load(self.picture)
        self.boom=True

    def noboom(self):
        self.picture = "hero1.png"
        self.pifu = pygame.image.load(self.picture)

"""
类名:子弹
属性:位置x,y;皮肤;血量
方法: show 
"""


class zidan:
    def __init__(self, CK, x, y, tupian="bullet.png"):
        self.x = x
        self.y = y
        self.pifu = pygame.image.load(tupian)
        self.chuangkou = CK

    def show(self):
        self.chuangkou.blit(self.pifu, (self.x, self.y))

    def move(self):
        self.y = self.y + 1

    def yuejie(self):
        if self.y < -1200:
            return True
        else:
            return False


# 敌机
"""
类名:diji
属性:位置x,y;皮肤;血量
方法: show, move
"""


class diji:
    def __init__(self, CK):
        self.x = 250
        self.y = 0
        self.pifu = pygame.image.load("enemy-1.gif")
        self.chuangkou = CK
        self.direction = "left"
        self.bullets = []
        # 是否爆炸
        self.boom = False
        self.boom_picture = []  # 爆炸图库
        self.join_boom_picture()
        self.boom_picture_num = 0
        self.image_index = 0

    def join_boom_picture(self):
        self.boom_picture.append(pygame.image.load("enemy0_down1.png"))
        self.boom_picture.append(pygame.image.load("enemy0_down2.png"))
        self.boom_picture.append(pygame.image.load("enemy0_down3.png"))
        self.boom_picture.append(pygame.image.load("enemy0_down4.png"))

    def show(self):
        if self.boom:
            self.chuangkou.blit(self.boom_picture[self.boom_picture_num], (self.x, self.y))
            self.image_index += 1
            if self.image_index == 7:
                self.image_index = 0
                self.boom_picture_num += 1
            if self.boom_picture_num>3:

                self.boom_picture_num = 3

        else:
            self.chuangkou.blit(self.pifu, (self.x, self.y))

        for dangezidan in self.bullets:
            dangezidan.y += 1
            dangezidan.show()

    def move(self):
        if not self.boom:
            self.y = self.y + 10
        # if self.direction == "left":
        #     self.x = self.x - 10
        # elif self.direction == "right":
        #     self.x = self.x + 10
        if self.y > 700:
            self.y=0
        # if self.x < 0:
        #     self.direction = "right"
        # elif self.x > 400:
        #     self.direction = "left"
    def isboom(self):
        # self.picture = "hero_blowup_n4.png"
        # self.pifu = pygame.image.load(self.picture)
        self.boom=True
    # # 发射子弹:
    # def xiu_xiu_xiu(self):
    #     self.bullets.append(zidan(self.chuangkou, self.x, self.y, tupian="enemy-1.gif"))


def key_control(hero):
    # 判断我们是否点红叉了
    # 事件
    for shi_jian in pygame.event.get():
        if shi_jian.type == pygame.QUIT:
            pygame.quit()
        if shi_jian.type == pygame.KEYDOWN:
            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_h:
                hero.youli()
            elif shi_jian.key == pygame.K_SPACE:
                hero.xiuxiuxiu()
            # elif shi_jian.key == pygame.K_b:
            #     hero.isboom()
            elif shi_jian.key == pygame.K_n:
                hero.noboom()




def main():
    # 2。初始化
    pygame.init()
    # 3。设置窗口大小
    chuang_kou = pygame.display.set_mode((400, 700))
    # 4。设置标题
    pygame.display.set_caption("米奇妙妙屋")
    # 1.导入图片
    background = pygame.image.load("background.png")
    # 6。刷新  120 hz   1080 hz 显卡 计算画面
    # 7. 新建对象:战机,敌机
    伞兵一号 = feiji(chuang_kou)
    炮灰一号 = diji(chuang_kou)
    # 导入字体
    font = pygame.font.Font(None,100)  # 设置字体参数

    # 加载音乐
    pygame.mixer.init()
    # 背景音乐
    pygame.mixer.music.load('hit_wall.wav')
    pygame.mixer.music.set_volume(0.3)
    pygame.mixer.music.play(-1)


    while True:
        key_control(伞兵一号)

        chuang_kou.blit(background, (0, 0))
        zx,zy = 伞兵一号.show()
        炮灰一号.show()
        炮灰一号.move()
        # xiubuxiu = random.randint(1, 100)
        # if xiubuxiu == 6:
        #     炮灰一号.xiu_xiu_xiu()
        print(炮灰一号.x,炮灰一号.y)
        print(伞兵一号.x,伞兵一号.y)
        print(zx,zy)
        # 炮灰一号.isboom()
        # 判断伞兵1号是否爆炸
        # if 伞兵一号.x+100>炮灰一号.x>伞兵一号.x:
        #     if 伞兵一号.y+124>炮灰一号.y>伞兵一号.y:
        #         伞兵一号.isboom()
        # 多个敌机
        # 子弹的坐标
        for i in range(len(zx)):
            if 炮灰一号.x<zx[i]<炮灰一号.x+51 and   炮灰一号.y<zy[i]<炮灰一号.y+39:
                炮灰一号.isboom()

        # 设置文字
        Text_score = font.render('score:100', True, (100, 0, 0))  # 设置加入文字的参数
        chuang_kou.blit(Text_score, (100, 100))
        pygame.display.update()

    # 5。退出游戏
    pygame.quit()


if __name__ == '__main__':
    main()






发布了390 篇原创文章 · 获赞 19 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/houlaos/article/details/104467039