qiyaun -并没有idea,飞机的子弹和爆炸

# 1。导包
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))


        for dangezidan in self.bullets:
            print(dangezidan.y)
            dangezidan.y = dangezidan.y - 10
            dangezidan.show()
            if dangezidan.yuejie():
                self.bullets.remove(dangezidan)

    # 向左
    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="bdzb.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 = []

    def show(self):
        self.chuangkou.blit(self.pifu, (self.x, self.y))
        for dangezidan in self.bullets:
            dangezidan.y += 1
            dangezidan.show()

    def move(self):
        if self.direction == "left":
            self.x = self.x - 10
        elif self.direction == "right":
            self.x = self.x + 10

        if self.x < 0:
            self.direction = "right"
        elif self.x > 400:
            self.direction = "left"

    # 发射子弹:
    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)

    while True:
        key_control(伞兵一号)
        chuang_kou.blit(background, (0, 0))
        伞兵一号.show()
        炮灰一号.show()
        炮灰一号.move()
        xiubuxiu = random.randint(1, 10)
        if xiubuxiu == 6:
            炮灰一号.xiu_xiu_xiu()
        pygame.display.update()
    # 5。退出游戏
    pygame.quit()


if __name__ == '__main__':
    main()
发布了352 篇原创文章 · 获赞 19 · 访问量 1万+

猜你喜欢

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