My Pygame Game Engine

poggers

WillM 85121c7c 30635c50

+3
.vscode/settings.json
··· 1 + { 2 + "python.pythonPath": "C:\\Users\\wille\\AppData\\Local\\Programs\\Python\\Python39\\python.exe" 3 + }
+2 -2
client.py
··· 1 1 import pygame, time 2 - from src.scripts.game import Game 2 + from src.scripts.game_manager import Game_Manager 3 3 from src.scripts.project import * 4 4 5 - game = Game() 5 + game = Game_Manager() 6 6 7 7 game.initialize() 8 8
src/resources/main/assets/images/player.png

This is a binary file and will not be displayed.

src/scripts/__pycache__/__init__.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/__pycache__/game.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/__pycache__/game_manager.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/__pycache__/project.cpython-39.pyc

This is a binary file and will not be displayed.

+5 -25
src/scripts/game.py src/scripts/game_manager.py
··· 9 9 from .modules.renderer import renderer 10 10 from .modules.fonts import font_handler 11 11 12 - from .modules.screens.screen_1 import screen_1 13 - from .modules.screens.screen_2 import screen_2 14 - from .modules.screens.screen_3 import screen_3 15 - from .modules.screens.screen_4 import screen_4 12 + from .screens.main_menu import main_menu 16 13 17 - class Game: 14 + class Game_Manager: 18 15 19 16 game_speed = 1 20 17 clock = pygame.time.Clock() ··· 56 53 self.change_title(self.properties["id"]) 57 54 self.input.input_state = "general" 58 55 59 - sscreen_1 = screen_1(self) 60 - sscreen_2 = screen_2(self) 61 - sscreen_3 = screen_3(self) 62 - sscreen_4 = screen_4(self) 63 - self.renderer.load_screen(sscreen_1, 'screen_1') 64 - self.renderer.load_screen(sscreen_2, 'screen_2') 65 - self.renderer.load_screen(sscreen_3, 'screen_3') 66 - self.renderer.load_screen(sscreen_4, 'screen_4') 67 - self.renderer.switch_screen('screen_1') 68 - 69 - self.screen = 1 56 + screen_main_menu = main_menu(self) 57 + self.renderer.load_screen(screen_main_menu, 'main_menu') 58 + self.renderer.switch_screen('main_menu') 70 59 71 60 def update(self): 72 61 # Calculate Delta Time ··· 80 69 self.stop() 81 70 if event.type == pygame.KEYDOWN: 82 71 self.input.any_key_pressed = True 83 - 84 - if self.input.is_just_pressed('LEFT'): 85 - self.screen -= 1 86 - if self.input.is_just_pressed('RIGHT'): 87 - self.screen += 1 88 - 89 - self.screen = min(4, max(self.screen, 1)) 90 - if self.renderer.current_screen != 'screen_' + str(self.screen): 91 - self.renderer.switch_screen('screen_' + str(self.screen)) 92 72 93 73 # Screens 94 74 self.renderer.update()
src/scripts/game/__pycache__/mouse.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/game/__pycache__/player.cpython-39.pyc

This is a binary file and will not be displayed.

+45
src/scripts/game/player.py
··· 1 + import pygame, math 2 + from src.scripts.modules.entity import * 3 + 4 + class player_movement_component: 5 + def __init__(self, game, parent) -> None: 6 + self.game = game 7 + self.parent = parent 8 + 9 + def update(self): 10 + if self.game.input.is_pressed("a"): 11 + self.parent.transform.x -= 1 12 + if self.game.input.is_pressed("d"): 13 + self.parent.transform.x += 1 14 + if self.game.input.is_pressed("w"): 15 + self.parent.transform.y-= 1 16 + if self.game.input.is_pressed("s"): 17 + self.parent.transform.y += 1 18 + 19 + def render(self): 20 + pass 21 + 22 + class player_renderer_component: 23 + def __init__(self, game, parent) -> None: 24 + self.game = game 25 + self.parent = parent 26 + 27 + self.image = self.game.image.load("player.png") 28 + 29 + def update(self): 30 + pass 31 + 32 + def render(self): 33 + self.game.renderer.screen.blit(self.image, (self.parent.transform.x, self.parent.transform.y)) 34 + 35 + class Player(Entity): 36 + def __init__(self, game) -> None: 37 + self.game = game 38 + 39 + self.components = [] 40 + 41 + self.transform = Transform() 42 + self.transform.y = 500 43 + 44 + self.add_component(player_movement_component(game, self)) 45 + self.add_component(player_renderer_component(game, self))
src/scripts/modules/__pycache__/colors.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/__pycache__/entity.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/__pycache__/fonts.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/__pycache__/gui.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/__pycache__/image.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/__pycache__/input.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/__pycache__/language.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/__pycache__/math_utils.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/__pycache__/renderer.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/__pycache__/sound.cpython-39.pyc

