-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwall.py
More file actions
167 lines (131 loc) · 3.48 KB
/
wall.py
File metadata and controls
167 lines (131 loc) · 3.48 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import pygame
from threading import Thread
import time
COLORS = [
(128, 0, 0),
(0, 128, 0),
(245, 179, 66),
(0, 0, 0),
(57, 81, 82),
()
]
DIRECTIONS = [
(0, 1),
(1, 0),
(0, -1),
(-1, 0)
]
def determine_direction(pos1, pos2):
difference = (pos1[0] - pos2[0], pos1[1] - pos2[1])
if difference in DIRECTIONS:
return DIRECTIONS.index(difference)
def apply_transform(positon, transform):
return (positon[0] + transform[0], positon[1] + transform[1])
class Cover:
def __init__(self, parent):
self.parent = parent
self.horizontal = self.parent.horizontal
h = self.parent.height
w = self.parent.width
if not self.horizontal:
self.height = h * (3 / 4)
self.width = w
else:
self.height = h
self.width = w * (3 / 4)
self.color = self.parent.parent.color
@property
def x(self):
if self.horizontal:
return self.parent.position[0] + (self.parent.width/8)
return self.parent.position[0]
@property
def y(self):
if not self.horizontal:
return self.parent.position[1] + (self.parent.height/8)
return self.parent.position[1]
def draw(self):
pygame.draw.rect(self.parent.display, self.color, ((self.x, self.y), (self.width, self.height)))
class Wall:
def __init__(self, display, position, color=COLORS[4], width=10, height=40, horizontal=1, parent=None):
self.display = display
self.position = position
self.color = color
self.original_color = self.color
self.horizontal = horizontal
if self.horizontal:
height, width = width, height
self.width = width
self.height = height
self._visible = True
self.friend = None
self.parent = parent
self.covered = False
self.cover = Cover(self)
def draw(self, corners=True):
if not self.covered or corners:
pygame.draw.rect(self.display, self.color, (self.position, (self.width, self.height)))
if self.covered:
self.cover.draw()
if not corners:
return
@property
def is_border_wall(self):
return self.friend == None
@property
def visible(self):
return self._visible
@visible.setter
def visible(self, value):
self._visible = value
if not self.visible:
if self.parent is not None:
self.color = self.parent.color
else:
self.color = self.original_color
def hide(self, cover=True, with_border_walls=False):
if not self.is_border_wall or with_border_walls:
if not cover:
self.visible = False
if not self.is_border_wall:
self.friend.visible = False
else:
self.covered = True
if not self.is_border_wall:
self.friend.covered = True
def show(self, with_border_walls=False):
if not self.is_border_wall or with_border_walls:
self.visible = True
if not self.is_border_wall:
self.friend.visible = True
if self.covered:
self.covered = False
if not self.is_border_wall:
self.friend.covered = False
def is_taken(self):
if self.is_border_wall:
return True
return self.friend.parent.taken
def merge_walls(wall1, wall2):
wall1.friend, wall2.friend = wall2, wall1
def main():
pygame.init()
DISPLAY = pygame.display.set_mode((600, 600))
pygame.display.set_caption('Wall Test')
wall = Wall(DISPLAY, (50, 50))
def update_display():
DISPLAY.fill((255, 255, 255))
wall.draw()
pygame.display.update()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
wall.hide()
update_display()
pygame.quit()
if __name__ == '__main__':
main()