当前位置:网站首页>【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)
【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)
2022-07-01 15:35:00 【顾木子吖】
导语
嘿嘿!木木子今日闪现——已经给大家写了很多内容啦~
涉及的人工智能、初学者、爬虫、数据分析(这方面的一般不过审核)游戏........
通过这么多篇的阅读量来说还是人工智能跟游戏的阅读量多吖!
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)主程序
import tkinter
import random
import time
import Param
import Image
import Bonus
import Deduction
import Bean
import Bomb
import pygame
# 定义物质列表(包含加分西瓜和消分葡萄和炸弹)
bonusth = []
deductionth = []
bigbombs = []
# 定义bean变量,保存豆豆对象
bean = ""
# 定义当前用户的初始分数
score = 0
life = 2
# 定义游戏状态
game_state = Param.GAME_START
# 创建窗体
game_window = tkinter.Tk()
# 窗口文字设置
game_window.title('I LOVE FRUIT')
# 窗口位置处理
screenwidth = game_window.winfo_screenwidth()
screenheight = game_window.winfo_screenheight()
size = '%dx%d+%d+%d' % (Param.GAME_WIDTH, Param.GAME_HEIGHT, (screenwidth-Param.GAME_WIDTH)/2, 50)
game_window.geometry(size)
# 加载游戏用到的所有的图片
background_image,bean_image,Bonus_image,Bomb_image,Deduction_image= Image.load_image(tkinter)
Start,Stop = Image.load_state_image(tkinter)
# 获取画布
window_canvas = tkinter.Canvas(game_window)
# 画布包装方式
window_canvas.pack(expand=tkinter.YES, fill=tkinter.BOTH)
# 时间标志
count = 0
num = 30
def create_fruit():# 生成水果
global count
global num
global score
if score % 10 ==1:
if num >= 8:
num -= 8
count += 1
if count % num == 0:
c = random.randint(1,10)
if c <= 5:
# 加分水果生成
bonus = Bonus.Bonus(Bonus_image)
bonusth.append(bonus) # 物质添加到列表中
window_canvas.create_image(bonus.x,bonus.y,anchor = tkinter.NW,image=bonus.image,tag=bonus.tag)
elif c<=8:
# 销分水果生成
deduction = Deduction.Deduction(Deduction_image)
deductionth.append(deduction)
window_canvas.create_image(deduction.x,deduction.y,anchor = tkinter.NW,image=deduction.image,tag=deduction.tag)
else:
#炸弹生成
bigbomb = Bomb.BigBomb(Bomb_image)
bigbombs.append(bigbomb)
window_canvas.create_image(bigbomb.x,bigbomb.y,anchor = tkinter.NW,image=bigbomb.image,tag=bigbomb.tag)
def step_fruit():
# 遍历所有的物质,调用移动的方法
for bonus in bonusth:
bonus.step(window_canvas)
for deduction in deductionth:
deduction.step(window_canvas)
for bigbomb in bigbombs:
bigbomb.step(window_canvas)
def judge_state(event):
global game_state
if game_state == Param.GAME_START:
game_state = Param.GAME_RUNNING
# 画分
window_canvas.create_text(20, 20, text="分数:%d" % (score), anchor=tkinter.NW, fill="white",\
font="time 12 bold",tag="SCORE")
# 画生命
window_canvas.create_text(20, 50, text="生命:%d" % (life), anchor=tkinter.NW, fill="white",\
font="time 12 bold",tag="LIFE")
# 删除启动图片
window_canvas.delete("Start")
elif game_state == Param.GAME_STOP:
window_canvas.delete("bean")
window_canvas.delete("STOP")
game_state = Param.GAME_START
game_start()
def bean_move(event):
if game_state == Param.GAME_RUNNING:
now_x = bean.x
now_y = bean.y
bean.x = event.x - bean.w/2
bean.y = event.y - bean.h/2
window_canvas.move("bean", bean.x-now_x, bean.y-now_y)
def out_of_bounds():
# 获取所有物质,判断是否越界
for deduction in deductionth:
if deduction.out_of_bounds():
window_canvas.delete(deduction.tag)
deductionth.remove(deduction)
for bonus in bonusth:
global outnum
if bonus.out_of_bounds():
outnum += 1
window_canvas.delete(bonus.tag)
bonusth.remove(bonus)
if outnum >= 5:
game_state = Param.GAME_STOP
# 画游戏结束的状态
game_over()
for bigbomb in bigbombs:
if bigbomb.out_of_bounds():
window_canvas.delete(bigbomb.tag)
bigbombs.remove(bigbomb)
def bomb_action():
global score
global life
global bean
global game_state
#加分
for bonus in bonusth:
if bonus.bomb(bean):
window_canvas.delete(bonus.tag)
bonusth.remove(bonus)
score += 3
#减分
for deduction in deductionth:
if deduction.bomb(bean):
window_canvas.delete(deduction.tag)
deductionth.remove(deduction)
if score - 5 < 0:
score = 0
game_state = Param.GAME_STOP
# 画游戏结束的状态
game_over()
else:
score -= 5
for bigbomb in bigbombs:
if bigbomb.bomb(bean):
window_canvas.delete(bigbomb.tag)
bigbombs.remove(bigbomb)
# 如果分数或生命小于0 游戏结束
if life - 1 <= 0:
life = 0
game_state = Param.GAME_STOP
# 画游戏结束的状态
game_over()
else:
life -= 1
def draw_action():
# 画分
window_canvas.delete("SCORE")
# 画生命
window_canvas.delete("LIFE")
window_canvas.create_text(20,20,text="分数:%d"%(score),anchor=tkinter.NW,fill="white",font="time 12 bold",tag="SCORE")
window_canvas.create_text(20,50,text="生命:%d"%(life),anchor=tkinter.NW,fill="white",font="time 12 bold",tag="LIFE")
def game_over():
global game_state
game_state = Param.GAME_STOP
for deduction in deductionth:
window_canvas.delete(deduction.tag)
for bonus in bonusth:
window_canvas.delete(bonus.tag)
for bigbomb in bigbombs:
window_canvas.delete(bigbomb.tag)
deductionth.clear()
bonusth.clear()
bigbombs.clear()
window_canvas.create_image(0,0,anchor=tkinter.NW,image=Stop,tag="STOP")
if pygame.mixer.music.get_busy() == True:
pygame.mixer.music.stop()#停止播放
def game_start():
global score
global life
global num
global outnum
num = 30
score = 0
life = 2
outnum = 0
# 画游戏背景
window_canvas.create_image(0, 0, anchor=tkinter.NW, image=background_image, tag="background")
# 创建豆豆对象
global bean
bean = Bean.Bean(bean_image)
window_canvas.create_image(bean.x, bean.y, anchor=tkinter.NW, image=bean.image, tag="bean")
window_canvas.create_image(0, 0, anchor=tkinter.NW, image=Start, tag="Start")
pygame.mixer.init()
pygame.mixer.music.load('Seve(钢琴版).mp3') #加载背景音乐
if pygame.mixer.music.get_busy() == False:
pygame.mixer.music.play(300,0)#重复300次,从第一秒开始播放
def game():
if game_state == Param.GAME_START:
game_start()
# 鼠标监听
window_canvas.bind("<Motion>",bean_move)
window_canvas.bind("<Button-1>",judge_state)
while True:
if game_state == Param.GAME_RUNNING:
# 物质入场
create_fruit()
# 物质动起来
step_fruit()
# 删除越界的物质
out_of_bounds()
# 检测碰撞
bomb_action()
if score >= 0:
# 画分和生命
draw_action()
# 更新显示
game_window.update()
time.sleep(0.04)
if __name__ == "__main__":
game()
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、案例等)
(文章汇总还有更多你案例等你来学习啦~源码找我即可免费!)

