-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixel.py
More file actions
92 lines (68 loc) · 2.12 KB
/
Pixel.py
File metadata and controls
92 lines (68 loc) · 2.12 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import random
class Pixel:
id_counter = 0
def __init__(self, board, x=0, y=0):
self.board = board
self.x = x
self.y = y
self.target_x = x
self.target_y = y
self.sleeping = False
self.id = Pixel.id_counter
Pixel.id_counter += 1
def move_possible(self, x, y):
test_x = self.x + x
test_y = self.y + y
if not (0 <= test_x < self.board.resolution.x):
return False
if not (0 <= test_y < self.board.resolution.y):
return False
# return True # asdf wip
if self.board.get_pixel(test_x, test_y):
return False
else:
return True
def move(self, x,y):
self.board.wake_neighbors(self)
self.board.clear_pixel(self, self.x, self.y)
self.target_x += x
self.target_y += y
self.board.set_pixel(self, self.target_x, self.target_y)
def update(self):
down_possible = False
left_possible = False
right_possible = False
if self.move_possible(0, 1):
down_possible = True
else:
if self.move_possible(-1, 0):
left_possible = True
if self.move_possible(1, 0):
right_possible = True
if down_possible:
self.move(0, 1)
elif left_possible and right_possible:
if random.random() > 0.5:
self.move(-1, 0)
else:
self.move(1, 0)
elif left_possible:
self.move(-1, 0)
elif right_possible:
self.move(1, 0)
else:
self.sleeping = True
def resolve_conflict(self):
self.board.clear_pixel(self, self.target_x, self.target_y)
self.target_x = self.x
self.target_y = self.y
self.board.set_pixel(self, self.x, self.y)
def handle_no_conflict(self):
self.x = self.target_x
self.y = self.target_y
def sleep(self):
self.sleeping = True
def wake_up(self):
self.sleeping = False
def free(self):
self.board.wake_neighbors(self)