相关文件

关注小编,私信小编领取哟!
当然别忘了一件三连哟~~

对了大家可以关注小编的公众号哟~~
Python日志

开发环境

Python版本:3.6.4
相关模块:
pygame模块;
以及一些Python自带的模块。

环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。

原理介绍

“使用方向键移动小鱼,小鱼会自动发射带有(Cu)的泡泡攻击其他的泡泡,并进行合成化学方程式”
大概了解了游戏规则之后,我们就可以开始写这个游戏啦~首先,进行一下游戏初始化操作并播放一首自己喜欢的游戏背景音乐:

# 初始化
pygame.init()
pygame.mixer.init()# 设置尺寸参数
bg_size = width,height = 480,800
# 设置窗口
screen = pygame.display.set_mode(bg_size)
# 设置标题
pygame.display.set_caption("消灭泡泡——彳余大胆")
# 加载背景图片
background = pygame.image.load("tuxiang/bg.png").convert()
# 加载游戏开始图片
game_start = pygame.image.load("tuxiang/game_start.png").convert_alpha()
# 加载游戏重新开始图片
game_restart = pygame.image.load("tuxiang/game_restart.png").convert_alpha()
game_restart_rect = game_restart.get_rect()
game_restart_rect.left, game_restart_rect.top = width - game_restart_rect.width - 160, 520
# 加载游戏准备图片
game_loading1 = pygame.image.load("tuxiang/game_loading1.png").convert_alpha()
game_loading2 = pygame.image.load("tuxiang/game_loading2.png").convert_alpha()
game_loading3 = pygame.image.load("tuxiang/game_loading3.png").convert_alpha()
game_loading_rect = game_loading1.get_rect()# 加载暂停图片
game_stop = pygame.image.load("tuxiang/game_stop.png").convert_alpha()
game_stop_rect = game_stop.get_rect()
# 加载游戏结束图片
game_over = pygame.image.load("tuxiang/game_over.png").convert()
# 加载暂停键图片
game_pause_nor = pygame.image.load("tuxiang/game_pause_nor.png").convert_alpha()
game_pause_pressed = pygame.image.load("tuxiang/game_pause_pressed.png").convert_alpha()
# 加载继续键图片
game_resume_nor = pygame.image.load("tuxiang/game_resume_nor.png").convert_alpha()
game_resume_pressed = pygame.image.load("tuxiang/game_resume_pressed.png").convert_alpha()
# 暂停键按钮范围值
paused_rect = game_pause_nor.get_rect()
paused_rect.left, paused_rect.top = width - paused_rect.width - 10, 10
paused_image = game_pause_nor
resume_image = game_resume_nor
# 加载'生命'数量图片
life_image = pygame.image.load("tuxiang/life.png").convert_alpha()
life_rect = life_image.get_rect()
life_num = 3# 设置音乐变量
bg_music = pygame.mixer.music
game_achievement_sound = pygame.mixer
game_over_sound = pygame.mixer
bullet_sound = pygame.mixer
bubble1_down_sound = pygame.mixer
bubble2_out_sound = pygame.mixer
bubble2_down_sound = pygame.mixer
pygame.mixer.set_num_channels(15)      # 设置音轨通道
volume = 0.6                           # 音量# 加载游戏音乐,设置音量
bg_music.load("music/game_music.mp3")
game_achievement_sound = game_achievement_sound.Sound("music/game_achievement.wav")
game_over_sound = game_over_sound.Sound("music/game_over.wav")
bubble1_down_sound = bubble1_down_sound.Sound("music/bubble1_down.wav")
bubble2_out_sound = bubble2_out_sound.Sound("music/bubble2_out.wav")
bubble2_down_sound = bubble2_down_sound.Sound("music/bubble2_down.wav")
bg_music.set_volume(volume)
game_achievement_sound.set_volume(volume)
game_over_sound.set_volume(volume)
bubble1_down_sound.set_volume(volume)
bubble2_out_sound.set_volume(volume)
bubble2_down_sound.set_volume(volume)# 基础参数
WHITE = (255,255,255)  # 白色
GREEN = (0, 255, 0)    # 绿色
RED = (255, 0, 0)      # 红色
destroy_speed = 5     # 销毁速度
bullet_speed = 10      # 子弹初始射速
msec = 45 * 1000       # 毫秒数

