-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaddle.py
41 lines (30 loc) · 1.21 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
36
37
38
39
40
41
import pygame
from Vector2D import Vector2D
import utils
class Paddle(pygame.sprite.Sprite):
def __init__(self, screen_width, screen_height, width, height, color):
super(Paddle, self).__init__()
self.width = width
self.height = height
self.color = color
self.initial_pos = Vector2D(screen_width / 2, screen_height - self.height)
self.pos = self.initial_pos.copy()
self.min_x = 0
self.max_x = screen_width - self.width
self.surface = pygame.Surface((self.width, self.height), pygame.SRCALPHA)
self.rect = pygame.draw.rect(self.surface, self.color, (0, 0, self.width, self.height))
self.reset_pos()
def lines(self):
l, t = self.rect.topleft
r, b = self.rect.bottomright
return utils.ltrb_rect_to_lines((l, t, r, b))
def midpoint(self):
return self.pos.x + (self.width / 2)
def update_pos(self):
self.rect.update(self.pos.x, self.pos.y, self.width, self.height)
def reset_pos(self):
self.pos = self.initial_pos.copy()
self.update_pos()
def update(self, new_x):
self.pos.x = utils.map_val(0, 100, self.min_x, self.max_x, new_x)
self.update_pos()