Skip to content

Commit

Permalink
[FEATURE] Implemented image rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickname2002 committed May 28, 2023
1 parent c5fe39d commit de28f97
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 17 deletions.
Binary file modified __pycache__/game.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/jorcademy.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/primitives.cpython-311.pyc
Binary file not shown.
8 changes: 4 additions & 4 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

def setup() -> None:
screen(800, 600)
backdrop(255, 255, 255)
backdrop((255, 255, 255))


def draw() -> None:
ellipse(RED, 400, 300, 100, 200)
rect(GREEN, 400, 300, 100, 100)
text("Hell yeah!", BLUE, 400, 100)
image('jorcademy.png', 400, 260, 0.4)
text("Think diffewent, UwU", (0, 0, 0), 400, 400)
pass
26 changes: 15 additions & 11 deletions jorcademy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,57 @@
from typing import Tuple

# Game settings
screen_size = (100, 100)
screen_title = "JorCademy Engine"
background_color = (0, 0, 0)
draw_buffer = []
screen_size: tuple = (100, 100)
screen_title: str = "JorCademy Engine (name not final)"
background_color: tuple = (0, 0, 0)
draw_buffer: list = []

# Create type aliases
color = Tuple[int, int, int]


# Change screen size
def screen(width: int, height: int) -> (int, int):
def screen(width: int, height: int) -> None:
global screen_size
screen_size = (width, height)


# Change screen title
def title(t: str):
def title(t: str) -> None:
global screen_title
screen_title = t


# Change screen background color
def backdrop(r: int, g: int, b: int):
def backdrop(c: color) -> None:
global background_color
background_color = (r, g, b)
background_color = c


# Draw a circle
def ellipse(c: (int, int, int), x: float, y: float, w: float, h: float):
def ellipse(c: color, x: float, y: float, w: float, h: float) -> None:
e = Ellipse(c, x, y, w, h)
draw_buffer.append(e)


# Draw a rectangle
def rect(c: (int, int, int), x: float, y: float, w: float, h: float):
def rect(c: color, x: float, y: float, w: float, h: float) -> None:
r = Rectangle(c, x, y, w, h)
draw_buffer.append(r)


# Draw a string of text
def text(content: str, c: (int, int, int), x: float, y: float):
def text(content: str, c: color, x: float, y: float) -> None:
font = pygame.font.Font(None, 48)
text_surface = font.render(content, True, c)
t = Text(content, text_surface, c, x, y, None, None)
draw_buffer.append(t)


# Draw an image
def image(url: str, x: float, y: float, scale: float) -> None:
i = Image(url, scale, x, y)
draw_buffer.append(i)



33 changes: 31 additions & 2 deletions primitives.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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
Expand All @@ -16,6 +16,7 @@ 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)
Expand All @@ -34,6 +35,7 @@ def draw(self, context: pygame.display):
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)
Expand All @@ -49,10 +51,11 @@ def draw(self, context: pygame.display):
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 = "Tex"
self.object_name = "Text"
self.color = color
self.contents = content
self.surface = surface
Expand All @@ -63,3 +66,29 @@ def draw(self, context: pygame.display):
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)

0 comments on commit de28f97

Please sign in to comment.