基于Pygame怎样实现吃水果的小游戏
Admin 2022-08-01 群英技术资讯 359 次浏览
随机掉落:西瓜加分、葡萄减分、炸弹一条生命值初始为二。鼠标右键移动。加减多少分具体就等你们自己玩儿了哈,都剧透了就不好玩了撒!每次的游戏代码都给你们留点儿底,嘻嘻,自己摸索嘛~
小编使用的环境:Python3、Pycharm社区版、tkinter、Pygame模块,部分自 带模块不展示。
模块安装:pip install -i https://pypi.douban.com/simple/+模块名
准备了背景音乐更有劲儿啦!记得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)消耗结束
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
我们有时候需要把一些机密文件发给多个客户,为了避免客户泄露文件,会在机密文件中添加水印。本文将利用Python实现批量为PDF添加水印,需要的可以参考一下
这篇文章主要介绍了Python 类方法和静态方法之间的区别,静态方法并不是真正意义上的类方法,它只是一个被放到类里的函数而已,更多内容需要的朋友可以参考一下
这篇文章主要介绍了Python 常见的配置文件写法梳理汇总,文章围绕主题展开主题相关梳理总结,需要的朋友可以参考一下
这篇文章主要为大家详细介绍了python pygame控制物体移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍了Python 迭代器Iterator详情,迭代器可以帮助我们解决面对复杂的数据场景时,快速简便的获取数据,下文关于其详细介绍,需要的小伙伴可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008