欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用pygame實(shí)現(xiàn)垃圾分類(lèi)小游戲功能(已獲校級(jí)二等獎(jiǎng))

 更新時(shí)間:2020年07月23日 14:29:39   作者:Vincentish  
這篇文章主要介紹了使用pygame實(shí)現(xiàn)垃圾分類(lèi)小游戲功能(已獲校級(jí)二等獎(jiǎng)),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

項(xiàng)目簡(jiǎn)介

本項(xiàng)目報(bào)名參加了“兗州中材杯”武漢理工大學(xué)第十一屆環(huán)保創(chuàng)意作品大賽藝術(shù)理念組比賽。組員共三名,本人負(fù)責(zé)代碼實(shí)現(xiàn)部分,其余兩人分別負(fù)責(zé)項(xiàng)目策劃與場(chǎng)景人物繪制。

項(xiàng)目背景

小組中負(fù)責(zé)策劃的同學(xué)經(jīng)過(guò)一定的調(diào)研之后發(fā)現(xiàn),我校在校學(xué)生普遍缺乏垃圾分類(lèi)方面的知識(shí)。經(jīng)過(guò)討論后,我們把游戲方向定位于“垃圾分類(lèi)”,游戲類(lèi)型定位于像素風(fēng)游戲,由于本人水平和時(shí)間有限,只能將玩法設(shè)計(jì)得盡量簡(jiǎn)單。感謝另外兩位組員,他們的努力掩蓋了我水平上的不足。

玩法介紹

家控制一名角色在操場(chǎng)背景上移動(dòng),垃圾桶在操場(chǎng)邊上。撿起垃圾丟進(jìn)相應(yīng)的垃圾桶內(nèi),若全部正確投放則游戲成功,否則游戲失敗。游戲期間,若有不清楚該作何分類(lèi)的垃圾,則可以點(diǎn)擊圖書(shū)館的按鈕,進(jìn)入圖書(shū)館查詢(xún)。圖書(shū)館提供垃圾圖鑒和分類(lèi)標(biāo)準(zhǔn)兩種信息。垃圾圖鑒幫助玩家分辨自己撿到的垃圾,而分類(lèi)標(biāo)準(zhǔn)指每種垃圾的定義及舉例。

項(xiàng)目成果

本項(xiàng)目在“兗州中材杯”武漢理工大學(xué)第十一屆環(huán)保創(chuàng)意作品大賽藝術(shù)理念組比賽中最終獲得了第五名、二等獎(jiǎng)的成績(jī),距第四名僅差0.5分。

項(xiàng)目實(shí)現(xiàn)

寫(xiě)項(xiàng)目時(shí)本人僅自學(xué)了一個(gè)月的Python,且本人是大一新生,對(duì)計(jì)算機(jī)科學(xué)的基礎(chǔ)知識(shí)了解甚少,若代碼風(fēng)格幼稚、愚蠢,還望讀者見(jiàn)諒。

模塊劃分

游戲按場(chǎng)景分為以下幾個(gè)模塊:開(kāi)始游戲界面、游戲說(shuō)明界面、人物選擇界面操場(chǎng)界面(主要游戲場(chǎng)地)、圖書(shū)館外界面圖書(shū)館內(nèi)界面、游戲結(jié)束界面。

開(kāi)始游戲界面:背景(像素化的學(xué)校建筑),游戲標(biāo)題,三個(gè)按鈕(開(kāi)始、游戲說(shuō)明、退出)。

開(kāi)始游戲界面

游戲說(shuō)明界面:背景(與開(kāi)始界面相同),游戲說(shuō)明文字,返回按鈕。

游戲說(shuō)明界面

人物選擇界面:背景,提示(選擇人物),兩個(gè)可選人物。

任務(wù)選擇界面

操場(chǎng)界面:背景,人物,垃圾桶,隨機(jī)產(chǎn)生的垃圾,圖書(shū)館按鈕。

操場(chǎng)背景
游戲中的操場(chǎng)界面

圖書(shū)館外界面:背景(我校圖書(shū)館的像素風(fēng)繪制)、操場(chǎng)按鈕(會(huì)到操場(chǎng))、進(jìn)入按鈕(進(jìn)入圖書(shū)館內(nèi)部)。

