-
Notifications
You must be signed in to change notification settings - Fork 50
/
paddle.py
58 lines (46 loc) · 1.73 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pygame
import math
import constants as const
class Paddle:
def __init__(self, x, y):
self.x = x
self.y = y
self.radius = const.PADDLE_SIZE
self.speed = const.PADDLE_SPEED
self.mass = const.PADDLE_MASS
self.angle = 0
def check_vertical_bounds(self, height):
# top
if self.y - self.radius <= 0:
self.y = self.radius
# bottom
elif self.y + self.radius > height:
self.y = height - self.radius
def check_left_boundary(self, width):
if self.x - self.radius <= 0:
self.x = self.radius
elif self.x + self.radius > int(width / 2):
self.x = int(width / 2) - self.radius
def check_right_boundary(self, width):
if self.x + self.radius > width:
self.x = width - self.radius
elif self.x - self.radius < int(width / 2):
self.x = int(width / 2) + self.radius
def move(self, up, down, left, right, time_delta):
dx, dy = self.x, self.y
self.x += (right - left) * self.speed * time_delta
self.y += (down - up) * self.speed * time_delta
dx = self.x - dx
dy = self.y - dy
self.angle = math.atan2(dy, dx)
def draw(self, screen, color):
position = (int(self.x), int(self.y))
pygame.draw.circle(screen, color, position, self.radius, 0)
pygame.draw.circle(screen, (0, 0, 0), position, self.radius, 2)
pygame.draw.circle(screen, (0, 0, 0), position, self.radius - 5, 2)
pygame.draw.circle(screen, (0, 0, 0), position, self.radius - 10, 2)
def get_pos(self):
return self.x, self.y
def reset(self, start_x, start_y):
self.x = start_x
self.y = start_y