资源描述
#encoding:utf-8importtkMessageBoxfromTkinter import Tk,Canvas,Framefrom random import randintimport sysclass Grid:def _init_(self,master=None,window_width=500,window_height=500,grid_width=25):self.width=grid_widthself.bg=7self.start=2#50/25self.end=18#450/25self.color=yellow,red,pink,black,orange,green,purple,cyanself.canvas=Canvas(master,width=window_width,height=window_height,bg=self.colorself.bg)self.draw_wall()self.canvas.pack()defdraw_rect(self,x,y,clr):self.canvas.create_rectangle(x,y,x+self.width,y+self.width,fill=self.colorclr,outline=self.colorself.bg)defdraw_wall(self):w=self.widths=self.start*we=self.end*wfori in range(s,e,w):self.canvas.create_rectangle(i,s,i+w,s+w,fill=green,outline=self.colorself.bg)self.canvas.create_rectangle(i,e-w,i+w,e,fill=green,outline=self.colorself.bg)self.canvas.create_rectangle(s,i,s+w,i+w,fill=green,outline=self.colorself.bg)self.canvas.create_rectangle(e-w,i,e,i+w,fill=green,outline=self.colorself.bg)classsnakenode:def _init_(self,clr,x,y):self.clr=clrself.x=xself.y=yclass Food:def _init_(self,Grid):self.grid=Gridself.set_food()defset_food(self):min=self.grid.start+1max=self.grid.end-2self.x=randint(min,max)*25self.y=randint(min,max)*25self.clr=randint(0,len(self.grid.color)-2)def display(self):self.grid.draw_rect(self.x,self.y,self.clr)class snake:def _init_(self,Grid):self.grid=Gridself.init_snake()self.direction=Upself.dir_x=0,0,-1,1self.dir_y=-1,1,0,0self.dir=Up:0,Down:1,Left:2,Right:3self.pause=1self.dead=0self.food=Food(self.grid)self.display_food()definit_snake(self):s1=snakenode(0,100,300)s2=snakenode(1,100,325)s3=snakenode(2,100,350)self.body=s1,s2,s3defdisplay_snake(self):for node in self.body:self.grid.draw_rect(node.x,node.y,node.clr)defdisplay_food(self):for node in self.body:ifnode.x=self.food.x and node.y=self.food.y:self.food.set_food()else:breakself.food.display()def move(self):index=self.dirself.directionm=self.dir_xindex*self.grid.width+self.body0.xn=self.dir_yindex*self.grid.width+self.body0.yif m=self.food.x and n=self.food.y:#吃到食物sn=snakenode(self.food.clr,m,n)self.body.insert(0,sn)self.display_food()returns=(m/self.grid.width)t=(n/self.grid.width)b1=s=self.grid.start or s=self.grid.end-1b2=t=self.grid.start or t=self.grid.end-1if b1 or b2:#撞到墙self.dead=1returnfor node in self.body:#撞到自身if m=node.x and n=node.y:self.dead=1return#沿着方向继续移动sn=snakenode(self.body0.clr,m,n)self.body.insert(0,sn)fori in range(1,len(self.body)-1):self.bodyi.clr=self.bodyi+1.clrlast=self.body.pop()self.grid.draw_rect(last.x,last.y,self.grid.bg)self.display_snake()class Game(Frame):def _init_(self,master=None):Frame._init_(self,master)self.bind_all(,self.keyrelease_event)self.master=masterself.grid=Grid(self.master)self.snake=snake(self.grid)self.snake.display_snake()def run(self):ifself.snake.pause!=0:self.snake.move()ifself.snake.dead=1:message=tkMessageBox.showinfo(提示,Game Over)if message=ok:sys.exit(0)self.after(300,self.run)defkeyrelease_event(self,event):#键盘监听事件key=event.keysymdir=self.snake.directionrule=Up:Down,Down:Up,Left:Right,Right:Leftifrule.has_key(key)and key!=ruledir:#按下方向键且不在规则 rule 中self.snake.direction=keyelifevent.char=:#实现暂停self.snake.pause=(self.snake.pause+1)%2if _name_=_main_:root=Tk(className=snake)game=Game(root)game.run()root.mainloop()
展开阅读全文