大家好,今天我们开始做一个新的游戏——火柴人逃脱。
以下是效果图:

温馨提示:代码里有图片资源,你可以自己用GIMP画,也可以去下载:
图片
我们来看第一部分代码:

from tkinter import *
import time,random

这一部分没什么特别的,你可以到我的Ball专栏中看细解。

class Game:def __init__(self):self.tk=Tk()self.tk.title('stick man game')self.tk.resizable(0,0)self.tk.wm_attributes('-topmost',1)self.canvas=Canvas(self.tk,width=500,height=500,highlightthickness=0)self.canvas.pack()self.tk.update()self.canvas_width=500self.canvas_height=500self.bg=PhotoImage(file='../image/background.gif')w=self.bg.width()h=self.bg.height()for i in range(0,5):for j in range(0,5):self.canvas.create_image(i*w,j*h,image=self.bg,anchor='nw')self.sprites=[]self.running=Truedef mainloop(self):while 1:if self.running==True:for sprite in self.sprites:sprite.move()else:canvas.create_text(100, 150, text='ok', fill='blue', font=('Times', 20))self.tk.update_idletasks()self.tk.update()time.sleep(0.01)

以上是第二部分,它主要控制画布的各项参数(详见详解)。

class Coords:def __init__(self,x1=0,y1=0,x2=0,y2=0):self.x1=x1self.y1=y1self.x2=x2self.y2=y2
def within_x(co1,co2):if (co1.x1 > co2.x1 and co1.x1<co2.x2) or (co1.x2>co2.x1 and co1.x1<co2.x1) or (co2.x2>co1.x1 and co2.x1<co1.x2)\or (co1.x2>co2.x1 and co1.x2<co2.x1):return Trueelse:return False
def within_y(co1,co2):if (co1.y1 > co2.y1 and co1.y1<co2.y2) or (co1.y2>co2.y1 and co1.y2<co2.y2) or (co2.y1>co1.y1 and co2.y1<co1.y2)\or (co1.y2>co2.y1 and co1.y2<co2.y1):return Trueelse:return False
def colided_left(co1,co2):if within_y(co1,co2):if co1.x1<=co2.x2 and co1.x1>=co2.x1:return Truereturn False
def colided_right(co1,co2):if within_y(co1,co2):if co1.x2>=co2.x1 and co1.x2<=co2.x2:return Truereturn False
def colided_top(co1,co2):if within_x(co1,co2):if co1.y1<=co2.y2 and co1.y1>=co2.y1:return Truereturn False
def collided_bottom(y,co1,co2):if within_x(co1,co2):y_calc=co1.y2+yif y_calc>=co2.y1 and y_calc<=co2.y2:return Truereturn False

第三部分主要是精灵的冲突检测。