This is a binary file and will not be displayed.

+21
src/scripts/modules/entity.py
··· 1 + class Transform: 2 + def __init__(self) -> None: 3 + self.x = 0 4 + self.y = 0 5 + 6 + class Entity: 7 + def add_component(self, component): 8 + if component not in self.components: 9 + self.components.append(component) 10 + 11 + def remove_component(self, component): 12 + if component not in self.components: 13 + self.components.remove(self.components.index(component)) 14 + 15 + def update(self): 16 + for component in self.components: 17 + component.update() 18 + 19 + def render(self): 20 + for component in self.components: 21 + component.render()
+71 -36
src/scripts/modules/gui.py
··· 1 1 import pygame 2 2 3 - class center_constraint(): 3 + class rect_constraints(): 4 4 pass 5 5 6 - class aspect_constraint(): 6 + class center_constraint(rect_constraints): 7 + pass 8 + 9 + class aspect_constraint(rect_constraints): 7 10 def __init__(self, aspect_ratio) -> None: 8 11 self.value = aspect_ratio 9 12 10 - class percentage_constraint(): 13 + class percentage_constraint(rect_constraints): 11 14 def __init__(self, percentage): 12 15 self.value = percentage 13 16 14 - class pixel_constraint(): 17 + class pixel_constraint(rect_constraints): 15 18 def __init__(self, pixel): 16 19 self.value = pixel 20 + 21 + class text_size_constraint: 22 + pass 23 + 24 + class resize_constraint: 25 + pass 26 + 27 + class samesize_constraint: 28 + pass 17 29 18 30 class gui_rect: 19 31 x_constraint = None ··· 114 126 if self.parent == None: 115 127 if class_use == center_constraint: 116 128 self.x = self.game.renderer.get_screen_size()[0] / 2 - (int(self.width) / 2) 117 - if class_use == percentage_constraint: 129 + elif class_use == percentage_constraint: 118 130 self.x = self.game.renderer.get_screen_size()[0] * self.x_constraint.value 119 - if class_use == pixel_constraint: 131 + elif class_use == pixel_constraint: 120 132 self.x = self.x_constraint.value 121 133 else: 122 134 if class_use == center_constraint: 123 135 self.x = self.parent.x + (self.parent.width / 2) - (int(self.width) / 2) 124 - if class_use == percentage_constraint: 136 + elif class_use == percentage_constraint: 125 137 self.x = self.parent.x + (self.parent.width * self.x_constraint.value) 126 - if class_use == pixel_constraint: 138 + elif class_use == pixel_constraint: 127 139 self.x = self.parent.x + self.x_constraint.value 128 140 129 141 # Set Y Constraints ··· 133 145 if self.parent == None: 134 146 if class_use == center_constraint: 135 147 self.y = self.game.renderer.get_screen_size()[1] / 2 - (int(self.height) / 2) 136 - if class_use == percentage_constraint: 148 + elif class_use == percentage_constraint: 137 149 self.y = self.game.renderer.get_screen_size()[1] * self.y_constraint.value 138 - if class_use == pixel_constraint: 150 + elif class_use == pixel_constraint: 139 151 self.y = self.y_constraint.value 140 152 else: 141 153 if class_use == center_constraint: 142 154 self.y = self.parent.y + (self.parent.height / 2) - (int(self.height) / 2) 143 - if class_use == percentage_constraint: 155 + elif class_use == percentage_constraint: 144 156 self.y = self.parent.y + (self.parent.height * self.y_constraint.value) 145 - if class_use == pixel_constraint: 157 + elif class_use == pixel_constraint: 146 158 self.y = self.parent.y + self.y_constraint.value 147 159 148 160 ## Set size thingy ··· 156 168 if self.parent == None: 157 169 if class_use == percentage_constraint: 158 170 self.width = self.game.renderer.get_screen_size()[0] * self.width_constraint.value 159 - if class_use == aspect_constraint: 171 + elif class_use == aspect_constraint: 160 172 self.width = self.height * self.width_constraint.value 161 - if class_use == pixel_constraint: 173 + elif class_use == pixel_constraint: 162 174 self.width = self.width_constraint.value 163 175 else: 164 176 if class_use == percentage_constraint: 165 177 self.width = self.parent.width * self.width_constraint.value 166 - if class_use == aspect_constraint: 178 + elif class_use == aspect_constraint: 167 179 self.width = self.height * self.width_constraint.value 168 - if class_use == pixel_constraint: 180 + elif class_use == pixel_constraint: 169 181 self.width = self.width_constraint.value 170 182 171 183 # Set Height Constraints ··· 175 187 if self.parent == None: 176 188 if class_use == percentage_constraint: 177 189 self.height = self.game.renderer.get_screen_size()[1] * self.height_constraint.value 178 - if class_use == aspect_constraint: 190 + elif class_use == aspect_constraint: 179 191 self.height = self.width * self.height_constraint.value 180 - if class_use == pixel_constraint: 192 + elif class_use == pixel_constraint: 181 193 self.height = self.height_constraint.value 182 194 else: 183 195 if class_use == percentage_constraint: 184 196 self.height = self.parent.height * self.height_constraint.value 185 - if class_use == aspect_constraint: 197 + elif class_use == aspect_constraint: 186 198 self.height = self.width * self.height_constraint.value() 187 - if class_use == pixel_constraint: 199 + elif class_use == pixel_constraint: 188 200 self.height = self.height_constraint.value 189 201 190 202 # Tween Position ··· 196 208 if self.parent == None: 197 209 if class_use == center_constraint: 198 210 tmp_x = self.game.renderer.get_screen_size()[0] / 2 - (int(self.width) / 2) 199 - if class_use == percentage_constraint: 211 + elif class_use == percentage_constraint: 200 212 tmp_x = self.game.renderer.get_screen_size()[0] * self.tween_x.value 201 - if class_use == pixel_constraint: 213 + elif class_use == pixel_constraint: 202 214 tmp_x = self.tween_x.value 203 215 else: 204 216 if class_use == center_constraint: 205 217 tmp_x = self.parent.x + (self.parent.width / 2) - (int(self.width) / 2) 206 - if class_use == percentage_constraint: 218 + elif class_use == percentage_constraint: 207 219 tmp_x = self.parent.x + (self.parent.width * self.tween_x.value) 208 - if class_use == pixel_constraint: 220 + elif class_use == pixel_constraint: 209 221 tmp_x = self.parent.x + self.tween_x.value 210 222 211 223 class_use = type(self.tween_y) 212 224 if self.parent == None: 213 225 if class_use == center_constraint: 214 226 tmp_y = self.game.renderer.get_screen_size()[1] / 2 - (int(self.height) / 2) 215 - if class_use == percentage_constraint: 227 + elif class_use == percentage_constraint: 216 228 tmp_y = self.game.renderer.get_screen_size()[1] * self.tween_y.value 217 - if class_use == pixel_constraint: 229 + elif class_use == pixel_constraint: 218 230 tmp_y = self.tween_y.value 219 231 else: 220 232 if class_use == center_constraint: 221 233 tmp_y = self.parent.y + (self.parent.height / 2) - (int(self.height) / 2) 222 - if class_use == percentage_constraint: 234 + elif class_use == percentage_constraint: 223 235 tmp_y = self.parent.y + (self.parent.height * self.tween_y.value) 224 - if class_use == pixel_constraint: 236 + elif class_use == pixel_constraint: 225 237 tmp_y = self.parent.y + self.tween_y.value 226 238 227 239 self.x += (tmp_x - self.x) / ((self.tween_mult * self.game.delta_time) * 600) ··· 234 246 if self.parent == None: 235 247 if class_use == percentage_constraint: 236 248 self.tmp_width = self.game.renderer.get_screen_size()[0] * self.tween_width.value 237 - if class_use == aspect_constraint: 249 + elif class_use == aspect_constraint: 238 250 self.tmp_width = self.tmp_height 239 - if class_use == pixel_constraint: 251 + elif class_use == pixel_constraint: 240 252 self.tmp_width = self.tween_width.value 241 253 else: 242 254 if class_use == percentage_constraint: 243 255 self.tmp_width = self.parent.width * self.tween_width.value 244 - if class_use == aspect_constraint: 256 + elif class_use == aspect_constraint: 245 257 self.tmp_width = self.tmp_height 246 - if class_use == pixel_constraint: 258 + elif class_use == pixel_constraint: 247 259 self.tmp_width = self.tween_width.value 248 260 249 261 class_use = type(self.tween_height) 250 262 if self.parent == None: 251 263 if class_use == percentage_constraint: 252 264 self.tmp_height = self.game.renderer.get_screen_size()[1] * self.tween_height.value 253 - if class_use == aspect_constraint: 265 + elif class_use == aspect_constraint: 254 266 self.tmp_height = self.tmp_width 255 - if class_use == pixel_constraint: 267 + elif class_use == pixel_constraint: 256 268 self.tmp_height = self.self.tmp_height.value 257 269 else: 258 270 if class_use == percentage_constraint: 259 271 self.tmp_height = self.parent.height * self.tween_height.value 260 - if class_use == aspect_constraint: 272 + elif class_use == aspect_constraint: 261 273 self.tmp_height = self.tmp_width 262 - if class_use == pixel_constraint: 274 + elif class_use == pixel_constraint: 263 275 self.tmp_height = self.self.tmp_height.value 264 276 265 277 self.width += (self.tmp_width - self.width) / ((self.tween_s_mult * self.game.delta_time) * 600) ··· 269 281 if self.visible: 270 282 pygame.draw.rect(self.game.renderer.screen, self.color, ((self.x, self.y), (self.width, self.height)), self.outline, self.border_radius) 271 283 284 + class gui_text: 285 + def set_size_constraint(self, constraint): 286 + self.size_constraint = constraint 287 + 288 + def __init__(self, game) -> None: 289 + self.game = game 290 + 291 + self.text = "" 292 + self.draw_text = self.text 293 + self.parent = None 294 + 295 + def update(self): 296 + if self.parent == None: 297 + print('Parent of text field must be set') 298 + return 299 + 300 + class_use = type(self.size_constraint) 301 + if class_use == resize_constraint: 302 + pass 303 + 304 + def render(self): 305 + pass 306 + 272 307 class gui_toggle_button: 273 308 def __init__(self, game) -> None: 274 309 self.game = game
src/scripts/modules/screens/__init__.py src/scripts/screens/__init__.py
src/scripts/modules/screens/__pycache__/__init__.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/screens/__pycache__/main_menu.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/screens/__pycache__/screen_1.cpython-39.pyc src/scripts/screens/__pycache__/screen_1.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/screens/__pycache__/screen_2.cpython-39.pyc src/scripts/screens/__pycache__/screen_2.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/screens/__pycache__/screen_3.cpython-39.pyc src/scripts/screens/__pycache__/screen_3.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/screens/__pycache__/screen_4.cpython-39.pyc src/scripts/screens/__pycache__/screen_4.cpython-39.pyc

