-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.py
53 lines (51 loc) · 1.91 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
import Table, random
class Ball:
#### constructor
def __init__( self, Table, width=14, height=14, colour="red",x_speed=6, y_speed=4, x_start=0, y_start=0):
self.width = width
self.height = height
self.x_posn = x_start
self.y_posn = y_start
self.colour = colour
self.x_start = x_start
self.y_start = y_start
self.x_speed = x_speed
self.y_speed = y_speed
self.Table = Table
self.circle = self.Table.draw_oval(self)
#### methods
def start_position(self):
self.x_posn = self.x_start
self.y_posn = self.y_start
def start_ball(self, x_speed, y_speed):
self.x_speed = -x_speed if random.randint(0,1) else x_speed
self.y_speed = -y_speed if random.randint(0,1) else y_speed
self.start_position()
def move_next(self):
self.x_posn = self.x_posn + self.x_speed
self.y_posn = self.y_posn + self.y_speed
# if the ball hits the left wall:
if(self.x_posn <= 3):
self.x_posn = 3
self.x_speed = -self.x_speed
# if it hits right wall:
if(self.x_posn >= (self.Table.width - (self.width - 3))):
self.x_posn = (self.Table.width - (self.width - 3))
self.x_speed = -self.x_speed
# if the ball hits hits the top wall:
if(self.y_posn <= 3):
self.y_posn = 3
self.y_speed = -self.y_speed
# if it hits bottom wall:
if(self.y_posn >= (self.Table.height - (self.height - 3))):
self.y_posn = (self.Table.height - (self.height - 3))
self.y_speed = -self.y_speed
# finally move the circle:
x1 = self.x_posn
x2 = self.x_posn+self.width
y1 = self.y_posn
y2 = self.y_posn+self.height
self.Table.move_item(self.circle, x1, y1, x2, y2)
def stop_ball(self):
self.x_speed = 0
self.y_speed = 0