Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/annotate/3619305694ad/engine.py
Дата изменения: Unknown
Дата индексирования: Fri Feb 28 23:00:39 2014
Кодировка:
snake: engine.py annotate

snake

annotate engine.py @ 96:3619305694ad

Actually fixed buttons resizing. Fixed code separation for buttons drawing.
author Danya Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 02:26:35 +0300
parents 378227a79ebc
children 1e97abfe23bc
rev   line source
martiran@12 1 import random as rnd
martiran@15 2 import Tkinter as tk
Alex@66 3 import snake
martiran@12 4
martiran@6 5 directions = [(0,1), (1,0), (0,-1), (-1,0)]
martiran@46 6 tm = [[0, -1], [1, 0]]
martiran@1 7
martiran@5 8 class Cell(object):
martiran@15 9 def __init__(self, x, y, canvas = None):
martiran@1 10 self.x = x
martiran@1 11 self.y = y
martiran@1 12 self.canvas = canvas
martiran@1 13 self.snake = None
martiran@28 14 self.type = 'empty'
martiran@1 15 return
martiran@6 16 def redraw(self):
martiran@54 17 field_size = min(self.canvas.winfo_height(), self.canvas.winfo_width())
martiran@55 18 offset = (self.canvas.winfo_width() - field_size, self.canvas.winfo_height() - field_size)
martiran@15 19 if self.type == 'wall':
martiran@15 20 self.canvas.create_rectangle(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1] + self.y*field_size/21.0, fill="grey")
martiran@15 21 pass
martiran@15 22 elif self.type == 'empty':
martiran@15 23 self.canvas.create_rectangle(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1] + self.y*field_size/21.0, fill="black")
martiran@15 24 pass
martiran@15 25 elif self.type == 'body':
martiran@15 26 self.canvas.create_rectangle(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1] + self.y*field_size/21.0, fill=self.snake.color)
martiran@15 27 pass
martiran@15 28 elif self.type == 'head':
martiran@15 29 self.canvas.create_oval(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1] + self.y*field_size/21.0, fill=self.snake.color)
martiran@15 30 pass
martiran@15 31 elif self.type == 'tail':
martiran@15 32 self.canvas.create_polygon(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1], offset[0] + self.x*field_size/(2*21.0), offset[1] + self.y*field_size/21.0, fill=self.snake.color)
martiran@15 33 pass
martiran@15 34 return
martiran@6 35 def __eq__(self, pattern):
martiran@52 36 if pattern.type == 'any':
martiran@6 37 return True
martiran@52 38 if pattern.type != self.type:
martiran@6 39 return False
martiran@52 40 if pattern.snake_type == 'my' and pattern.snake != self.snake:
martiran@52 41 return False
martiran@52 42 elif pattern.snake_type == 'enemy' and pattern.snake == self.snake:
martiran@52 43 return False
martiran@52 44 return True
martiran@6 45 def clear(self):
martiran@1 46 self.snake = None
martiran@1 47 self.type = 'empty'
martiran@1 48 return
martiran@1 49
martiran@2 50
martiran@5 51 class Engine(object):
martiran@6 52 def __init__(self, canvas):
martiran@1 53 self.canvas = canvas
martiran@54 54 self.w = min(canvas.winfo_height(), canvas.winfo_width())
martiran@54 55 self.h = min(canvas.winfo_height(), canvas.winfo_width())
martiran@12 56 self.snakes = [None, None, None, None]
martiran@9