边栏推荐
- 【云动向】6月上云新风向!云商店热榜揭晓
- 采集数据工具推荐,以及采集数据列表详细图解流程
- skywalking 6.4 分布式链路跟踪 使用笔记
- Microservice tracking SQL (support Gorm query tracking under isto control)
- GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
- An intrusion detection model
- [target tracking] |stark
- 张驰咨询:家电企业用六西格玛项目减少客户非合理退货案例
- 【ROS进阶篇】第五讲 ROS中的TF坐标变换
- Tableapi & SQL and MySQL grouping statistics of Flink
猜你喜欢
![[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb](/img/ec/fa51b21468708609f998de1b2b84fe.jpg)
[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb

Photoshop plug-in HDR (II) - script development PS plug-in

如何实现时钟信号分频?

Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan

华为发布HCSP-Solution-5G Security人才认证,助力5G安全人才生态建设
![[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)

Summary of point cloud reconstruction methods I (pcl-cgal)

张驰课堂:六西格玛数据的几种类型与区别

STM32F4-TFT-SPI时序逻辑分析仪调试记录

智能运维实战:银行业务流程及单笔交易追踪
随机推荐
【OpenCV 例程200篇】216. 绘制多段线和多边形
入侵检测模型(An Intrusion-Detection Model)
SAP S/4HANA: 一条代码线,许多种选择
Qt+pcl Chapter 6 point cloud registration ICP Series 2
Reading notes of top performance version 2 (V) -- file system monitoring
Go zero actual combat demo (I)
使用 csv 导入的方式在 SAP S/4HANA 里创建 employee 数据
Flink 系例 之 TableAPI & SQL 与 Kafka 消息获取
go-zero实战demo(一)
Tableapi & SQL and Kafka message insertion in Flink
Can I choose to open an account on Great Wall Securities? Is it safe?
TS reports an error don't use 'object' as a type The `object` type is currently hard to use
张驰咨询:家电企业用六西格玛项目减少客户非合理退货案例
远程办公经验?来一场自问自答的介绍吧~ | 社区征文
Flink 系例 之 TableAPI & SQL 与 Kafka 消息插入
《性能之巅第2版》阅读笔记(五)--file-system监测
做空蔚来的灰熊,以“碰瓷”中概股为生?
重回榜首的大众,ID依然乏力
Qt+pcl Chapter 9 point cloud reconstruction Series 2
[one day learning awk] conditions and cycles