forked from JorCademy/JorCademy-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives.py
124 lines (96 loc) · 3.81 KB
/
primitives.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import pygame
# Super class - representing objects that can be drawn on the screen
class DrawableObject:
def __init__(self, x, y, w, h, rotation=0):
self.x = x
self.y = y
self.width = w
self.height = h
self.object_name = "Drawable"
self.rotation = rotation
def get_type(self):
return self.object_name
def draw(self, context: pygame.display):
pygame.draw.circle(context, self.x, self.y, 100)
# Derived class - representing an ellipse object
class Ellipse(DrawableObject):
def __init__(self, color, x, y, w, h, rotation=0):
super().__init__(x, y, w, h, rotation)
self.object_name = "Ellipse"
self.color = color
def draw(self, context: pygame.display):
# Determine circle rect
center = (self.x, self.y)
rect = pygame.Rect(center[0] - self.width / 2,
center[1] - self.height / 2,
self.width,
self.height)
# Create surface for rotated circle
target_rect = pygame.Rect(rect)
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.ellipse(shape_surf, self.color, (0, 0, *target_rect.size), self.width)
# Rotate circle
rotated_surf = pygame.transform.rotate(shape_surf, self.rotation)
# Draw circle
context.blit(rotated_surf, rotated_surf.get_rect(center=target_rect.center))
# Derived class - representing a rectangular object
class Rectangle(DrawableObject):
def __init__(self, color, x, y, w, h, rotation=0):
super().__init__(x, y, w, h, rotation)
self.object_name = "Rectangle"
self.color = color
def draw(self, context: pygame.display):
# Create rect surface
surface = pygame.Surface((self.width, self.height))
surface.set_colorkey((0, 0, 0))
surface.fill(self.color)
# Convert surface to image
image = surface.copy()
# Rotate surface
image = pygame.transform.rotate(image, self.rotation)
rect = image.get_rect()
rect.center = (self.x, self.y)
# Draw surface
context.blit(image, rect)
# Derived class - representing a text object
class Text(DrawableObject):
def __init__(self, content, surface, color, x, y, w, h, size, font):
super().__init__(x, y, w, h)
self.object_name = "Text"
self.color = color
self.contents = content
self.surface = surface
self.size = size
self.font = font
def draw(self, context: pygame.display):
# Set the position of the text
text_position = self.surface.get_rect()
text_position.center = (self.x, self.y) # Centered on the screen
context.blit(self.surface, text_position)
# Derived class - representing an image object
class Image(DrawableObject):
def __init__(self, url, scale, x, y, rotation=0):
super().__init__(x, y, None, None, rotation)
self.object_name = "Image"
self.url = url
self.scale = scale
# NOTE: image must be inside the assets folder
def draw(self, context: pygame.display):
image = pygame.image.load("assets/" + self.url)
# Resize image
w = image.get_width()
h = image.get_height()
new_size = (w * self.scale, h * self.scale)
image = pygame.transform.scale(image, new_size)
# Place image in center of specified coordinates
image_rect = image.get_rect()
image_rect.center = (self.x, self.y)
# Rotate image
image = pygame.transform.rotate(image, self.rotation)
# Draw image
context.blit(image, image_rect)
# Representing an audio object
class Audio:
def __init__(self, channel: int, path: str):
self.filepath = path
self.channel = channel