summaryrefslogtreecommitdiff
path: root/pong/ball.py
diff options
context:
space:
mode:
authorRobert Scheibe <robert.scheibe@mailbox.org>2020-06-18 11:37:11 +0200
committerRobert Scheibe <robert.scheibe@mailbox.org>2020-06-18 11:37:11 +0200
commitd3a4e5b0e664aea57c4cce59631785ff5b61eabf (patch)
treeebfb8bddea356b72410ba9c2ead488deae2dfdf1 /pong/ball.py
initial commit
Diffstat (limited to 'pong/ball.py')
-rw-r--r--pong/ball.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/pong/ball.py b/pong/ball.py
new file mode 100644
index 0000000..f4e0168
--- /dev/null
+++ b/pong/ball.py
@@ -0,0 +1,33 @@
+import pygame
+from random import randint
+
+BLACK = (0, 0, 0)
+
+class Ball(pygame.sprite.Sprite):
+ #This class represents a car. It derives from the "Sprite" class in Pygame.
+
+ def __init__(self, color, width, height):
+ # Call the parent class (Sprite) constructor
+ super().__init__()
+
+ # Pass in the color of the car, and its x and y position, width and height.
+ # Set the background color and set it to be transparent
+ self.image = pygame.Surface([width, height])
+ self.image.fill(BLACK)
+ self.image.set_colorkey(BLACK)
+
+ # Draw the ball (a rectangle!)
+ pygame.draw.rect(self.image, color, [0, 0, width, height])
+
+ self.velocity = [randint(4,8),randint(-8,8)]
+
+ # Fetch the rectangle object that has the dimensions of the image.
+ 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)