snake
changeset 15:7393cf186fd6
Cell.redrw(), Engine.redraw()
author | Alex Martynov <martiran@kodomo.fbb.msu.ru> |
---|---|
date | Tue, 14 Dec 2010 02:28:25 +0300 |
parents | d7650ea84300 |
children | 0e10ae8006e9 |
files | engine.py |
diffstat | 1 files changed, 23 insertions(+), 3 deletions(-) [+] |
line diff
1.1 --- a/engine.py Mon Dec 13 00:26:38 2010 +0300 1.2 +++ b/engine.py Tue Dec 14 02:28:25 2010 +0300 1.3 @@ -1,10 +1,11 @@ 1.4 import random as rnd 1.5 +import Tkinter as tk 1.6 1.7 directions = [(0,1), (1,0), (0,-1), (-1,0)] 1.8 rtm = [[0, -1], [1, 0]] 1.9 1.10 class Cell(object): 1.11 - def __init__(self, x, y, canvas): 1.12 + def __init__(self, x, y, canvas = None): 1.13 self.x = x 1.14 self.y = y 1.15 self.canvas = canvas 1.16 @@ -12,7 +13,24 @@ 1.17 self.type = empty 1.18 return 1.19 def redraw(self): 1.20 - pass 1.21 + field_size = min(canvas.height, canvas.width) 1.22 + offset = (canvas.width - field_size, canvas.hieght - field_size) 1.23 + if self.type == 'wall': 1.24 + 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") 1.25 + pass 1.26 + elif self.type == 'empty': 1.27 + 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") 1.28 + pass 1.29 + elif self.type == 'body': 1.30 + 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) 1.31 + pass 1.32 + elif self.type == 'head': 1.33 + 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) 1.34 + pass 1.35 + elif self.type == 'tail': 1.36 + 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) 1.37 + pass 1.38 + return 1.39 def __eq__(self, pattern): 1.40 if pattern.type == self.type: 1.41 return True 1.42 @@ -110,7 +128,9 @@ 1.43 pass 1.44 return 1.45 def redraw(self): 1.46 - pass 1.47 + for cell in self.field: 1.48 + cell.redraw() 1.49 + return 1.50 def legal_moves(self, snake): 1.51 snake.legal_dir = [] 1.52 for direction in directions: