/
game.py
374 lines (357 loc) · 16.3 KB
/
game.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import pygame
from settings import *
import balls
import math
from vec2D import Vec2d as Vec2D
from collections import deque
from cue import Cue
from score import Score
from player import Player
from draw import Draw
class Game():
clock = pygame.time.Clock()
pockets = {'ur_pocket': [Vec2D(UR_POCKET), Vec2D(125, 94), Vec2D(113, 78),
Vec2D(143, 75), Vec2D(128, 63)],
'ul_pocket': [Vec2D(UL_POCKET), Vec2D(125, 480),
Vec2D(113, 495), Vec2D(143, 498),
Vec2D(128, 510)],
'dl_pocket': [Vec2D(DL_POCKET), Vec2D(974, 480),
Vec2D(986, 495), Vec2D(956, 498),
Vec2D(971, 510)],
'dr_pocket': [Vec2D(DR_POCKET), Vec2D(956, 75), Vec2D(971, 63),
Vec2D(974, 94), Vec2D(986, 79)],
'ml_pocket': [Vec2D(ML_POCKET), Vec2D(530, 498),
Vec2D(539, 510), Vec2D(568, 498),
Vec2D(560, 510)],
'mr_pocket': [Vec2D(MR_POCKET), Vec2D(530, 75), Vec2D(539, 63),
Vec2D(568, 75), Vec2D(560, 63)]}
def __init__(self):
self.moving_balls = deque([])
self.hitted_balls = deque([])
self.potted = []
pygame.display.set_caption('Cool Snooker')
self.table = pygame.image.load('Snooker_table3.png')
self.white_ball = balls.WhiteBall(coords=POS_WHITE)
self.redball1 = balls.RedBall(coords=POS_RED1)
self.redball2 = balls.RedBall(coords=POS_RED2)
self.redball3 = balls.RedBall(coords=POS_RED3)
self.redball4 = balls.RedBall(coords=POS_RED4)
self.redball5 = balls.RedBall(coords=POS_RED5)
self.redball6 = balls.RedBall(coords=POS_RED6)
self.redball7 = balls.RedBall(coords=POS_RED7)
self.redball8 = balls.RedBall(coords=POS_RED8)
self.redball9 = balls.RedBall(coords=POS_RED9)
self.redball10 = balls.RedBall(coords=POS_RED10)
self.redball11 = balls.RedBall(coords=POS_RED11)
self.redball12 = balls.RedBall(coords=POS_RED12)
self.redball13 = balls.RedBall(coords=POS_RED13)
self.redball14 = balls.RedBall(coords=POS_RED14)
self.redball15 = balls.RedBall(coords=POS_RED15)
self.black = balls.ColorBall(coords=POS_BLACK, COLOR=BLACK, points=7)
self.pink = balls.ColorBall(coords=POS_PINK, COLOR=PINK, points=6)
self.blue = balls.ColorBall(coords=POS_BLUE, COLOR=BLUE, points=5)
self.brown = balls.ColorBall(coords=POS_BROWN, COLOR=BROWN, points=4)
self.green = balls.ColorBall(coords=POS_GREEN, COLOR=GREEN, points=3)
self.yellow = balls.ColorBall(coords=POS_YELLOW,
COLOR=YELLOW, points=2)
self.first_player = Player("Player 1")
self.second_player = Player("Player 2")
self.all_balls = deque([
self.redball1, self.redball2, self.redball3,
self.redball4, self.redball5, self.redball6,
self.redball7, self.redball8, self.redball9,
self.redball10, self.redball11, self.redball12,
self.redball13, self.redball14, self.redball15,
self.white_ball, self.black, self.pink,
self.blue, self.brown, self.green, self.yellow
])
self.cue = Cue()
self.turn = self.first_player
self.board_status = STATICK
self.colol_order = iter([x for x in range(2, 8)])
self.next_target_ball = next(self.colol_order)
self.condition = STILL_RED
self.score = Score()
self.foul = False
self.hit = False
self.painter = Draw()
def ball_update(self):
for a in range(0, len(self.all_balls)-1):
for b in range(a+1, len(self.all_balls)):
ball, next_ball = self.all_balls[a], self.all_balls[b]
delta = next_ball.coords - ball.coords
if (next_ball.coords - ball.coords).length <= ball.RADIUS * 2:
if ball.velocity.length > 0 and \
next_ball.velocity.length > 0:
ball.coords += Vec2D.normalized(delta) *\
(delta.length - ball.RADIUS * 2)
next_ball.coords += Vec2D.normalized(-delta) *\
(delta.length - ball.RADIUS * 2)
self.ball_collision(ball, next_ball)
elif ball.velocity.length > 0:
if isinstance(ball, balls.WhiteBall):
self.hitted_balls.append(next_ball)
ball.coords += Vec2D.normalized(delta) *\
(delta.length - ball.RADIUS * 2)
self.ball_collision(ball, next_ball)
elif next_ball.velocity.length > 0:
if isinstance(next_ball, balls.WhiteBall):
self.hitted_balls.append(ball)
next_ball.coords += Vec2D.normalized(-delta) *\
(delta.length - ball.RADIUS * 2)
self.ball_collision(ball, next_ball)
def balls_handler(self):
for ball in self.all_balls:
if ball.velocity.length > 0:
ball.move(self.pockets)
if ball.is_potted and ball not in self.potted:
self.potted.append(ball)
if ball.vizibility:
self.painter.draw_balls(ball)
def white_ball_grab(self):
mouse_pos = Vec2D(pygame.mouse.get_pos())
if self.white_ball.coords.x-8 < mouse_pos.x < \
self.white_ball.coords.x+8 and self.white_ball.coords.y-8\
< mouse_pos.y < self.white_ball.coords.y+8:
for event in pygame.event.get():
(mouse1, mouse2, mouse3) = pygame.mouse.get_pressed()
if mouse1:
self.white_ball.grabed = True
else:
self.white_ball.grabed = False
if self.white_ball.grabed:
self.white_ball.coords = mouse_pos
def cue_handler(self):
start_pos, end_pos = self.cue.get_cue_pos(self.white_ball.coords)
self.painter.cue_draw(self.cue, start_pos, end_pos)
keys = pygame.key.get_pressed()
if keys[pygame.K_KP_ENTER]:
new_velocity = Vec2D.normalized(start_pos - end_pos)
force = Vec2D(self.white_ball.coords - start_pos).length
self.white_ball.velocity = new_velocity *\
force ** 2 / MIN_HITTING_FORCE
self.hit = True
def if_statick_board(self):
for ball in self.all_balls:
if ball.velocity.length > 0 and ball not in self.moving_balls:
self.moving_balls.append(ball)
elif ball in self.moving_balls and ball.velocity.length == 0:
self.moving_balls.remove(ball)
if not self.moving_balls:
self.board_status = STATICK
else:
self.board_status = NON_STATICK
def potted_ball_handler(self, potted, color_points=None):
red_ball = 0
color_ball = 0
points = 0
if color_points is None:
color_points = [0]
else:
color_points = color_points
for ball in potted:
if isinstance(ball, balls.WhiteBall):
self.foul = True
if isinstance(ball, balls.ColorBall):
color_ball += 1
color_points.append(ball.points)
if self.turn.target != COLOR_TARGET:
self.foul = True
if isinstance(ball, balls.RedBall):
if self.turn.target != RED_TARGET:
self.foul = True
red_ball += 1
points += ball.points
ball.velocity = Vec2D(0, 0)
if isinstance(ball, balls.RedBall):
self.all_balls.remove(ball)
ball.is_potted = False
else:
self.ball_return(ball)
ball.is_potted = False
self.potted = []
if color_ball > 1 or (red_ball > 0 and color_ball > 0):
self.foul = True
if self.foul is True:
# print("Foul wrong ball potted")
self.change_turn()
if max(color_points) > FOUL_POINTS:
self.turn.points += max(color_points)
else:
self.turn.points += FOUL_POINTS
else:
if self.turn.target == RED_TARGET:
self.turn.points += points
else:
self.turn.points += max(color_points)
self.turn.change_target()
def game_handler(self):
self.score.show_score(self.first_player, self.second_player, self.turn)
self.ball_update()
self.if_statick_board()
self.check_condition()
self.foul = False
if self.board_status == STATICK:
if not self.hitted_balls and self.hit is True and not self.potted:
self.change_turn()
self.turn.points += FOUL_POINTS
# print("Foul no ball hit")
self.hit = False
self.cue_handler()
if self.hitted_balls:
if self.condition == STILL_RED:
if (isinstance(self.hitted_balls[0], balls.ColorBall)
and self.turn.target != COLOR_TARGET) or\
(isinstance(self.hitted_balls[0], balls.RedBall)
and self.turn.target != RED_TARGET):
if not self.potted:
self.foul = True
self.change_turn()
# print("Foul wrong ball hit")
if self.hitted_balls[0].points > FOUL_POINTS:
self.turn.points += self.hitted_balls[0].points
else:
self.turn.points += FOUL_POINTS
else:
self.foul = True
points = [self.hitted_balls[0].points]
self.potted_ball_handler(self.potted,
color_points=points)
if self.potted and self.foul is not True:
self.potted_ball_handler(self.potted)
elif self.foul is not True:
# print("No ball poted")
self.change_turn()
else:
self.no_red_game_handler()
self.hitted_balls = deque([])
if self.potted:
self.potted_ball_handler(self.potted)
def change_turn(self):
if self.turn == self.first_player:
self.turn = self.second_player
else:
self.turn = self.first_player
self.turn.target = RED_TARGET
def ball_return(self, potted_ball):
potted_ball.vizibility = True
returning_pos = Vec2D(potted_ball.pos)
color_balls_pos = [x.pos for x in self.all_balls
if isinstance(x, balls.ColorBall)]
empty_place = False
my_place_taken = self.chek_for_place(potted_ball)
if my_place_taken is True:
for position in color_balls_pos:
empty_position = self.chek_for_place(potted_ball, pos=position)
if empty_position is not True:
empty_place = True
returning_pos = position
break
if my_place_taken is True and empty_place is not True:
found_place = False
while found_place is not True:
flag = self.chek_for_place(potted_ball, pos=returning_pos)
if flag is not True:
found_place = True
break
returning_pos.x += 1
potted_ball.coords = returning_pos
else:
potted_ball.coords = returning_pos
def chek_for_place(self, potted_ball, pos=None):
if pos is None:
pos = potted_ball.pos
for ball in self.all_balls:
if ball is not potted_ball:
delta = ball.coords - pos
if delta.length < ball.RADIUS * 2:
return True
return False
def check_condition(self):
flag = True
for ball in self.all_balls:
if isinstance(ball, balls.RedBall):
flag = False
break
if flag:
if self.turn.target == COLOR_TARGET:
self.condition = STILL_RED
else:
self.condition = RED_FREE
def no_red_game_handler(self):
points = [0]
if self.hitted_balls[0].points == self.next_target_ball:
if self.potted:
if len(self.potted) > 1:
# print("Foul more then 1 colorball potted")
self.change_turn()
for ball in self.potted:
points.append(ball.points)
self.ball_return(ball)
ball.is_potted = False
self.potted = []
if max(points) > FOUL_POINTS:
self.turn.points += max(points)
else:
self.turn.points += FOUL_POINTS
else:
if self.potted[0].points == self.next_target_ball:
self.turn.points += self.potted[0].points
self.all_balls.remove(self.potted[0])
try:
self.next_target_ball = next(self.colol_order)
except:
self.next_target_ball = False
# print("Game finished")
else:
# print("Foul wrong colorball potted")
self.change_turn()
if self.potted[0].points > FOUL_POINTS:
self.turn.points += self.potted[0].points
else:
self.turn.points += FOUL_POINTS
self.ball_return(self.potted[0])
self.potted[0].is_potted = False
self.potted.remove(self.potted[0])
else:
# print("No colorball potted")
self.change_turn()
else:
# print("Foul wrong colorball hited")
self.change_turn()
if self.potted:
for ball in self.potted:
points.append(ball.points)
self.ball_return(ball)
ball.is_potted = False
self.potted = []
points.append(self.hitted_balls[0].points)
if max(points) > FOUL_POINTS:
self.turn.points += max(points)
else:
self.turn.points += FOUL_POINTS
else:
if self.hitted_balls[0].points > FOUL_POINTS:
self.turn.points += self.hitted_balls[0].points
else:
self.turn.points += FOUL_POINTS
def ball_collision(self, ball, next_ball):
delta = next_ball.coords - ball.coords
unit_delta = Vec2D(delta/delta.get_length())
unit_tangent = Vec2D(unit_delta.perpendicular())
velocity_1_n = unit_delta.dot(ball.velocity)
velocity_1_t = unit_tangent.dot(ball.velocity)
velocity_2_n = unit_delta.dot(next_ball.velocity)
velocity_2_t = unit_tangent.dot(next_ball.velocity)
new_velocity_1_t = velocity_1_t
new_velocity_2_t = velocity_2_t
new_velocity_1_n = velocity_2_n
new_velocity_2_n = velocity_1_n
new_velocity_1_n = Vec2D(new_velocity_1_n * unit_delta)
new_velocity_2_n = Vec2D(new_velocity_2_n * unit_delta)
new_velocity_1_t = Vec2D(new_velocity_1_t * unit_tangent)
new_velocity_2_t = Vec2D(new_velocity_2_t * unit_tangent)
new_velocity_1 = Vec2D(new_velocity_1_n + new_velocity_1_t)
new_velocity_2 = Vec2D(new_velocity_2_n + new_velocity_2_t)
ball.velocity = new_velocity_1
next_ball.velocity = new_velocity_2