使用pygame实现垃圾分类小游戏功能(已获校级二等奖)


Posted in Python onJuly 23, 2020

项目简介

本项目报名参加了“兖州中材杯”武汉理工大学第十一届环保创意作品大赛艺术理念组比赛。组员共三名,本人负责代码实现部分,其余两人分别负责项目策划与场景人物绘制。

项目背景

小组中负责策划的同学经过一定的调研之后发现,我校在校学生普遍缺乏垃圾分类方面的知识。经过讨论后,我们把游戏方向定位于“垃圾分类”,游戏类型定位于像素风游戏,由于本人水平和时间有限,只能将玩法设计得尽量简单。感谢另外两位组员,他们的努力掩盖了我水平上的不足。

玩法介绍

家控制一名角色在操场背景上移动,垃圾桶在操场边上。捡起垃圾丢进相应的垃圾桶内,若全部正确投放则游戏成功,否则游戏失败。游戏期间,若有不清楚该作何分类的垃圾,则可以点击图书馆的按钮,进入图书馆查询。图书馆提供垃圾图鉴和分类标准两种信息。垃圾图鉴帮助玩家分辨自己捡到的垃圾,而分类标准指每种垃圾的定义及举例。

项目成果

本项目在“兖州中材杯”武汉理工大学第十一届环保创意作品大赛艺术理念组比赛中最终获得了第五名、二等奖的成绩,距第四名仅差0.5分。

项目实现

写项目时本人仅自学了一个月的Python,且本人是大一新生,对计算机科学的基础知识了解甚少,若代码风格幼稚、愚蠢,还望读者见谅。

模块划分

游戏按场景分为以下几个模块:开始游戏界面游戏说明界面人物选择界面操场界面(主要游戏场地)、图书馆外界面图书馆内界面游戏结束界面

开始游戏界面:背景(像素化的学校建筑),游戏标题,三个按钮(开始、游戏说明、退出)。

使用pygame实现垃圾分类小游戏功能(已获校级二等奖)

游戏说明界面:背景(与开始界面相同),游戏说明文字,返回按钮。

使用pygame实现垃圾分类小游戏功能(已获校级二等奖)

人物选择界面:背景,提示(选择人物),两个可选人物。

使用pygame实现垃圾分类小游戏功能(已获校级二等奖)

操场界面:背景,人物,垃圾桶,随机产生的垃圾,图书馆按钮。

使用pygame实现垃圾分类小游戏功能(已获校级二等奖)
使用pygame实现垃圾分类小游戏功能(已获校级二等奖)

图书馆外界面:背景(我校图书馆的像素风绘制)、操场按钮(会到操场)、进入按钮(进入图书馆内部)。

使用pygame实现垃圾分类小游戏功能(已获校级二等奖)

图书馆内界面:背景(我校图书馆内部),各类垃圾图标按钮(厨余垃圾、可回收垃圾、有害垃圾、其他垃圾),垃圾图鉴按钮,返回按钮。子界面:各类垃圾信息界面,垃圾图鉴界面,返回按钮。

使用pygame实现垃圾分类小游戏功能(已获校级二等奖)
使用pygame实现垃圾分类小游戏功能(已获校级二等奖)
使用pygame实现垃圾分类小游戏功能(已获校级二等奖)
使用pygame实现垃圾分类小游戏功能(已获校级二等奖)
使用pygame实现垃圾分类小游戏功能(已获校级二等奖)
使用pygame实现垃圾分类小游戏功能(已获校级二等奖)

游戏结束界面:胜利界面,失败界面。

使用pygame实现垃圾分类小游戏功能(已获校级二等奖)
使用pygame实现垃圾分类小游戏功能(已获校级二等奖)

代码实现

直接上代码:

import pygame as py
import sys
import random
from pygame.locals import *
#===========================================================
#========================前期准备===========================
py.init()
#注:游戏需要的所有文件(图片等)都放在同游戏目录的"Files"目录下。
#定义一个按钮类
class Button(py.rect.Rect):
 def __init__(self, obj):
  super().__init__(obj)
 def has(self, pos):
  if self.right >= pos[0] >= self.left and self.bottom >= pos[1] >= self.top:
   return True
  else:
   return False

screen = py.display.set_mode((1000, 650))
#===========================================================
#=========================图书馆内==========================

