forked from JorCademy/JorCademy-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives.py
94 lines (75 loc) · 3.01 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
import pygame
# Super class - representing objects that can be drawn on the screen
class DrawableObject:
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.width = w
self.height = h
self.object_name = "Drawable"
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):
super().__init__(x, y, w, h)
self.object_name = "Ellipse"
self.color = color
def draw(self, context: pygame.display):
center = (self.x, self.y)
circle_rect = pygame.Rect(center[0] - self.width / 2,
center[1] - self.height / 2,
self.width,
self.height)
pygame.draw.ellipse(context,
self.color,
circle_rect,
self.width)
# Derived class - representing a rectangular object
class Rectangle(DrawableObject):
def __init__(self, color, x, y, w, h):
super().__init__(x, y, w, h)
self.object_name = "Rectangle"
self.color = color
def draw(self, context: pygame.display):
center = (self.x, self.y)
rectangle_rect = pygame.Rect(center[0] - self.width / 2,
center[1] - self.height / 2,
self.width,
self.height)
pygame.draw.rect(context, self.color, rectangle_rect, self.width)
# Derived class - representiung a text object
class Text(DrawableObject):
def __init__(self, content, surface, color, x, y, w, h):
super().__init__(x, y, w, h)
self.object_name = "Text"
self.color = color
self.contents = content
self.surface = surface
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):
super().__init__(x, y, None, None)
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)
# Draw image
context.blit(image, image_rect)