-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaddle.py
48 lines (40 loc) · 1.54 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
import pygame
from util import *
class paddle:
def __init__(self, width: int, height: int, ball):
self.width = width
self.height = height
self.paddleSize = 1
self.paddle = pygame.Rect(width // 2 - 75, height - 40, 150, 20)
self.lives = 4
self.died = 1
self.ball = ball
def changeSize(self):
if self.paddleSize < 0:
self.paddle.width //= 3
self.paddleSize -= 1
def draw(self, screen):
pygame.draw.rect(screen, (0, 255, 0), self.paddle)
def handleInput(self, input):
for event in input:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE or event.key == pygame.K_RETURN:
self.launchBall()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
self.paddle.x -= 4
self.paddle.x = clamp(self.paddle.x, 0, self.width - 150)
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
self.paddle.x += 4
self.paddle.x = clamp(self.paddle.x, 0, self.width - 150)
if self.died:
self.ball.ball.center = (self.paddle.centerx, self.paddle.y - 10)
def launchBall(self):
if self.died:
self.ball.speed = [0, -4]
self.died = 0
def collied(self):
if self.paddle.colliderect(self.ball.ball):
if not self.died:
self.ball.speed[0] = int((self.ball.ball.centerx - self.paddle.centerx) // 10)
self.ball.speed[1] *= -1