python:简单而简陋的飞机大战游戏

该游戏代码根据某教程编写,未实现音效和游戏终止功能。很多地方写得不好,待优化。安装pygame,即可运行。
界面效果:

python:简单而简陋的飞机大战游戏_第1张图片

简陋的代码:
import pygame
import time
from pygame.locals import *
import random
#基类
class Base(object):
    def __init__(self, screen_temp, x, y, image_name):
        self.x = x
        self.y = y
        self.screen = screen_temp
        self.image = pygame.image.load(image_name)

#飞机基类
class BasePlane(Base):
    def __init__(self, screen_temp, x, y, image_name):
        Base.__init__(self, screen_temp, x, y, image_name)
    #飞机以及子弹展示           
    def display(self, plane):
        self.screen.blit(self.image, (self.x, self.y))
        for b in self.bullet_list:
            b.move()
            b.display()
            if b.judge():
                self.bullet_list.remove(b)
            #根据图片大小调整
            if isinstance(plane, HeroPlane) and b.y >= 540 and b.y <= 600 and b.x >= plane.x + 20  and b.x <= plane.x + 100:
                plane.crash('./feiji/hero_blowup_n3.png')
                self.bullet_list.remove(b)
            elif isinstance(plane, EnemyPlane) and b.y >= 0 and b.y <= 39 and b.x >= plane.x and b.x <= plane.x + 51:
                plane.crash('./feiji/enemy0_down2.png')
                self.bullet_list.remove(b)
    #飞机爆炸
    def crash(self, image_name):
        self.image = pygame.image.load(image_name)

#子弹基类
class BaseBullet(Base):
    def __init__(self, screen_temp, x, y, image_name):
        Base.__init__(self, screen_temp, x, y, image_name)

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

#敌机
class EnemyPlane(BasePlane):
    def __init__(self, screen_temp, x, y, image_name):
        BasePlane.__init__(self, screen_temp, x, y, image_name)
        self.direction = 'right'
        self.bullet_list = []

    #左右移动
    def move(self):
        if self.direction == 'right':
            self.x += 5
        elif self.direction == 'left':
            self.x -= 5
        #移动到边界则往反方向移动
        if self.x > 480 - 50:
            self.direction = 'left'
        elif self.x < 0:
            self.direction = 'right'
    #敌机开火
    def fire(self):
        r = random.randint(1, 100)
        if r == 30 or r == 60 or r == 90:
            self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y, './feiji/bullet1.png'))

#玩家飞机
class HeroPlane(BasePlane):
    def __init__(self, screen_temp, x, y, image_name):
        BasePlane.__init__(self, screen_temp, x, y, image_name)
        self.bullet_list = []
    #飞机左移
    def move_left(self):
        self.x-=5
    #飞机右移
    def move_right(self):
        self.x+=5
    #玩家开火
    def fire(self):
        self.bullet_list.append(Bullet(self.screen, self.x, self.y, './feiji/bullet.png'))

#敌机子弹
class EnemyBullet(BaseBullet):
    def __init__(self, screen_temp, x, y, image_name):
        BaseBullet.__init__(self, screen_temp, x + 25, y + 40, image_name)
    #子弹移动速度
    def move(self):
        self.y += 20
    #判断是否越界
    def judge(self):
        if self.y > 750:
            return True
        else:
            return False

#玩家子弹
class Bullet(BaseBullet):
    def __init__(self, screen_temp, x, y, image_name):
        BaseBullet.__init__(self, screen_temp, x + 40, y - 20, image_name)

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

    def judge(self):
        if self.y < 0:
            return True
        else:
            return False

def key_control(hero):
    #获取事件,比如按键等
    for event in pygame.event.get():
        #判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
            #判断是否是按下了键
        elif event.type == KEYDOWN:
            #检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                hero.move_left()
            #检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                hero.move_right()
                #检测按键是否是空格键
            elif event.key == K_SPACE:
                print('space')
                hero.fire()

def main():
    #创建游戏窗口
    screen = pygame.display.set_mode((480, 750), 0, 32)
    pygame.display.set_caption("飞机大战")
    #创建窗口背景
    background = pygame.image.load('./feiji/background.png')
    #创建玩家飞机
    hero = HeroPlane(screen, 200, 600, './feiji/hero1.png')
    #创建敌机
    enemy = EnemyPlane(screen, 0, 0, './feiji/enemy0.png')
    while True:
        #背景图片位置
        screen.blit(background, (0, 0))
        #玩家飞机展示
        hero.display(enemy)
        enemy.display(hero)
        enemy.move()
        enemy.fire()
        # 整个界面刷新
        pygame.display.update()
        key_control(hero)
        #看个人电脑性能配置
        time.sleep(0.02)

#主程序    
if __name__ == '__main__':
    main()

你可能感兴趣的:(python)