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()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章