学习 python 一段时间。 今天 练习做了个 贪吃蛇, 一共才 150 多行。
把代码贴这里了。(有没有方法打包传上来呢? )
这个是 主程序 run.py
from Tkinter import *
from modsfd import snake
from modsfd import food
from time import sleep
class App(object):
def __init__(self):
self.master = Tk()
self.ledge = 0
self.redge = 300
self.tedge = 0
self.bedge = 400
self.winNum = 3
self.direction = 'd'
self.dirc = 'wsad'
self.dirn = {'w':-1, 'd':-2, 's':1, 'a':2}
self.toolBar = Frame(self.master, width=40, bg='gray')
self.toolBar.pack(fill=Y, expand=False, side = LEFT)
self.var = StringVar()
self.scoreLabel = Label(self.toolBar, bg='yellow',textvariable = self.var)
self.scoreLabel.pack(fill=X)
self.butt = Button(self.toolBar, bg='Green', text="Start", command=self.startG)
self.butt.pack(fill=X)
self.pauseButt = Button(self.toolBar, foreground='white', bg='Blue', text='Pause', command=self.pauseG)
self.pauseButt.pack(fill=X)
self.w = Canvas(self.master, width=self.redge, height=self.bedge, background="white")
self.w.pack(side = RIGHT)
self.swl = False
self.tail = [0,0]
self.sk = snake.Snake()
self.pie = food.food()
self.master.bind("<Key>", self.key)
#self.w.focus_set()
self.master.mainloop()
def startG(self):
self.w.create_rectangle(0,0, 300,400, fill="white")
self.pauseFlag = False
self.score = -1
self.var.set('0')
self.tail = [0,0]
self.swl = False
self.sk = snake.Snake()
self.pie = food.food()
self.master.after(10, self.task)
def pauseG(self):
if self.pauseFlag == True:
self.pauseFlag = False
else:
self.pauseFlag = True
def drawBlack(self, x, y):
self.w.create_rectangle(x, y,
x+10, y+10,
outline="white", fill="black")
def drawWhite(self, x,y):
self.w.create_rectangle(x, y,
x+10, y+10,
outline="white", fill="white")
def key(self, event):
if event.char and event.char in self.dirc:
#print self.dirn[event.char], self.dirn[self.direction]
if self.dirn[event.char] + self.dirn[self.direction] != 0:
self.direction = event.char
def task(self):
if self.pauseFlag:
self.w.after(500)
self.master.after(150, self.task)
return
if self.score == self.winNum:
#print 'You win!'
self.w.create_text(200, 200, text="You Win")
return
self.sk.tail = list(self.sk.body[-1])
self.sk.body_follow()
self.sk.move[self.direction.lower()](self.ledge, self.redge, self.tedge, self.bedge)
if not self.swl:
self.score += 1
self.var.set(self.score)
self.pie.makeFood(self.sk, self.redge, self.bedge)
self.drawBlack(self.pie.x, self.pie.y)
self.swl = True
self.drawWhite(self.sk.tail[0], self.sk.tail[1])
self.drawBlack(self.sk.body[0][0], self.sk.body[0][1])
if self.sk.body[0][0] == self.pie.x and self.sk.body[0][1] == self.pie.y:
self.swl = False
self.sk.body.append(list(self.sk.tail))
self.sk.l += 1
self.drawBlack(self.sk.body[-1][0], self.sk.body[-1][1])
#self.w.after(900)
#self.w.update()
self.master.after(150, self.task)
App()
建个 module文件夹 modsfd
在里面放上 __init__.py
import food
import snake
food.py :
import random
class food(object):
def __init__(self):
self.x=0
self.y=0
def makeFood(self, snake, redge, bedge):
while 1:
self.x = random.randint(0,redge)/10*10
self.y = random.randint(0,bedge)/10*10
if not snake.isBody(self.x, self.y):
break
snake.py:
#
import random
class Snake(object):
def __init__(self):
self.body = [[random.randint(100, 200)/10*10, random.randint(100, 200)/10*10]]
#self.body.append([self.body[0][0]-10, self.body[0][1]])
self.curDir = 2
self.move = {'w':self.move_up, 'd':self.move_right, 's':self.move_down, 'a':self.move_left}
self.tail = []
self.l = 1
def move_left(self, ledge, redge, t, b):
self.body[0][0] -= 10
if self.body[0][0] < ledge:
self.body[0][0] = redge-10
def move_right(self, ledge, redge, t, b):
self.body[0][0] += 10
if self.body[0][0] >= redge:
self.body[0][0] = ledge
def move_up(self, l, r,tedge, bedge):
self.body[0][1] -= 10
if self.body[0][1] < tedge:
self.body[0][1] = bedge-10
def move_down(self, l,r, tedge, bedge):
self.body[0][1] += 10
if self.body[0][1] >= bedge:
self.body[0][1] = tedge
def body_follow(self):
for i in range(len(self.body)-1, 0, -1):
self.body[i][0] = self.body[i-1][0]
self.body[i][1] = self.body[i-1][1]
def isBody(self, x, y):
for i in xrange(0, len(self.body)):
if x == self.body[i][0] and y == self.body[i][1]:
return True
return False