我们开始实例化子弹:

 # 实例化子弹bullets = []# 子弹索引下标bullets_index = 0# 初始子弹数目bullet_num = 4for i in range(bullet_num):bullets.append(bullet.Bullet(hero.rect.midtop))#定时supply_time =  USEREVENTpygame.time.set_timer(supply_time, msec)

然后游戏的主体:

 # 游戏循环while run:# 事件检测for event in pygame.event.get():# 右上角关闭按钮检测if event.type == QUIT:pygame.quit()sys.exit()# 鼠标点击检测elif event.type == MOUSEBUTTONDOWN:if event.button == 1:# 游戏开始start = Trueif event.button == 1 and paused_rect.collidepoint(event.pos):paussed = not paussedif not paussed:pygame.time.set_timer(supply_time, 0)# 暂停音效pygame.mixer.music.pause()pygame.mixer.pause()else:pygame.time.set_timer(supply_time, msec)# 恢复音效pygame.mixer.music.unpause()pygame.mixer.unpause()if event.button == 1 and game_restart_rect.collidepoint(event.pos):# 重新开始main()# 鼠标放置坐标检测elif event.type == MOUSEMOTION:if paused_rect.collidepoint(event.pos):if paussed:paused_image = game_resume_pressedelse:paused_image = game_pause_pressedelse:if paussed:paused_image = game_resume_norelse:paused_image = game_pause_nor#            # 键盘按键检测
