-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode.py
More file actions
42 lines (35 loc) · 1.08 KB
/
Copy pathnode.py
File metadata and controls
42 lines (35 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
Created on 2009-4-13
@author: hippo
"""
from random import randint
from properties import *
from err import SnakeException
UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'
NONE = 'none'
def _setcolor():
if COLORFUL:
return '#%02x%02x%02x' % (randint(0, 255), randint(0, 255), randint(0, 255))
return BODY_COLOR
class Node:
def __init__(self, x, y, direct=None):
if direct is not None:
if direct not in (UP, DOWN, LEFT, RIGHT, NONE):
raise SnakeException('The snake don''t know how to move!')
self._direct = direct
self._x = x
self._y = y
self.color = _setcolor()
self.edgecolor = 'black'
def reset(self):
self._x = randint(0, GRID_LEN - 1)
self._y = randint(0, GRID_LEN - 1)
def _changedirect(self, direct):
_map = {UP: -1, DOWN: 1, LEFT: -2, RIGHT: 2, NONE: 0}
if _map[self._direct] + _map[direct]:
self._direct = direct
def __str__(self):
return 'x=' + repr(self._x) + ',y=' + repr(self._y) + ',direct=' + repr(self._direct)