python 飞机大战 实例

飞机大战

#coding=utf-8
import pygame
from pygame.locals import *
import time
import random
class Base(object):
    def __init__(self,x,y,screen,image_name):
        self.x=x
        self.y=y
        self.screen=screen
        self.image=pygame.image.load(image_name).convert()
class BaseBullet(Base):
    def __init__(self,x,y,screen,image_name):
        Base.__init__(self,x,y,screen,image_name)
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
class Bullet(BaseBullet):
    def __init__(self,x,y,screen):
          BaseBullet.__init__(self,x+40, y-20, screen,"C:/Users/lenovo/Desktop/feiji/bullet-3.gif")
    def move(self):
            self.y-=10
    def judge(self):
        if self.y<0:
            return True
        else:
            return False
 #谁发射 谁创建
class EnemyBullet(BaseBullet):
    def __init__(self,x,y,screen):
         BaseBullet.__init__(self,x+25, y+40, screen,"C:/Users/lenovo/Desktop/feiji/bullet1.png")
    def move(self):
            self.y+=5
    def judge(self):
        if self.y>600:
            return True
        else:
            return False
class BasePlane(Base):
    def __init__(self,x,y,screen,image_name):
        Base.__init__(self,x,y,screen,image_name)
        self.bullet_list=[]#存储子弹
    def display(self):
         #更新飞机的位置
        self.screen.blit(self.image,(self.x,self.y))
        #存放需要删除的对象信息
        needDelItemList=[]
        for i in self.bullet_list:
            if i.judge():
                needDelItemList.append(i)
        for i in needDelItemList:
            self.bullet_list.remove(i)
        #更新及这架飞机发射出的所有子弹的位置
        #子弹移动了 判断每一颗子弹和子弹的位置
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()
class HeroPlane(BasePlane):
    def __init__(self,screen):#默认有照片 默认有位置
        BasePlane.__init__(self,200,500,screen,"C:/Users/lenovo/Desktop/feiji/hero.gif")
        self.hit=False #表示是否要爆炸
        self.bomb_list=[]#用来存储爆炸时需要的图片
        self.__create_images()
        self.image_num = 0
     #   用来记录while True的次数,当次数达到一定值时才显示一张爆炸的图,然后清空,,当这个次数再次达到时,再显示下一个爆炸效果的图片
        self.image_index = 0#用来记录当前要显示的爆炸效果的图片的序号
    def __create_images(self):
        self.bomb_list.append(pygame.image.load("C:/Users/lenovo/Desktop/feiji/hero_blowup_n1.png"))
        self.bomb_list.append(pygame.image.load("C:/Users/lenovo/Desktop/feiji/hero_blowup_n2.png"))
        self.bomb_list.append(pygame.image.load("C:/Users/lenovo/Desktop/feiji/hero_blowup_n3.png"))
        self.bomb_list.append(pygame.image.load("C:/Users/lenovo/Desktop/feiji/hero_blowup_n4.png"))
    def display(self):
        if self.hit==True:
            self.screen.blit(self.bomb_list[self.image_index],(self.x,self.y))
            self.image_num+=1
            if self.image_num == 5:
                self.image_num=0
                self.image_index+=1
            if self.image_index>3:
                time.sleep(0.1)
                print("failure")
                exit()
        else:
                self.screen.blit(self.image,(self.x, self.y))
            #存放需要删除的对象信息
        needDelItemList=[]
        for i in self.bullet_list:
            if i.judge():
                needDelItemList.append(i)
        for i in needDelItemList:
            self.bullet_list.remove(i)
        #更新及这架飞机发射出的所有子弹的位置
        #子弹移动了 判断每一颗子弹和子弹的位置
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()
    def bomb(self):
        self.hit = True

    def moveLeft(self):
        self.x-=20
    def moveRight(self):
        self.x+=20
    def moveUp(self):
        self.y-=20
    def moveDown(self):
        self.y+=20
    def fire(self):
        newBullet=Bullet(self.x,self.y,self.screen)
        self.bullet_list.append(newBullet)
class EnemyPlane(BasePlane):
    def __init__(self,screen):
        BasePlane.__init__(self,0,0,screen,"C:/Users/lenovo/Desktop/feiji/enemy0.png")
        self.direction="right"#用来存储飞机默认的显示方向
    def move(self):
        if self.direction=="right":
            self.x+=2
        elif self.direction=="left":
            self.x-=2
        if(self.x>480-50):
            self.direction="left"
        elif(self.x<0):
           self.direction="right"
    def fire(self):
        random_num=random.randint(1,200)
        if random_num==7 or random_num==20:
            self.bullet_list.append(EnemyBullet(self.x, self.y,self.screen))
def key_control(heroPlane):
     for event in pygame.event.get():
            if event.type == QUIT:
                print("exit")
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_a or event.key==K_LEFT:
                    print('left')
                    heroPlane.moveLeft()
                elif event.key == K_d or event.key==K_RIGHT:
                    print('right')
                    heroPlane.moveRight()
                elif event.key == K_w or event.key==K_UP:
                     print('up')
                     heroPlane.moveUp()
                elif event.key ==K_s or event.key==K_DOWN:
                     print('down')
                     heroPlane.moveDown()
                elif event.key == K_SPACE:
                     heroPlane.fire()
                elif event.key == K_b:
                     print('b')
                     heroPlane.bomb()

