从<趣学Python编程>上看到的例子,小人在石头上跑跳直到达到门

我做了以下改进:

1. 模块化:

  • helper.py 含有基类以及碰撞检测函数
  • man.py 小人
  • door,py 门
  • stone.py 脚踩的石头
  • game.py 主函数,初始化canvas,游戏主循环
2. 修复一个Bug,原先人踩在石头上会掉下来
if btm and falling and self.y == 0 \
and (co.y2 >= self.game.canvas_h \
or collide_bottom(1, co, s_co)): #man stand on the stone
falling = False

素材:

背景bg.gif   

门d_1.gif    

跑动的小人

man_l_1  

man_l_2  

man_l_3 

man_r_1 

man_r_2 

man_r_3 

模块化代码:

helper.py

#Object
class Sprite:def __init__(self, game):self.game = gameself.endGame = Falseself.co = Nonedef move(self):passdef coords(self):return self.coclass Coords:def __init__(self, x1 = 0, y1 = 0, x2 = 0, y2 = 0):self.x1 = x1self.x2 = x2self.y1 = y1self.y2 = y2#intersection
def inter_x(co1, co2):if (co1.x1 >= co2.x2) or (co1.x2 <= co2.x1):return Falseelse:return Truedef inter_y(co1, co2):if (co1.y1 >= co2.y2) or (co1.y2 <= co2.y1):return Falseelse:return Truedef collide_left(co1, co2):if inter_y(co1,co2) and co1.x1 <= co2.x2 and co1.x1 >= co2.x1:return Truereturn Falsedef collide_right(co1, co2):if inter_y(co1,co2) and co1.x2 <= co2.x2 and co1.x2 >= co2.x1:return Truereturn Falsedef collide_top(co1, co2):if inter_x(co1,co2) and co1.y1 <= co2.y2 and co1.y1 >= co2.y1:return Truereturn Falsedef collide_bottom(y, co1, co2):y = co1.y2 + yif inter_x(co1,co2) and y <= co2.y2 and y >= co2.y1:return Truereturn False

stone.py

from helper import *class Stone(Sprite):def __init__(self, game, x, y, w, h):Sprite.__init__(self, game);#call fatherself.image = game.canvas.create_rectangle(x, y, x + w, y + h, fill='pink')self.co = Coords(x, y, x + w, y + h) 

door.py

from helper import *
from tkinter import *class Door(Sprite):def __init__(self, game, x, y):Sprite.__init__(self, game);#call fatherself.photo_image = PhotoImage(file = 'd_1.gif');self.image = game.canvas.create_image(x, y, image = self.photo_image, anchor = 'nw')self.co = Coords(x, y, x + 27, y + 30)self.endGame = True

man.py

