-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink_map.py
More file actions
170 lines (120 loc) · 3.95 KB
/
link_map.py
File metadata and controls
170 lines (120 loc) · 3.95 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
168
169
170
import pygame
pygame.init()
clock = pygame.time.Clock()
clock.tick(20)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
screen = pygame.display.set_mode((500, 500))
running = True
connections_key_list = []
num_clicks = 0
LEFT = 1
def get_instance_name(position):
"""
generates a name string for a clicked positon to be used for instantiating Connection()
"""
position_x, position_y = position
instance_name = str(position_x) + str(position_y)
return instance_name
class Marker(pygame.sprite.Sprite):
"""
used to draw the marker at a given position
"""
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 50))
self.image.fill(RED)
self.image.set_alpha(50)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def check_click(self, mouse):
if self.rect.collidepoint(mouse):
self.image.set_alpha(200)
if not self.rect.collidepoint(mouse):
self.image.set_alpha(50)
class ConnectionHashMap:
def __init__(self, array_size):
self.array_size = array_size
self.array = [None for item in range(array_size)]
def hash(self, key, count_collisions=0):
key_bytes = key.encode()
hash_code = sum(key_bytes)
return hash_code + count_collisions
def compressor(self, hash_code):
return hash_code % self.array_size
def assign(self, key, value):
array_index = self.compressor(self.hash(key))
current_array_value = self.array[array_index]
if current_array_value is None:
self.array[array_index] = [key, value]
return
if current_array_value[0] == key:
self.array[array_index] = [key, value]
return
# Collision!
number_collisions = 1
while(current_array_value[0] != key):
new_hash_code = self.hash(key, number_collisions)
new_array_index = self.compressor(new_hash_code)
current_array_value = self.array[new_array_index]
if current_array_value is None:
self.array[new_array_index] = [key, value]
return
if current_array_value[0] == key:
self.array[new_array_index] = [key, value]
return
number_collisions += 1
return
def retrieve(self, key):
array_index = self.compressor(self.hash(key))
possible_return_value = self.array[array_index]
if possible_return_value is None:
return None
if possible_return_value[0] == key:
return possible_return_value[1]
retrieval_collisions = 1
while (possible_return_value != key):
new_hash_code = self.hash(key, retrieval_collisions)
retrieving_array_index = self.compressor(new_hash_code)
possible_return_value = self.array[retrieving_array_index]
if possible_return_value is None:
return None
if possible_return_value[0] == key:
return possible_return_value[1]
retrieval_collisions += 1
return
connections = ConnectionHashMap(200)
#test2 = Marker(10, 10)
all_sprites = pygame.sprite.Group()
# all_sprites.add(test2)
running = True
while running:
if num_clicks > 0:
for s in all_sprites:
mouse_pos = pygame.mouse.get_pos()
s.check_click(mouse_pos)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
mouse = pygame.mouse.get_pos()
num_clicks += 1
if num_clicks % 3 == 1:
var = get_instance_name(mouse)
connections_key_list.append(var)
connections.assign(connections_key_list[-1], mouse)
elif num_clicks % 3 == 2:
old_pos = connections.retrieve(connections_key_list[-1])
connections.assign(connections_key_list[-1], (old_pos, mouse))
# print(all_sprites)
screen.fill(BLACK)
all_sprites.update()
all_sprites.draw(screen)
pygame.display.update()
print((num_clicks % 3))
for i in connections_key_list:
var = connections.retrieve(i)
print('key: ' + i + '; value: ' + str(var))
pygame.quit()