-
Notifications
You must be signed in to change notification settings - Fork 2
/
paddle.py
35 lines (26 loc) · 1.1 KB
/
paddle.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
import pygame
import common as c
class Paddle(pygame.sprite.Sprite):
# This class represents a paddle. 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(c.BLACK)
self.image.set_colorkey(c.BLACK)
# Draw the paddle (a rectangle!)
pygame.draw.rect(self.image, color, [0, 0, width, height])
# Fetch the rectangle object that has the dimensions of the image.
self.rect = self.image.get_rect()
def moveLeft(self, pixels):
self.rect.x -= pixels
# Bounds check
if self.rect.x < 0:
self.rect.x = 0
def moveRight(self, pixels):
self.rect.x += pixels
# Bounds check
if self.rect.x > c.WIN_X - c.PADDLE_X:
self.rect.x = c.WIN_X - c.PADDLE_X