圖書(shū)館外界面

圖書(shū)館內(nèi)界面:背景(我校圖書(shū)館內(nèi)部),各類(lèi)垃圾圖標(biāo)按鈕(廚余垃圾、可回收垃圾、有害垃圾、其他垃圾),垃圾圖鑒按鈕,返回按鈕。子界面:各類(lèi)垃圾信息界面,垃圾圖鑒界面,返回按鈕。

圖書(shū)館內(nèi)界面
廚余垃圾信息
可回收物信息
有害垃圾信息
其他垃圾信息
垃圾圖鑒界面

游戲結(jié)束界面:勝利界面,失敗界面。

勝利界面
失敗界面

代碼實(shí)現(xiàn)

直接上代碼:

import pygame as py
import sys
import random
from pygame.locals import *
#===========================================================
#========================前期準(zhǔn)備===========================
py.init()
#注:游戲需要的所有文件(圖片等)都放在同游戲目錄的"Files"目錄下。
#定義一個(gè)按鈕類(lèi)
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))
#===========================================================
#=========================圖書(shū)館內(nèi)==========================

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()
 #進(jìn)入事件循環(huán)
 while True:
  for event in py.event.get():
   if event.type == QUIT:
    sys.exit()
   if event.type == MOUSEBUTTONDOWN:
    pos = py.mouse.get_pos()
    #點(diǎn)擊離開(kāi)該頁(yè)面
    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))
 #退出圖書(shū)館的按鈕
 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
 #進(jìn)入事件循環(huán)
 while True:
  for event in py.event.get():
   if event.type == QUIT:
    sys.exit()
   if event.type == MOUSEBUTTONDOWN:
    pos = py.mouse.get_pos()
    #點(diǎn)擊離開(kāi)圖書(shū)館
    if exit_button.has(pos):
     selection = 0
     break
    #點(diǎn)擊廚余垃圾
    if rubbish1_button.has(pos):
     selection = 1
     break
    #點(diǎn)擊可回收垃圾
    if rubbish2_button.has(pos):
     selection = 2
     break
    #點(diǎn)擊有害垃圾
    if rubbish3_button.has(pos):
     selection = 3
     break
    #點(diǎn)擊不可回收垃圾
    if rubbish4_button.has(pos):
     selection = 4
     break
    #點(diǎn)擊圖鑒
    if rubbish5_button.has(pos):
     selection = 5
     break
  if selection != -1:
   break
 if selection:
  knowledge(selection)
  inside_Liberary()
#===========================================================
#=========================圖書(shū)館外==========================

def outside_Liberary():
 olib = py.image.load('Files\\outside_liberary\\Outside.jpg')
 olib = py.transform.smoothscale(olib,(1000,650))
 screen.blit(olib,(0,0))
 #進(jìn)入圖書(shū)館的按鈕
 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)
 #退出圖書(shū)館的按鈕
 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
 #進(jìn)入事件循環(huán)
 while True:
  for event in py.event.get():
   if event.type == QUIT:
    sys.exit()
   if event.type == MOUSEBUTTONDOWN:
    pos = py.mouse.get_pos()
    #點(diǎn)擊返回操場(chǎng)
    if exit_button.has(pos):
     selection = 0
     break
    #點(diǎn)擊進(jìn)入圖書(shū)館
    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
    
#===========================================================
#==========================游戲結(jié)束=========================

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

#===========================================================
#==========================操場(chǎng)環(huán)節(jié)=========================

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'
    
#===========================================================
#========================開(kāi)始游戲界面========================

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()

游戲效果

游戲演示如下:

點(diǎn)此觀看視頻:垃圾分類(lèi)小游戲

總結(jié)

這個(gè)游戲是本人第一次寫(xiě)出一百行以上的代碼,寫(xiě)完后沒(méi)來(lái)得及進(jìn)行進(jìn)一步的優(yōu)化就提交了項(xiàng)目。

