-
Notifications
You must be signed in to change notification settings - Fork 2
/
sprites.py
74 lines (55 loc) · 2.28 KB
/
sprites.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import pygame
from constants import *
class Paddle(pygame.sprite.Sprite):
def __init__(self, player, fieldRect):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((1, 5))
self.rect = self.image.get_rect()
self.image.fill(COLOR_WHITE)
self._direction = DIR_NONE
self._player = player
self._fieldRect = fieldRect
if player == PLAYER_LEFT: self.rect.midleft = self._fieldRect.midleft
else: self.rect.midright = self._fieldRect.midright
def move(self, direction):
self._direction = direction
def update(self, *args):
_rect = self.rect.move(0, self._direction).clamp(self._fieldRect)
self.rect = _rect
class Ball(pygame.sprite.Sprite):
def __init__(self, fieldRect, speed = 0.4):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((1, 1))
self.rect = self.image.get_rect()
self.image.fill(COLOR_WHITE)
self.rect.center = fieldRect.center
self._fieldRect = fieldRect
self._dirx = 1
self._diry = 1
self._speed = speed
self._movement = 0.0
def increase_speed(self):
new_speed = self._speed * 1.05
self._speed = new_speed if new_speed < 1 else 1
def update(self, *args):
self._movement += self._speed
if self._movement >= 1.0:
if self.rect.top <= self._fieldRect.top or self.rect.bottom >= self._fieldRect.bottom:
self._diry *= -1
if self.rect.left <= self._fieldRect.left or self.rect.right >= self._fieldRect.right:
self._dirx *= -1
_rect = self.rect.move(self._movement * self._dirx, self._movement * self._diry)
self.rect = _rect
self._movement -= 1.0
class Wall(pygame.sprite.Sprite):
def __init__(self, player, fieldRect):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((1, fieldRect.height))
self.rect = self.image.get_rect()
#self.image.fill(COLOR_RED)
self._player = player
self._fieldRect = fieldRect
if player == PLAYER_RIGHT: self.rect.midleft = self._fieldRect.midleft
else: self.rect.midright = self._fieldRect.midright
def get_player(self):
return self._player