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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
import pygame
class RectangleLite:
horizontal_padding = 5
vertical_padding = 5
def __init__(self, pos, width, height, centered_x=True, centered_y=True,
filled=True, fill_colour=(255, 255, 255, 255),
outlined=True, outline_colour=(0, 0, 0, 255), outline_thickness=2, outline_radius=0):
x, y = pos
self.position = pygame.Vector2(x, y)
self.width = width
self.height = height
self.centered_x = centered_x
self.centered_y = centered_y
self.centering = pygame.Vector2(0, 0)
self.filled = filled
self.fill_colour = fill_colour
self.outlined = outlined
self.outline_colour = outline_colour
self.outline_thickness = outline_thickness
self.outline_radius = outline_radius
def update(self, delta, x=None, y=None, width=None, height=None, fill_colour=None):
if x is not None:
self.position.x = x
if y is not None:
self.position.y = y
if width is not None:
self.width = width
if height is not None:
self.height = height
if fill_colour is not None:
self.fill_colour = fill_colour
if self.centered_x:
self.centering.x = self.width / 2
if self.centered_y:
self.centering.y = self.height / 2
def get_collision_rect(self):
return pygame.Rect(
self.position - pygame.Vector2(self.horizontal_padding, self.vertical_padding) - self.centering,
(self.width + 2 * self.horizontal_padding, self.height + 2 * self.vertical_padding))
def draw(self, surface):
r = self.get_collision_rect()
if self.filled:
pygame.draw.rect(surface, self.fill_colour, r,border_radius=self.outline_radius)
if self.outlined:
pygame.draw.rect(surface, self.outline_colour, r, self.outline_thickness, self.outline_radius)
class Rectangle(RectangleLite):
def __init__(self, pos, width, height, centered_x=True, centered_y=True,
filled=True, fill_colour=(255, 255, 255, 255),
outlined=True, outline_colour=(0, 0, 0, 255), outline_thickness=2, outline_radius=0,
horizontal_padding=12, vertical_padding=12):
super().__init__(pos, width, height, centered_x, centered_y, filled, fill_colour,
outlined, outline_colour, outline_thickness, outline_radius)
self.horizontal_padding = horizontal_padding
self.vertical_padding = vertical_padding
def update(self, delta, x=None, y=None, dx=0, dy=0, width=None, height=None, fill_colour=None,
centered_x=None, centered_y=None, outlined=None, outline_colour=None, outline_thickness=None,
outline_radius=None):
if x is not None:
self.position.x = x
if y is not None:
self.position.y = y
self.position += delta * pygame.Vector2(dx, dy)
if width is not None:
self.width = width
if height is not None:
self.height = height
if fill_colour is not None:
self.fill_colour = fill_colour
if centered_x is not None:
self.centered_x = centered_x
if centered_y is not None:
self.centered_y = centered_y
if outlined is not None:
self.outlined = outlined
if self.outlined:
if outline_colour is not None:
self.outline_colour = outline_colour
if outline_thickness is not None:
self.outline_thickness = outline_thickness
if outline_radius is not None:
self.outline_radius = outline_radius
if self.centered_x:
self.centering.x = self.width / 2
if self.centered_y:
self.centering.y = self.height / 2
def draw(self, surface):
s = pygame.Surface((surface.get_width(), surface.get_height()), pygame.SRCALPHA)
super().draw(s)
surface.blit(s, (0, 0))
|