Python版飞机大战

前面学了java用java写了飞机大战这次学完python基础后写了个python版的飞机大战,有兴趣的可以看下。

  父类是飞行物类是所有对象的父类,setting里面是需要加载的图片,你可以换称自己的喜欢的图片,敌机可以分为敌机和奖励,enemy为普通敌人的父类,award为奖励敌机的父类。

各个类的基本属性

主类的大概逻辑

具体的代码:

settings配置

import pygame

class Settings(object):
    """设置常用的属性"""

    def __init__(self):
        self.bgImage = pygame.image.load('img/background.png')  # 背景图

        self.bgImageWidth = self.bgImage.get_rect()[2]  # 背景图宽
        self.bgImageHeight = self.bgImage.get_rect()[3]  # 背景图高
        self.start=pygame.image.load("img/start.png")
        self.pause=pygame.image.load("img/pause.png")
        self.gameover=pygame.image.load("img/gameover.png")
        self.heroImages = ["img/hero.gif",
                           "img/hero1.png", "img/hero2.png"]  # 英雄机图片
        self.airImage = pygame.image.load("img/enemy0.png") # airplane的图片
        self.beeImage = pygame.image.load("img/bee.png") # bee的图片
        self.heroBullet=pygame.image.load("img/bullet.png")# 英雄机的子弹

英雄机

from flyingObject import FlyingObject
from bullet import Bullet
import pygame

class Hero(FlyingObject):
    """英雄机"""
    index = 2  # 标志位
    def __init__(self, screen, images):

        self.screen = screen
        self.life = 3  # 生命值为3
        self.doubleFire = 0  # 初始火力值为0
        self.images = images  # 英雄级图片数组,为Surface实例
        self.image = pygame.image.load(images[0])
        self.width = self.image.get_rect()[2]
        self.height = self.image.get_rect()[3]
        self.x = screen.get_rect().centerx - self.width / 2
        self.y = screen.get_rect().bottom + self.height

    def isDoubleFire(self):
        """获取双倍火力"""
        return self.doubleFire

    def setDoubleFire(self):
        """设置双倍火力"""
        self.doubleFire = 40

    def addDoubleFire(self):
        """增加火力值"""
        self.doubleFire += 100
    def clearDoubleFire(self):
        """清空火力值"""
        self.doubleFire=0

    def addLife(self):
        """增命"""
        self.life += 1

    def sublife(self):
        """减命"""
        self.life-=1

    def getLife(self):
        """获取生命值"""
        return self.life
    def reLife(self):
        self.life=3
        self.clearDoubleFire()

    def blitme(self):
        """绘制英雄机"""
        self.screen.blit(self.image, (self.x, self.y))

    def outOfBounds(self):
        return False

    def step(self):
        """动态显示飞机"""
        if(len(self.images) > 0):
            Hero.index += 1
            Hero.index %= len(self.images)
            self.image = pygame.image.load(self.images[int(Hero.index)])  # 切换图片

    def move(self, x, y):
        self.x = x - self.width / 2
        self.y = y - self.height / 2

    def shoot(self,image):
        """英雄机射击"""
        xStep=int(self.width/4-5)
        yStep=20
        if self.doubleFire>=100:
            heroBullet=[Bullet(self.screen,image,self.x+1*xStep,self.y-yStep),Bullet(self.screen,image,self.x+2*xStep,self.y-yStep),Bullet(self.screen,image,self.x+3*xStep,self.y-yStep)]
            self.doubleFire-=3
            return heroBullet
        elif self.doubleFire<100 and self.doubleFire > 0:
            heroBullet=[Bullet(self.screen,image,self.x+1*xStep,self.y-yStep),Bullet(self.screen,image,self.x+3*xStep,self.y-yStep)]
            self.doubleFire-=2
            return heroBullet
        else:
            heroBullet=[Bullet(self.screen,image,self.x+2*xStep,self.y-yStep)]
            return heroBullet

    def hit(self,other):
        """英雄机和其他飞机"""
        x1=other.x-self.width/2
        x2=other.x+self.width/2+other.width
        y1=other.y-self.height/2
        y2=other.y+self.height/2+other.height
        x=self.x+self.width/2
        y=self.y+self.height
        return x>x1 and x<x2 and y>y1 and y<y2

enemys

import abc

class Enemy(object):
    """敌人,敌人有分数"""
    @abc.abstractmethod
    def getScore(self):
        """获得分数"""
        pass