def knowledge(selection):
 path = 'Files\\inside_liberary\\knowledge' + str(selection) + '.jpg'
 know = py.image.load(path)
 know = py.transform.smoothscale(know,(1000,650))
 screen.blit(know,(0,0))
 exits = py.image.load('Files\\inside_liberary\\back.jpg')
 exits = py.transform.smoothscale(exits,(72,72))
 exit_button = screen.blit(exits,(918, 570))
 exit_button = Button(exit_button)
 py.display.flip()
 #进入事件循环
 while True:
  for event in py.event.get():
   if event.type == QUIT:
    sys.exit()
   if event.type == MOUSEBUTTONDOWN:
    pos = py.mouse.get_pos()
    #点击离开该页面
    if exit_button.has(pos):
     selection = 0
     break
  if not selection:
   break

def inside_Liberary():
 ilib = py.image.load('Files\\inside_liberary\\Inside.jpg')
 ilib = py.transform.smoothscale(ilib,(1000,650))
 screen.blit(ilib,(0,0))
 #退出图书馆的按钮
 exits = py.image.load('Files\\inside_liberary\\exit.jpg')
 exits = py.transform.smoothscale(exits,(72,81))
 exit_button = screen.blit(exits,(5, 560))
 exit_button = Button(exit_button)
 #厨余垃圾按钮
 rubbish1 = py.image.load('Files\\inside_liberary\\rubbish1.jpg')
 rubbish1 = py.transform.smoothscale(rubbish1,(150,298))
 rubbish1_button = screen.blit(rubbish1, (80,150))
 rubbish1_button = Button(rubbish1_button)
 #可回收垃圾按钮
 rubbish2 = py.image.load('Files\\inside_liberary\\rubbish2.jpg')
 rubbish2 = py.transform.smoothscale(rubbish2,(150,298))
 rubbish2_button = screen.blit(rubbish2, (310,150))
 rubbish2_button = Button(rubbish2_button)
 #有害垃圾按钮
 rubbish3 = py.image.load('Files\\inside_liberary\\rubbish3.jpg')
 rubbish3 = py.transform.smoothscale(rubbish3,(150,298))
 rubbish3_button = screen.blit(rubbish3, (540,150))
 rubbish3_button = Button(rubbish3_button)
 #不可回收垃圾按钮
 rubbish4 = py.image.load('Files\\inside_liberary\\rubbish4.jpg')
 rubbish4 = py.transform.smoothscale(rubbish4,(150,298))
 rubbish4_button = screen.blit(rubbish4, (770,150))
 rubbish4_button = Button(rubbish4_button)
 #图鉴按钮
 rubbish5 = py.image.load('Files\\inside_liberary\\rubbish5.jpg')
 rubbish5 = py.transform.smoothscale(rubbish5,(82, 72))
 rubbish5_button = screen.blit(rubbish5, (903,560))
 rubbish5_button = Button(rubbish5_button)
 
 py.display.flip()
 selection = -1
 #进入事件循环
 while True:
  for event in py.event.get():
   if event.type == QUIT:
    sys.exit()
   if event.type == MOUSEBUTTONDOWN:
    pos = py.mouse.get_pos()
    #点击离开图书馆
    if exit_button.has(pos):
     selection = 0
     break
    #点击厨余垃圾
    if rubbish1_button.has(pos):
     selection = 1
     break
    #点击可回收垃圾
    if rubbish2_button.has(pos):
     selection = 2
     break
    #点击有害垃圾
    if rubbish3_button.has(pos):
     selection = 3
     break
    #点击不可回收垃圾
    if rubbish4_button.has(pos):
     selection = 4
     break
    #点击图鉴
    if rubbish5_button.has(pos):
     selection = 5
     break
  if selection != -1:
   break
 if selection:
  knowledge(selection)
  inside_Liberary()
#===========================================================
#=========================图书馆外==========================

def outside_Liberary():
 olib = py.image.load('Files\\outside_liberary\\Outside.jpg')
 olib = py.transform.smoothscale(olib,(1000,650))
 screen.blit(olib,(0,0))
 #进入图书馆的按钮
 enter = py.image.load('Files\\outside_liberary\\enter_lib.jpg')
 enter = py.transform.smoothscale(enter,(72, 72))
 enter_button = screen.blit(enter,(470,550))
 enter_button = Button(enter_button)
 #退出图书馆的按钮
 exits = py.image.load('Files\\outside_liberary\\playground.jpg')
 exits = py.transform.smoothscale(exits,(72, 51))
 exit_button = screen.blit(exits,(5, 590))
 exit_button = Button(exit_button)
 py.display.flip()
 selection = -1
 #进入事件循环
 while True:
  for event in py.event.get():
   if event.type == QUIT:
    sys.exit()
   if event.type == MOUSEBUTTONDOWN:
    pos = py.mouse.get_pos()
    #点击返回操场
    if exit_button.has(pos):
     selection = 0
     break
    #点击进入图书馆
    if enter_button.has(pos):
     selection = 1
     break
  if selection != -1:
   break
 if selection:
  inside_Liberary()
  outside_Liberary()

