Skip to content

Commit

Permalink
[FEATURE] Implemented primitive classes for representing draw buffer …
Browse files Browse the repository at this point in the history
…objects.

[FEATURE] Implemented helper functions for drawing primitive shapes.
  • Loading branch information
nickname2002 committed May 28, 2023
1 parent ebb0bf1 commit c3fa8e3
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .idea/jorcademy-template.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
running = True

# Set app icon
pygame_icon = pygame.image.load('assets/jorcademy.png')
pygame_icon = pygame.image.load('assets/jc_icon.png')
pygame.display.set_icon(pygame_icon)


def render_objects_on_screen():
for obj in jc.draw_buffer:
obj.draw(screen)


while running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
Expand All @@ -28,11 +34,12 @@

# Render game
game.draw()
render_objects_on_screen()

# flip() the display to put your work on screen
pygame.display.flip()

clock.tick(60) # limits FPS to 60


pygame.quit()
pygame.quit()
9 changes: 7 additions & 2 deletions game.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from jorcademy import *

RED: color = (255, 0, 0)
GREEN: color = (0, 255, 0)
BLUE: color = (0, 0, 255)


def setup() -> None:
screen(800, 600)
title("Best game ever")
backdrop(255, 255, 255)


def draw() -> None:
pass
ellipse(RED, 400, 300, 100, 200)
rect(GREEN, 400, 300, 100, 100)
text("Hell yeah!", BLUE, 400, 100)
31 changes: 21 additions & 10 deletions jorcademy.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import pygame
from primitives import *
from typing import Tuple

# Game settings
screen_size = (100, 100)
screen_title = "JorCademy Engine"
background_color = (0, 0, 0)
draw_buffer = []

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


# Change screen size
Expand All @@ -25,20 +31,25 @@ def backdrop(r: int, g: int, b: int):


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


# Draw a rectangle
def rect():
pass
def rect(c: (int, int, int), x: float, y: float, w: float, h: float):
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):
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 a triangle
def triangle():
pass


# Draw a string of text
def text():
pass
65 changes: 65 additions & 0 deletions primitives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import pygame


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)


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)


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)


class Text(DrawableObject):
def __init__(self, content, surface, color, x, y, w, h):
super().__init__(x, y, w, h)
self.object_name = "Tex"
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)

0 comments on commit c3fa8e3

Please sign in to comment.