-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.py
48 lines (40 loc) · 1.44 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
# pylint: disable=too-many-instance-attributes
"""
Required imports
Pygame
Height (from Utilities)
Width (from Utilities)
"""
import random
import pygame
from Utilities import HEIGHT, WIDTH
class Ball(pygame.sprite.Sprite):
"""Class to keep track of the ball."""
def __init__(self):
super().__init__()
self.surf = pygame.Surface((20, 20))
self.surf.fill((255, 255, 255))
self.rect = self.surf.get_rect(center = (50, HEIGHT / 2))
self.x_vel = self.y_vel = 3
self.p1_score = self.p2_score = 0
def move(self):
"""Handling the movement of the ball"""
# Handling the bouncing off bottom or top
if self.rect.bottom >= HEIGHT or self.rect.top <= 0:
self.y_vel *= -1
# Scoring
if self.rect.left <= 0:
self.rect = self.surf.get_rect(center=(self.rect.centerx + 20, \
random.randint(0, HEIGHT)))
self.x_vel *= -1
self.p2_score += 1
elif self.rect.right >= WIDTH:
self.rect = self.surf.get_rect(center=(self.rect.centerx - 20, \
random.randint(0, HEIGHT)))
self.x_vel *= -1
self.p1_score += 1
self.rect.move_ip(self.x_vel, self.y_vel)
def collide(self, player):
"""Handling collision with the player's platform"""
self.x_vel *= -1
self.y_vel = abs(self.y_vel) * player.dir if player.dir != 0 else self.y_vel