-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClock.py
85 lines (67 loc) · 2.69 KB
/
Clock.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
import pygame
class Ball:
def __init__(self, x, y, c):
self.x = x
self.y = y
self.vx = 0
self.vy = c
self.r = 5
def update(self, dt):
self.x += self.vx * dt
self.y += self.vy * dt
class ExceededSpeedOfLight(Exception):
def __init__(self):
super().__init__("EXCEEDED SPEED OF LIGHT")
class Clock:
def __init__(self, x, y, height, width, c):
self.x = x
self.y = y
self.ball = Ball(0, 0, c)
self.height = height
self.width = width
# def draw(self, screen):
# thickness = 4
# pygame.draw.line(screen, (100, 100, 100),
# (self.get_left(), self.get_top()), (self.get_right(), self.get_top()), thickness)
# pygame.draw.line(screen, (100, 100, 100),
# (self.get_left(), self.get_bottom()), (self.get_right(), self.get_bottom()), thickness)
#
# pygame.draw.circle(screen, (100, 100, 100), (int(self.ball.x), int(self.ball.y)), int(self.ball.r))
def draw(self, screen, x=None, y=None):
if x is None or y is None:
x = self.x
y = self.y
thickness = 4
left = x - 0.5*self.width
right = x + 0.5 * self.width
top = y + 0.5 * self.height
bottom = y - 0.5 * self.height
pygame.draw.line(screen, (100, 100, 100),
(left, top), (right, top), thickness)
pygame.draw.line(screen, (100, 100, 100),
(left, bottom), (right, bottom), thickness)
pygame.draw.circle(screen, (100, 100, 100),
(int(self.x + self.ball.x), int(self.y + self.ball.y)),
int(self.ball.r))
def update(self, dt, vx, vy, c):
if not self.get_left() + self.ball.r <= self.x + self.ball.x <= self.get_right() - self.ball.r:
self.ball.vx *= -1
if not self.get_bottom() + self.ball.r <= self.y + self.ball.y <= self.get_top() - self.ball.r:
self.ball.vy *= -1
# discriminant = 1.0 - ((vx+self.ball.vx)**2+(vy+self.ball.vy)**2)/c**2
# discriminant = 1.0 - ((vx)**2+(vy)**2)/c**2
# if discriminant < 0:
# raise ExceededSpeedOfLight
# dt *= discriminant**0.5
v = (vx**2 + vy**2)**0.5
# v -> c - v (but keep same sign)
self.ball.vy = self.ball.vy / float(abs(self.ball.vy)) * (c - v)
self.ball.update(dt)
def get_left(self):
return self.x - 0.5*self.width
def get_right(self):
return self.x + 0.5 * self.width
def get_top(self):
return self.y + 0.5 * self.height
def get_bottom(self):
return self.y - 0.5 * self.height