from helper import *
import time
from tkinter import *class Man(Sprite):def __init__(self, game):Sprite.__init__(self, game);#call fatherself.image_l = [PhotoImage(file="man_l_1.gif"),PhotoImage(file="man_l_2.gif"),PhotoImage(file="man_l_3.gif")]self.image_r = [PhotoImage(file="man_r_1.gif"),PhotoImage(file="man_r_2.gif"),PhotoImage(file="man_r_3.gif")]self.image = game.canvas.create_image(0, 0, image=self.image_l[0], anchor='nw')self.x = -2self.y = 0self.current_img = 0#image indexself.current_img_add = 1self.jump_count = 0#use for jumpself.last_time = time.time()self.co = Coords()game.canvas.bind_all("<Key-Left>", self.turn_left)game.canvas.bind_all("<Key-Right>", self.turn_right)game.canvas.bind_all("<Key-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 = 0#important change img of Mandef animate(self):if self.x != 0 and self.y == 0:if time.time() - self.last_time > 0.1:  #change img slowlyself.last_time = time.time()self.current_img += self.current_img_addif self.current_img >= 2:self.current_img_add = -1elif self.current_img <= 0:self.current_img_add = 1if self.x < 0:if self.y != 0:self.game.canvas.itemconfig(self.image, \image = self.image_l[2])else:self.game.canvas.itemconfig(self.image, \image = self.image_l[self.current_img])if self.x > 0:if self.y != 0:self.game.canvas.itemconfig(self.image, \image = self.image_r[2])else:self.game.canvas.itemconfig(self.image, \image = self.image_r[self.current_img])    def coords(self):xy = self.game.canvas.coords(self.image)self.co.x1 = xy[0]self.co.y1 = xy[1]self.co.x2 = xy[0] + 27self.co.y2 = xy[1] + 30return self.co#importantdef move(self):self.animate()#for jump case, update yif self.y < 0:self.jump_count = self.jump_count + 1if self.jump_count > 20:self.y = 4elif self.y > 0:self.jump_count = self.jump_count - 1#collision checkco = self.coords()left = Trueright = Truetop = Truebtm = Truefalling = True#1.collide with canvasif self.y > 0 and co.y2 >= self.game.canvas_h:self.y = 0btm = Falseelif self.y < 0 and co.y2 <= 0:self.y = 0top = Falseif self.x > 0 and co.x2 >= self.game.canvas_w:self.x = 0right = Falseelif self.x < 0 and co.x2 <= 0:self.x = 0left = False#2.collide with stonefor s in self.game.sprites:if s == self:continues_co = s.coords()if top and self.y < 0 and collide_top(co, s_co):#collide topself.y = - self.ytop = Falseif btm and self.y > 0 and collide_bottom(self.y, co, s_co):#collide btm#self.y = s_co.y1 - co.y2#if self.y < 0:self.y = 0btm = Falsetop = Falseif btm and falling and self.y == 0 \and (co.y2 >= self.game.canvas_h \or collide_bottom(1, co, s_co)): #man stand on the stonefalling = Falseif left and self.x < 0 and collide_left(co, s_co):#collide leftself.x = 0left = Falseif s.endGame:#if s is doorself.game.running = Falseif right and self.x > 0 and collide_right(co, s_co):#collide rightself.x = 0right = Falseif s.endGame:#if s is doorself.game.running = Falseprint ("btm is %s" % btm);#let the man fallif falling and btm and self.y == 0\and co.y2 < self.game.canvas_h:self.y = 4 self.game.canvas.move(self.image, self.x, self.y)

game.py

from tkinter import *
import random
from stone import *
from man import *
from door import *#Game controller
class Game:def __init__(self):self.tk = Tk()self.tk.title("Run away from gost house")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_h = 500self.canvas_w = 500self.bg = PhotoImage(file="bg.gif")w = self.bg.width()h = self.bg.height()for x in range(0,5):for y in range(0,5):self.canvas.create_image( x * w, y * h, image=self.bg, anchor='nw')self.sprites = []self.running = Truedef loop(self):while 1:if self.running:for sprite in self.sprites:sprite.move()self.tk.update_idletasks()self.tk.update()time.sleep(0.01)g = Game()
for i in range(60, 500, 30):x = random.randint(0, 450)y = random.randint(-5, 5)width = random.randint(40, 100)g.sprites.append(Stone(g, x, i + y, width, 10))
#door
door = Door(g, 50, 300)
g.sprites.append(door)
lastStone = Stone(g, 50, 300, 60, 10)
g.sprites.append(lastStone)
m = Man(g)
g.sprites.append(m)
g.loop()