class Sprite:def __init__(self,game):self.game=gameself.endgame=Falseself.coordinates=Nonedef move(self):passdef coords(self):return self.coordinatesclass PlatformSprite(Sprite):def __init__(self,game,photo_image,x,y,width,height):Sprite.__init__(self, game)self.photo_image=photo_imageself.image=game.canvas.create_image(x,y,image=self.photo_image,anchor='nw')self.coordinates=Coords(x,y,x+width,y+height)
class StickFigureSprite(Sprite):def __init__(self,game):Sprite.__init__(self,game)self.images_left=[PhotoImage(file='../image/stick-L1.gif'),PhotoImage(file='../image/stick-L2.gif'),PhotoImage(file='../image/stick-L3.gif')]self.images_right = [PhotoImage(file='../image/stick-R1.gif'),PhotoImage(file='../image/stick-R2.gif'),PhotoImage(file='../image/stick-R3.gif')]self.image=game.canvas.create_image(200,200,image=self.images_left[0],anchor='nw')self.x=-2self.y=0self.current_image=0self.current_image_add=1self.jump_count=0self.last_time=time.time()self.coordinates=Coords()game.canvas.bind_all('<KeyPress-Left>',self.turn_left)game.canvas.bind_all('<KeyPress-Right>', self.turn_right)game.canvas.bind_all('<space>', self.jump)def turn_left(self,evt):if self.y==0:self.x=-2def turn_right(self,evt):if self.y==0:self.x=2def jump(self,evt):if self.y==0:self.y=-4self.jump_count=0def animate(self):if self.x!=0 and self.y==0:if time.time()-self.last_time>0.1:self.last_time=time.time()self.current_image+=self.current_image_addif self.current_image>=2:self.current_image_add=-1if self.current_image<=0:self.current_image_add=1if self.x<0:if self.y!=0:self.game.canvas.itemconfig(self.image,image=self.images_left[2])else:self.game.canvas.itemconfig(self.image, image=self.images_left[self.current_image])elif self.x>0:if self.y != 0:self.game.canvas.itemconfig(self.image, image=self.images_right[2])else:self.game.canvas.itemconfig(self.image, image=self.images_right[self.current_image])def coords(self):xy=self.game.canvas.coords(self.image)self.coordinates.x1=xy[0]self.coordinates.y1 = xy[1]self.coordinates.x2 = xy[0]+27self.coordinates.y2 = xy[1]+30return self.coordinatesdef move(self):self.animate()if self.y<0:self.jump_count+=1if self.jump_count>20:self.y=4if self.y>0:self.jump_count-=1co=self.coords()left=Trueright=Truetop=Truebottom=Truefalling=Trueif self.y>0 and co.y2>=self.game.canvas_height:self.y=0bottom=Trueelif self.y<0 and co.y1<=0:self.y=0top=Falseif self.x>0 and co.x2>=self.game.canvas_width:self.x=0right=Trueelif self.x<0 and co.x1<=0:self.x=0left=Falsefor sprite in self.game.sprites:if sprite==self:continuesprite_co=sprite.coords()if top and self.y<0 and colided_top(co,sprite_co):self.y=-self.ytop=Falseif bottom and self.y>0 and collided_bottom(self.y,co,sprite_co):self.y=sprite_co.y1-co.y2if self.y<0:self.y=0bottom=Falsetop=Falseif bottom and falling and self.y==0 and co.y2<self.game.canvas_height and collided_bottom(1,co,sprite_co):falling=Falseif left and self.x<0 and colided_left(co,sprite_co):self.x=0left=Falseif sprite.endgame:self.game.running=Falseif right and self.x<0 and colided_right(co,sprite_co):self.x=0right=Falseif sprite.endgame:self.game.running=Falseif sprite.endgame:self.game.running = Falseif falling and bottom and self.y==0 and co.y2<self.game.canvas_height:self.y=4self.game.canvas.move(self.image,self.x,self.y)
class DoorSprite(Sprite):def __init__(self,game,photo_image,x,y,width,height):Sprite.__init__(self,game)self.photo_image=photo_imageself.image=game.canvas.create_image(x,y,image=self.photo_image,anchor='nw')self.coordinates=Coords(x,y,x+(width/2),y+height)self.endgame=True

第四部分主要是精灵的运动和一些碰撞检测(别问我为什么讲的这么粗略,问就是之后有详解,今天我们只要搞清楚框架)。

g=Game()
platform1=PlatformSprite(g,PhotoImage(file='../image/platform3.gif'),0,480,100,10)
platform2=PlatformSprite(g,PhotoImage(file='../image/platform3.gif'),150,440,100,10)
platform3=PlatformSprite(g,PhotoImage(file='../image/platform2.gif'),300,400,100,10)
platform4=PlatformSprite(g,PhotoImage(file='../image/platform1.gif'),300,160,100,10)
platform5=PlatformSprite(g,PhotoImage(file='../image/platform2.gif'),175,350,66,10)
platform6=PlatformSprite(g,PhotoImage(file='../image/platform2.gif'),50,300,66,10)
platform7=PlatformSprite(g,PhotoImage(file='../image/platform2.gif'),170,120,66,10)
platform8=PlatformSprite(g,PhotoImage(file='../image/platform2.gif'),45,60,66,10)
platform9=PlatformSprite(g,PhotoImage(file='../image/platform3.gif'),170,250,32,10)
platform10=PlatformSprite(g,PhotoImage(file='../image/platform3.gif'),230,200,32,10)
g.sprites.append(platform1)
g.sprites.append(platform2)
g.sprites.append(platform3)
g.sprites.append(platform4)
g.sprites.append(platform5)
g.sprites.append(platform6)
g.sprites.append(platform7)
g.sprites.append(platform8)
g.sprites.append(platform9)
g.sprites.append(platform10)
door=DoorSprite(g,PhotoImage(file='../image/door1.gif'),45,30,40,35)
sf=StickFigureSprite(g)
g.sprites.append(sf)
g.mainloop()

第五部分是运行部分,可以把上面的代码调用。
以下是主体代码:

#stickman game
from tkinter import *
import time,random
class Game:def __init__(self):self.tk=Tk()self.tk.title('stick man game')self.tk.resizable(0,0)self.tk.wm_attributes('-topmost',1)self.canvas=Canvas(self.tk,width=500,height=500,highlightthickness=0)self.canvas.pack()self.tk.update()self.canvas_width=500self.canvas_height=500self.bg=PhotoImage(file='../image/background.gif')w=self.bg.width()h=self.bg.height()for i in range(0,5):for j in range(0,5):self.canvas.create_image(i*w,j*h,image=self.bg,anchor='nw')self.sprites=[]self.running=Truedef mainloop(self):while 1:if self.running==True:for sprite in self.sprites:sprite.move()else:canvas.create_text(100, 150, text='ok', fill='blue', font=('Times', 20))self.tk.update_idletasks()self.tk.update()time.sleep(0.01)
class Coords:def __init__(self,x1=0,y1=0,x2=0,y2=0):self.x1=x1self.y1=y1self.x2=x2self.y2=y2
def within_x(co1,co2):if (co1.x1 > co2.x1 and co1.x1<co2.x2) or (co1.x2>co2.x1 and co1.x1<co2.x1) or (co2.x2>co1.x1 and co2.x1<co1.x2)\or (co1.x2>co2.x1 and co1.x2<co2.x1):return Trueelse:return False
def within_y(co1,co2):if (co1.y1 > co2.y1 and co1.y1<co2.y2) or (co1.y2>co2.y1 and co1.y2<co2.y2) or (co2.y1>co1.y1 and co2.y1<co1.y2)\or (co1.y2>co2.y1 and co1.y2<co2.y1):return Trueelse:return False
def colided_left(co1,co2):if within_y(co1,co2):if co1.x1<=co2.x2 and co1.x1>=co2.x1:return Truereturn False
def colided_right(co1,co2):if within_y(co1,co2):if co1.x2>=co2.x1 and co1.x2<=co2.x2:return Truereturn False
def colided_top(co1,co2):if within_x(co1,co2):if co1.y1<=co2.y2 and co1.y1>=co2.y1:return Truereturn False
def collided_bottom(y,co1,co2):if within_x(co1,co2):y_calc=co1.y2+yif y_calc>=co2.y1 and y_calc<=co2.y2:return Truereturn False
class Sprite:def __init__(self,game):self.game=gameself.endgame=Falseself.coordinates=Nonedef move(self):passdef coords(self):return self.coordinatesclass PlatformSprite(Sprite):def __init__(self,game,photo_image,x,y,width,height):Sprite.__init__(self, game)self.photo_image=photo_imageself.image=game.canvas.create_image(x,y,image=self.photo_image,anchor='nw')self.coordinates=Coords(x,y,x+width,y+height)
class StickFigureSprite(Sprite):def __init__(self,game):Sprite.__init__(self,game)self.images_left=[PhotoImage(file='../image/stick-L1.gif'),PhotoImage(file='../image/stick-L2.gif'),PhotoImage(file='../image/stick-L3.gif')]self.images_right = [PhotoImage(file='../image/stick-R1.gif'),PhotoImage(file='../image/stick-R2.gif'),PhotoImage(file='../image/stick-R3.gif')]self.image=game.canvas.create_image(200,200,image=self.images_left[0],anchor='nw')self.x=-2self.y=0self.current_image=0self.current_image_add=1self.jump_count=0self.last_time=time.time()self.coordinates=Coords()game.canvas.bind_all('<KeyPress-Left>',self.turn_left)game.canvas.bind_all('<KeyPress-Right>', self.turn_right)game.canvas.bind_all('<space>', self.jump)def turn_left(self,evt):if self.y==0:self.x=-2def turn_right(self,evt):if self.y==0:self.x=2def jump(self,evt):if self.y==0:self.y=-4self.jump_count=0def animate(self):if self.x!=0 and self.y==0:if time.time()-self.last_time>0.1:self.last_time=time.time()self.current_image+=self.current_image_addif self.current_image>=2:self.current_image_add=-1if self.current_image<=0:self.current_image_add=1if self.x<0:if self.y!=0:self.game.canvas.itemconfig(self.image,image=self.images_left[2])else:self.game.canvas.itemconfig(self.image, image=self.images_left[self.current_image])elif self.x>0:if self.y != 0:self.game.canvas.itemconfig(self.image, image=self.images_right[2])else:self.game.canvas.itemconfig(self.image, image=self.images_right[self.current_image])def coords(self):xy=self.game.canvas.coords(self.image)self.coordinates.x1=xy[0]self.coordinates.y1 = xy[1]self.coordinates.x2 = xy[0]+27self.coordinates.y2 = xy[1]+30return self.coordinatesdef move(self):self.animate()if self.y<0:self.jump_count+=1if self.jump_count>20:self.y=4if self.y>0:self.jump_count-=1co=self.coords()left=Trueright=Truetop=Truebottom=Truefalling=Trueif self.y>0 and co.y2>=self.game.canvas_height:self.y=0bottom=Trueelif self.y<0 and co.y1<=0:self.y=0top=Falseif self.x>0 and co.x2>=self.game.canvas_width:self.x=0right=Trueelif self.x<0 and co.x1<=0:self.x=0left=Falsefor sprite in self.game.sprites:if sprite==self:continuesprite_co=sprite.coords()if top and self.y<0 and colided_top(co,sprite_co):self.y=-self.ytop=Falseif bottom and self.y>0 and collided_bottom(self.y,co,sprite_co):self.y=sprite_co.y1-co.y2if self.y<0:self.y=0bottom=Falsetop=Falseif bottom and falling and self.y==0 and co.y2<self.game.canvas_height and collided_bottom(1,co,sprite_co):falling=Falseif left and self.x<0 and colided_left(co,sprite_co):self.x=0left=Falseif sprite.endgame:self.game.running=Falseif right and self.x<0 and colided_right(co,sprite_co):self.x=0right=Falseif sprite.endgame:self.game.running=Falseif sprite.endgame:self.game.running = Falseif falling and bottom and self.y==0 and co.y2<self.game.canvas_height:self.y=4self.game.canvas.move(self.image,self.x,self.y)
class DoorSprite(Sprite):def __init__(self,game,photo_image,x,y,width,height):Sprite.__init__(self,game)self.photo_image=photo_imageself.image=game.canvas.create_image(x,y,image=self.photo_image,anchor='nw')self.coordinates=Coords(x,y,x+(width/2),y+height)self.endgame=True
g=Game()
platform1=PlatformSprite(g,PhotoImage(file='../image/platform3.gif'),0,480,100,10)
platform2=PlatformSprite(g,PhotoImage(file='../image/platform3.gif'),150,440,100,10)
platform3=PlatformSprite(g,PhotoImage(file='../image/platform2.gif'),300,400,100,10)
platform4=PlatformSprite(g,PhotoImage(file='../image/platform1.gif'),300,160,100,10)
platform5=PlatformSprite(g,PhotoImage(file='../image/platform2.gif'),175,350,66,10)
platform6=PlatformSprite(g,PhotoImage(file='../image/platform2.gif'),50,300,66,10)
platform7=PlatformSprite(g,PhotoImage(file='../image/platform2.gif'),170,120,66,10)
platform8=PlatformSprite(g,PhotoImage(file='../image/platform2.gif'),45,60,66,10)
platform9=PlatformSprite(g,PhotoImage(file='../image/platform3.gif'),170,250,32,10)
platform10=PlatformSprite(g,PhotoImage(file='../image/platform3.gif'),230,200,32,10)
g.sprites.append(platform1)
g.sprites.append(platform2)
g.sprites.append(platform3)
g.sprites.append(platform4)
g.sprites.append(platform5)
g.sprites.append(platform6)
g.sprites.append(platform7)
g.sprites.append(platform8)
g.sprites.append(platform9)
g.sprites.append(platform10)
door=DoorSprite(g,PhotoImage(file='../image/door1.gif'),45,30,40,35)
sf=StickFigureSprite(g)
g.sprites.append(sf)
g.mainloop()