#===========================================================
#==========================游戏帮助=========================

def help_page():
 background = py.image.load('Files\\help\\background.jpg')
 background = py.transform.smoothscale(background,(1000,650))
 screen.blit(background,(0,0))
 #返回按钮
 exits = py.image.load('Files\\help\\back.jpg')
 exits = py.transform.smoothscale(exits,(72,57))
 exit_button = screen.blit(exits,(5, 585))
 exit_button = Button(exit_button)
 py.display.flip()
 back = 0
 while True:
  for event in py.event.get():
   if event.type == QUIT:
    sys.exit()
   if event.type == MOUSEBUTTONDOWN:
    pos = py.mouse.get_pos()
    if exit_button.has(pos):
     back = 1
     break
  if back:
   break
    
#===========================================================
#==========================游戏结束=========================

def game_over(result):
 path = 'Files\\game_over\\result' + str(result) + '.jpg'
 background = py.image.load(path)
 background = py.transform.smoothscale(background,(1000,650))
 screen.blit(background,(0,0))
 py.display.flip()
 temp = 0
 while True:
  for event in py.event.get():
   if event.type == QUIT:
    sys.exit()
   if event.type == MOUSEBUTTONDOWN:
    temp = 1
    break
  if temp:
   break

#===========================================================
#==========================操场环节=========================

choices = ['01', '02', '11', '12', '21', '22', '31']
class Rubbish():
 def __init__(self, sort):
  self.sort = sort
  self.img = py.image.load('Files\\playground\\' + sort + '.jpg')
  x = random.randint(100, 1400)
  y = random.randint(110, 900)
  self.position = self.img.get_rect()
  self.position = self.position.move((x, y))
  screen.blit(self.img, self.position)
class Role():
 def __init__(self, role):
  self.r_side = py.image.load('Files\\playground\\' + role + '1.jpg')
  self.r_walk = py.image.load('Files\\playground\\' + role + '2.jpg')
  self.l_side = py.transform.flip(self.r_side, True, False)
  self.l_walk = py.transform.flip(self.r_walk, True, False)
  self.img = self. r_side
  self.position = self.img.get_rect()
  screen.blit(self.img, self.position)
  self.rubbish = None
 def move(self, key):
  if key == K_UP:
   if self.position.top <= 200:
    return (0, 2)
   else:
    self.position = self.position.move(0, -2)
    return 0
  if key == K_DOWN:
   if self.position.bottom >= 450:
    return (0, -2)
   else:
    self.position = self.position.move(0, 2)
    return 0
  if key == K_RIGHT:
   if self.position.right >= 800:
    return (-2, 0)
   else:
    self.position = self.position.move(2, 0)
    return 0
  if key == K_LEFT:
   if self.position.left <= 200:
    return (2, 0)
   else:
    self.position = self.position.move(-2, 0)
    return 0
  
class Trash_can():
 def __init__(self, num):
  self. num = num
  self.img = py.image.load('Files\\playground\\' + str(num) + '.jpg')
  self.img = py.transform.smoothscale(self.img,(100, 92))
  self.position = self.img.get_rect()
  self.position = self.position.move((100 + num*200, 0))
  screen.blit(self.img, self.position)
