前一段在数据分析中突然感觉到了一阵阵的空虚寂寞冷,所以我决定小小放松一下,于是萌生出了写一个小游戏的想法。所以在pygame中摸索了2天,终于完成了无聊的飞机大战代码。之前从来没写过游戏,所以感觉还蛮好玩儿的。在此分享出来,有兴趣的可以拿去玩玩儿咯!!

游戏完成的主要功能:

①用户飞机由用户控制;敌机自动移动。

②用户飞机由用户按空格键开火;敌机自动开火。

③击毁敌机会使敌机子弹速度逐渐增加,以达到逐渐增加游戏困难的目的。

④加入飞机坠毁的五毛特效。

⑤加入bonus功能,吃到bonus可以增加用户子弹速度,并减缓敌机子弹速度。

具体代码如下:

#coding=utf-8import pygame
import time
from pygame.locals import *
import randomkeydown_list = [0]class Bullet(object):def __init__(self, screen, x, y, image):self.x = x self.y = yself.image = pygame.image.load(image)self.screen = screendef blit(self):              #放置玩家飞机的子弹图片self.screen.blit(self.image, (self.x, self.y))   class userBullet(Bullet):def __init__(self, screen, x, y):Bullet.__init__(self, screen, x + 40, y - 20, "./feiji/bullet.png")def blit(self):                #放置玩家飞机的子弹图片Bullet.blit(self)      def move(self, move_variable):self.y -= move_variabledef judge(self):               #判断子弹是否越界if self.y < -50:return Falseelse:return Trueclass enemyBullet(Bullet):def __init__(self, screen, x, y):Bullet.__init__(self, screen, x + 30, y + 45, "./feiji/bullet1.png")def blit(self):              #放置玩家飞机的子弹图片Bullet.blit(self)def move(self, bullet_move_variable):                  #敌机子弹移动self.y += bullet_move_variabledef judge(self):             #判断子弹是否越界if self.y > 800:return Falseelse:return Trueclass Bonus(object):def __init__(self, screen, x, y, image):self.x = xself.y = yself.image = pygame.image.load(image)self.screen = screendef blit(self):self.screen.blit(self.image,(self.x, self.y))def move(self):self.x += random.randint(-5,4)self.y += 5def missed(self):if (self.y > 700):return 1         class Plane(object):def __init__(self, screen, x, y, image):self.x = xself.y = yself.image = pygame.image.load(image)      #导入玩家飞机图片self.screen = screenself.bullet_list = []self.destroy_director = 0def blit(self, bullet_move_variable):   #放置玩家飞机方法  Notice:飞机发射的炸弹,所以fire()以后在飞机的blit中调用并且移动self.screen.blit(self.image, (self.x, self.y)) for bullet in self.bullet_list:bullet.blit() bullet.move(bullet_move_variable)if not bullet.judge():self.bullet_list.remove(bullet)class user_Plane(Plane):def __init__(self, screen):Plane.__init__(self,  screen, 190, 550, "./feiji/hero1.png")def blit(self, bullet_move_variable):   #放置玩家飞机方法  Notice:飞机发射的炸弹,所以fire()以后在飞机的blit中调用并且移动Plane.blit(self, bullet_move_variable)def fire(self):          #玩家开火self.bullet_list.append(userBullet(self.screen, self.x, self.y))def destroyed(self, enemy_Plane):if self.destroy_director == 0:for bullet in enemy_Plane.bullet_list:if (bullet.x > self.x and bullet.x < (self.x + 100)) and (bullet.y > self.y and bullet.y < (self.y + 120)):enemy_Plane.bullet_list.remove(bullet)self.image = pygame.image.load("./feiji/hero_blowup_n1.png")self.destroy_director = 1else:self.destroy_director += 1if self.destroy_director == 4:self.image = pygame.image.load("./feiji/hero_blowup_n2.png")elif self.destroy_director == 8:self.image = pygame.image.load("./feiji/hero_blowup_n3.png")elif self.destroy_director == 12:self.image = pygame.image.load("./feiji/hero_blowup_n4.png")elif self.destroy_director == 16:exit()def getBonus(self, bonus):if (bonus.x > self.x and bonus.x < (self.x + 100)) and (bonus.y > self.y and bonus.y < (self.y + 120)):del bonusreturn 1def __del__(self):print "Game Over"class enemy_Plane(Plane):def __init__(self, screen):randx = random.randint(0,410)randy = random.randint(0,200)Plane.__init__(self, screen, randx, randy, "./feiji/enemy0.png")self.times = 0      #敌机自动左右移的标志变量def blit(self, bullet_move_variable):  #放置玩家飞机方法  Notice:飞机发射的炸弹,所以fire()以后在飞机的blit中调用并且移动Plane.blit(self, bullet_move_variable)def fire(self):          #让敌机自动开火random_num =  random.randint(0,100)random_list = [15,30,45,60,75,90]if random_num in random_list:self.bullet_list.append(enemyBullet(self.screen, self.x, self.y))def move(self):     #让敌机自动移动if self.times % 2 == 0:self.x += 5else:self.x -= 5if self.x >= 430 or self.x <= 0:self.times += 1def destroyed(self, user_Plane):if self.destroy_director == 0:for bullet in user_Plane.bullet_list:if (bullet.x > self.x and bullet.x < (self.x + 45)) and (bullet.y > self.y and bullet.y < (self.y + 33)):user_Plane.bullet_list.remove(bullet)self.image = pygame.image.load("./feiji/enemy0_down1.png")self.destroy_director = 1else:self.destroy_director += 1if self.destroy_director == 4:self.image = pygame.image.load("./feiji/enemy0_down2.png")elif self.destroy_director == 8:self.image = pygame.image.load("./feiji/enemy0_down3.png")elif self.destroy_director == 12:self.image = pygame.image.load("./feiji/enemy0_down4.png")elif self.destroy_director >= 16:self.image = pygame.image.load("./feiji/point.png")return 1def monitor_keyboard_userplane(userplane):    #监测键盘以及鼠标点击global keydown_listfor event in pygame.event.get():   if event.type == QUIT:exit()elif event.type == KEYDOWN:if event.key == K_LEFT and userplane.x >= 0:            #检测按键并规定不能出界if keydown_list[0] == 0:keydown_list[0] = K_LEFTelse:keydown_list.append(K_LEFT)elif event.key == K_RIGHT and (userplane.x + 100) <= 480:     if keydown_list[0] == 0:keydown_list[0] = K_RIGHTelse:keydown_list.append(K_RIGHT)elif event.key == K_DOWN and (userplane.y + 120) <= 700:        if keydown_list[0] == 0:keydown_list[0] = K_DOWNelse:keydown_list.append(K_DOWN)elif event.key == K_UP and userplane.y >= 0:           if keydown_list[0] == 0:keydown_list[0] = K_UPelse:keydown_list.append(K_UP)elif event.key == K_SPACE:     #检测空格键,开火userplane.fire()elif event.key == K_ESCAPE:       #检测退出键exit()elif event.type == KEYUP:if (event.key == K_LEFT or event.key == K_RIGHT or event.key == K_UP or event.key == K_DOWN) and len(keydown_list) > 1:keydown_list.remove(event.key)elif (event.key == K_LEFT or event.key == K_RIGHT or event.key == K_UP or event.key == K_DOWN) and len(keydown_list) == 1:keydown_list[0] = 0def realMove(userplane):if keydown_list[-1] == K_LEFT and userplane.x >= 0:userplane.x -= 6elif keydown_list[-1] == K_RIGHT and (userplane.x + 100) <= 480:userplane.x += 6elif keydown_list[-1] == K_DOWN and (userplane.y + 120) <= 700: userplane.y += 6 elif keydown_list[-1] == K_UP and userplane.y >= 0:userplane.y -= 6def main():screen = pygame.display.set_mode((480, 700), 0, 32)           #创建一个480*700尺寸的窗口background = pygame.image.load("./feiji/background.png")    #导入背景图片enemyplane_list = []userplane_list = []bonus_list = []user_plane = user_Plane(screen)    #创建玩家飞机userplane_list.append(user_plane)   #存储玩家飞机的列表enemy_plane = enemy_Plane(screen) #创建敌机enemyplane_list.append(enemy_plane) #存储敌机的列表bullet_move_variable_user = 12        #敌人和玩家的子弹速度bullet_move_variable_enemy = 12score = 0while True:screen.blit(background, (0, 0))    #将背景图片贴到窗口上,对齐点是(0,0)点处对齐贴if len(enemyplane_list) == 0:    #若敌机被毁,再创建敌机score += 1print "大笨Q,你现在得分是%d分,加油啊"  %  scorebullet_move_variable_enemy += 1enemy_plane = enemy_Plane(screen)    enemyplane_list.append(enemy_plane)user_plane.blit(bullet_move_variable_user)    #显示玩家飞机并且显示移动的子弹user_plane.destroyed(enemy_plane)if enemy_plane.destroyed(user_plane) != 1:  #若敌机被毁,则停止敌机的行动enemy_plane.move()        #敌机自动移动调用enemy_plane.fire()         #敌机发射子弹enemy_plane.blit(bullet_move_variable_enemy)         #显示敌机if enemy_plane.destroyed(user_plane) == 1:    #删除敌机对象时候的条件,否则敌机子弹会立刻消失if len(enemy_plane.bullet_list) == 0:del enemy_planedel enemyplane_list[0]elif enemy_plane.bullet_list[-1].y > 690: del enemy_planedel enemyplane_list[0]if random.randint(1,300) == 1 and len(bonus_list) == 0:          #产生Bonus情况randx = random.randint(50, 430)bonus = Bonus(screen, randx, -50, "./feiji/bomb-1.png")bonus_list.append(bonus)if len(bonus_list) != 0:         #通过bonus改变玩家或者敌机的子弹速度bonus.move()bonus.blit()if user_plane.getBonus(bonus) == 1:bullet_move_variable_user += 1bullet_move_variable_enemy -= 1del bonus_list[0]elif bonus.missed() == 1:bullet_move_variable_user -= 1bullet_move_variable_enemy += 1del bonus_list[0] pygame.display.update()    #贴完以后不会显示,必须调用update函数更新后才会显示新图monitor_keyboard_userplane(user_plane)     #监测键盘realMove(user_plane)time.sleep(0.01)main()

