-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.py
49 lines (40 loc) · 1.12 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
from turtle import Turtle
import random
WIDTH = 20
HEIGHT = 20
class Ball(Turtle):
def __init__(self):
super().__init__()
self.color("white")
self.shape("circle")
self.penup()
self.x_move = 10
self.y_move = 5
self.move_speed = 0.1
def is_collide_paddle(self, paddleObject):
result = False
if self.distance(paddleObject) < 50 and self.xcor() > 320:
result = True
elif self.distance(paddleObject) < 50 and self.xcor() < -320:
result = True
return result
def move(self):
new_x = self.xcor() + self.x_move
new_y = self.ycor() + self.y_move
self.goto(new_x, new_y)
def bounce(self):
self.y_move *= -1
self.x_move *= -1
def bounce_x(self):
self.x_move *= -1
self._increase_speed()
def bounce_y(self):
self.y_move *= -1
def reset_position(self):
self.goto(0, 0)
self._reset_speed()
self.y_move *= -1
def _increase_speed(self):
self.move_speed *= 0.9
def _reset_speed(self):
self.move_speed = 0.1