def playground(selection):
 background = py.image.load('Files\\playground\\Playground.jpg')
 screen.blit(background, [0, 0])
 lib = py.image.load('Files\\playground\\liberary.jpg')
 lib = py.transform.smoothscale(lib, (78, 72))
 lib_button = screen.blit(lib, (900, 10))
 lib_button = Button(lib_button)
 trash_can = []
 for num in range(0, 4):
  trash_can.append(Trash_can(num))
 role = Role(selection)
 rubbish = []
 for sort in choices:
  rubbish.append(Rubbish(sort))
 py.display.flip()
 down = 0
 go = None
 move_bg = [0, 0]
 temp = 0
 while True:
  for event in py.event.get():
   if event.type == QUIT:
    sys.exit()
   if event.type == MOUSEBUTTONDOWN:
    pos = py.mouse.get_pos()
    if lib_button.has(pos):
     outside_Liberary()
   if event.type == KEYDOWN and\
    event.key in (K_UP, K_DOWN, K_RIGHT, K_LEFT):
    if event.key == K_RIGHT:
     role.img = role.r_side
    elif event.key == K_LEFT:
     role.img = role.l_side
    down = 1
    go = event.key
   if event.type == KEYUP and event.key == go:
    if event.key == K_RIGHT:
     role.img = role.r_side
    elif event.key == K_LEFT:
     role.img = role.l_side
    down = 0
  take = role.position.collidelist([each.position for each in rubbish])
  if take >= 0 and not role.rubbish:
   role.rubbish = rubbish[take].sort[0]
   del rubbish[take]
  put = role.position.collidelist([each.position for each in trash_can])
  if put >= 0 and role.rubbish:
   if role.rubbish == str(trash_can[put].num):
    role.rubbish = None
    if not len(rubbish):
     game_over(1)
     break
   else:
    game_over(2)
    break
  if down:
   moved = role.move(go)
   temp += 1
   if not temp % 20:
    if role.img == role.r_side:
     role.img = role.r_walk
    elif role.img == role.r_walk:
     role.img = role.r_side
    elif role.img == role.l_side:
     role.img = role.l_walk
    else:
     role.img = role.l_side
   if moved:
    if 0 >= moved[0] + move_bg[0] >= -497 and \
     0 >= moved[1] + move_bg[1] >= -326:
     for i in range(2):
      move_bg[i] += moved[i]
     for each in rubbish:
      each.position = each.position.move(moved)
     for each in trash_can:
      each.position = each.position.move(moved)
    elif role.position.left - moved[0] >= 0 and\
      role.position.right - moved[0] <= 1000 and\
      role.position.top - moved[1] >= 0 and\
      role.position.bottom - moved[1] <= 650:
     role. position = role.position.move([-i for i in moved])
  screen.blit(background, move_bg)
  lib = py.image.load('Files\\playground\\liberary.jpg')
  lib = py.transform.smoothscale(lib, (78, 72))
  lib_button = screen.blit(lib, (900, 10))
  lib_button = Button(lib_button)
  for each in trash_can:
   screen.blit(each.img, each.position)
  for each in rubbish:
   screen.blit(each.img, each.position)
  screen.blit(role.img, role.position)
  py.display.flip()


#===========================================================
#==========================选择人物=========================

def choose_role():
 background = py.image.load('Files\\choose_player\\background.jpg')
 background = py.transform.smoothscale(background,(1000,650))
 screen.blit(background, (0, 0))
 man = py.image.load('Files\\choose_player\\man.jpg')
 man = py.transform.smoothscale(man,(123, 325))
 man_button = screen.blit(man,(200, 200))
 man_button = Button(man_button)
 woman = py.image.load('Files\\choose_player\\woman.jpg')
 woman = py.transform.smoothscale(woman,(113, 325))
 woman_button = screen.blit(woman,(687, 200))
 woman_button = Button(woman_button)
 py.display.flip()
 while True:
  for event in py.event.get():
   if event.type == QUIT:
    sys.exit()
   if event.type == MOUSEBUTTONDOWN:
    pos = py.mouse.get_pos()
    if man_button.has(pos):
     return 'man'
    if woman_button.has(pos):
     return 'woman'
    
#===========================================================
#========================开始游戏界面========================

def start():
 background = py.image.load('Files\\start\\background.jpg')
 background = py.transform.smoothscale(background,(1000,650))
 screen.blit(background, (0, 0))
 start_game = py.image.load('Files\\start\\start_game.jpg')
 start_game = py.transform.smoothscale(start_game,(140, 149))
 start_button = screen.blit(start_game,(150, 330))
 start_button = Button(start_button)
 game_help = py.image.load('Files\\start\\game_help.jpg')
 game_help = py.transform.smoothscale(game_help,(280, 182))
 help_button = screen.blit(game_help,(380, 320))
 help_button = Button(help_button)
 quit_game = py.image.load('Files\\start\\quit_game.jpg')
 quit_game = py.transform.smoothscale(quit_game,(200,160))
 quit_button = screen.blit(quit_game,(680, 330))
 quit_button = Button(quit_button)
 py.display.flip()
 while True:
  for event in py.event.get():
   if event.type == QUIT:
    sys.exit()
   if event.type == MOUSEBUTTONDOWN:
    pos = py.mouse.get_pos()
    if start_button.has(pos):
     role = choose_role()
     playground(role)
     break
    elif help_button.has(pos):
     help_page()
     break
    elif quit_button.has(pos):
     sys.exit()
  break
 start()
