python基础运用
1、定义块
定义相应的块,每个方块实际就是一个按钮,所以继承Button类。每个方块的基本数据,除了方块的类型以外还有左上角的坐标,一旦确定方块的类型和坐标后,就可以确定方块对应的角色和位置了。方块左上角坐标用Point类的对象来表示,并在Block类中定义一个属性Location来表示
from tkinter import *
One=1;TowH=2;TowV=1;Four=4
class Point:
def __init__(self,x,y): #定义坐标X,Y
self.X=x
self.Y=y
class Block(Button): #块类
def __init__(self,p,blockType,master,r,bm):
Button.__init__(self,master)
self.Location=p
self.BType=blockType
self["text"]=r
self["image"]=bm
self.bind("<ButtonPress>",btn_MouseDown);
self.bind("<ButtonRelease>",btn_Release);
self.place(x=self.Location.X*80,y=self.Location.Y*80)
2、定义初始坐标位置,判断条件
坐标图如下:
每个块都相应完成具体的操作,每个块功能如下:
(1) Block类中定义一个函数GetPoints来
获取该方块所占据的所有坐标位置的列表,
通过方块类型和左上角的坐标就可以得到
占据的所有坐标位置。
(2) 再定义函数 IsValid 来判断这个方块是
否在游戏区域内,如果有任何部分出界就
返回False,可以通过方块类型和左上角坐
标来判断。
(3) 函数Intersects(Block b) 用来判断一个
角色是否和另一个角色有重叠部分,如果
有则返回True,通过获取两个角色各自占
据的点来判断是否有重叠。
#定义得到游戏图片的初始位置
def getPoints(self):
pList=[]
if self.BType==One:
pList.append(self.Location)
elif self.BType==TowH:
pList.append(self.Location)
pList.append(Point(self.Location.X+1,self.Location.Y))
elif self.BType==TowV:
pList.append(self.Location)
pList.append(Point(self.Location.X,self.Location.Y+1<