别忘了点赞加关注。

(资源)
如果我的文章点赞达100,免费半天;
达500,免费3天;
关注我的,我给私发;
我的粉丝达到20,免费一周;
打赏的,永久免费,还有更多资源可得。
(可能会有点慢)
新手上路,如有不足,请见谅。

火柴人逃脱小游戏(python 超详细)相关推荐

  1. 不愧是大厂牛人!用Java实现象棋小游戏(附超详细,超长究极无敌代码)

    本文实例为大家分享了java实现象棋小游戏的具体代码,供大家参考,具体内容如下 用Eclipse编写 java环境1.8jdk 代码如下 package xiangqi象棋; /***中国象棋Java ...

  2. 微信火柴人html5小游戏,20个好玩的微信小游戏推荐!你玩过几个?

    50000+游戏爱好者已加入我们! 每天推荐好玩游戏! 加入我们,沐沐带你发现好游戏! 只有你想不到, 没有我找不到的好游戏! 「良心好游戏推荐」 搜罗了好玩的微信小游戏大全, 模拟经营游戏.恐怖游戏 ...

  3. 三子棋小游戏(超详细)

    目录 一.实现三子棋游戏的基本逻辑 二.具体的函数实现 1. 菜单函数 2. 棋盘的实现 2.1 棋盘的初始化 2.2 棋盘的打印 3. 玩家下棋 4. 电脑下棋 5.判断棋盘状态 三. 三个文件 3 ...

  4. Java实战项目:新手入门小游戏——连连看超详细教程

    小伙伴们应该都玩过连连看吧,今天呢叫大家用Java制作一个属于自己的连连看小游戏! 众所周知,想要学好Java光看视频或看书是不行的,一定要动手实践才可以,而且在面试中,面试官也会问你做过些什么项目? ...

  5. 用jquery制作2048小游戏(超详细)

    先放个效果图吧 首先,先把html结构搭好.新建一个html文件,设置好标题2048,按钮New Game以及score:0,这里按钮是用a标签来将javascript中的newgame函数给传进来, ...

  6. Unity学习制作Flappy Bird小游戏(超详细全教程,可发布到PC、Web、Android平台)

    本文中Flappy Bird基于Unity2019.4.7f1完成,源工程已部分代码改为适配安卓 flappy bird:一夜爆红的胖鸟 这是一款简单又困难的手机游戏,游戏中玩家必须控制一只胖乎乎的小 ...

  7. 火柴人生存挑战2html5游戏在线玩,火柴人生存挑战

    火柴人生存挑战是一款由独立制作人制作的横版跑酷过关小游戏,火柴人生存挑战拥有许多个不同的关卡等你来挑战,在这里你可以释放自己的力量,轻松展现出过关的本领!控制自己的火柴人进行跳跃,完成这场惊心动魄的试 ...

  8. 用python画简单火柴人代码-趣学Python编程

    Python是一种强大并通俗易懂的编程语言,而且它易学又好用!但是关于学习Python语言的书大多很枯燥无趣,读起来没什么乐趣.本书把你带入一个鲜活的Python编程世界.作者Jason R. Bri ...

  9. python超详细的常用笔记

    关注微信公众号:(新生程序员教程) 下载python的word笔记,了解更多编程语言和程序员学习网站,学习平台 目录 第1章:Python的基础篇 1.1计算机的组成[了解] 1.2.计算机如何处理程 ...

最新文章

  1. python爬虫论文摘要怎么写_Python爬虫基础教学(写给入门的新手)
  2. 网络:XSS和HttpOnly
  3. mysql端口转发_SSH做MySQL端口转发
  4. 读取Exchange的用户未读邮件数3种办法
  5. c语言程序的入口是哪部分,C语言入口函数和LD_PRELOAD环境变量
  6. 2008服务器系统配置dns,配置Win2008系统DNS服务器的具体步骤
  7. P3403 跳楼机 同余最短路
  8. 渝粤教育 陕西师范大学 《金融中介学Ⅰ》作业
  9. easyui datagrid一般创建模板
  10. python 大智慧自定义数据_大智慧自定义指数
  11. 台式计算机连wifi,台式电脑怎么连wifi
  12. 初学前端需要掌握的HTML知识点
  13. PCB 铜厚厚度和线宽的选择
  14. 从0开始实现目标检测——原理篇
  15. 卡片左右滑动 带动任务条滑动 vue
  16. Synology NAS群晖DS218play 运行内存测试
  17. 游戏原来也可以严肃?——严肃游戏及在中国的应用前景
  18. C语言位运算农夫过河,位运算常见操作和农夫过河问题(C++实现)
  19. Linux中Sl命令的使用
  20. Oracle数据库1521端口telnet不通问题

热门文章

  1. 【测宽仪项目】Hight-Speed Charting控件改变曲线的颜色、宽度、样式
  2. 不同数据范围对应的时间复杂度及算法(合集)
  3. 【高等工程数学】南理工研究生课程 突击笔记8 最优化方法1——线性搜索
  4. Win11 桌面菜单无法创建记事本(.txt) 的解决办法
  5. Nand Flash探索之旅
  6. LeeCode 1379. 找出克隆二叉树中的相同节点
  7. Android联系人、ListView实现姓名首字母分类和字母定位查找
  8. 6.0.高等数学3-欧拉方程
  9. 钛阀门市场深度研究分析报告
  10. 网线线序排列以及八字口诀