-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
212 lines (159 loc) · 5.66 KB
/
main.py
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'''
Joining Points
Inspired by IOI 2006 Joining Points
Created by Andi Qu
'''
from random import randint
import pygame
from pygame.locals import QUIT
SIZE = (640, 640)
DIAMETER = 10
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
NUM_POINTS = 20
def ccw(A, B, C):
'''
Checks if B is counterclockwise from C with origin A
'''
return (C[1] - A[1]) * (B[0] - A[0]) > (B[1] - A[1]) * (C[0] - A[0])
def intersect(A, B, C, D):
'''
Determines if the lines (A, B) and (C, D) intersect
Returns false if they just share an endpoint
'''
if (A == C or A == D or B == C or B == D):
return False
return ccw(A, C, D) != ccw(B, C, D) and ccw(A, B, C) != ccw(A, B, D)
component = [i for i in range(NUM_POINTS)]
def find(A):
'''
Find of DSU
'''
while A != component[A]:
component[A], A = component[component[A]], component[A]
return A
def union(A, B):
'''
Union of DSU
'''
component[find(A)] = component[find(B)]
class Point(pygame.sprite.Sprite):
'''
A class representing a point in the game
'''
def __init__(self, colour, pos, num):
super().__init__()
self.colour = colour
self.id = num
self.image = pygame.Surface((DIAMETER, DIAMETER))
self.image.fill(BLACK)
pygame.draw.ellipse(self.image, colour, (0, 0, DIAMETER, DIAMETER))
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = pos
class Game():
'''
A class representing the actual game and its state
'''
def __init__(self):
self._running = True
self._drawing = False
self._clicked_colour = None
self._clicked_pos = None
self._clicked = -1
self._display_surf = None
self._image_surf = None
self._text = None
self.points = pygame.sprite.Group()
self.lines = []
def on_init(self):
'''
Handles initiation of the game
'''
pygame.init()
pygame.font.init()
self._text = pygame.font.SysFont('Ubuntu', 60)
self._display_surf = pygame.display.set_mode(SIZE, pygame.HWSURFACE)
self._running = True
pygame.display.set_caption('Joining Points')
icon = pygame.image.load('joining_points.png')
pygame.display.set_icon(icon)
def on_event(self, event):
'''
Handles events
'''
if event.type == QUIT:
self._running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
clicked = [
point for point in self.points if point.rect.collidepoint(pos)]
if self._drawing:
self._drawing = False
if clicked and clicked[0].rect.center != self._clicked_pos and clicked[0].colour == self._clicked_colour:
# Check if the new line intersects any existing lines
for line in self.lines:
if intersect(line[1], line[2], self._clicked_pos, clicked[0].rect.center):
return
# Check that the two points you're connecting aren't already connected
if find(clicked[0].id) != find(self._clicked):
self.lines.append(
(self._clicked_colour, self._clicked_pos, clicked[0].rect.center))
union(clicked[0].id, self._clicked)
else:
if clicked:
self._drawing = True
self._clicked_colour = clicked[0].colour
self._clicked_pos = clicked[0].rect.center
self._clicked = clicked[0].id
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r and pygame.key.get_mods() & pygame.KMOD_CTRL and len(self.lines):
self.lines.clear()
for i in range(NUM_POINTS):
component[i] = i
def on_loop(self):
'''
Handles game logic
'''
pass
def on_render(self):
'''
Renders points and lines
'''
self._display_surf.fill(BLACK)
self.points.draw(self._display_surf)
for line in self.lines:
pygame.draw.line(self._display_surf, line[0], line[1], line[2], 2)
if self._drawing:
pygame.draw.line(self._display_surf, self._clicked_colour,
self._clicked_pos, pygame.mouse.get_pos(), 2)
if len(self.lines) == NUM_POINTS - 2:
win_message = self._text.render('You Win!', False, WHITE)
self._display_surf.blit(
win_message, (SIZE[0] // 2 - 120, SIZE[1] // 2 - 60))
pygame.display.flip()
def on_execute(self):
'''
Called when first executing game
'''
if self.on_init():
self._running = False
self.points.add(Point(GREEN, (10, 10), 0))
self.points.add(Point(GREEN, (SIZE[0] - 20, 10), 1))
self.points.add(Point(RED, (10, SIZE[1] - 20), 2))
self.points.add(Point(RED, (SIZE[0] - 20, SIZE[1] - 20), 3))
for i in range((NUM_POINTS - 4) // 2):
self.points.add(
Point(GREEN, (randint(11, SIZE[0] - 21), randint(11, SIZE[1] - 21)), 4 + 2 * i))
self.points.add(
Point(RED, (randint(11, SIZE[0] - 21), randint(11, SIZE[1] - 21)), 5 + 2 * i))
while self._running:
for event in pygame.event.get():
self.on_event(event)
self.on_loop()
self.on_render()
pygame.quit()
if __name__ == "__main__":
GAME = Game()
GAME.on_execute()