-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathball.py
42 lines (35 loc) · 938 Bytes
/
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
import pygame, sys
import random
from settings import WIDTH, HEIGHT
pygame.init()
class Ball:
def __init__(self, x, y, radius):
self.x = x
self.y = y
self.radius = radius
self.rect = pygame.Rect(self.x, self.y, radius, radius)
self.color = pygame.Color("red")
self.direction = None
self.speed_x = 0
self.speed_y = 0
self._random_direction()
def _random_direction(self):
direction = ("right", "left")
self.direction = random.choice(direction)
def _ball_movement(self):
# horizontal handling
if self.direction == "right":
self.speed_x = 18
else:
self.speed_x = -18
# vertical handling
if self.rect.y >= HEIGHT - self.radius:
self.speed_y = -18
elif self.rect.y <= 0 + self.radius:
self.speed_y = 18
# wall bounce handling
self.rect.x += self.speed_x
self.rect.y += self.speed_y
def update(self, screen):
self._ball_movement()
pygame.draw.rect(screen, self.color, self.rect)