收藏 分销(赏)

python实现贪吃蛇.pdf

上传人:鱼** 文档编号:851952 上传时间:2024-03-29 格式:PDF 页数:5 大小:98.81KB
下载 相关 举报
python实现贪吃蛇.pdf_第1页
第1页 / 共5页
python实现贪吃蛇.pdf_第2页
第2页 / 共5页
点击查看更多>>
资源描述
#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()
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 通信科技 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服