pgzero模块制作汤姆猫游戏
游戏运行示例
游戏初始界面
猫咪动画界面
游戏代码
import pgzrun
import pygame
def yinyue(n):pygame.mixer.init()pygame.mixer.music.load('music/' + n + '.m4a')pygame.mixer.music.play(1, 0)WIDTH = 384
HEIGHT = 614cat = Actor('eat/eat_0.jpg')chifan = Actor('吃饭.png',[22, 300])
shengqi = Actor('吃饭.png',[272, 527])
qiaoluo = Actor('音乐.png',[22, 350])
he = Actor('喝.png',[22, 400])
modu = Actor('气压.png',[22, 450])
mozuojiao = Actor('吃饭.png',[220, 568])
moyoujiao = Actor('吃饭.png',[157, 570])
datou = Actor('datou.png',[194, 153])
rengdongxi = Actor('蛋糕.png',[22, 500])
kouping = Actor('猫爪.png',[22, 550])
duzitong = Actor('duzi.png',[188, 442])num = 0
name = ''def draw():cat.draw()chifan.draw()qiaoluo.draw()he.draw()modu.draw()rengdongxi.draw()kouping.draw()def playImg():global numcat.image = name +'/' + name + '_' + str(num) + '.jpg'num = num + 1if num >= name1 + 1:num = 1clock.unschedule(playImg)def action(a,n):global name,name1name = nname1 = aclock.schedule_interval(playImg, 0.07)def on_mouse_down(pos):global numif chifan.collidepoint(pos):num = 0action(39,'eat')yinyue('eat')if shengqi.collidepoint(pos): num = 0action(25,'angry')if qiaoluo.collidepoint(pos): num = 0action(12,'cymbal')if he.collidepoint(pos):num = 0action(79,'drink') if modu.collidepoint(pos): num = 0action(27,'fart')if mozuojiao.collidepoint(pos): num = 0action(21,'foot_left')if moyoujiao.collidepoint(pos):num = 0action(29,'foot_right')if datou.collidepoint(pos): num = 0action(75,'knockout')if rengdongxi.collidepoint(pos): num = 0action(23,'pie')if kouping.collidepoint(pos): num = 0action(55,'scratch')if duzitong.collidepoint(pos): num = 0action(33,'stomach')pgzrun.go()
游戏图片及其音效详见我的下载