start()

游戏效果

游戏演示如下:

使用pygame实现垃圾分类小游戏功能(已获校级二等奖)

点此观看视频:垃圾分类小游戏

总结

这个游戏是本人第一次写出一百行以上的代码,写完后没来得及进行进一步的优化就提交了项目。

到此这篇关于使用pygame实现垃圾分类小游戏功能(已获校级二等奖)的文章就介绍到这了,更多相关pygame垃圾分类小游戏内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
浅谈python中scipy.misc.logsumexp函数的运用场景
Jun 23 Python
Python使用matplotlib实现的图像读取、切割裁剪功能示例
Apr 28 Python
Python3实现的字典、列表和json对象互转功能示例
May 22 Python
Python实现模拟登录网易邮箱的方法示例
Jul 05 Python
解决Python pandas plot输出图形中显示中文乱码问题
Dec 12 Python
详解小白之KMP算法及python实现
Apr 04 Python
Python 使用 PyMysql、DBUtils 创建连接池提升性能
Aug 14 Python
Django实现网页分页功能
Oct 31 Python
利用Python自动化操作AutoCAD的实现
Apr 01 Python
Python实现动态循环输出文字功能
May 07 Python
Python分析最近大火的网剧《隐秘的角落》
Jul 02 Python
Pycharm自动添加文件头注释和函数注释参数的方法
Oct 23 Python
Python字符串函数strip()原理及用法详解
Jul 23 #Python
Python使用sys.exc_info()方法获取异常信息
Jul 23 #Python
Python中Selenium库使用教程详解
Jul 23 #Python
Python基于traceback模块获取异常信息
Jul 23 #Python
Python TestSuite生成测试报告过程解析
Jul 23 #Python
快速解释如何使用pandas的inplace参数的使用
Jul 23 #Python
Python分类测试代码实例汇总
Jul 23 #Python
You might like
codeigniter中测试通过的分页类示例
2014/04/17 PHP
php实现过滤字符串中的中文和数字实例
2015/07/29 PHP
php读取出一个文件夹及其子文件夹下所有文件的方法示例
2017/06/15 PHP
推荐dojo学习笔记
2007/03/24 Javascript
jquery api参考 visualjquery 中国线路 速度快
2007/11/30 Javascript
js 调整select 位置的函数
2008/02/21 Javascript
基于jquery的无刷新分页技术
2011/06/11 Javascript
JavaScript 高级篇之DOM文档,简单封装及调用、动态添加、删除样式(六)
2012/04/07 Javascript
javascript 通用loading动画效果实例代码
2014/01/14 Javascript
为什么Node.js会这么火呢?Node.js流行的原因
2014/12/01 Javascript
你知道setTimeout是如何运行的吗?
2016/08/16 Javascript
vue2.0 子组件改变props值,并向父组件传值的方法
2018/03/01 Javascript
JavaScript读写二进制数据的方法详解
2018/09/09 Javascript
微信小程序select下拉框实现源码
2019/11/08 Javascript
javascript实现简易的计算器
2020/01/17 Javascript
基于leaflet.js实现修改地图主题样式的流程分析
2020/05/15 Javascript
解决父组件将子组件作为弹窗调用只执行一次created的问题
2020/07/24 Javascript
详解Python中映射类型(字典)操作符的概念和使用
2015/08/19 Python
Python中pillow知识点学习
2018/04/30 Python
Python下使用Scrapy爬取网页内容的实例
2018/05/21 Python
PyCharm设置SSH远程调试的方法
2018/07/17 Python
浅析python的Lambda表达式
2019/02/27 Python
安装好Pycharm后如何配置Python解释器简易教程
2019/06/28 Python
python统计字符串中字母出现次数代码实例
2020/03/02 Python
利用python实现凯撒密码加解密功能
2020/03/31 Python
工程师岗位职责
2013/11/08 职场文书
数控技术专科生自我评价
2014/01/08 职场文书
校园十佳歌手策划书
2014/01/22 职场文书
《狼和小羊》教学反思
2014/04/20 职场文书
解放思想大讨论活动总结
2015/05/09 职场文书
2015年幼儿教师个人工作总结
2015/05/20 职场文书
金正昆讲礼仪观后感
2015/06/11 职场文书
小学语文教学随笔
2015/08/14 职场文书
关于运动会的广播稿
2015/08/19 职场文书
python全面解析接口返回数据
2022/02/12 Python
Windows Server 2019 域控制器安装图文教程
2022/04/28 Servers