deqin -飞机大战6.0

# 制作游戏   导包
import random
import time
import pygame
import sys
import plane
import enemy


def key_control(hero):
    # event 事件,我们对电脑的每一次操作都是一个事件
    for shi_jian in pygame.event.get():
        # print(shi_jian)
        if shi_jian.type == pygame.QUIT:
            pygame.quit()
            sys.exit()  # 系统文件的退出
        # 判断我们有没有按键 KEY
        elif shi_jian.type == pygame.KEYDOWN:
            # 判读我们按了什么键
            if shi_jian.key == pygame.K_a:
                hero.move_left()
            if shi_jian.key == pygame.K_d:
                hero.move_right()
            if shi_jian.key == pygame.K_w:
                hero.move_up()
            if shi_jian.key == pygame.K_s:
                hero.move_down()
            if shi_jian.key == pygame.K_f:
                hero.fire()


def main():
    # 1。初始化
    pygame.init()
    # 2。制作窗口
    window = pygame.display.set_mode((400, 600))
    # 3。给一个标题
    pygame.display.set_caption("星球大战")
    # 导入图片
    background = pygame.image.load("图片/background.png")
    # 新建一个战机对象
    hero = plane.plane(window)
    bad_egg = enemy.enemy(window)
    # 5。刷新   update
    # 电脑每隔多少毫秒时间响应一次
    pygame.key.set_repeat(1, 1)
    while True:
        # 控制函数
        key_control(hero)
        # 敌机运动轨迹:一直往右走
        window.blit(background, (0, 0))
        # 显示战机
        hero.show()
        bad_egg.show()
        # bad_egg.move()
        # 判断碰撞:
        print(hero.x,hero.y)
        print(bad_egg.x,bad_egg.y)
        if hero.x < bad_egg.x<hero.x+120 :
            hero.boom()
        pygame.display.update()
        # time.sleep(0.1)
    pygame.quit()


if __name__ == '__main__':
    main()
import pygame
import bullet
import sys
import time
# 属性:
# 方法:移动

class plane():
    def __init__(self, window):
        self.x = 240
        self.y = 426
        self.skin = pygame.image.load("图片/hero1.png")
        self.window = window
        self.biu = []  # 子弹库
        # 布尔表达式
        self.isboom = False
        self.boom_picture = [] # 爆炸图库

    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.isboom:
            for i in range(4):
                self.window.blit(self.boom_picture[i], (self.x, self.y))
                pygame.display.update()
                time.sleep(0.1)


            pygame.quit()
            sys.exit()
        else:
            self.window.blit(self.skin, (self.x, self.y))
        for zidan in self.biu:
            zidan.show()
            zidan.move()
            print(zidan.x, zidan.y)
            zidan.yuejie()
            if zidan.cross:
                self.biu.remove(zidan)


    def move_left(self):
        self.x -= 10
        if self.x < -50:
            self.x = -50

    def move_right(self):
        self.x += 10
        if self.x > 350:
            self.x = 350

    def move_up(self):
        self.y -= 10
        if self.y < -124:
            self.y = 600

    def move_down(self):
        self.y += 10
        if self.y > 600:
            self.y = -124





    # 键帽
    # 发射子弹
    def fire(self):
        # 创建一个子弹对象
        for i in range(1,100,20):
            self.biu.append(bullet.bullet(self.window, self.x + i, self.y))
            self.biu.append(bullet.bullet(self.window, self.x + 50+i, self.y))

    # 爆炸
    def boom(self):
        self.isboom = True
        self.join_boom_picture()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值