Pygame实战:花巨资筹备的一款Tom猫游戏,你玩过嘛?

Pygame实战:花巨资筹备的一款Tom猫游戏,你玩过嘛?

首页休闲益智欢乐马鲁猫更新时间:2024-05-08
导语

咳咳咳.....《猫和老鼠》相比几乎是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 静态截图展示——

总结

这款就是儿童版的小游戏了哈哈哈~​

完整的免费源码领取处:

如需完整的项目源码 素材源码基地见:#私信小编06#、即可获取免费的福利!

你们的支持是我最大的动力!!记得三连哦~mua 欢迎大家阅读往期的文章哦~

查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved