-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.py
92 lines (72 loc) · 2.01 KB
/
ball.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
"""Contains Ball and BallGroup classes."""
from __future__ import division, print_function
from render import BallRenderer
from vector2d import Vector2D
__author__ = "Zander Otavka"
class Ball(object):
"""
:type _number: int
:type _position: Vector2D
:type _renderer: BallRenderer
"""
RADIUS = 11.25
_number = None
_position = None
_renderer = None
def __init__(self, number, position):
self._number = number
self._position = position
self._renderer = BallRenderer(number, position, Ball.RADIUS)
@property
def number(self):
return self._number
@property
def position(self):
return self._position
@position.setter
def position(self, new):
self._position = new
self._renderer.position = new
def __repr__(self):
return "Ball({}, {})".format(self.number, self.position)
def delete(self):
self._renderer.delete()
class BallGroup(list):
"""
:type _size: int
"""
_size = None
def __init__(self):
super(BallGroup, self).__init__()
self._size = 0
def update(self, data):
"""
:type data: list[int]
"""
point_list = [Vector2D(data[i:i + 2])
for i in range(0, len(data), 2)]
if len(point_list) != self._size:
self.delete()
for index, point in enumerate(point_list):
if point:
self.append(Ball(index, point))
self._size = len(point_list)
else:
for ball in self:
point = point_list[ball.number]
if point:
ball.offset = point
else:
ball.delete()
self.remove(ball)
def copy(self):
"""
:rtype: BallGroup
"""
clone = BallGroup()
clone[:] = self[:]
return clone
def delete(self):
for ball in self:
ball.delete()
self[:] = []