Python逃生游戏相关推荐

  1. 华为OD机试 - 密室逃生游戏(Python)

    密室逃生游戏 题目 小强增在参加<密室逃生>游戏,当前关卡要求找到符合给定 密码 K(升序的不重复小写字母组成) 的箱子, 并给出箱子编号,箱子编号为 1~N . 每个箱子中都有一个 字符 ...

  2. 【华为OD机试真题 python】密室逃生游戏【2022 Q4 | 100分】

    ■ 题目描述 [密室逃生游戏] 小强增在参加<密室逃生>游戏,当前关卡要求找到符合给定 密码K(升序的不重复小写字母组成) 的箱子, 并给出箱子编号,箱子编号为 1~N . 每个箱子中都有 ...

  3. 华为OD机试真题Python实现【密室逃生游戏】真题+解题思路+代码(20222023)

    密室逃生游戏 题目 小强增在参加<密室逃生>游戏,当前关卡要求找到符合给定 密码 K(升序的不重复小写字母组成) 的箱子, 并给出箱子编号,箱子编号为 1~N . 每个箱子中都有一个 字符 ...

  4. python写好的代码怎么给别人使用-10分钟学会用python写游戏!Python其实很简单!...

    原标题:10分钟学会用python写游戏!Python其实很简单! Python现在非常火,语法简单而且功能强大,很多同学都想学Python!所以在这里给各位看官们准备了高价值Python学习视频教程 ...

  5. python编程小游戏-python编程游戏有哪些

    python编程游戏有哪些?下面给大家介绍几款由Python开发的游戏: 1.Github上面有个项目Free Python Games,里面集合了不少的Python开发的小游戏,能玩,也适合新手用来 ...

  6. 少儿编程python线上课程-少儿编程课堂|python – 用游戏学编程

    学习编程是很快乐的事情.当我们自己开发出一套时下流行的游戏时,这满满的成就感比玩儿游戏本身高出了不知道会有多少倍. 接下来一段时间我们就python从0开始学习怎么开发 flappy brid 游戏. ...

  7. python小游戏编程实例-10分钟教你用Python写一个贪吃蛇小游戏,适合练手项目

    另外要注意:光理论是不够的.这里顺便总大家一套2020最新python入门到高级项目实战视频教程,可以去小编的Python交流.裙 :七衣衣九七七巴而五(数字的谐音)转换下可以找到了,还可以跟老司机交 ...

  8. python写游戏脚本-使用Python写一个小游戏

    引言 最近python语言大火,除了在科学计算领域python有用武之地之外,在游戏.后台等方面,python也大放异彩,本篇博文将按照正规的项目开发流程,手把手教大家写个python小游戏,来感受下 ...

  9. python编的俄罗斯方块游戏_手把手制作Python小游戏:俄罗斯方块(一)

    手把手制作Python小游戏:俄罗斯方块1 大家好,新手第一次写文章,请多多指教 A.准备工作: 这里我们运用的是Pygame库,因为Python没有内置,所以需要下载 如果没有pygame,可以到官 ...

最新文章

  1. WINCE下实现USB转RS232
  2. 卷积的C语言实现的MFC版本
  3. STM8S105系列单片机管脚复用配置(选项字节的配置)
  4. 《帝友 P2P 网络借贷系统》
  5. Java——网络编程(实现基于命令行的多人聊天室)
  6. sqlerver 字符串转整型_sqlerver2005(2)
  7. python爬虫JS逆向之人口流动态势大数据
  8. 数据库实验8 数据库安全性(用户与权限管理)实验
  9. cenos各个版本下载地址
  10. Axure 8.1.0.3382 激活码(转)
  11. 机器人也能打排球了,击球成功率80%!日本东京大学新型气动仿人机器人
  12. 关于selenium获取网页下一页的点击事件
  13. HTML标签-排版标签、媒体标签、列表标签、表格标签、表单标签、语义化标签、字符实体
  14. 中国工业钩环市场深度研究分析报告
  15. Python爬虫抓取网页图片
  16. 数据结构:二叉树及堆排序
  17. 如何控制苹果Mac电池健康管理功能?
  18. 不知道如何进行图片文字翻译?看完这篇你就知道了
  19. 服务器连接盘柜后盘符空间显示不对,服务器连接磁盘阵列柜
  20. 【SSH框架/国际物流商综平台】-01-分三期(仓储管理,货运全流程管理,决策分析)- 项目背景 界面原型 用例图 企业组织结构 功能模块图 系统框架 项目表单收集

热门文章

  1. VMDq (Virtual Machine Device Queue) in OpenSolaris
  2. 计算机类型变废为宝创意设计,旧玩具还能这么玩!10个DIY创意教你如何变废为宝...
  3. Java---MyBatis框架
  4. SQL Server如何还原误删除的数据-操作篇
  5. 店宝宝:“会员电商”,电商发展的新风口
  6. python云顶之翼
  7. FileInputStream 读取文件内容
  8. KafkaConsumer Api
  9. python使用谷歌翻译
  10. 神吐槽:对自己太狠 男子自己割包皮