deqin-飞机大战4。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()
        pygame.display.update()
        # time.sleep(0.1)
    pygame.quit()


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

class bullet():
    def __init__(self, window,x,y):
        self.x = x
        self.y = y
        self.skin = pygame.image.load("图片/bullet1.png")
        self.window = window

    def move(self):
        self.y -= 10

    # 放窗口上
    def show(self):
        self.window.blit(self.skin, (self.x, self.y))

import pygame
import bullet

# 属性:
# 方法:移动

class plane():
    def __init__(self, window):
        self.x = 240
        self.y = 426
        self.skin = pygame.image.load("图片/hero1.png")
        self.window = window
        self.biu = []  # 子弹库

    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 show(self):
        self.window.blit(self.skin, (self.x, self.y))
        for i in range(len(self.biu)):
            self.biu[i].show()
            self.biu[i].move()


    # 发射子弹
    def fire(self):
        # 创建一个子弹对象
        self.biu.append(bullet.bullet(self.window,self.x,self.y))



import pygame
import random
class enemy():
    def __init__(self, window):
        self.x = 0
        self.y = 0
        self.skin = pygame.image.load("图片/enemy-1.gif")
        self.window = window

    def move(self):
        self.x += random.randint(1, 2)
        self.y += random.randint(1, 2)

    # 放窗口上
    def show(self):
        self.window.blit(self.skin, (self.x, self.y))


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

猜你喜欢

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