forked from JorCademy/JorCademy-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives.py
144 lines (109 loc) · 4.66 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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)
def rotate(self, image, angle, pivot):
pos = (self.x,self.y)
originPos = pivot
# offset from pivot to center
image_rect = image.get_rect(topleft = (pos[0] - originPos[0], pos[1]-originPos[1]))
offset_center_to_pivot = pygame.math.Vector2(pos) - image_rect.center
# roatated offset from pivot to center
rotated_offset = offset_center_to_pivot.rotate(-angle)
# roatetd image center
rotated_image_center = (pos[0] - rotated_offset.x, pos[1] - rotated_offset.y)
# get a rotated image
rotated_image = pygame.transform.rotate(image, angle)
rotated_image_rect = rotated_image.get_rect(center = rotated_image_center)
return (rotated_image, rotated_image_rect)
# 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,rotated_rect = self.rotate(shape_surf, self.rotation, (self.width/2,self.height/2))
# Draw circle
context.blit(rotated_surf, rotated_rect)
# 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,rect= self.rotate(image, self.rotation, (self.width/2,self.height/2))
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, rotation):
super().__init__(x, y, w, h, rotation)
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
self.surface, text_position = self.rotate(self.surface, self.rotation, (self.width/2,self.height/2))
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, image_rect = self.rotate(image, self.rotation, (new_size[0]/2, new_size[1]/2))
# 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