• 【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)


    导语

    嘿嘿!木木子今日闪现——已经给大家写了很多内容啦~

    涉及的人工智能、初学者、爬虫、数据分析(这方面的一般不过审核)游戏........

    通过这么多篇的阅读量来说还是人工智能跟游戏的阅读量多吖!

    SO 最近的内容刚开始更新还是准备从游戏着手,毕竟很多小伙伴儿肯定已经等不及啦~

    后续慢慢更新人工智能方面的内容~说实话就学这么多不知带更新啥子了,但是还会继续努力

    学习新知识再更文的啦!FigthingFigthingFigthing、

    所有文章完整的素材+源码都在👇👇

    🎁🎁🎁文末公众hao自取哦🎁🎁🎁

    PS:

    吃豆人我写过了哈👇

    【Pygame小游戏】确实会玩—教你如何在”吃豆豆“上完美躺赢……(收藏起来偷偷学)

    切水果我写过了哈👇

    【Pygame实战】风靡全球的切水果游戏升级版“水果忍者”上线啦,你敢来PK嘛?

    今天二者集合,做出一款新游戏,哈哈哈,名字叫做《疯狂🤪吃水果》小游戏,其实听着挺🐂

    的,但是做出来的效果其实没有想象中那么高大尚呐!(给你们打个预防针)

    正文

    本文是基于Pygame写的一款游戏哈!

    一、准备中 

    1)游戏玩法

    随机掉落:西瓜🍉加分、葡萄🍇减分、炸弹💣一条生命值初始为二。鼠标右键移动。加减多

    少分具体就等你们自己玩儿了哈,都剧透了就不好玩了撒!每次的游戏代码都给你们留点儿

    底,嘻嘻,自己摸索嘛~

    2)环境安装

    小编使用的环境:Python3、Pycharm社区版、tkinter、Pygame模块,部分自 带模块不展

    示。(不会安装的:小编会发给你们新手大礼包,包括安装包、视频、解答 疑问可以找我哈戳

    文末) 

    模块安装:pip install -i https://pypi.douban.com/simple/+模块名

     3)素材准备

    准备了背景音乐🎵更有劲儿啦!记得seven这首歌嘛,还挺好听的。

    ​准备好的素材图片背景掉落的物品等。

    二、代码展示

    代码超级多的!仅展示部分,文末全免费拿哈!🎃🎃🎃

    1)主程序

    1. import tkinter
    2. import random
    3. import time
    4. import Param
    5. import Image
    6. import Bonus
    7. import Deduction
    8. import Bean
    9. import Bomb
    10. import pygame
    11. # 定义物质列表(包含加分西瓜和消分葡萄和炸弹)
    12. bonusth = []
    13. deductionth = []
    14. bigbombs = []
    15. # 定义bean变量,保存豆豆对象
    16. bean = ""
    17. # 定义当前用户的初始分数
    18. score = 0
    19. life = 2
    20. # 定义游戏状态
    21. game_state = Param.GAME_START
    22. # 创建窗体
    23. game_window = tkinter.Tk()
    24. # 窗口文字设置
    25. game_window.title('I LOVE FRUIT')
    26. # 窗口位置处理
    27. screenwidth = game_window.winfo_screenwidth()
    28. screenheight = game_window.winfo_screenheight()
    29. size = '%dx%d+%d+%d' % (Param.GAME_WIDTH, Param.GAME_HEIGHT, (screenwidth-Param.GAME_WIDTH)/2, 50)
    30. game_window.geometry(size)
    31. # 加载游戏用到的所有的图片
    32. background_image,bean_image,Bonus_image,Bomb_image,Deduction_image= Image.load_image(tkinter)
    33. Start,Stop = Image.load_state_image(tkinter)
    34. # 获取画布
    35. window_canvas = tkinter.Canvas(game_window)
    36. # 画布包装方式
    37. window_canvas.pack(expand=tkinter.YES, fill=tkinter.BOTH)
    38. # 时间标志
    39. count = 0
    40. num = 30
    41. def create_fruit():# 生成水果
    42. global count
    43. global num
    44. global score
    45. if score % 10 ==1:
    46. if num >= 8:
    47. num -= 8
    48. count += 1
    49. if count % num == 0:
    50. c = random.randint(1,10)
    51. if c <= 5:
    52. # 加分水果生成
    53. bonus = Bonus.Bonus(Bonus_image)
    54. bonusth.append(bonus) # 物质添加到列表中
    55. window_canvas.create_image(bonus.x,bonus.y,anchor = tkinter.NW,image=bonus.image,tag=bonus.tag)
    56. elif c<=8:
    57. # 销分水果生成
    58. deduction = Deduction.Deduction(Deduction_image)
    59. deductionth.append(deduction)
    60. window_canvas.create_image(deduction.x,deduction.y,anchor = tkinter.NW,image=deduction.image,tag=deduction.tag)
    61. else:
    62. #炸弹生成
    63. bigbomb = Bomb.BigBomb(Bomb_image)
    64. bigbombs.append(bigbomb)
    65. window_canvas.create_image(bigbomb.x,bigbomb.y,anchor = tkinter.NW,image=bigbomb.image,tag=bigbomb.tag)
    66. def step_fruit():
    67. # 遍历所有的物质,调用移动的方法
    68. for bonus in bonusth:
    69. bonus.step(window_canvas)
    70. for deduction in deductionth:
    71. deduction.step(window_canvas)
    72. for bigbomb in bigbombs:
    73. bigbomb.step(window_canvas)
    74. def judge_state(event):
    75. global game_state
    76. if game_state == Param.GAME_START:
    77. game_state = Param.GAME_RUNNING
    78. # 画分
    79. window_canvas.create_text(20, 20, text="分数:%d" % (score), anchor=tkinter.NW, fill="white",\
    80. font="time 12 bold",tag="SCORE")
    81. # 画生命
    82. window_canvas.create_text(20, 50, text="生命:%d" % (life), anchor=tkinter.NW, fill="white",\
    83. font="time 12 bold",tag="LIFE")
    84. # 删除启动图片
    85. window_canvas.delete("Start")
    86. elif game_state == Param.GAME_STOP:
    87. window_canvas.delete("bean")
    88. window_canvas.delete("STOP")
    89. game_state = Param.GAME_START
    90. game_start()
    91. def bean_move(event):
    92. if game_state == Param.GAME_RUNNING:
    93. now_x = bean.x
    94. now_y = bean.y
    95. bean.x = event.x - bean.w/2
    96. bean.y = event.y - bean.h/2
    97. window_canvas.move("bean", bean.x-now_x, bean.y-now_y)
    98. def out_of_bounds():
    99. # 获取所有物质,判断是否越界
    100. for deduction in deductionth:
    101. if deduction.out_of_bounds():
    102. window_canvas.delete(deduction.tag)
    103. deductionth.remove(deduction)
    104. for bonus in bonusth:
    105. global outnum
    106. if bonus.out_of_bounds():
    107. outnum += 1
    108. window_canvas.delete(bonus.tag)
    109. bonusth.remove(bonus)
    110. if outnum >= 5:
    111. game_state = Param.GAME_STOP
    112. # 画游戏结束的状态
    113. game_over()
    114. for bigbomb in bigbombs:
    115. if bigbomb.out_of_bounds():
    116. window_canvas.delete(bigbomb.tag)
    117. bigbombs.remove(bigbomb)
    118. def bomb_action():
    119. global score
    120. global life
    121. global bean
    122. global game_state
    123. #加分
    124. for bonus in bonusth:
    125. if bonus.bomb(bean):
    126. window_canvas.delete(bonus.tag)
    127. bonusth.remove(bonus)
    128. score += 3
    129. #减分
    130. for deduction in deductionth:
    131. if deduction.bomb(bean):
    132. window_canvas.delete(deduction.tag)
    133. deductionth.remove(deduction)
    134. if score - 5 < 0:
    135. score = 0
    136. game_state = Param.GAME_STOP
    137. # 画游戏结束的状态
    138. game_over()
    139. else:
    140. score -= 5
    141. for bigbomb in bigbombs:
    142. if bigbomb.bomb(bean):
    143. window_canvas.delete(bigbomb.tag)
    144. bigbombs.remove(bigbomb)
    145. # 如果分数或生命小于0 游戏结束
    146. if life - 1 <= 0:
    147. life = 0
    148. game_state = Param.GAME_STOP
    149. # 画游戏结束的状态
    150. game_over()
    151. else:
    152. life -= 1
    153. def draw_action():
    154. # 画分
    155. window_canvas.delete("SCORE")
    156. # 画生命
    157. window_canvas.delete("LIFE")
    158. window_canvas.create_text(20,20,text="分数:%d"%(score),anchor=tkinter.NW,fill="white",font="time 12 bold",tag="SCORE")
    159. window_canvas.create_text(20,50,text="生命:%d"%(life),anchor=tkinter.NW,fill="white",font="time 12 bold",tag="LIFE")
    160. def game_over():
    161. global game_state
    162. game_state = Param.GAME_STOP
    163. for deduction in deductionth:
    164. window_canvas.delete(deduction.tag)
    165. for bonus in bonusth:
    166. window_canvas.delete(bonus.tag)
    167. for bigbomb in bigbombs:
    168. window_canvas.delete(bigbomb.tag)
    169. deductionth.clear()
    170. bonusth.clear()
    171. bigbombs.clear()
    172. window_canvas.create_image(0,0,anchor=tkinter.NW,image=Stop,tag="STOP")
    173. if pygame.mixer.music.get_busy() == True:
    174. pygame.mixer.music.stop()#停止播放
    175. def game_start():
    176. global score
    177. global life
    178. global num
    179. global outnum
    180. num = 30
    181. score = 0
    182. life = 2
    183. outnum = 0
    184. # 画游戏背景
    185. window_canvas.create_image(0, 0, anchor=tkinter.NW, image=background_image, tag="background")
    186. # 创建豆豆对象
    187. global bean
    188. bean = Bean.Bean(bean_image)
    189. window_canvas.create_image(bean.x, bean.y, anchor=tkinter.NW, image=bean.image, tag="bean")
    190. window_canvas.create_image(0, 0, anchor=tkinter.NW, image=Start, tag="Start")
    191. pygame.mixer.init()
    192. pygame.mixer.music.load('Seve(钢琴版).mp3') #加载背景音乐
    193. if pygame.mixer.music.get_busy() == False:
    194. pygame.mixer.music.play(300,0)#重复300次,从第一秒开始播放
    195. def game():
    196. if game_state == Param.GAME_START:
    197. game_start()
    198. # 鼠标监听
    199. window_canvas.bind("<Motion>",bean_move)
    200. window_canvas.bind("<Button-1>",judge_state)
    201. while True:
    202. if game_state == Param.GAME_RUNNING:
    203. # 物质入场
    204. create_fruit()
    205. # 物质动起来
    206. step_fruit()
    207. # 删除越界的物质
    208. out_of_bounds()
    209. # 检测碰撞
    210. bomb_action()
    211. if score >= 0:
    212. # 画分和生命
    213. draw_action()
    214. # 更新显示
    215. game_window.update()
    216. time.sleep(0.04)
    217. if __name__ == "__main__":
    218. game()
    219. game_window.mainloop()

    三、效果展示

    1)游戏界面

    ​2)随机截图

    3)消耗结束

    总结

    嗯哼~这款疯狂吃水果游戏到这里就正式结束了,老规矩源码文末的公众hao自己去拿的哈!

    都是免费滴,欢迎学习进步~

    🎯完整的免费源码领取处:找我吖!文末可得自行领取,滴滴我也可!

    🎉往期部分文章推荐——

    项目1.3 视频播放器

    用了都说好的Python专属无广告视频播放器,良心到想为它疯狂打call

    项目2.7 刮刮卡小程序

    周末老板请吃东西,刮到多少算多少?Python带你制作一款刮刮卡小程序。

    项目3.2 自动换壁纸

    【Python高级技能】超炫酷,电脑每天自动换壁纸,这个神器适合你。

    项目3.3 艺术字签名

    【艺术字签名生成器】】试卷家长签字居然被嫌弃了|“我觉得我还能再抢救一下,你看行嘛?“

    项目 3.9 码住雪景漫天飘雪小程序

    【Python码住雪景小程序】雪景人像最强攻略:让你一下美10倍、美醉了(中国人不骗中国人)

    项目 4.0 GIF制作神奇(斗罗大陆为例)

    【Python神器】推荐这款傻瓜式GIF制作工具,以后别再说不会了(好用到爆~)

    🎄文章汇总——

    项目1.0 Python—2021 |已有文章汇总 | 持续更新,直接看这篇就够了

    (更多内容+源码都在文章汇总哦!!欢迎阅读~)

    🎁文章汇总——

    汇总: Python文章合集 | (入门到实战、游戏、Turtle、案例等)

    (文章汇总还有更多你案例等你来学习啦~源码找我即可免费!)

  • 相关阅读:
    什么是【固件】?
    Alpha-Beta剪枝的原理的深入理解(无图预警)
    【docker】docker的基础命令
    C语言:strlen() --- 计算字符串长度
    数据结构与算法_排序算法_四个基础排序算法性能对比
    树上背包问题动态规划
    InputAction的使用
    TiUP 镜像参考指南
    基于深度学习的表格检测与识别技术的优势
    向毕业妥协系列之机器学习笔记:构建ML系统(三)
  • 原文地址:https://blog.csdn.net/weixin_55822277/article/details/125463050