python飞机大战源代码(可直接运行)_飞机大战python源代码-程序员宅基地

技术标签: python  fjdz  

喜欢的点个赞支持一下哦

联系方式见评论区--------------欢迎大家一起探讨-----------------------------------------------------------------

 

具体的代码:
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")# 英雄机的子弹
飞行物类
import abc




class FlyingObject(object):
    """飞行物类,基类"""


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


    @abc.abstractmethod
    def outOfBounds(self):
        """检查是否越界"""
        pass


    @abc.abstractmethod
    def step(self):
        """飞行物移动一步"""
        pass


    def shootBy(self, bullet):
        """检查当前飞行物是否被子弹bullet(x,y)击中"""
        x1 = self.x
        x2 = self.x + self.width
        y1 = self.y
        y2 = self.y + self.height
        x = bullet.x
        y = bullet.y
        return x > x1 and x < x2 and y > y1 and y < y2


    def blitme(self):
        """打印飞行物"""
        self.screen.blit(self.image, (self.x, self.y))
英雄机
from flyingObject import FlyingObject
from bullet import Bullet
import pygame




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


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


    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 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, image):


        x = random.randint(0, screen.get_rect()[2] - 50)
        y = -40
        super(Airplane, self).__init__(screen, x, y, image)


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


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


        return self.y < 715


    def step(self):
        """移动"""
        self.y += 3  # 移动步数
Bee
import random
from flyingObject import FlyingObject
from award import Award




class Bee(FlyingObject, Award):


    def __init__(self, screen, image):
        x = random.randint(0, screen.get_rect()[2] - 60)
        y = -50
        super(Bee, self).__init__(screen, x, y, image)
        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
主类
```python
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的文本书写是没有换行,得在新建一个对象去写,为了简单我把文本都书写在了一行。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/caozewei/article/details/81015192

智能推荐

前端开发之vue-grid-layout的使用和实例-程序员宅基地

文章浏览阅读1.1w次,点赞7次,收藏34次。vue-grid-layout的使用、实例、遇到的问题和解决方案_vue-grid-layout

Power Apps-上传附件控件_powerapps点击按钮上传附件-程序员宅基地

文章浏览阅读218次。然后连接一个数据源,就会在下面自动产生一个添加附件的组件。把这个控件复制粘贴到页面里,就可以单独使用来上传了。插入一个“编辑”窗体。_powerapps点击按钮上传附件

C++ 面向对象(Object-Oriented)的特征 & 构造函数& 析构函数_"object(cnofd[\"ofdrender\"])十条"-程序员宅基地

文章浏览阅读264次。(1) Abstraction (抽象)(2) Polymorphism (多态)(3) Inheritance (继承)(4) Encapsulation (封装)_"object(cnofd[\"ofdrender\"])十条"

修改node_modules源码,并保存,使用patch-package打补丁,git提交代码后,所有人可以用到修改后的_修改 node_modules-程序员宅基地

文章浏览阅读133次。删除node_modules,重新npm install看是否成功。在 package.json 文件中的 scripts 中加入。修改你的第三方库的bug等。然后目录会多出一个目录文件。_修改 node_modules

【】kali--password:su的 Authentication failure问题,&sudo passwd root输入密码时Sorry, try again._password: su: authentication failure-程序员宅基地

文章浏览阅读883次。【代码】【】kali--password:su的 Authentication failure问题,&sudo passwd root输入密码时Sorry, try again._password: su: authentication failure

整理5个优秀的微信小程序开源项目_微信小程序开源模板-程序员宅基地

文章浏览阅读1w次,点赞13次,收藏97次。整理5个优秀的微信小程序开源项目。收集了微信小程序开发过程中会使用到的资料、问题以及第三方组件库。_微信小程序开源模板

随便推点

Centos7最简搭建NFS服务器_centos7 搭建nfs server-程序员宅基地

文章浏览阅读128次。Centos7最简搭建NFS服务器_centos7 搭建nfs server

Springboot整合Mybatis-Plus使用总结(mybatis 坑补充)_mybaitis-plus ruledataobjectattributemapper' and '-程序员宅基地

文章浏览阅读1.2k次,点赞2次,收藏3次。前言mybatis在持久层框架中还是比较火的,一般项目都是基于ssm。虽然mybatis可以直接在xml中通过SQL语句操作数据库,很是灵活。但正其操作都要通过SQL语句进行,就必须写大量的xml文件,很是麻烦。mybatis-plus就很好的解决了这个问题。..._mybaitis-plus ruledataobjectattributemapper' and 'com.picc.rule.management.d

EECE 1080C / Programming for ECESummer 2022 Laboratory 4: Global Functions Practice_eece1080c-程序员宅基地

文章浏览阅读325次。EECE 1080C / Programming for ECESummer 2022Laboratory 4: Global Functions PracticePlagiarism will not be tolerated:Topics covered:function creation and call statements (emphasis on global functions)Objective:To practice program development b_eece1080c

洛谷p4777 【模板】扩展中国剩余定理-程序员宅基地

文章浏览阅读53次。被同机房早就1年前就学过的东西我现在才学,wtcl。设要求的数为\(x\)。设当前处理到第\(k\)个同余式,设\(M = LCM ^ {k - 1} _ {i - 1}\) ,前\(k - 1\)个的通解就是\(x + i * M\)。那么其实第\(k\)个来说,其实就是求一个\(y\)使得\(x + y * M ≡ a_k(mod b_k)\)转化一下就是\(y * M ...

android 退出应用没有走ondestory方法,[Android基础论]为何Activity退出之后,系统没有调用onDestroy方法?...-程序员宅基地

文章浏览阅读1.3k次。首先,问题是如何出现的?晚上复查代码,发现一个activity没有调用自己的ondestroy方法我表示非常的费解,于是我检查了下代码。发现再finish代码之后接了如下代码finish();System.exit(0);//这就是罪魁祸首为什么这样写会出现问题System.exit(0);////看一下函数的原型public static void exit (int code)//Added ..._android 手动杀死app,activity不执行ondestroy

SylixOS快问快答_select函数 导致堆栈溢出 sylixos-程序员宅基地

文章浏览阅读894次。Q: SylixOS 版权是什么形式, 是否分为<开发版税>和<运行时版税>.A: SylixOS 是开源并免费的操作系统, 支持 BSD/GPL 协议(GPL 版本暂未确定). 没有任何的运行时版税. 您可以用她来做任何 您喜欢做的项目. 也可以修改 SylixOS 的源代码, 不需要支付任何费用. 当然笔者希望您可以将使用 SylixOS 开发的项目 (不需要开源)或对 SylixOS 源码的修改及时告知笔者.需要指出: SylixOS 本身仅是笔者用来提升自己水平而开发的_select函数 导致堆栈溢出 sylixos

推荐文章

热门文章

相关标签