the-honk/python/pong/Ball.py
2024-10-09 18:02:48 +01:00

21 lines
No EOL
664 B
Python

import pygame
from random import randint
BLACK = (0,0,0)
class Ball(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
pygame.draw.rect(self.image, color, [0, 0, width, height])
self.velocity = [randint(4,8), randint(-8, 8)]
self.rect = self.image.get_rect()
def update(self):
self.rect.x += self.velocity[0]
self.rect.y += self.velocity[1]
def bounce(self):
self.velocity[0] = -self.velocity[0]
self.velocity[1] = randint(-8, 8)