Python+Pygame实战之疯狂吃水果游戏的实现
作者:顾木子吖 发布时间:2021-09-17 11:37:58
标签:Python,Pygame,疯狂吃水果,游戏
导语
嘿嘿!木木子今日闪现——已经给大家写了很多内容啦~
涉及的人工智能、初学者、爬虫、数据分析(这方面的一般不过审核)游戏........
PS:
吃豆人我写过了哈
Python+Pygame实战之吃豆豆游戏的实现
切水果我写过了哈
Python Pygame实战之水果忍者游戏的实现
今天二者集合,做出一款新游戏,哈哈哈,名字叫做《疯狂🤪吃水果》小游戏,其实听着挺🐂的,但是做出来的效果其实没有想象中那么高大尚呐!(给你们打个预防针)
本文是基于Pygame写的一款游戏哈!
一、准备中
1)游戏玩法
随机掉落:西瓜🍉加分、葡萄🍇减分、 * 💣一条生命值初始为二。鼠标右键移动。加减多少分具体就等你们自己玩儿了哈,都剧透了就不好玩了撒!每次的游戏代码都给你们留点儿底,嘻嘻,自己摸索嘛~
2)环境安装
小编使用的环境:Python3、Pycharm社区版、tkinter、Pygame模块,部分自 带模块不展示。
模块安装:pip install -i https://pypi.douban.com/simple/+模块名
3)素材准备
准备了背景音乐更有劲儿啦!记得seven这首歌嘛,还挺好听的。
准备好的素材图片背景掉落的物品等。
二、代码展示
代码超级多的!仅展示部分
主程序
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)消耗结束
来源:https://blog.csdn.net/weixin_55822277/article/details/125463050
0
投稿
猜你喜欢
- 深藏多年秘笈大公开,全世界唯一一家公布,怎么点就是点不出,纯CSS去掉按钮或者链接点击产生的虚线。运行代码框<style type=&
- 终于完成了偶的拖动窗口,花了近15个小时,庆祝一下(*^__^*);以前写了IE下的功能,于是又写了firefox下的功能,在firefox
- 一,PHP脚本与动态页面。 PHP脚本是一种服务器端脚本程序,可通过嵌入等方 法与HTML文件混合,也可以类,函数封
- 前言:由于使用Django框架来做网站,需要动态显示数据库内的信息,所以读取数据库必须要做,写此博文来记录。接下来分两步来做这个事,添加网页
- ASP 错误代码 说明 ASP 0100 内存不足 ASP 0101 意外错误 ASP 0102 需要字符串输入 ASP 0103 需要数字
- 最近刚刚接触深度学习,并尝试学习制作数据集,制作过程中发现了一个问题,现在跟大家分享一下。问题是这样的,在制作voc数据集时,我采集的是灰度
- 当程序中出现错误时怎么解决?也就是我们所说的bug(缺陷),以及工作中如何对bug进行调试❤ 什么是bug(缺陷)软件缺陷就是通
- 今天再为大家提供一种方法:不需要安装Excel也可以导入到我们的SQL Server数据库。首先用SQL Server自身的数据转换功能把E
- 如何用Access加密页面?很简单哦,看看这个用用Access和ASP做的加密程序: <%userid =&nbs
- 前言索引和切片是NumPy中最重要最常用的操作。熟练使用NumPy切片操作是数据处理和机器学习的前提,所以一定要掌握好。参考NumPy官方文
- 随着jQuery、Mootools、prototype等知名的JavaScript框架的应用变的越来越强大,浏览器对最新版本CSS属性的支持
- 前言提起selenium想必大家都不陌生,作为一款知名的Web自动化测试框架,selenium支持多款主流浏览器,提供了功能丰富的API接口
- 随着信息技术的飞速发展,数据处理不仅在数量上要求越来越大,而且在质量上也要求越来越高。操作系统的稳定对数据库来说是十分紧要的,在数据库可操作
- 基于selenium进行动作链由于最近很多人聊到滑动验证码怎么处理,所以决定自己动手试一下。做一个东西前。我们首先要对这个东西的操作过程有一
- 参与测试的浏览器:IE6 / IE7 / IE8 / FF3 / OP9.6 / SF3 / Chrome2操作系统:Windows我喜欢p
- 1.--区分大小写select * from a where a=’AbCdE’ collate C
- SQL语句先前写的时候,很容易把一些特殊的用法忘记,我特此整理了一下SQL语句操作,方便自己写SQL时方便一点,想贴上来,一起看看,同时希望
- 一 简介python-mysql-replication 是基于python实现的 MySQL复制协议工具,我们可以用它来解析binlog
- 越简单越丰富——极简网页设计视觉呈现技巧如何让杂乱又咄咄逼人的网页变得轻薄简洁而美观,又需保留完整功能、同时很好的区分出重点模块?这往往是让
- 我就废话不多说了,大家还是直接看代码吧~# 导入pptx包from pptx import Presentationprs = Presen