#            elif event.type == KEYDOWN:
#                if event.key == K_SPACE:# 视情况提升等级if level < get_level(score) + 1:level += 1print('等级提升!')game_achievement_sound.play()# 增加一定批次的泡泡add_enemies('mini', bg_size, mini_enemise, enemies, 3)add_enemies('other', bg_size, other_enemise, enemies, 3)add_enemies('other2', bg_size, other2_enemise, enemies, 3)add_enemies('other3', bg_size, other3_enemise, enemies, 3)add_enemies('medium', bg_size, medium_enemise, enemies, 2)        # 游戏开始if start:# 绘制背景图像screen.blit(background, (0, 0))# 绘制分数score_text = score_font.render("Score : %s" % str(score), True, WHITE)screen.blit(score_text, (10,5))# 绘制暂停按钮screen.blit(paused_image, paused_rect)if life_num and paussed:# 碰撞检测enemies_down = pygame.sprite.spritecollide(hero,enemies, False, pygame.sprite.collide_mask)if enemies_down:hero.survival = Falsefor bubble_down in enemies_down:bubble_down.survival = False# 发射子弹if not(delay % bullet_speed):bullets[bullets_index].set_position(hero.rect.midtop)bullets_index = (bullets_index + 1) % bullet_num# 子弹碰撞检测#bulletfor b in bullets:if b.survival:b.move()screen.blit(b.bullet, b.rect)bubble_hit = pygame.sprite.spritecollide(b, enemies, False, pygame.sprite.collide_mask)# 被击中后if bubble_hit:b.survival = Falsefor e in bubble_hit:if e in medium_enemise:e.hp -= 1e.hit = Trueif e.hp == 0: e.survival = Falseelse: e.survival = False# 绘制小型泡泡图像for mini_bubble in mini_enemise:# 存活判断if mini_bubble.survival:# 小型泡泡移动mini_bubble.move()# 绘制图像screen.blit(mini_bubble.bubble,mini_bubble.rect)else:# 销毁泡泡if not(delay % destroy_speed):if mini_bubble.spirits_index == 0:# 播放泡泡销毁声音bubble1_down_sound.play()# 绘制销毁动画screen.blit(mini_bubble.death_spirits[mini_bubble.spirits_index], mini_bubble.rect)mini_bubble.spirits_index = mini_bubble.spirits_index + 1if mini_bubble.spirits_index == 3:# 增加分数score += grade1# 重新设置泡泡mini_bubble.set_position()# 绘制泡泡图像for other_bubble in other_enemise:# 存活判断if other_bubble.survival:# 小型泡泡移动other_bubble.move()# 绘制图像screen.blit(other_bubble.bubble,other_bubble.rect)else:# 销毁泡泡if not(delay % destroy_speed):if other_bubble.spirits_index == 0:# 播放泡泡销毁声音bubble1_down_sound.play()# 绘制销毁动画screen.blit(other_bubble.death_spirits[other_bubble.spirits_index], other_bubble.rect)other_bubble.spirits_index = other_bubble.spirits_index + 1if other_bubble.spirits_index == 3:# 增加分数score += grade1# 重新设置泡泡other_bubble.set_position()# 绘制小型泡泡图像for other2_bubble in other2_enemise:# 存活判断if other2_bubble.survival:# 小型泡泡移动other2_bubble.move()# 绘制图像screen.blit(other2_bubble.bubble,other2_bubble.rect)else:# 销毁泡泡if not(delay % destroy_speed):if other2_bubble.spirits_index == 0:# 播放敌机销毁声音bubble1_down_sound.play()# 绘制销毁动画screen.blit(other2_bubble.death_spirits[other2_bubble.spirits_index], other2_bubble.rect)other2_bubble.spirits_index = other2_bubble.spirits_index + 1if other2_bubble.spirits_index == 3:# 增加分数score += grade1# 重新设置泡泡other2_bubble.set_position()# 绘制小型泡泡图像for other3_bubble in other3_enemise:# 存活判断if other3_bubble.survival:# 小型泡泡移动other3_bubble.move()# 绘制图像screen.blit(other3_bubble.bubble,other3_bubble.rect)else:# 销毁泡泡if not(delay % destroy_speed):if other3_bubble.spirits_index == 0:# 播放泡泡销毁声音bubble1_down_sound.play()# 绘制销毁动画screen.blit(other3_bubble.death_spirits[other3_bubble.spirits_index], other3_bubble.rect)other3_bubble.spirits_index = other3_bubble.spirits_index + 1if other3_bubble.spirits_index == 3:# 增加分数score += grade1# 重新设置敌机other3_bubble.set_position()# 绘制中型泡泡图像for medium_bubble in medium_enemise:# 存活判断if medium_bubble.survival:# 中型泡泡移动medium_bubble.move()# 被击中判定if medium_bubble.hit:screen.blit(medium_bubble.bubble_hit,medium_bubble.rect)medium_bubble.hit = Falseelse:# 绘制图像screen.blit(medium_bubble.bubble,medium_bubble.rect)# 绘制血槽draw_start = (medium_bubble.rect.left, medium_bubble.rect.top - 5)draw_end = (medium_bubble.rect.right, medium_bubble.rect.top - 5)pygame.draw.line(screen, RED, draw_start, draw_end, 2)# 计算剩余血量surplus_hp = medium_bubble.hp / bubble.MediumBubble.hpdraw_end = (medium_bubble.rect.left + medium_bubble.rect.width * surplus_hp, medium_bubble.rect.top - 5)pygame.draw.line(screen, GREEN, draw_start, draw_end, 2)else:# 销毁泡泡if not(delay % destroy_speed):if medium_bubble.spirits_index == 0:# 播放敌机销毁声音bubble2_down_sound.play()# 绘制销毁动画screen.blit(medium_bubble.death_spirits[medium_bubble.spirits_index], medium_bubble.rect)medium_bubble.spirits_index = medium_bubble.spirits_index + 1if medium_bubble.spirits_index == 3:# 增加分数score += grade2# 重新设置敌机medium_bubble.set_position()# 绘制鱼的图像if hero.survival:# 检测键盘,控制鱼的移动hero.move()screen.blit(hero.fish,hero.rect)else:bubble2_down_sound.play()if not(delay % destroy_speed):# 绘制销毁动画screen.blit(hero.death_spirits[hero.spirits_index], hero.rect)hero.spirits_index = hero.spirits_index + 1if hero.spirits_index == 3:print("Code Over")life_num -= 1# 重新设置战机hero.set_position()# 绘制飞机生命数量if life_num:for i in range(life_num):life_left = width - 10 - (i + 1) * life_rect.widthlife_top = height - 10 - life_rect.heightscreen.blit(life_image, (life_left, life_top))elif life_num == 0:# 读取历史最高分highest_score = int()# 判断记录文件是否存在,不存在则创建if not os.path.exists("score.txt"):#os.mknod("score.txt")highest_score = 0else:# 文件存在着进行读取with open("score.txt","r") as f:highest_score = int(f.read())f.close()# 若打破记录,则重新写入分数if score > highest_score:with open("score.txt","w+") as f:f.write(str(score))f.close()# 绘制游戏结束背景图screen.blit(game_over, (0, 0))# 绘制最终得分if score < highest_score:game_over_text = game_over_font.render("%s" % str(score), True, WHITE)else:game_over_text = game_over_font.render("%s" % str(score), True, WHITE)over_text_width = str(score).__len__() + 50screen.blit(game_over_text, ((width - over_text_width) / 2, height / 2 ))# 绘制历史记录game_over_text = game_over_font.render("%s" % "Highest Score: " + str(highest_score), True, WHITE)over_text_width = (str(highest_score).__len__() + 15) * 12screen.blit(game_over_text, ((width - over_text_width) / 2, height / 2 + 60))# 绘制重新开始按钮screen.blit(game_restart, game_restart_rect)# 停止背景音乐pygame.mixer.music.pause()# 停止音效pygame.mixer.pause()else:# 绘制游戏停止标志图screen.blit(game_stop, ((width - game_stop_rect.width) / 2, (height - game_stop_rect.height) / 2))else:# 绘制背景图像screen.blit(background, (0, 0))# 绘制游戏名称screen.blit(game_start, (0, 0))# 绘制 loading 图像game_loading_X = (width - game_loading_rect.width) / 2game_loading_Y = (height - game_loading_rect.height) / 2 + 50screen.blit(game_loadings[game_loadings_index], (game_loading_X, game_loading_Y))# loding 下标变更if not (delay % 8):game_loadings_index = (game_loadings_index + 1) % game_loadings_num# 绘制界面(双缓冲)pygame.display.flip()# 帧率设置clock.tick(30)delay -= 1if delay == 0:delay = 10

