-
Notifications
You must be signed in to change notification settings - Fork 0
/
visual.py
271 lines (212 loc) · 9.14 KB
/
visual.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import pygame, sys, os
from pygame.locals import *
import model
import time
from threading import Thread, Event
import math
SCREEN_RES = {'width':1300, 'height':800}
BALL_COLOR = (255,255,255)
THROTTLE_VALUE = 0.5
MOVEABLE_COLOR = (123, 0, 233)
pygame.init()
res = tuple(SCREEN_RES.values())
if sys.version_info.major == 3:
if input("Enter f for fullscreen:") == "f":
screen = pygame.display.set_mode(res, FULLSCREEN)
else:
screen = pygame.display.set_mode(res)
elif sys.version_info.major == 2:
if raw_input("Enter f for fullscreen:") == "f":
screen = pygame.display.set_mode(res, FULLSCREEN)
else:
screen = pygame.display.set_mode(res)
class ThrottleThread(Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self, kwargs, daemon):
super(ThrottleThread, self).__init__(kwargs = kwargs, daemon = daemon)
self._stop_event = Event()
def stop(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
def run(self):
if sys.version_info.major == 3:
kwargs = self._kwargs
elif sys.version_info.major == 2:
kwargs = self.__kwargs
self.throttle_control(**kwargs)
def throttle_control(self, board, delta_vx, delta_vy):
while True:
board.moveable_ball.throttle(delta_vx, delta_vy)
time.sleep(0.1)
if self.stopped():
break
def add_balls1(board):
board.add_ball(r = 100, x = 900, y = 600, vx = 20, vy = 0, m = 23)
board.add_ball(r = 50, x = 600, y = 400, vx = 40, vy = -20, m = 23)
def add_balls2(board):
board.add_ball(r = 100, x = 500, y = 600, vx = 20, vy = 0, m = 23)
board.add_ball(r = 50, x = 650, y = 600, vx = 40, vy = 0, m = 10)
def add_balls3(board):
board.add_ball(r = 100, x = 480, y = 600, vx = 10, vy = 0, m = 23)
board.add_ball(r = 100, x = 700, y = 600, vx = -10, vy = 0, m = 23)
def add_balls4(board):
ball_1 = board.add_ball(r = 1, x = 45, y = 50, vx = 5, vy = 0, m = 23)
ball_2 = board.add_ball(r = 1, x = 50, y = 50, vx = -5, vy = 0, m = 23)
def add_balls5(board):
ball_1 = board.add_ball(r = 10, x = 500, y = 50, vx = 5, vy = 0, m = 23)
ball_2 = board.add_ball(r = 10, x = 556, y = 50, vx = -5, vy = 0, m = 23)
def add_balls5(board):
ball_1 = board.add_ball(r = 10, x = 500, y = 550, vx = 5, vy = -5, m = 23)
ball_2 = board.add_ball(r = 10, x = 580, y = 450, vx = -5, vy = 5, m = 23)
def add_balls6(board):
ball_1 = board.add_ball(r = 5, x = 50, y = 55, vx = 0, vy = -1, m = 1)
ball_2 = board.add_ball(r = 5, x = 50, y = 45, vx = 0, vy = 1, m = 1)
def add_balls7(board):
ball_1 = board.add_ball(r = 40, x = 900, y = 750, vx = 0, vy = -1, m = 1)
ball_2 = board.add_ball(r = 5, x = 700, y = 700, vx = 0, vy = 1, m = 1)
ball_3 = board.add_ball(r = 50, x = 500, y = 500, vx = 5, vy = 3, m = 100)
ball_4 = board.add_ball(r=100, x = 100, y = 100, vx = 7, vy = -5, m = 20)
ball_5 = board.add_ball(r = 5, x = 800, y = 750, vx = 0, vy = -1, m = 1)
ball_6 = board.add_ball(r = 5, x = 750, y = 700, vx = 0, vy = 1, m = 1)
ball_7 = board.add_ball(r = 5, x = 800, y = 700, vx = 0, vy = -1, m = 1)
ball_8 = board.add_ball(r = 5, x = 700, y = 600, vx = 0, vy = 1, m = 1)
ball_9 = board.add_ball(r = 5, x = 770, y = 600, vx = 0, vy = 1, m = 1)
ball_9 = board.add_ball(r = 5, x = 790, y = 600, vx = 0, vy = 1, m = 1)
ball_10 = board.add_ball(r = 5, x = 500, y = 600, vx = 0, vy = 1, m = 1)
ball_11 = board.add_ball(r = 5, x = 800, y = 660, vx = 0, vy = 1, m = 1)
ball_12 = board.add_ball(r = 5, x = 700, y = 680, vx = 0, vy = 1, m = 1)
ball_13 = board.add_ball(r = 5, x = 700, y = 610, vx = 0, vy = 1, m = 1)
ball_14 = board.add_ball(r = 5, x = 700, y = 620, vx = 0, vy = 1, m = 1)
def add_balls8(board):
ball_1 = board.add_ball(r=10, x = 500, y=500, vx=0,vy=0)
ball_2 = board.add_ball(r=10, x=520, y=500, vx=0, vy=0)
ball_3 = board.add_ball(r=10, x = 520, y=520, vx=0,vy=0)
ball_5 = board.add_ball(r=10, x=510, y=481, vx=0, vy=1)
ball_6 = board.add_ball(r=100, x=600, y= 200, vx=2, vy=0, m=100)
def add_balls9(board):
ball_1 = board.add_ball(r=10, x = 500, y=500, vx=0,vy=0)
ball_2 = board.add_ball(r=10, x=520, y=500, vx=0, vy=0)
ball_3 = board.add_ball(r=10, x = 520, y=520, vx=0,vy=0)
ball_4 = board.add_ball(r=10, x=510, y=451, vx=0, vy=2)
ball_5 = board.add_ball(r=10, x=400, y=500, vx=0, vy=0)
ball_6 = board.add_ball(r=10, x=420, y=500, vx=0, vy=0)
ball_7 = board.add_ball(r=10, x=420, y=520, vx=0, vy=0)
ball_8 = board.add_ball(r=10, x=410, y=451, vx=0, vy=2)
def add_balls10(board):
ball_1 = board.add_ball(r=10, x = 500, y=500, vx=0,vy=0)
ball_2 = board.add_ball(r=10, x=520, y=500, vx=0, vy=0)
ball_3 = board.add_ball(r=10, x = 520, y=520, vx=0,vy=0)
ball_4 = board.add_ball(r=10, x = 500, y=520, vx = 0, vy=0)
ball_5 = board.add_ball(r=10, x=510, y=430, vx=0, vy=1)
moving_ball = board.add_ball(r=20, x = 30, y = 30, vx = 0, vy = 0, m = 100, moveable = True)
def add_balls11(board):
ball_1 = board.add_ball(r=10, x = 500, y=500, vx=0,vy=0)
ball_2 = board.add_ball(r=10, x=520, y=500, vx=0, vy=0)
ball_3 = board.add_ball(r=10, x = 500, y=520, vx = 0, vy=0)
ball_4 = board.add_ball(r=10, x=510, y=430, vx=0, vy=1)
ball_5 = board.add_ball(r=10, x = 600, y=500, vx=0,vy=0)
ball_6 = board.add_ball(r=10, x=620, y=500, vx=0, vy=0)
ball_7 = board.add_ball(r=10, x = 620, y=520, vx = 0, vy=0)
ball_8 = board.add_ball(r=10, x=610, y=430, vx=0, vy=1)
def add_balls12(board): # all separated by 1
ball_1 = board.add_ball(r=10, x = 500, y=500, vx=0,vy=0)
ball_2 = board.add_ball(r=10, x=521, y=500, vx=0, vy=0)
ball_4 = board.add_ball(r=10, x = 500, y=521, vx = 0, vy=0)
ball_3 = board.add_ball(r=10, x = 521, y=521, vx=0,vy=0)
ball_5 = board.add_ball(r=10, x=510, y=430, vx=0, vy=1)
def add_balls13(board):
ball_1 = board.add_ball(r=10, x = 500, y = 500, vx=0, vy=0)
ball_2 = board.add_ball(r=10, x=500, y=520, vx=0, vy=0)
ball_3 = board.add_ball(r=10, x=500, y=540, vx=0, vy=0)
ball_4 = board.add_ball(r=10, x=500, y=400, vx=0, vy=1)
def add_balls14(board):
ball_1 = board.add_ball(r=10, x = 500, y=500, vx=0,vy=0)
ball_2 = board.add_ball(r=10, x=520, y=500, vx=0, vy=0)
ball_4 = board.add_ball(r=10, x = 500, y=520, vx = 0, vy=0)
ball_3 = board.add_ball(r=10, x = 521, y=520, vx=0,vy=0)
ball_5 = board.add_ball(r=10, x=510, y=430, vx=0, vy=1)
def add_balls15(board):
ball = board.add_ball(r=10, x=500, y=500, vx = 0, vy=0, moveable = True)
def setup_board():
board = model.boardmodel.Board(**SCREEN_RES)
add_balls10(board)
return board
def draw_ball(color, center, radius):
pygame.draw.circle(screen, color, center, radius)
def disp_balls(board):
balls = board.get_non_moveable_balls_graphics()
for ball in balls:
center = (int(ball['x']), int(ball['y']))
radius = int(ball['r'])
draw_ball(BALL_COLOR, center, radius)
if board.moveable_ball:
coords = board.moveable_ball.get_coords_graphics()
center = (int(coords["x"]), int(coords["y"]))
radius = int(coords["r"])
draw_ball(MOVEABLE_COLOR, center, radius)
def load_lenne():
lenne = pygame.image.load("pics/lenne.png")
lenne = pygame.transform.scale(lenne, (100,100))
lenne.convert()
def disp_lenne(board):
balls = board.get_balls_pygame()
for ball in balls:
center = (int(ball['x']), int(ball['y']))
radius = int(ball['r'])
screen.blit(lenne, center)
## Thread functions
def start():
while True:
board.step()
time.sleep(0.005)
def check_if_stopped(thread):
while True:
if thread.stopped():
break
board = setup_board()
running = True
game = Thread(target = start, daemon=True)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
try:
game.start()
except ValueError as e:
print("Got Error of type "+ str(type(e)) +" and message "+e.message)
except RuntimeError:
pass
elif event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
throttle_up = ThrottleThread(kwargs = {"board":board, "delta_vx":0, "delta_vy":THROTTLE_VALUE}, daemon = True)
throttle_up.start()
elif event.type == pygame.KEYUP and event.key == pygame.K_UP:
if throttle_up:
throttle_up.stop()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
throttle_right = ThrottleThread(kwargs = {"board":board, "delta_vx":THROTTLE_VALUE, "delta_vy":0}, daemon = True)
throttle_right.start()
elif event.type == pygame.KEYUP and event.key == pygame.K_RIGHT:
if throttle_right:
throttle_right.stop()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
throttle_down = ThrottleThread(kwargs = {"board":board, "delta_vx":0, "delta_vy":-THROTTLE_VALUE}, daemon = True)
throttle_down.start()
elif event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
if throttle_down:
throttle_down.stop()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
throttle_left = ThrottleThread(kwargs = {"board":board, "delta_vx":-THROTTLE_VALUE, "delta_vy":0}, daemon = True)
throttle_left.start()
elif event.type == pygame.KEYUP and event.key == pygame.K_LEFT:
if throttle_left:
throttle_left.stop()
screen.fill((0,0,0))
disp_balls(board)
pygame.display.flip()