if __name__=="__main__":
    screen=pygame.display.set_mode((480,600),0, 32)
    background=pygame.image.load("C:/Users/lenovo/Desktop/feiji/background.png").convert()
    heroPlane=HeroPlane(screen)
    enemy=EnemyPlane(screen)
    while True:
        screen.blit(background,(0,0))
        heroPlane.display()
        enemy.display()#让敌机显示
        enemy.move()#调用敌机的move方法
        enemy.fire()#让敌机开火
        pygame.display.update()
        key_control(heroPlane)
        time.sleep(0.01)    

for循环的坑

(防止列表循环的时候删自己列表元素出现bug)
==不能边遍历边删==
是指不能删自己循环的列表,可以删其他人

for 循环遍历一个列表的时候删除一个元素是有坑的

刚好指向下一个元素

11 22 33 删除了 33 ,44刚好进一位(补上),所以44没有删掉

==把谁要删的记下来==

a=[11,22,33,44,55]
b=[]
for i in a:
    if i=33 or i=44:
        b.append(i)
for i in b:
        a.remove(i)
print(a)

原文地址:https://www.cnblogs.com/is-Tina/p/8337929.html

时间: 2024-08-30 10:13:22

python 飞机大战 实例的相关文章

Python飞机大战实例有感——pygame如何实现“切歌”以及多曲重奏?

目录 pygame如何实现"切歌"以及多曲重奏? 一.pygame实现切歌 初始化路径 尝试一 尝试二 尝试三 成功 总结 二.如何在python多线程顺序执行的情况下实现音乐和音效同时播放? 尝试一 尝试二 尝试三 尝试四 成功 总结 pygame如何实现"切歌"以及多曲重奏? 昨天晚上研究了好久pygame的音乐混合器mixer,出了很多问题后最终成功,不过学习本来也不可能一帆风顺的吗,下面我就来讲一讲我遇到的问题. 一.pygame实现切歌 初始化路径 # 导

小甲鱼python基础教程飞机大战源码及素材

百度了半天小甲鱼python飞机大战的源码和素材,搜出一堆不知道是什么玩意儿的玩意儿. 最终还是自己对着视频一行行代码敲出来. 需要的同学点下面的链接自取. 下载 原文地址:https://www.cnblogs.com/144823836yj/p/10162920.html

Python版飞机大战

前面学了java用java写了飞机大战这次学完python基础后写了个python版的飞机大战,有兴趣的可以看下. 父类是飞行物类是所有对象的父类,setting里面是需要加载的图片,你可以换称自己的喜欢的图片,敌机可以分为敌机和奖励,enemy为普通敌人的父类,award为奖励敌机的父类. 各个类的基本属性 主类的大概逻辑 具体的代码: settings配置 import pygame class Settings(object): """设置常用的属性"&quo

【python】步骤四 第二课、实现飞机大战

第二课.实现飞机大战 一.项目介绍 项目实战:飞机大战 课程目标 掌握面向对象分析和开发的思想 能对项目进行拆分,进行模块化开发 了解项目开发的基本流程 理解并运用python的包.模块相关知识 理解并运用文件读写,函数式编程 理解简单2D游戏开发的基本思路 能独立开发简单的2D游戏项目 掌握IDE的调试技巧 项目功能模块 我方飞机 敌方小型飞机 敌方中型飞机 图片资源 音效资源 游戏历史 子弹 游戏展示结果 所需技能点 python的基础,包括变量.字符串.分支.条件控制.循环等 python

基于Cocos2d-x-1.0.1的飞机大战游戏开发实例(下)

在飞机大战游戏开发中遇到的问题和解决方法: 1.在添加菜单时,我要添加一个有背景的菜单,需要在菜单pMenu中添加一个图片精灵,结果编译过了但是运行出错,如下图: 查了很多资料,调试了很长时间,整个人都要崩溃了. 最后发现引擎中CCMenu::itemForTouch函数中有遍历子节点的行为,但是循环中没有判断子节点类型是否为CCMenuItem.如图:码,这样一来,加入到pMenu中的图片精灵被当作菜单项取了出来使用,导致报错.老版本的果然又不完善的地方,整个人都不好了...果断修改引擎里的源

js实例--飞机大战

<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>飞机大战</title> <style> #did{ width:500px;height:500px; background:url("./images/bg2.png") no-repeat 0px -1036px; position:relative;ove

基于Cocos2d-x-1.0.1的飞机大战游戏开发实例(中)

接<基于Cocos2d-x-1.0.1的飞机大战游戏开发实例(上)> 三.代码分析 1.界面初始化 1 bool PlaneWarGame::init() 2 { 3 bool bRet = false; 4 do 5 { 6 CC_BREAK_IF(! CCLayer::init()); 7 8 _size = CCDirector::sharedDirector()->getWinSize(); 9 10 // 设置触摸可用 11 this->setIsTouchEnabled

基于Cocos2d-x-1.0.1的飞机大战游戏开发实例(上)

最近接触过几个版本的cocos2dx,决定每个大变动的版本都尝试一下.本实例模仿微信5.0版本中的飞机大战游戏,如图: 一.工具 1.素材:飞机大战的素材(图片.声音等)来自于网络 2.引擎:cocos2d-1.0.1-x-0.9.2 3.环境:vs2010 二.使用的类 1.游戏菜单界面类:PlaneWarMenu——派生自CCLayer类. 1 // 游戏菜单界面类 2 class PlaneWarMenu: public CCLayer 3 { 4 public: 5 virtual bo

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

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