永发信息网

如何用Python写一个贪吃蛇

答案:1  悬赏:40  手机版
解决时间 2021-03-11 20:09
如何用Python写一个贪吃蛇
最佳答案
以前在远标写过:from Tkinter import *
import tkMessageBox,sys
from random import randint

class Grid(object):
def __init__(self,master=None,window_width=800,window_height=600,grid_width=50,offset=10):
self.height = window_height
self.width = window_width
self.grid_width = grid_width
self.offset = offset
self.grid_x = self.width/self.grid_width
self.grid_y = self.height/self.grid_width
self.bg = "#EBEBEB"
self.canvas = Canvas(master, width=self.width+2*self.offset, height=self.height+2*self.offset, bg=self.bg)
self.canvas.pack()
self.grid_list()
def draw(self, pos, color,):
x = pos[0]*self.grid_width + self.offset
y = pos[1]*self.grid_width + self.offset
self.canvas.create_rectangle(x, y, x+self.grid_width, y+self.grid_width,fill=color,outline=self.bg)
def grid_list(self):
grid_list = []
for y in range(0,self.grid_y):
for x in range(0,self.grid_x):
grid_list.append((x,y))
self.grid_list = grid_list

class Food(object):
def __init__(self, Grid):
self.grid = Grid
self.color = "#23D978"
self.set_pos()
def set_pos(self):
x = randint(0,self.grid.grid_x - 1)
y = randint(0,self.grid.grid_y - 1)
self.pos = (x, y)
def display(self):
self.grid.draw(self.pos,self.color)

class Snake(object):
def __init__(self, Grid):
self.grid = Grid
self.body = [(10,6),(10,7),(10,8)]
self.direction = "Up"
self.status = ['run','stop']
self.speed = 300
self.color = "#5FA8D9"
self.food = Food(self.grid)
self.display_food()
self.gameover = False
self.score = 0
def available_grid(self):
return [i for i in self.grid.grid_list if i not in self.body[2:]]
def change_direction(self, direction):
self.direction = direction
def display(self):
for (x,y) in self.body:
self.grid.draw((x,y),self.color)
def display_food(self):
while(self.food.pos in self.body):
self.food.set_pos()
self.food.display()
def move(self):
head = self.body[0]
if self.direction == 'Up':
new = (head[0], head[1]-1)
elif self.direction == 'Down':
new = (head[0], head[1]+1)
elif self.direction == 'Left':
new = (head[0]-1,head[1])
else:
new = (head[0]+1,head[1])
if not self.food.pos == head:
pop = self.body.pop()
self.grid.draw(pop,self.grid.bg)
else:
self.display_food()
self.score += 1
self.body.insert(0,new)
if not new in self.available_grid():
self.status.reverse()
self.gameover = True
else:
self.grid.draw(new,color=self.color)

class SnakeGame(Frame):
def __init__(self,master=None, *args, **kwargs):
Frame.__init__(self, master)
self.master = master
self.grid = Grid(master=master,*args, **kwargs)
self.snake = Snake(self.grid)
self.bind_all("", self.key_release)
self.snake.display()
def run(self):
if not self.snake.status[0] == 'stop':
self.snake.move()
if self.snake.gameover == True:
message = tkMessageBox.showinfo("Game Over", "your score: %d" % self.snake.score)
if message == 'ok':
sys.exit()
self.after(self.snake.speed,self.run)
def key_release(self, event):
key = event.keysym
key_dict = {"Up":"Down","Down":"Up","Left":"Right","Right":"Left"}
if key_dict.has_key(key) and not key == key_dict[self.snake.direction]:
self.snake.change_direction(key)
self.snake.move()
elif key == 'p':
self.snake.status.reverse()

if __name__ == '__main__':
root = Tk()
snakegame = SnakeGame(root)
snakegame.run()
snakegame.mainloop()
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
世嘉方向助力油,刹车油到两年必须更换吗
有一部电影男主角用十块钱赢三千万叫什么电影
微信抢红包被禁三天,重新安装微信后依然被禁,
作意要行此好事,知人知面不知心,是什么意思
驭道汽车用品批发地址在哪,我要去那里办事
日暮是什么时候
科沃斯智能扫地机出现故障有微信号吗
左脚左路正常 ,但是右脚外八啊?我该怎么做
dnf双攻什么意思?求一个明白人,能给我说明
【光学显微镜和电子显微镜的区别】光学显微镜
观音和佛项链怎么分男女戴呢
手机验证码使用过后被别人知道了会怎么样?
国清鱼药渔具饲料店怎么去啊,有知道地址的么
模拟器No$gba怎样调掉声音?
钟祥市旧口前一段时间派出所抓了几个进去了是
推荐资讯
我带着金黄的花束。我带着林间的香气。我带着
中江有哪几个健身房,月卡是多少啊,老是弄不
在C语言中(m+1)*m/2怎么运算的
浙江省位于27°03′N到31°11′N,东经118°0
韶关市宝利通机动车驾驶员培训报名咨询处我想
奇味烤肉(磴口店)在什么地方啊,我要过去处理
惠州学院有哪些专业
为什么视频网站的广告不会卡?
2012年1月8日(农历腊月十五)生的孩子姓曾,
同一数据线,我的手机显示未认证,别人却可以用
下列各项不属于生物的是CA. 细菌B. 病毒C. 珊
德克萨金属探测仪好用吗?谢谢了
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?