到此這篇關(guān)于使用pygame實(shí)現(xiàn)垃圾分類(lèi)小游戲功能(已獲校級(jí)二等獎(jiǎng))的文章就介紹到這了,更多相關(guān)pygame垃圾分類(lèi)小游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python如何自定義鄰接表圖類(lèi)

    Python如何自定義鄰接表圖類(lèi)

    這篇文章主要介紹了Python如何自定義鄰接表圖類(lèi)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • python遞歸&迭代方法實(shí)現(xiàn)鏈表反轉(zhuǎn)

    python遞歸&迭代方法實(shí)現(xiàn)鏈表反轉(zhuǎn)

    這篇文章主要介紹了python遞歸&迭代方法實(shí)現(xiàn)鏈表反轉(zhuǎn),文章分享一段詳細(xì)實(shí)現(xiàn)代碼,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)或工作有所幫助
    2022-02-02
  • Python使用Matplotlib繪制三維散點(diǎn)圖詳解流程

    Python使用Matplotlib繪制三維散點(diǎn)圖詳解流程

    matplotlib是基建立在python之上,適用于創(chuàng)建靜態(tài),動(dòng)畫(huà)和交互式可視化,通常與數(shù)據(jù)分析模塊pandas搭配使用,用于數(shù)據(jù)的分析和展示,適用于主流的操作系統(tǒng),如Linux、Win、Mac
    2022-11-11
  • Python Pandas處理csv文件常用示例

    Python Pandas處理csv文件常用示例

    Pandas是一個(gè)非常強(qiáng)大的數(shù)據(jù)操作python包,支持各種數(shù)據(jù)格式,包括CSV文件,本文就來(lái)介紹一下Python Pandas處理csv文件常用示例,感興趣的可以了解一下
    2023-12-12
  • python中eval與int的區(qū)別淺析

    python中eval與int的區(qū)別淺析

    這篇文章主要給大家介紹了關(guān)于python中eval與int的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 利用PyQt5中QLabel組件實(shí)現(xiàn)亞克力磨砂效果

    利用PyQt5中QLabel組件實(shí)現(xiàn)亞克力磨砂效果

    Windows10 在 UWP 應(yīng)用中支持亞克力畫(huà)刷,可以在部件的底部繪制亞克力效果的背景圖。本文將使用QLabel來(lái)模擬這個(gè)磨砂過(guò)程,感興趣的可以了解一下
    2022-03-03
  • python sys模塊及常用功能

    python sys模塊及常用功能

    在Python中,sys模塊提供了訪(fǎng)問(wèn)和使用解釋器的許多功能的方法,包括命令行參數(shù)、環(huán)境變量、路徑管理、標(biāo)準(zhǔn)輸入輸出流等,sys模塊是Python的標(biāo)準(zhǔn)庫(kù)的一部分,不需要額外安裝即可使用,本文介紹python sys模塊相關(guān)知識(shí),感興趣的朋友一起看看吧
    2024-09-09
  • python實(shí)現(xiàn)給微信指定好友定時(shí)發(fā)送消息

    python實(shí)現(xiàn)給微信指定好友定時(shí)發(fā)送消息

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)給微信指定好友定時(shí)發(fā)消息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Python+pyecharts繪制雙動(dòng)態(tài)曲線(xiàn)教程詳解

    Python+pyecharts繪制雙動(dòng)態(tài)曲線(xiàn)教程詳解

    pyecharts 是一個(gè)用于生成 Echarts 圖表的類(lèi)庫(kù)。Echarts 是百度開(kāi)源的一個(gè)數(shù)據(jù)可視化 JS 庫(kù)。用 Echarts 生成的圖可視化效果非常棒。本文將用pyecharts繪制雙動(dòng)態(tài)曲線(xiàn),需要的可以參考一下
    2022-06-06
  • Python列表的淺拷貝與深拷貝

    Python列表的淺拷貝與深拷貝

    這篇文章主要介紹了Python列表的淺拷貝與深拷貝,對(duì)列表深拷貝就是無(wú)論怎樣改動(dòng)新列表,單維or多維,原列表都不變,需要的小伙伴可以參考下面更詳細(xì)內(nèi)容
    2022-03-03

最新評(píng)論