導語


咳咳咳.....《貓和老鼠》相比幾乎是90后的童年吧,說實話小時候還蠻喜歡看來著!
今天的話出場的也是跟上面影片片兒同款貓咪的名字TOM貓的游戲哈,來看看具體是什么?
Ps:小介紹
TOM貓,出自蘋果公司的一款游戲,這只貓會學你說話,撫摸它的頭和肚子會發出呼嚕聲,打它
的肚子它會叫,踩它的兩只腳,會分別發出不同的叫聲,摸它的鼻子還會打噴嚏哦~


正文
完整的原始碼+素材都在原始碼基地,小編主頁左側的資訊獲取,都是免費分滴!
一、簡介
湯姆1.0版本:Tom是一只寵物貓,
玩法 :點擊按鈕會做出相應的動作哈,敲鑼、放屁、向你扔東西、吃了一只鳥、
倒一杯牛奶給他喝、生氣了要用爪子撓你,點擊頭會暈倒、點擊肚子會疼、點擊二只jio也會跳腳等
等哈哈哈,與湯姆一起玩耍,享受歡樂和笑聲吧~
二、準備中
2.1 素材準備(很多區域分)
按鈕:

喝牛奶:

等等等........

2.2 環境
本文用到的環境模塊:Python3、Pycharm、Pygame以及一些自帶的模塊,
模塊安裝部分:
pip install +模塊名 或帶鏡像源:pip install -i https://pypi.douban.com/simple/ +模塊名
三、正式敲代碼
主要是分為三大塊py檔案寫的游戲小程式:TomCat.py、setting.py、Tom.py,
3.1 設定setting.py
定義的setting.py這個源檔案主要是設定背景、獲取各種圖片原始碼檔案,
import pygame
class Setting(object):
# 初始化變數函式 ==> 加載圖片及初始化變數
def __init__(self):
# 1. 背景圖片
self.background = pygame.image.load("images/screenshot1.png")
# 2. 設定卡片圖片
self.cardImages = [
pygame.image.load("cards/plants/SunFlower.png"),
pygame.image.load("cards/plants/SunFlowerG.png"),
pygame.image.load("cards/plants/Peashooter.png"),
pygame.image.load("cards/plants/PeashooterG.png"),
pygame.image.load("cards/plants/WallNut.png"),
pygame.image.load("cards/plants/WallNutG.png"),
]
# 3.獲取植物圖片方法
def getPlantsImage(self,count, name, status):
# 3.1 獲取圖片
images = []
for i in range(0, count):
# 3.2 判斷路徑地址
if i < 10: # name ==> "eat"
imgPath = "plants/" + name + "/" + status + "/" + status + "_0" + str(i) + ".png"
else:
imgPath = "plants/" + name + "/" + status + "/" + status + "_" + str(i) + ".png"
# 3.3 存盤路徑地址
images.append(pygame.image.load(imgPath))
return images
# 4. 獲取僵尸圖片方法
def getZombiesImage(self,count, name, status):
# 4.1 獲取圖片
images = []
for i in range(0, count):
# 4.2 判斷路徑地址
if i < 10: # name ==> "eat"
imgPath = "zombies/" + name + "/" + status + "/" + status +"_0" + str(i) + ".png"
else:
imgPath = "zombies/" + name + "/" + status + "/" + status + "_" + str(i) + ".png"
# 4.3 存盤路徑地址
images.append(pygame.image.load(imgPath))
return images
3.2 設定TomCat.py
?定義TomCat.py源檔案主要是搭建游戲的視窗資訊、業務邏輯處理、繪制圖形、屬性初始化、獲取圖片串列,
'''
Tomcat
1. 搭建視窗資訊
'''
import pygame,sys
class Tom(object):
''' 1. main() 視窗基本注釋 '''
def main(self):
# 1.1設定視窗標題
pygame.display.set_caption('湯姆貓',)
# 1.2設定死回圈
while True:
# 1.4 業務邏輯執行
self.action()
# 1.5 圖形圖片繪制
self.paint()
# 1.3 更新螢屏
pygame.display.update()
''' 2. action函式 業務邏輯處理 '''
def action(self):
# 2.1 事件監聽迭代
for event in pygame.event.get():
# 2.2 判斷事件型別
if event.type == pygame.QUIT:
sys.exit()
# 2.3 滑鼠單擊
mouseX, mouseY = pygame.mouse.get_pos()
# 獲取滑鼠單擊事件
# [0] 左鍵單擊 [1] 左鍵雙擊 [2] 右擊
leftFlag = pygame.mouse.get_pressed()[0]
# 2.4 判斷
if leftFlag and 30 < mouseX < 30+60 \
and 300 < mouseY < 300 + 60:
# 吃鳥的動作
#print("進入了")
self.animation = 0
# 重新設定index
self.index = 0
elif leftFlag and 30 < mouseX < 30+60 \
and 370 < mouseY < 370 + 60:
self.animation = 1
self.index = 0
elif leftFlag and 30 < mouseX < 30+60 \
and 440 < mouseY < 440 + 60:
self.animation = 2
self.index = 0
elif leftFlag and 250 < mouseX < 250+60 \
and 300 < mouseY < 300 + 60:
self.animation = 3
self.index = 0
elif leftFlag and 250 < mouseX < 250+60 \
and 370 < mouseY < 370 + 60:
self.animation = 4
self.index = 0
elif leftFlag and 250 < mouseX < 250+60 \
and 440 < mouseY < 440 + 60:
self.animation = 5
self.index = 0
#....未完
elif leftFlag and 250 < mouseX < 250+60 \
and 440 < mouseY < 440 + 60:
self.animation = 6
self.index = 0
''' 3. paint函式 繪制圖形'''
def paint(self):
# 3.6 判斷是否執行影片效果
if self.animation == 0:
# 3.2 圖片集轉換值增加
self.index += 1
# 3.3 設定圖片轉換頻率
# index 0/10 ==> 0 % 10 ==> 0
# index 1/10 ==> 0 % 10 ==> 0
# ...
# index 11/10 ==> 1 % 10 ==> 1
# ix = 0 0 0 0 0 0 0 0 0 0 1
# ix 每呼叫一次函式 10次才會重繪一次
ix = self.index / 10 % len(self.eats)
# 3.4 重新賦值圖片 ix --> float
self.background = self.eats[int(ix)]
# 判斷是否是最后一張
if int(ix) == 39:
self.animation = -1
elif self.animation == 1:
self.index += 1
ix = self.index / 10 % len(self.drinks)
self.background = self.drinks[int(ix)]
if int(ix) == 79:
self.animation = -1
elif self.animation == 2:
self.index += 1
ix = self.index / 30 % len(self.cymbals)
self.background = self.cymbals[int(ix)]
if int(ix) == 11:
self.animation = -1
elif self.animation == 3:
self.index += 1
ix = self.index / 10 % len(self.farts)
self.background = self.farts[int(ix)]
if int(ix) == 26:
self.animation = -1
elif self.animation == 4:
self.index += 1
ix = self.index / 10 % len(self.pies)
self.background = self.pies[int(ix)]
if int(ix) == 22:
self.animation = -1
elif self.animation == 5:
self.index += 1
ix = self.index / 10 % len(self.scratchs)
self.background = self.scratchs[int(ix)]
if int(ix) == 54:
self.animation = -1
elif self.animation == 6:#頭
self.index += 1
ix = self.index / 10 % len(self.angrys)
self.background = self.angrys[int(ix)]
if int(ix) == 24:
self.animation = -1
elif self.animation == 7:#肚子
self.index += 1
ix = self.index / 10 % len(self.stomachs)
self.background = self.stomachs[int(ix)]
if int(ix) == 32:
self.animation = -1
elif self.animation == 8:#尾巴
self.index += 1
ix = self.index / 10 % len(self.knockouts)
self.background = self.knockouts[int(ix)]
if int(ix) == 79:
self.animation = -1
elif self.animation == 9:#左腳
self.index += 1
ix = self.index / 10 % len(self.footLefts)
self.background = self.stomachs[int(ix)]
if int(ix) == 28:
self.animation = -1
elif self.animation == 10:#右腳
self.index += 1
ix = self.index / 10 % len(self.footRights)
self.background = self.footRights[int(ix)]
if int(ix) == 28:
self.animation = -1
# 3.1 繪制背景圖片
self.screen.blit(pygame.transform.scale(self.background,(320, 512)), (0, 0))
# 3.5 繪制吃鳥動作
self.screen.blit(self.eat, (30, 300))
self.screen.blit(self.drink, (30, 370))
self.screen.blit(self.cymbal, (30, 440))
self.screen.blit(self.fart, (250, 300))
self.screen.blit(self.pie, (250, 370))
self.screen.blit(self.scratch, (250, 440))
#self.screen.blit(self.angry, (250, 440))
#self.screen.blit(self.stomach, (250, 440))
#self.screen.blit(self.knockout, (250, 440))
#self.screen.blit(self.footLeft, (250, 440))
#self.screen.blit(self.footRights, (250, 440))
''' 4.__init__ 函式 屬性初始化'''
def __init__(self):
# 視窗大小設定
self.screen = pygame.display.set_mode((320, 512), 0, 0)
# 背景圖片
self.background = pygame.image.load("Animations/Eat/eat_00.jpg")
# 圖片串列存盤
self.eats = self.getImage("Animations/Eat/eat_0", "Animations/Eat/eat_", ".jpg", 40)
self.drinks = self.getImage("Animations/Drink/drink_0","Animations/Drink/drink_",".jpg", 80)
self.cymbals = self.getImage("Animations/Cymbal/cymbal_0", "Animations/Cymbal/cymbal_", ".jpg", 12)
self.farts = self.getImage("Animations/Fart/fart_0", "Animations/Fart/fart_", ".jpg", 27)
self.pies = self.getImage("Animations/Pie/pie_0", "Animations/Pie/pie_", ".jpg", 23)
self.scratchs = self.getImage("Animations/Scratch/scratch_0", "Animations/Scratch/scratch_", ".jpg", 55)
self.angrys = self.getImage("Animations/Angry/angry_0", "Animations/Angry/angry_", ".jpg", 25)
self.stomachs = self.getImage("Animations/Stomach/stomach_0", "Animations/Stomach/stomach_", ".jpg", 33)
self.knockouts = self.getImage("Animations/Knockout/knockout_0", "Animations/Knockout/knockout_", ".jpg", 80)
self.footLefts = self.getImage("Animations/FootLeft/footLeft_0", "Animations/FootLeft/footLeft_", ".jpg", 29)
self.footRights = self.getImage("Animations/FootRight/footRight_0", "Animations/FootRight/footRight_", ".jpg", 29)
# 圖片集轉換值
self.index = 0
# 吃鳥
self.eat = pygame.image.load("Buttons/eat.png")
# 判斷影片動作
self.animation = -1
# 喝奶
self.drink = pygame.image.load("Buttons/drink.png")
self.cymbal = pygame.image.load("Buttons/cymbal.png")
self.fart = pygame.image.load("Buttons/fart.png")
self.pie = pygame.image.load("Buttons/pie.png")
self.scratch = pygame.image.load("Buttons/scratch.png")
''' 5. getImage() 獲取圖片串列'''
def getImage(self,prePath1, prePath2, endPath, picNum):
# 5.4 定義串列
imageList = []
# 5.1 回圈迭代賦值圖片
for i in range(0, picNum):
# 5.2 獲取圖片路徑
if i < 10:
imgPath = prePath1 + str(i) + endPath
else:
imgPath = prePath2 + str(i) + endPath
# 5.3 回傳串列
imageList.append(pygame.image.load(imgPath))
# 5.5 回傳
return imageList
if __name__ == '__main__':
# 獲取類
tm = Tom()
# 呼叫類方法
tm.main()
3.3 設定Tom.py
?定義TomCat.py源是主程式,運行就在這里哈,
import pygame,sys
class Tom1(object):
''' 1.mian() 函式 '''
def main(self):
# 1.1設定標題
pygame.display.set_caption("會說話的湯姆貓")
# 1.2 死回圈
while True:
# 1.4 業務邏輯執行
self.action()
# 1.5 繪制圖形
self.paint()
# 1.3 重繪螢屏
pygame.display.update()
''' 2. action() '''
def action(self):
# 2.1 事件迭代監聽
for event in pygame.event.get():
# 2.2 退出
if event.type == pygame.QUIT:
sys.exit()
# 2.3 滑鼠監聽
if event.type == pygame.MOUSEBUTTONDOWN:
# 2.4獲取滑鼠資訊
mouseX, mouseY = pygame.mouse.get_pos()
# 2.5 獲取滑鼠點擊
leftFlag = pygame.mouse.get_pressed()[0]
# 2.6 判斷吃鳥動作
if leftFlag and 30 < mouseX < 90\
and 300 < mouseY <360:
# 2.7設定圖片總數
self.count = 40
# 2.8 獲取圖片
self.getImage("eat")
# 2.9 設定圖片集轉換值
self.index = 0
# 2.7 判斷喝牛奶動作
elif leftFlag and 30 < mouseX < 90\
and 360 < mouseY <420:
self.count = 80
self.getImage("drink")
# 2.8 判斷敲鑼動作
elif leftFlag and 30 < mouseX < 90\
and 420 < mouseY <480:
self.count = 12
self.getImage("cymbal")
elif leftFlag and 250 < mouseX < 310\
and 300 < mouseY <360:
self.count = 27
self.getImage("fart")
elif leftFlag and 250 < mouseX < 310\
and 360 < mouseY <420:
self.count = 23
self.getImage("pie")
elif leftFlag and 250 < mouseX < 310\
and 420 < mouseY <480:
self.count = 55
self.getImage("scratch")
elif leftFlag and 90 < mouseX < 200\
and 90 < mouseY <250:
self.count = 55
self.getImage("knockout")
elif leftFlag and 220 < mouseX < 280\
and 400 < mouseY <480:
self.count = 25
self.getImage("angry")
elif leftFlag and 100 < mouseX < 180\
and 360 < mouseY <420:
self.count = 33
self.getImage("stomach")
elif leftFlag and 155 < mouseX < 205\
and 470 < mouseY <512:
self.count = 29
self.getImage("footLeft")
elif leftFlag and 100 < mouseX < 150\
and 470 < mouseY <512:
self.count = 29
self.getImage("footRight")
''' 3. paint()'''
def paint(self):
# 3.1繪制背景圖片
self.screen.blit(pygame.transform.scale(self.background, (320, 512)),(0, 0))
# 3.2 繪制吃鳥動作
self.screen.blit(self.eat,(30, 300))
# 3.3 繪制喝牛奶動作
self.screen.blit(self.drink, (30, 360))
self.screen.blit(self.cymbal, (30, 420))
self.screen.blit(self.fart, (250, 300))
self.screen.blit(self.pie, (250, 360))
self.screen.blit(self.scratch, (250, 420))
# 繪制頭部
# 3.3 判斷是否運行影片
# count = -1 index = 0
if self.count*10 > self.index:
self.index += 1
ix = self.index / 10 % len(self.images)
# 賦值
self.background = self.images[int(ix)]
else:
# 結束清空所有值
self.count = -1
self.index = 0
# 串列的清空
self.images = []
''' 4. init() '''
def __init__(self):
# 4.1 背景
self.screen = pygame.display.set_mode((320, 512),0,0)
# 4.2 背景圖
self.background = pygame.image.load("Animations/Eat/eat_00.jpg")
# 4.3 吃鳥的動作
self.eat = pygame.image.load("Buttons/eat.png")
# 4.4 圖片存盤串列
self.images = []
# 4.5 圖片集轉換值
self.index = 0
# 4.6 圖片數量
self.count = -1
# 4.7 喝牛奶
self.drink = pygame.image.load("Buttons/drink.png")
# 4.8 敲鑼
self.cymbal = pygame.image.load("Buttons/cymbal.png")
# 4.9
self.fart = pygame.image.load("Buttons/fart.png")
# 4.10
self.pie = pygame.image.load("Buttons/pie.png")
# 4.11
self.scratch = pygame.image.load("Buttons/scratch.png")
''' 5. getImage() '''
def getImage(self, name):
# 5.1 獲取圖片
for i in range(0, self.count):
# 5.2 判斷路徑地址
if i < 10:# name ==> "eat"
imgPath = "Animations/"+name+"/"+name+"_0"+str(i)+".jpg"
else:
imgPath = "Animations/"+name+"/"+name+"_"+str(i)+".jpg"
# 5.3 存盤路徑地址
self.images.append(pygame.image.load(imgPath))
四、效果展示
4.1 動態視頻展示——
Pygame版會說話的Tom貓來襲~超有趣滴
4.1 靜態截圖展示——

?

總結

這款就是會說話的tom貓就是一款兒童版的小游戲了哈哈哈~?我不玩,我就看看.jpg
?
關注小編獲取更多精彩內容拉~
👑文章匯總——
1.1Python—2021 |已有文章匯總 | 持續更新,直接看這篇就夠了~

?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/353523.html
標籤:其他
上一篇:SimpleFOC之ESP32(一)—— 搭建開發環境
下一篇:三字棋的實作以及模塊化的思想
