forked from JorCademy/JorCademy-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jorcademy.py
134 lines (95 loc) · 3.21 KB
/
jorcademy.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
from primitives import *
from typing import Tuple
from events import (handle_mouse_input, add_mouse_button_down_event,
add_mouse_button_up_event, remove_mouse_button_down_event,
remove_mouse_button_up_event, add_key_down_event, add_key_up_event,
remove_key_down_event, remove_key_up_event)
# Game settings
screen_size: tuple = (100, 100)
screen_title: str = "JorCademy Engine"
background_color: tuple = (0, 0, 0)
__draw_buffer: list = []
audio_channel_count: int = 0
# Initialize audio component
pygame.mixer.init()
# Create type aliases
color = Tuple[int, int, int]
# ==== Keyboard input ====
__key_status = {}
# Get whether a specific key is down
def is_key_down(key: str) -> bool:
if key in __key_status:
return __key_status[key]
else:
return False
# ==== Mouse input ====
__mouse_status = {}
__scroll_up: bool = False
__scroll_down: bool = False
mouse_position: (int, int) = (0, 0)
# Get whether a specific mouse button is down
def is_mouse_button_down(button: str) -> bool:
if button in __mouse_status:
return __mouse_status[button]
else:
return False
def is_scrolling_up() -> bool:
return __scroll_up
# Get whether the player is scrolling down
def is_scrolling_down() -> bool:
return __scroll_down
# Change screen size
def screen(width: int, height: int) -> None:
global screen_size
screen_size = (width, height)
# Change screen title
def title(t: str) -> None:
global screen_title
screen_title = t
# Change app icon
def icon(name: str) -> None:
app_icon = pygame.image.load("./assets/" + name)
pygame.display.set_icon(app_icon)
# Change screen background color
def backdrop(c: color) -> None:
global background_color
__draw_buffer.clear()
background_color = c
# Draw a circle
def ellipse(c: color, x: float, y: float, w: float, h: float, rotation=0) -> None:
e = Ellipse(c, x, y, w, h, rotation)
__draw_buffer.append(e)
# Draw a rectangle
def rect(c: color, x: float, y: float, w: float, h: float, rotation=0) -> None:
r = Rectangle(c, x, y, w, h, rotation)
__draw_buffer.append(r)
# Draw a string of text
def text(content: str, size: int, c: color, x: float, y: float, font="Nunito", rotation=0) -> None:
# Fetch font
try:
font = pygame.font.Font("./assets/" + font, size)
except:
font = pygame.font.SysFont(font, size)
# Draw font
text_surface = font.render(content, True, c)
w, h = font.size(content)
t = Text(content, text_surface, c, x, y, w, h, size, font, rotation)
__draw_buffer.append(t)
# Draw an image
def image(url: str, x: float, y: float, scale: float, rotation=0) -> None:
i = Image(url, scale, x, y, rotation)
__draw_buffer.append(i)
# Load new sound
def load_sound(path: str):
global audio_channel_count
sound: Audio = Audio(audio_channel_count, path)
audio_channel_count += 1
return pygame.mixer.Sound(sound.filepath)
# Play audio
def play_sound(audio_obj: Audio):
sound = pygame.mixer.Sound("./assets/" + audio_obj.filepath)
if not pygame.mixer.Channel(audio_obj.channel).get_busy():
pygame.mixer.Channel(audio_obj.channel).play(sound)
# Wait for new action
def sleep(msec: int):
pygame.time.wait(msec)