友情提示:想要代码完美成功运行,要添加对应敌机,用户飞机等图片到相应路径并可能需要修改对应参数。

总体总结:

①我采用的是面向对象的设计方法,创建了多个类,其中还有几个基类让其他类继承以达到减少代码量的目的。

②游戏整体算法采用的都是pygame中的基本操作,因为只是无聊消遣,所以我并没有太深的去探究pygame。

③在处理用户连续的不抬起按键时遇到了一些小问题,采用KEYUP与KEYDOWN结合可以解决这个小问题。

④在敌机摧毁时,如何消除敌机与敌机已经发出的子弹是个小难点。我采用的方法是当敌机被摧毁后,该敌机最后一颗子弹消失  后,再创建出一架新的敌机。

⑤bonus包的下落轨迹我采用随机函数产生随机数来决定,并不是让它直线下落以稍稍增加捡包的难度。

飞机大战基本就是这些内容啦。第一次写游戏,还是值得纪念一下的!!!

无聊消遣——基于pygame库的飞机大战相关推荐

  1. 基于pygame实现的飞机大战游戏

    目录 1.引言 1.1 背景 1.2 意义 1.3 功能 2.系统结构 2.1 整体框架 2.2 精灵与精灵组 2.3 功能介绍 2.3.1 玩家飞机 2.3.2 敌机类型和关卡设定 2.3.3 敌机 ...

  2. c语言基于easyx库的 飞机大战游戏(鼠标控制飞机移动,武器升级,boss发射散弹,boss血条等功能)

    课设题目 实现功能: 飞机鼠标控制–飞机武器升级–敌机发射子弹–boss发射散弹–boss血条记录–我方多条生命 图片资源和源码在下面 链接:https://pan.baidu.com/s/1uTQV ...

  3. 【Python实训项目】pygame制作【飞机大战】

    目录 一.课程设计目的及应用背景 二.课程设计内容 三.课程设计代码实现 1. 创建子弹类 2.创建玩家飞机类 3. 创建敌机类 4.检查键盘输入 5.创建主模块 四.测试结果 五.思考.心得和改进以 ...

  4. (七)通过pygame来设置飞机大战中 敌机 的速度、位置等

    python飞机大战系列文章(按顺序) (一)通过pygame 将自己的图像添加到游戏中 (二)通过pygame让游戏人物 动起来 (三)通过pygame处理用户的鼠标键盘操作(事件和监听 (四)详解 ...

  5. 【java毕业设计】基于java+Eclipse的飞机大战游戏设计与实现(毕业论文+程序源码)——飞机大战游戏

    基于java+Eclipse的飞机大战游戏设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于java+Eclipse的飞机大战游戏设计与实现,文章末尾附有本毕业设计的论文和源码下载地址哦. ...

  6. Python使用pygame模块实现飞机大战

    文章目录 ReadMe 1.开发环境 2.准备环境 2.1 创建工程文件夹和虚拟环境 2.2 安装pygame工具包 3.帮助文档 4.开发步骤 5.项目分析 5.1 对象关系图 5.2 飞机具有的属 ...

  7. 【Python游戏】pygame模块制作飞机大战、贪吃蛇(多模式)含源码

    pygame模块制作飞机大战.贪吃蛇(多版本) 本章意在学习pygame模块以及巩固python语法. 若有需请转去个人github:https://github.com/onlyhyl/pyGame

  8. pygame之《飞机大战》(一)

    前言: 学习完Python后,为巩固与练习,因而编写了<飞机大战>小游戏.(共六个章节) GitHub:https://github.com/GYT0313/FeiJiDaZhan 最终成 ...

  9. 【python】使用pygame写的飞机大战游戏

    制作原因 做这个小游戏是因为学校的实训内容,老师给的要求是飞机大战,当时我感觉飞机大战这个题材太多了,就做了个飞艇大战,图片也是很久之前学习前端时攒下的素材,自己用ps改了改拿来用效果还不错 因为实训 ...

最新文章

  1. Koa实现下载excel
  2. 计算机视觉开源库OpenCV形态学morphologyEx函数之开运算和闭运算
  3. 注解源代码学习 - Annotation @InjectMocks and @Mock debug
  4. c语言编程代码对父母感恩,c语言中编程:每个做父母的都关心自己孩子成人后孩子的生高:...
  5. .Net中json序列化与反序列化
  6. greendao3.0版本更新 新增字段遇到的问题
  7. 图像坐标球面投影_比较常用的坐标几种投影
  8. 高速无人驾驶车辆防滑移MPC控制 学习笔记(未完结)
  9. vue实例的参数说明
  10. cropper.js 裁剪图片并上传(文档翻译+demo)
  11. javaFX,Scene Builder引入Jfoenix
  12. 麦氏细菌浊度分析仪的校准物质选择
  13. 2018软工—团队现场编程实战(抽奖系统)
  14. 嵌入式论坛展示微控制器、工具、软件、物联网、连接性、安全性
  15. 机器学习 任务管理器中显示cpu占用很高,gpu很低,但是设置了Gpu运算
  16. python绘制的Svg图打开一片空白
  17. php 模板 下载xml,模板用xml的思路_PHP
  18. 小编教你如何打印出一张好看的思维导图
  19. 与传统招聘方式相比,小程序招聘都有哪些优势?
  20. 案例研究 | Soul是如何破解Z世代社交密码的?

热门文章

  1. 发撒富商大贾是的鬼地方和
  2. 前端面试题汇总集合(初级)
  3. 先让爸爸做做测试,再送他父亲节礼物「Scratch单项选择题实现鼠标+键盘操作」
  4. 查询准考证电脑上显示暂无信息
  5. Unity手柄UI设计
  6. SQL Server2012数据库还原以及单用户改为多用户
  7. 面试官说我离高薪 offer 只差一个Redis入门,他是认真的
  8. 艾司博讯:拼多多修改价格对权重的影响
  9. 数码管显示实验(一)(初步明白片选、段选)
  10. shell中source命令与sh命令的区别