python-飞机大战

效果图

main.py

import time

import pygame

from EnemyPlane import EnemyPlane
from HeroPlane import HeroPlane
from KeyControl import key_control


def main():
    screen = pygame.display.set_mode((515, 960), 0, 32)
    background = pygame.image.load("./images/background.png")

    hero = HeroPlane(screen)
    enemy = EnemyPlane(screen)

    right = 0
    down = 0
    while True:
        screen.blit(background, (0, 0))

        hero.display()
        hero.move(right, down)

        enemy.display()
        enemy.move()
        enemy.fire()

        pygame.display.update()

        right, down = key_control(hero, right, down)

        time.sleep(0.01)


if __name__ == "__main__":
    main()

Base.py

import pygame


# 加载素材
class Base(object):
    def __init__(self, screen_temp, x, y, image_path):
        self.x = x
        self.y = y
        self.screen = screen_temp
        self.image = pygame.image.load(image_path)

BasePlane.py

from Base import Base


# 飞机基类
class BasePlane(Base):
    def __init__(self, screen_temp, x, y, image_path):
        super().__init__(screen_temp, x, y, image_path)
        self.bullet_list = []

    # 展示飞机
    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

        # 循环展示子弹
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()
            # 判断子弹是否越界
            if bullet.judge():
                self.bullet_list.remove(bullet)

BaseBullet.py

from Base import Base


# 子弹基类
class BaseBullet(Base):

    # 展示子弹
    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

HeroPlane.py

from BasePlane import BasePlane
from HeroBullet import HeroBullet


# 英雄飞机类
class HeroPlane(BasePlane):
    def __init__(self, screen_temp):
        super().__init__(screen_temp, 204, 800, "./images/me.png")

    def move(self, x_right, y_down):
        self.x += x_right
        self.y += y_down

    # 开火
    def fire(self):
        # 子弹列表创建子弹对象
        self.bullet_list.append(HeroBullet(self.screen, self.x, self.y))

EnemyPlane.py

import random

from BasePlane import BasePlane
from EnemyBullet import EnemyBullet


# 敌机类
class EnemyPlane(BasePlane):
    def __init__(self, screen_temp):
        super().__init__(screen_temp, 0, 0, "./images/e0.png")
        self.direction = "right"

    # 左右移动
    def move(self):
        if self.direction == "right":
            self.x += 5
        elif self.direction == "left":
            self.x -= 5

        # 改变移动方向
        if self.x > 399:
            self.direction = "left"
        elif self.x < 0:
            self.direction = "right"

    # 开火
    def fire(self):
        # 子弹列表创建子弹对象
        random_num = random.randint(1, 100)
        if random_num == 25 or random_num == 75:
            self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y))

HeroBullet.py

from BaseBullet import BaseBullet


# 子弹类
class HeroBullet(BaseBullet):
    def __init__(self, screen_temp, x, y):
        super().__init__(screen_temp, x + 53, y - 20, "./images/pd.png")

    # 子弹移动
    def move(self):
        self.y -= 10

    # 判断子弹是否越界
    def judge(self):
        if self.y < 0:
            return True
        else:
            return False

EnemyBullet.py

from BaseBullet import BaseBullet


# 敌机子弹
class EnemyBullet(BaseBullet):
    def __init__(self, screen_temp, x, y):
        super().__init__(screen_temp, x + 53, y + 82, "./images/epd.png")

    # 子弹移动
    def move(self):
        self.y += 10

    # 判断子弹是否越界
    def judge(self):
        if self.y > 960:
            return True
        else:
            return False

KeyControl.py

import pygame
from pygame.locals import *


# 按键控制
def key_control(hero_temp, right, down):
    for event in pygame.event.get():
        if event.type == QUIT:
            print("exit")
            exit()
        elif event.type == KEYUP:
            print("keyup")
            right = 0
            down = 0
        elif event.type == KEYDOWN:
            # 按左键
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                right = -10
            # 按右键
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                right = 10
            # 按上键
            elif event.key == K_w or event.key == K_UP:
                print('up')
                down = -10
            # 按下键
            elif event.key == K_s or event.key == K_DOWN:
                print("down")
                down = 10
            # 按空格键
            elif event.key == K_SPACE:
                print('space')
                hero_temp.fire()
    return right, down

最后在Main.py里运行即可

注意点:

1.py文件名和里面的类名不需要相同,py文件里可以放class,也可以只有一个函数,这点和java非常不一样

2.py文件开头导包的时候必须是  from EnemyPlane import EnemyPlane (导入XXX文件里的某某内容) ,不能只写 import EnemyPlane

posted @ 2018-11-17 18:36  嘉禾世兴  阅读(542)  评论(0编辑  收藏  举报