award

import abc

class Award(object):
    """奖励"""
    DOUBLE_FIRE = 0
    LIFE = 1

    @abc.abstractmethod
    def getType(self):
        """获得奖励类型"""
        pass

if __name__ == '__main__':

    print(Award.DOUBLE_FIRE)

airplane

import random
from flyingObject import FlyingObject
from enemy import Enemy

class Airplane(FlyingObject, Enemy):
    """普通敌机"""

    def __init__(self, screen, airImage):
        self.screen = screen
        self.image = airImage
        self.width = self.image.get_rect()[2]
        self.height = self.image.get_rect()[3]
        self.x = random.randint(0, screen.get_rect()[2] - self.width)
        self.y = -self.height

    def getScore(self):
        """获得的分数"""
        return 5

    def outOfBounds(self):
        """是否越界"""

        return self.y < 715

    def step(self):
        """移动"""
        self.y += 3  # 移动步数

    def blitme(self):
        """打印飞行物"""
        self.screen.blit(self.image, (self.x, self.y))

Bee

import random
from flyingObject import FlyingObject
from award import Award

class Bee(FlyingObject, Award):

    def __init__(self, screen, beeImage):
        self.screen = screen
        self.image = beeImage
        self.width = self.image.get_rect()[2]
        self.height = self.image.get_rect()[3]
        self.x = random.randint(0, screen.get_rect()[2] - self.width)
        self.y = -self.height
        self.awardType = random.randint(0, 1)
        self.index = True

    def outOfBounds(self):
        """是否越界"""
        return self.y < 715

    def step(self):
        """移动"""
        if self.x + self.width > 480:
            self.index = False
        if self.index == True:
            self.x += 3
        else:
            self.x -= 3
        self.y += 3  # 移动步数

    def getType(self):
        return self.awardType

    def blitme(self):
        """打印飞行物"""
        self.screen.blit(self.image, (self.x, self.y))

主类

import pygame
import sys
import random
from setting import Settings
from hero import Hero
from airplane import Airplane
from bee import Bee
from enemy import Enemy
from award import Award

START=0
RUNNING=1
PAUSE=2
GAMEOVER=3
state=START
sets = Settings()
screen = pygame.display.set_mode(
        (sets.bgImageWidth, sets.bgImageHeight), 0, 32) #创建窗口
hero=Hero(screen,sets.heroImages)
flyings=[]
bullets=[]
score=0
def hero_blitme():
    """画英雄机"""
    global hero
    hero.blitme()

def bullets_blitme():
    """画子弹"""
    for b in bullets:
        b.blitme()

def flyings_blitme():
    """画飞行物"""
    global sets
    for fly in flyings:
        fly.blitme()

def score_blitme():
    """画分数和生命值"""
    pygame.font.init()
    fontObj=pygame.font.Font("SIMYOU.TTF", 20) #创建font对象
    textSurfaceObj=fontObj.render(u'生命值:%d\n分数:%d\n火力值:%d'%(hero.getLife(),score,hero.isDoubleFire()),False,(135,100,184))
    textRectObj=textSurfaceObj.get_rect()
    textRectObj.center=(300,40)
    screen.blit(textSurfaceObj,textRectObj)

def state_blitme():
    """画状态"""
    global sets
    global state
    if state==START:
        screen.blit(sets.start, (0,0))
    elif state==PAUSE:
        screen.blit(sets.pause,(0,0))
    elif state== GAMEOVER:
        screen.blit(sets.gameover,(0,0))

def blitmes():
    """画图"""
    hero_blitme()
    flyings_blitme()
    bullets_blitme()
    score_blitme()
    state_blitme()

def nextOne():
    """生成敌人"""
    type=random.randint(0,20)
    if type<4:
        return Bee(screen,sets.beeImage)
    elif type==5:
        return Bee(screen,sets.beeImage) #本来准备在写几个敌机的,后面没找到好看的图片就删了
    else:
        return Airplane(screen,sets.airImage)

flyEnteredIndex=0
def enterAction():
    """生成敌人"""
    global flyEnteredIndex
    flyEnteredIndex+=1
    if flyEnteredIndex%40==0:
        flyingobj=nextOne()
        flyings.append(flyingobj)

