花费巨资制作一款儿童游戏!95后必玩过!Python来实现!

379 阅读4分钟

导语

哈喽大家好!

我是木子,想我了吗??

大声点儿,听不见~

ok 收到了,每日游戏更新!你们的木木子又来,准时更新中~

图片

《会说话的TOM猫》是多少人的童年记忆?

​​​​   哈哈哈~我姐就经常把这个打开给家里的孩子玩儿,小孩子玩的经常哈哈大笑!

这只猫会学你说话,抚摸它的头和肚子会发出呼噜声,打它的肚子它会叫,踩它的两只脚,会分别发出不同的叫声,摸它的鼻子还会打喷嚏。

我看着也感觉挺有趣的对,没错,今天就手动带大家仿写一款会说话的TOM猫游戏给大家

欢迎阅读往期游戏文章:

1.成语接龙游戏项目。 2.塔防游戏项目。 3.记忆翻牌游戏项目。 4.吃豆豆小游戏项目。

5.外星人入侵游戏项目。6.数织游戏项目。 7.脑力锻炼游戏项目。 8.垃圾分类小游戏项目。

9.雷霆战机游戏项目。 10.”我的兔子“游戏项目11.八音符游戏项目。12.拼图小游戏项目。

13.滑雪小游戏项目。 14.桌面宠物项目。 15.无敌金身小恐龙。 16.坦克大战游戏项目。

17.走迷宫游戏项目。 18.像素鸟游戏项目。19.21款python一行代码小游戏 20.贪食蛇游戏。

21.打地鼠游戏项目。 22.测试打字游戏项目。

更多内容每日更新......

正文

开始啦,先准备好开始界面的一些图片素材等等:

​​

这上面的是tom猫会做的一些动作的按钮。

  • (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)定义tom猫动作action。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函数 绘制图形。tom猫的形象。

    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

  • 效果图:

​​​​​​​

​​总结

好啦!一款会说话的tom猫就完成了!

代码真的超多的,如需要完整的代码老规矩。源码基地:#私信小编06#即可免费领取哦!!

制作不易,记得一键三连哦!!

 如果需要新手安装包激活码、配套完整项目+源码笔记、更多Python资料 也在源码基地哦!

图片