游戏启动:

if __name__ == '__main__':main()try:main()except SystemExit:passexcept:traceback.print_exc()pygame.quit()input()

效果展示:

开始界面

当泡泡相撞的时候会形成化学方程式:

游戏暂停:

游戏结束:

很多小伙伴在学习python的时候总会遇到一些问题和瓶颈,没有方向感,不知道该从哪里入手去提升,对此我整理了一些资料,希望能够去帮助到小伙伴们,可以加Python学习交流裙:773162165

好啦,今天的小游戏就给大家安利到这里啦,有啥不懂的大家可以在下方评论,需要源码的可以找小编领取哟,记得关注小编的公众号哈~

Python安利一个会化学方程式的消灭泡泡小游戏~相关推荐

  1. 利用Python编写一个AI脚本自动控制2048网页小游戏

    前言 本文将使用python+selenium自动控制游戏运行.当然采用的是伪随机数进行键盘控制.只作为一个抛砖迎玉的参考,不涉及专业算法. – 一.前期准备(必须有) 1.在安装好的pyCharm中 ...

  2. 【Python游戏】基于化学方程式的基础上,用Python实现一个消灭泡泡小游戏 | 附源码

    前言 halo,包子们下午好 今天实现的这个小游戏呀,说实话化学不太好的小伙伴可能看起来会有点懵逼 不过不用担心,咱们今天不是来学化学的,我们是来学习Python的 所以呀,不要太担心啦,大家先好好看 ...

  3. 利用python做一个小游戏_如何使用python做一个简单的猜数字的小游戏

    1 首先小编先打开IDLE,如下图: 2 然后这里点击菜单栏的'File',然后点击菜单"New File",如下图: 3 然后我们就在idle中新建了一个python文件,如下图 ...

  4. 使用Python开发一个超级简单的接水果小游戏,零基础也可以学会

    Pylash项目地址 创建项目 这样的话我们的项目就创建好了,然后只用往Main.py里填写代码运行即可. 编写Hello World小程序 编写游戏 有以上对pylash的小小了解,我们接下来可以开 ...

  5. python接水果游戏代码_使用Python开发一个超级简单的接水果小游戏,零基础也可以学会...

    Pylash项目地址 创建项目 这样的话我们的项目就创建好了,然后只用往Main.py里填写代码运行即可. 编写Hello World小程序 编写游戏 有以上对pylash的小小了解,我们接下来可以开 ...

  6. python接水果游戏代码_【Python】python制作一个接水果和金币的小游戏

    开发工具 Python版本:3.6.4 相关模块: pygame模块: 以及一些Python自带的模块. 相关文件 环境搭建 安装Python并添加到环境变量,pip安装需要的相关模块即可. 原理简介 ...

  7. python制作一个接水果和金币的小游戏

    开发工具 Python版本:3.6.4 相关模块: pygame模块: 以及一些Python自带的模块. 相关文件 关注公众号:Python学习指南,回复"game9"获取相关文件 ...

  8. python循环10次_开发一个循环 5 次计算的小游戏, 设置随机种子为10,每次随机产生两个 1~10的数字以及随机选择...

    开发一个循环 5 次计算的小游戏, 设置随机种子为10,每次随机产生两个 1~10的数字以及随机选择 "+.-.*"运算符,构成一个表达式, 让用户计算式子结果并输入结果,如果计算 ...

  9. python编写数据库连接工具_详解使用Python写一个向数据库填充数据的小工具(推荐)...

    一. 背景 公司又要做一个新项目,是一个合作型项目,我们公司出web展示服务,合作伙伴线下提供展示数据. 而且本次项目是数据统计展示为主要功能,并没有研发对应的数据接入接口,所有展示数据源均来自数据库 ...

最新文章

  1. echarts格式化tooltip数据
  2. web java工程的创建_简单JavaWeb工程创建
  3. 作者:李喜莲(1992-),女,北京大学信息科学技术学院硕士生。
  4. Producing function in SQL.
  5. python%20是什么类型的语言
  6. 结构体01:结构体的定义和使用
  7. 【数值分析】python实现复化高斯积分
  8. 【编译打包】btsync-1.2.82-beta.el6.src.rpm
  9. php background,CSS BACKGROUND定位背景上下左右偏移
  10. win7计算机默认用户名,win7系统任务管理器中用户名没有显示的解决方法
  11. 智力推理:三个孩子的年龄分别是多少?
  12. 编程实现库函数strcat
  13. vue中获取本地IP地址
  14. 计算机网络基础 之三:数据链路层
  15. 计算机三级 网络技术 大题第一题 答题技巧分享
  16. FPGA与CPLD的概念及其区别
  17. 大厂面经系列 | 前端 | 美团,字节,京东,顺丰,携程 等题目分享
  18. 第一部分:简单句——第一章:简单句的核心——一、简单句的核心构成
  19. 数据代码分享|Python用NLP自然语言处理LSTM神经网络Twitter推特灾难文本数据、词云可视化与SVM,KNN,多层感知器,朴素贝叶斯,随机森林,GBDT对比
  20. 语音处理 之 fastspeech代码

热门文章

  1. 微软修复Win7 SP1安装时出现的黑屏错误“0xC0000009A”
  2. 亚马逊企业文化交流分享
  3. 前后端分离的项目——图书管理系统(上)
  4. linux打开开发者权限,在UOS系统中关闭开发者模式和在UOS个人版中打开开发者模式...
  5. I51开发板----STC15F2K60S2教程
  6. 【魔改练习题】五只小猪称体重
  7. 《Google 软件测试之道》摘录
  8. HP 3PAR StoreServ 7200 存储调试相关
  9. ios热更新JSPatch
  10. 程序员鄙视链python_关于程序员之间的鄙视链