shootIndex=0
def shootAction():
    """子弹入场,将子弹加到bullets"""
    global shootIndex
    shootIndex +=1
    if shootIndex % 10 ==0:
        heroBullet=hero.shoot(sets.heroBullet)
        for bb in heroBullet:
            bullets.append(bb)

def stepAction():
    """飞行物走一步"""

    hero.step()
    for flyobj in flyings:
        flyobj.step()
    global bullets
    for b in bullets:
        b.step()

def outOfBoundAction():
    """删除越界的敌人和飞行物"""
    global flyings
    flyingLives=[]
    index=0
    for f in flyings:
        if f.outOfBounds()==True:
            flyingLives.insert(index,f)
            index+=1
    flyings=flyingLives
    index=0
    global bullets
    bulletsLive=[]
    for b in bullets:
        if b.outOfBounds()==True:
            bulletsLive.insert(index,b)
            index+=1
    bullets=bulletsLive

j=0
def bangAction():
    """子弹与敌人碰撞"""
    for b in bullets:
        bang(b)

def bang(b):
    """子弹与敌人碰撞检测"""
    index=-1
    for x in range(0,len(flyings)):
        f=flyings[x]
        if f.shootBy(b):
            index=x
            break
    if index!=-1:
        one=flyings[index]
        if isinstance(one,Enemy):
            global score
            score+=one.getScore() # 获得分数
            flyings.remove(one) # 删除

        if isinstance(one,Award):
            type=one.getType()
            if type==Award.DOUBLE_FIRE:
                hero.addDoubleFire()
            else:
                hero.addLife()
            flyings.remove(one)

        bullets.remove(b)

def checkGameOverAction():
    if isGameOver():
        global state
        state=GAMEOVER
        hero.reLife()

def isGameOver():
    for f in flyings:
        if hero.hit(f):
            hero.sublife()
            hero.clearDoubleFire()
            flyings.remove(f)

    return hero.getLife()<=0

def action():
    x, y = pygame.mouse.get_pos()

    blitmes()  #打印飞行物
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            flag=pygame.mouse.get_pressed()[0] #左键单击事件
            rflag=pygame.mouse.get_pressed()[2] #右键单击事件
            global state
            if flag==True and (state==START or state==PAUSE):
                state=RUNNING
            if flag==True and state==GAMEOVER:
                state=START
            if rflag==True:
                state=PAUSE

    if state==RUNNING:
        hero.move(x,y)
        enterAction()
        shootAction()
        stepAction()
        outOfBoundAction()
        bangAction()
        checkGameOverAction()

def main():

    pygame.display.set_caption("飞机大战")

    while True:
        screen.blit(sets.bgImage, (0, 0))  # 加载屏幕

        action()
        pygame.display.update()  # 重新绘制屏幕
        # time.sleep(0.1)       # 过0.01秒执行,减轻对cpu的压力

if __name__ == '__main__':
    main()

 写这个主要是练习一下python,把基础打实些。pygame的文本书写是没有换行,得在新建一个对象去写,为了简单我把文本都书写在了一行。

background.png

hero.gif

bee.png

enemy.png

gameover.png

start.png

时间: 2024-08-17 18:15:33

Python版飞机大战的相关文章

豪华版飞机大战系列(一)

鉴于最近在学习cocos2d-x开发手游,对于学习过程中的一些东西做个总结,也记录下学习历程,同时分享些项目源码来和大家一起学习. 第一次写系列教程,可能中间有疏漏的,看到的还请给提个醒,不好的也多多吐槽,以便自己能更好的以后的开发中基类经验. 此次教程分享下豪华版的飞机大战,老规矩,先上图: 介绍下开发环境:cocos2d-x3.2 alpha + Ubuntu14.04 + eclipse + 命令行终端 + android 用的引擎为3.2版本的,3.0以上的应该都能运行跑下来,windo

python学习——飞机大战之初期

在开始正式编写飞机大战游戏之前,对pygame所提供的模块进行学习,以下代码只是验证了一些功能,并不能进行飞机大战游戏. 在开始正式的编写代码之前一定一定要先调用pygame.init()方法,并相应的调用pygame.quit()方法,这里要养成好的习惯,把成对出现的代码一并写好,以免最后忘记写,而导致错误,然后在pygame.init()与pygame.quit()之间写游戏代码. import pygame #导入pygame模块,pygame是专门设计用来设计游戏的python模块. f

python开发飞机大战

