导语
咳咳咳.....《猫和老鼠》相比几乎是90后的童年吧,说实话小时候还蛮喜欢看来着!
今天的话出场的也是跟上面动画片儿同款猫咪的名字TOM猫的游戏哈,来看看具体是什么?
Ps:小介绍
TOM猫,出自苹果公司的一款游戏。这只猫会学你说话,抚摸它的头和肚子会发出呼噜声,打它的肚子它会叫,踩它
的两只脚,会分别发出不同的叫声,摸它的鼻子还会打喷嚏哦~
正文
一、简介
汤姆1.0版本:Tom是一只宠物猫。
玩法 :点击按钮会做出相应的动作哈。敲锣、放屁、向你扔东西、吃了一只鸟、
倒一杯牛奶给他喝、生气了要用爪子挠你。点击头会晕倒、点击肚子会疼、点击二只jio也会跳脚等等
哈哈哈,与汤姆一起玩耍,享受欢乐和笑声吧~
二、准备中
2.1 素材准备(很多仅部分)
按钮:
喝牛奶:
等等等........
2.2 环境
本文用到的环境模块:Python3、Pycharm、Pygame以及一些自带的模块。
模块安装部分:
pip install +模块名 或带镜像源:pip install -i +模块名
三、正式敲代码
主要是分为三大块py文件写的游戏小程序:Tomcat.py、、Tom.py。
3.1 设置
定义的这个源文件主要是设置背景、获取各种图片源码文件。
import pygame
class Setting(object):
# 初始化变量函数 ==> 加载图片及初始化变量
def __init__(self):
# 1. 背景图片
= pygame.image.load("image;)
# 2. 设置卡片图片
= [
pygame.image.load("cards/plan;),
pygame.image.load("cards/plan;),
pygame.image.load("cards/plan;),
pygame.image.load("cards/plan;),
pygame.image.load("cards/plan;),
pygame.image.load("cards/plan;),
]
# 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设置窗口标题
('汤姆猫',)
# 1.2设置死循环
while True:
# 1.4 业务逻辑执行
()
# 1.5 图形图片绘制
()
# 1.3 更新屏幕
()
''' 2. action函数 业务逻辑处理 '''
def action(self):
# 2.1 事件监听迭代
for event in ():
# 2.2 判断事件类型
if event.type == :
()
# 2.3 鼠标单击
mouseX, mouseY = ()
# 获取鼠标单击事件
# [0] 左键单击 [1] 左键双击 [2] 右击
leftFlag = ()[0]
# 2.4 判断
if leftFlag and 30 < mouseX < 30+60 \
and 300 < mouseY < 300 + 60:
# 吃鸟的动作
#print("进入了")
= 0
# 重新设置index
= 0
elif leftFlag and 30 < mouseX < 30+60 \
and 370 < mouseY < 370 + 60:
= 1
= 0
elif leftFlag and 30 < mouseX < 30+60 \
and 440 < mouseY < 440 + 60:
= 2
= 0
elif leftFlag and 250 < mouseX < 250+60 \
and 300 < mouseY < 300 + 60:
= 3
= 0
elif leftFlag and 250 < mouseX < 250+60 \
and 370 < mouseY < 370 + 60:
= 4
= 0
elif leftFlag and 250 < mouseX < 250+60 \
and 440 < mouseY < 440 + 60:
= 5
= 0
#....未完
elif leftFlag and 250 < mouseX < 250+60 \
and 440 < mouseY < 440 + 60:
= 6
= 0
''' 3. paint函数 绘制图形'''
def paint(self):
# 3.6 判断是否执行动画效果
if == 0:
# 3.2 图片集转换值增加
+= 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 = / 10 % len)
# 3.4 重新赋值图片 ix --> float
= [int(ix)]
# 判断是否是最后一张
if int(ix) == 39:
= -1
elif == 1:
+= 1
ix = / 10 % len)
= [int(ix)]
if int(ix) == 79:
= -1
elif == 2:
+= 1
ix = / 30 % len)
= [int(ix)]
if int(ix) == 11:
= -1
elif == 3:
+= 1
ix = / 10 % len)
= [int(ix)]
if int(ix) == 26:
= -1
elif == 4:
+= 1
ix = / 10 % len)
= [int(ix)]
if int(ix) == 22:
= -1
elif == 5:
+= 1
ix = / 10 % len)
= [int(ix)]
if int(ix) == 54:
= -1
elif == 6:#头
+= 1
ix = / 10 % len)
= [int(ix)]
if int(ix) == 24:
= -1
elif == 7:#肚子
+= 1
ix = / 10 % len)
= [int(ix)]
if int(ix) == 32:
= -1
elif == 8:#尾巴
+= 1
ix = / 10 % len)
= [int(ix)]
if int(ix) == 79:
= -1
elif == 9:#左脚
+= 1
ix = / 10 % len)
= [int(ix)]
if int(ix) == 28:
= -1
elif == 10:#右脚
+= 1
ix = / 10 % len)
= [int(ix)]
if int(ix) == 28:
= -1
# 3.1 绘制背景图片
(,(320, 512)), (0, 0))
# 3.5 绘制吃鸟动作
, (30, 300))
, (30, 370))
, (30, 440))
, (250, 300))
, (250, 370))
, (250, 440))
#, (250, 440))
#, (250, 440))
#, (250, 440))
#, (250, 440))
#, (250, 440))
''' 4.__init__ 函数 属性初始化'''
def __init__(self):
# 窗口大小设置
= ((320, 512), 0, 0)
# 背景图片
= pygame.image.load("Animations/Ea;)
# 图片列表存储
= ("Animations/Eat/eat_0", "Animations/Eat/eat_", ".jpg", 40)
= ("Animations/Drink/drink_0","Animations/Drink/drink_",".jpg", 80)
= ("Animations/Cymbal/cymbal_0", "Animations/Cymbal/cymbal_", ".jpg", 12)
= ("Animations/Fart/fart_0", "Animations/Fart/fart_", ".jpg", 27)
= ("Animations/Pie/pie_0", "Animations/Pie/pie_", ".jpg", 23)
= ("Animations/Scratch/scratch_0", "Animations/Scratch/scratch_", ".jpg", 55)
= ("Animations/Angry/angry_0", "Animations/Angry/angry_", ".jpg", 25)
= ("Animations/Stomach/stomach_0", "Animations/Stomach/stomach_", ".jpg", 33)
= ("Animations/Knockout/knockout_0", "Animations/Knockout/knockout_", ".jpg", 80)
= ("Animations/FootLeft/footLeft_0", "Animations/FootLeft/footLeft_", ".jpg", 29)
= ("Animations/FootRight/footRight_0", "Animations/FootRight/footRight_", ".jpg", 29)
# 图片集转换值
= 0
# 吃鸟
= pygame.image.load("Button;)
# 判断动画动作
= -1
# 喝奶
= pygame.image.load("Button;)
= pygame.image.load("Button;)
= pygame.image.load("Button;)
= pygame.image.load("Button;)
= pygame.image.load("Button;)
''' 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 返回列表
imageLi(pygame.image.load(imgPath))
# 5.5 返回
return imageList
if __name__ == '__main__':
# 获取类
tm = Tom()
# 调用类方法
()
3.3 设置Tom.py
定义TomCat.py源是主程序,运行就在这里哈。
import pygame,sys
class Tom1(object):
''' 1.mian() 函数 '''
def main(self):
# 1.1设置标题
("会说话的汤姆猫")
# 1.2 死循环
while True:
# 1.4 业务逻辑执行
()
# 1.5 绘制图形
()
# 1.3 刷新屏幕
()
''' 2. action() '''
def action(self):
# 2.1 事件迭代监听
for event in ():
# 2.2 退出
if event.type == :
()
# 2.3 鼠标监听
if event.type == :
# 2.4获取鼠标信息
mouseX, mouseY = ()
# 2.5 获取鼠标点击
leftFlag = ()[0]
# 2.6 判断吃鸟动作
if leftFlag and 30 < mouseX < 90\
and 300 < mouseY <360:
# 2.7设置图片总数
= 40
# 2.8 获取图片
("eat")
# 2.9 设置图片集转换值
= 0
# 2.7 判断喝牛奶动作
elif leftFlag and 30 < mouseX < 90\
and 360 < mouseY <420:
= 80
("drink")
# 2.8 判断敲锣动作
elif leftFlag and 30 < mouseX < 90\
and 420 < mouseY <480:
= 12
("cymbal")
elif leftFlag and 250 < mouseX < 310\
and 300 < mouseY <360:
= 27
("fart")
elif leftFlag and 250 < mouseX < 310\
and 360 < mouseY <420:
= 23
("pie")
elif leftFlag and 250 < mouseX < 310\
and 420 < mouseY <480:
= 55
("scratch")
elif leftFlag and 90 < mouseX < 200\
and 90 < mouseY <250:
= 55
("knockout")
elif leftFlag and 220 < mouseX < 280\
and 400 < mouseY <480:
= 25
("angry")
elif leftFlag and 100 < mouseX < 180\
and 360 < mouseY <420:
= 33
("stomach")
elif leftFlag and 155 < mouseX < 205\
and 470 < mouseY <512:
= 29
("footLeft")
elif leftFlag and 100 < mouseX < 150\
and 470 < mouseY <512:
= 29
("footRight")
''' 3. paint()'''
def paint(self):
# 3.1绘制背景图片
(, (320, 512)),(0, 0))
# 3.2 绘制吃鸟动作
,(30, 300))
# 3.3 绘制喝牛奶动作
, (30, 360))
, (30, 420))
, (250, 300))
, (250, 360))
, (250, 420))
# 绘制头部
# 3.3 判断是否运行动画
# count = -1 index = 0
if *10 > :
+= 1
ix = / 10 % len(self.images)
# 赋值
= self.images[int(ix)]
else:
# 结束清空所有值
= -1
= 0
# 列表的清空
self.images = []
''' 4. init() '''
def __init__(self):
# 4.1 背景
= ((320, 512),0,0)
# 4.2 背景图
= pygame.image.load("Animations/Ea;)
# 4.3 吃鸟的动作
= pygame.image.load("Button;)
# 4.4 图片存储列表
self.images = []
# 4.5 图片集转换值
= 0
# 4.6 图片数量
= -1
# 4.7 喝牛奶
= pygame.image.load("Button;)
# 4.8 敲锣
= pygame.image.load("Button;)
# 4.9
= pygame.image.load("Button;)
# 4.10
= pygame.image.load("Button;)
# 4.11
= pygame.image.load("Button;)
''' 5. getImage() '''
def getImage(self, name):
# 5.1 获取图片
for i in range(0, ):
# 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 静态截图展示——
总结
这款就是儿童版的小游戏了哈哈哈~
完整的免费源码领取处:
如需完整的项目源码+素材源码基地见:#私信小编06#、即可获取免费的福利!
你们的支持是我最大的动力!!记得三连哦~mua 欢迎大家阅读往期的文章哦~