This is a binary file and will not be displayed.

+1 -1
src/scripts/modules/screens/screen_1.py src/scripts/screens/gui_test.py
··· 2 2 from src.scripts.modules.gui import * 3 3 import os 4 4 5 - class screen_1(Screen): 5 + class gui_test(Screen): 6 6 def __init__(self, game): 7 7 self.game = game 8 8
-33
src/scripts/modules/screens/screen_2.py
··· 1 - from src.scripts.modules.renderer import Screen 2 - from src.scripts.modules.gui import * 3 - import os 4 - 5 - class screen_2(Screen): 6 - def __init__(self, game): 7 - self.game = game 8 - 9 - self.slider = gui_slider(game, 0, 100) 10 - 11 - self.slider.slider_body.set_x_constraint(center_constraint()) 12 - self.slider.slider_body.set_y_constraint(center_constraint()) 13 - self.slider.slider_body.set_width_constraint(pixel_constraint(200)) 14 - self.slider.slider_body.set_height_constraint(pixel_constraint(5)) 15 - self.slider.slider_body.set_border_radius(10) 16 - self.slider.slider_body.set_draw_color(self.game.color_handler.get_rgb('main_menu.bright_bg')) 17 - 18 - self.slider.slider_head.parent = self.slider.slider_body 19 - self.slider.slider_head.set_x_constraint(pixel_constraint(0)) 20 - self.slider.slider_head.set_y_constraint(center_constraint()) 21 - self.slider.slider_head.set_width_constraint(pixel_constraint(20)) 22 - self.slider.slider_head.set_height_constraint(pixel_constraint(20)) 23 - self.slider.slider_head.set_border_radius(10) 24 - self.slider.slider_head.set_draw_color(self.game.color_handler.get_rgb('main_menu.text')) 25 - 26 - def update(self): 27 - self.slider.update() 28 - 29 - def render(self): 30 - self.game.renderer.screen.fill(self.game.color_handler.get_rgb('main_menu.background')) 31 - self.slider.render() 32 - #value = int((self.slider_head.x - self.slider_body.x) / (self.slider_body.width / 100)) 33 - #self.game.renderer.screen.blit(self.game.font_handler.render(str(value), 'default', 48, self.game.color_handler.get_rgb('main_menu.text')), (20, 20))
-22
src/scripts/modules/screens/screen_3.py
··· 1 - from src.scripts.modules.renderer import Screen 2 - from src.scripts.modules.gui import * 3 - import os 4 - 5 - class screen_3(Screen): 6 - def __init__(self, game): 7 - self.game = game 8 - 9 - self.rect = gui_rect(game) 10 - 11 - self.rect.set_x_constraint(center_constraint()) 12 - self.rect.set_y_constraint(pixel_constraint(20)) 13 - self.rect.set_width_constraint(percentage_constraint(0.1)) 14 - self.rect.set_height_constraint(aspect_constraint(1)) 15 - self.rect.set_draw_color(self.game.color_handler.get_rgb('main_menu.bright_bg')) 16 - 17 - def update(self): 18 - self.rect.update() 19 - 20 - def render(self): 21 - self.game.renderer.screen.fill(self.game.color_handler.get_rgb('main_menu.background')) 22 - self.rect.render()
-46
src/scripts/modules/screens/screen_4.py
··· 1 - from src.scripts.modules.renderer import Screen 2 - from src.scripts.modules.gui import * 3 - import os 4 - 5 - class screen_4(Screen): 6 - def __init__(self, game): 7 - self.game = game 8 - 9 - self.button = gui_toggle_button(game) 10 - 11 - self.button.rect.set_x_constraint(center_constraint()) 12 - self.button.rect.set_y_constraint(center_constraint()) 13 - self.button.rect.set_width_constraint(percentage_constraint(0.12)) 14 - self.button.rect.set_height_constraint(percentage_constraint(0.1)) 15 - self.button.rect.set_outline_radius(5) 16 - self.button.rect.set_border_radius(30) 17 - self.button.rect.set_draw_color(self.game.color_handler.get_rgb('main_menu.bright_bg')) 18 - 19 - self.button_inside_rect = gui_rect(game) 20 - 21 - self.button_inside_rect.parent = self.button.rect 22 - self.button_inside_rect.set_x_constraint(percentage_constraint(0.05)) 23 - self.button_inside_rect.set_y_constraint(center_constraint()) 24 - self.button_inside_rect.set_width_constraint(aspect_constraint(1)) 25 - self.button_inside_rect.set_height_constraint(percentage_constraint(0.8)) 26 - self.button_inside_rect.set_border_radius(30) 27 - self.button_inside_rect.set_draw_color(self.game.color_handler.get_rgb('main_menu.bright_bg')) 28 - 29 - 30 - def update(self): 31 - self.button.update() 32 - self.button_inside_rect.update() 33 - 34 - if self.button.hover: 35 - self.button_inside_rect.set_draw_color(self.game.color_handler.get_rgb('main_menu.bright_bg_2')) 36 - else: 37 - self.button_inside_rect.set_draw_color(self.game.color_handler.get_rgb('main_menu.bright_bg')) 38 - 39 - self.button_inside_rect.tween_to(percentage_constraint(0.05), center_constraint(), 24) 40 - if self.button.toggled: 41 - self.button_inside_rect.tween_to(percentage_constraint(0.57), center_constraint(), 24) 42 - 43 - def render(self): 44 - self.game.renderer.screen.fill(self.game.color_handler.get_rgb('main_menu.background')) 45 - self.button.render() 46 - self.button_inside_rect.render()
src/scripts/screens/__pycache__/__init__.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/screens/__pycache__/main_menu.cpython-39.pyc

This is a binary file and will not be displayed.

+18
src/scripts/screens/main_menu.py
··· 1 + from src.scripts.modules.renderer import Screen 2 + from src.scripts.modules.gui import * 3 + import os 4 + 5 + from src.scripts.game.player import Player 6 + 7 + class main_menu(Screen): 8 + def __init__(self, game): 9 + self.game = game 10 + 11 + self.player = Player(game) 12 + 13 + def update(self): 14 + self.player.update() 15 + 16 + def render(self): 17 + self.game.renderer.screen.fill(self.game.color_handler.get_rgb('main_menu.background')) 18 + self.player.render()