1.使用pygame包,使用Python3.5版本2.达到的效果是: 出现一个窗口,显示一张背景图片,出现一架敌机和一架自己的飞机 敌机在最上面,左右移动,随机发子弹,自己的飞机使用键盘左右键左右移动,使用空格键发子弹 当自己的飞机发出的子弹碰到敌机发出的子弹时,敌机的子弹消失 当自己的飞机发出的子弹碰到敌机时,敌机原地摧毁后,再次出现开始游戏 当敌机发出的子弹碰到自己的飞机时,飞机原地摧毁,并退出游戏 3.主要涉及,飞机运动,子弹检测,子弹碰撞检测等,使用面向对象的思路 import pyga

豪华版飞机大战系列(四)

上一篇介绍了敌人类EnemySprite的实现,这篇来介绍下主角类的实现.在游戏中可以看到主角类的周围有一直在飞行的小猪,时刻跟在主角飞机的旁边,我们先介绍下PigSprite的实现,因为后面的主角飞机类要用到此类. 看PigSprite.h的实现: #include "cocos2d.h" USING_NS_CC; class PigSprite : public cocos2d::Sprite { public: PigSprite(); ~PigSprite(); virtual

豪华版飞机大战系列(六)--附源代码

最后一篇讲一下游戏中的主要逻辑推断,在上面的工作都做充分准备后,游戏主要逻辑将变得特别清晰,接下来你会看到全部的逻辑都是那么的清晰自然,由于前面已经做好了充分的准备工作,这里仅仅是整合了前面的工作,略微增加了一些游戏推断元素. 同一时候源代码会在文章最后给出链接地址,源代码托管在github上,全部的东西都是开源免费的,在如今的大环境下.开源才是王道,分享才干双赢,我始终认为这是对的.你有一种思想我有一种思想,交流分享后我们都有了两种思想,何乐而不为呢. 好了,回归正题.游戏主要推断逻辑都在Ga

豪华版飞机大战系列(六)--附源码

最后一篇讲一下游戏中的主要逻辑判断,在上面的工作都做充分准备后,游戏主要逻辑将变得特别清晰,接下来你会看到所有的逻辑都是那么的清晰自然,因为前面已经做好了充分的准备工作,这里只是整合了前面的工作,稍微加入了一些游戏判断元素. 同时源码会在文章最后给出链接地址,源码托管在github上,所有的东西都是开源免费的,在现在的大环境下,开源才是王道,分享才能双赢,我始终觉得这是对的,你有一种思想我有一种思想,交流分享后我们都有了两种思想,何乐而不为呢. 好了,回归正题,游戏主要判断逻辑都在GameSce

豪华版飞机大战系列(五)

在介绍了前面的几篇后,对于源代码会在下一篇中上传,须要源代码的能够在下一篇中看到下载链接,开源才是王道,分享才干成长. 这篇继续介绍下游戏中的子弹层的渲染.也就是BulletSprite的介绍. 对于子弹层的渲染有两种机制.一种是直接从缓存中进行精灵创建,创建后的精灵直接加入并使用,另外一种机制为将创建的精灵加入到SpriteBatchNode中,这样进行渲染效率更高.对于这样渲染的机制我在这里略微提一下, 普通的渲染机制为:准备,渲染,清除.准备,渲染,清除.... 批次渲染机制为:准备,渲染

python项目飞机大战

实现步骤 1.创建窗口 2.创建一个玩家飞机,按方向键可以左右移动 3.给玩家飞机添加按空格键发射子弹功能 4.创建一个敌机 5.敌机自动左右移动 6.敌机自动发射子弹 1.创建窗口 import pygame import time def main(): #1.创建窗口 screen = pygame.display.set_mode((480,852)) #2 创建一个背景图片 background = pygame.image.load('./feiji/background.png')

豪华版飞机大战系列(二)

既上一篇介绍了游戏的主要概况下,这篇来开始讲一下游戏中的各个文件. 先来看看cocos2d-x 3.0 中一个比较赞的功能,比起3.0以前的要令人非常激动的.虽说3.0出来很久了,我还是说下这个功能,知道的飘过. 对于在不同环境下用cocos2d-x开发手游,屏幕尺寸是一个比较蛋疼的问题,比如在3.0以前,在代码中修改屏幕尺寸还是比较麻烦的,而且在电脑上运行良好的尺寸到手机端感觉效果就差了一点. AppDelegate.cpp是整个游戏的入口程序,3.0以前设置游戏尺寸时主要代码如下: bool