My Pygame Game Engine

dfgjhkl;

WillM 3cf9aee6 0e64a13b

src/resources/main/assets/images/player/walking.aseprite

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_00.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_01.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_02.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_03.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_04.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_05.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_06.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_07.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_08.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_09.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_10.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_11.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_12.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_13.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_14.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_15.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_16.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_17.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_18.png

This is a binary file and will not be displayed.

src/resources/main/assets/images/player/walking_19.png

This is a binary file and will not be displayed.

+18
src/resources/main/data/levels/level
··· 1 + [ 2 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 3 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 4 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 5 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 6 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 7 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 8 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 9 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 10 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 11 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 12 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 13 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 14 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 15 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 16 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 17 + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 18 + ]
src/scripts/__pycache__/game_manager.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.

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

This is a binary file and will not be displayed.

+54 -3
src/scripts/game/player.py
··· 1 1 from src.scripts.modules.entity import * 2 + import pygame, math, numpy 3 + 4 + class Player_Renderer_Component: 5 + def __init__(self, game, parent) -> None: 6 + self.game = game 7 + self.parent = parent 8 + 9 + self.walking_animation = [] 10 + self.walking_animation_frame = 0 11 + self.walking_animation_frame_counter = 0 12 + for i in range(18): 13 + if i < 10: 14 + i = '0' + str(i) 15 + self.walking_animation.append(self.game.image.load('player/walking_' + str(i) + '.png')) 16 + 17 + def update(self): 18 + pass 19 + 20 + def render(self): 21 + self.walking_animation_frame_counter += 100 * self.game.delta_time 22 + if self.walking_animation_frame_counter >= 3: 23 + self.walking_animation_frame += 1 24 + self.walking_animation_frame_counter = 0 25 + if self.walking_animation_frame > 17: 26 + self.walking_animation_frame = 0 27 + 28 + mx, my = pygame.mouse.get_pos() 29 + self.parent.transform.direction = 180 - self.game.math.direction_between_points(self.parent.transform.x, self.parent.transform.y, mx, my) + 360 30 + 31 + image = self.walking_animation[self.walking_animation_frame] 32 + image = pygame.transform.scale(image, (64, 64)) 33 + image, rect = self.game.math.rotate_center(image, self.parent.transform.direction, self.parent.camera_relative_x, self.parent.camera_relative_y) 34 + self.game.renderer.main_surface.blit(image, rect) 35 + 36 + class Player_Movement_Component(): 37 + def __init__(self, game, parent) -> None: 38 + self.game = game 39 + self.parent = parent 40 + 41 + def update(self): 42 + move_speed = 400 * self.game.delta_time 43 + mx, my = pygame.mouse.get_pos() 44 + angle = self.game.math.direction_between_points(self.parent.transform.x, self.parent.transform.y, mx, my) 45 + if self.game.input.is_pressed('w'): 46 + self.parent.transform.x += self.game.math.lengthdir_x(move_speed, angle) 47 + self.parent.transform.y += self.game.math.lengthdir_y(move_speed, angle) 48 + 49 + 50 + 51 + 52 + def render(self): 53 + pass 2 54 3 55 class player(Entity): 4 56 def __init__(self, game) -> None: ··· 9 61 self.transform = Transform() 10 62 11 63 self.add_component(Camera_Relative_Component(game, self)) 12 - self.add_component(Sprite_Renderer_Component(game, self)) 13 - self.get_component(Sprite_Renderer_Component).sprite = self.game.image.load('error.png') 14 - self.get_component(Sprite_Renderer_Component).surface = self.game.renderer.main_surface 64 + self.add_component(Player_Renderer_Component(game, self)) 65 + self.add_component(Player_Movement_Component(game, self))
+28
src/scripts/game/world.py
··· 1 + from src.scripts.modules.entity import * 2 + import pygame, math, numpy, os 3 + 4 + class World_Renderer_Component: 5 + def __init__(self, game, parent) -> None: 6 + self.game = game 7 + self.parent = parent 8 + 9 + self.level_data = open(os.path.join('src', 'resources', self.game.current_assetpack, 'data', 'levels', 'level'), 'r').read() 10 + exec('self.level_data = ' + self.level_data) 11 + print(self.level_data) 12 + 13 + def update(self): 14 + pass 15 + 16 + def render(self): 17 + pass 18 + 19 + class world(Entity): 20 + def __init__(self, game) -> None: 21 + 22 + self.game = game 23 + 24 + self.components = [] 25 + self.transform = Transform() 26 + 27 + self.add_component(Camera_Relative_Component(game, self)) 28 + self.add_component(World_Renderer_Component(game, self))
+2
src/scripts/game_manager.py
··· 20 20 21 21 from .screens.engine_main_screen import engine_main_screen 22 22 from .screens.game_screen import game_screen 23 + from .screens.level_editor import level_editor_screen 23 24 24 25 ## Game Manager Class 25 26 ··· 83 84 84 85 self.renderer.load_screen(engine_main_screen(self), "engine_main") 85 86 self.renderer.load_screen(game_screen(self), "game_screen") 87 + #self.renderer.load_screen(level_editor_screen(self), "level_editor") 86 88 self.renderer.switch_screen("game_screen") 87 89 88 90 ## Main Update Method
src/scripts/modules/__pycache__/entity.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__/math_utils.cpython-39.pyc

This is a binary file and will not be displayed.

+1
src/scripts/modules/entity.py
··· 4 4 def __init__(self) -> None: 5 5 self.x = 0 6 6 self.y = 0 7 + self.direction = 0 7 8 8 9 ## Base Entity 9 10
+4 -1
src/scripts/modules/image.py
··· 11 11 ## Loads image from the current active image folder using the current Assetpack 12 12 13 13 def load(self, image): 14 - return pygame.image.load(os.path.join('src', 'resources', self.game.current_assetpack, 'assets', 'images', image)) 14 + if os.path.isfile(os.path.join('src', 'resources', self.game.current_assetpack, 'assets', 'images', image)): 15 + return pygame.image.load(os.path.join('src', 'resources', self.game.current_assetpack, 'assets', 'images', image)) 16 + else: 17 + return pygame.image.load(os.path.join('src', 'resources', self.game.current_assetpack, 'assets', 'images', 'error.png'))
+3 -3
src/scripts/modules/input.py
··· 89 89 90 90 ## Returns if mb_left is pressed 91 91 92 - def is_mouse_button_pressed(self, input_state=""): 92 + def is_mouse_button_pressed(self, button=0, input_state=""): 93 93 if input_state == "": 94 - return pygame.mouse.get_pressed()[0] 94 + return pygame.mouse.get_pressed()[button] 95 95 else: 96 - return pygame.mouse.get_pressed()[0] and self.input_state == input_state 96 + return pygame.mouse.get_pressed()[button] and self.input_state == input_state 97 97 98 98 ## Returns if mb_left was pressed this frame 99 99
+5
src/scripts/modules/math_utils.py
··· 31 31 radians = math.atan2(y2-y1, x2-x1) 32 32 return(math.degrees(radians)) 33 33 34 + ## Gets the difference between angles 35 + 36 + def angle_difference(self, x, y): 37 + return math.atan2(math.sin(x-y), math.cos(x-y)) 38 + 34 39 ## Two functions that return the Vector2 from (0, 0) if you were to go at an angle at a speed for example 35 40 ## length=2 and angle=45 would go to (1.4, 1.4) 36 41 ## This is useful for example cars in topdown that usually move in a 360 direction
src/scripts/screens/__pycache__/game_screen.cpython-39.pyc

This is a binary file and will not be displayed.

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

This is a binary file and will not be displayed.

+4
src/scripts/screens/game_screen.py
··· 3 3 import os 4 4 5 5 from src.scripts.game.player import player 6 + from src.scripts.game.world import world 6 7 7 8 class game_screen(Screen): 8 9 def __init__(self, game): 9 10 self.game = game 10 11 11 12 self.player = player(game) 13 + self.world = world(game) 12 14 13 15 def update(self): 14 16 self.player.update() 17 + self.world.update() 15 18 16 19 def render(self): 17 20 self.game.renderer.main_surface.fill((0, 0, 0)) 18 21 22 + self.player.render() 19 23 self.player.render()
+55
src/scripts/screens/level_editor.py
··· 1 + from src.scripts.modules.renderer import Screen 2 + from src.scripts.modules.gui import * 3 + import os 4 + 5 + class level_editor_screen(Screen): 6 + def __init__(self, game): 7 + self.game = game 8 + 9 + self.surface = pygame.Surface((16, 16)) 10 + self.surface.fill((0, 0, 0)) 11 + 12 + self.save_button = gui_press_button(game) 13 + self.save_button.rect.set_pos(percentage_constraint(0.6), percentage_constraint(0.05), 1) 14 + self.save_button.rect.set_size(percentage_constraint(0.2), percentage_constraint(0.1), 1) 15 + self.save_button.rect.set_border_radius(5) 16 + 17 + self.save_button_text = gui_text(game) 18 + self.save_button_text.text = "Save" 19 + self.save_button_text.parent = self.save_button.rect 20 + self.save_button_text.set_size_constraint(percentage_constraint(0.8)) 21 + self.save_button_text.set_x_constraint(center_constraint()) 22 + self.save_button_text.set_y_constraint(center_constraint()) 23 + 24 + def update(self): 25 + mx, my = pygame.mouse.get_pos() 26 + mx = mx / 45 27 + my = my / 45 28 + if self.game.input.is_mouse_button_pressed(0): 29 + pygame.draw.rect(self.surface, (255, 255, 255), ((mx, my), (1, 1))) 30 + 31 + if self.game.input.is_mouse_button_pressed(2): 32 + pygame.draw.rect(self.surface, (0, 0, 0), ((mx, my), (1, 1))) 33 + 34 + self.save_button.rect.set_color((47, 93, 98), 4) 35 + if self.save_button.hover: 36 + self.save_button.rect.set_color((94, 139, 126), 2) 37 + 38 + if self.save_button.pressed: 39 + self.save_button.rect.set_color((255, 255, 255), 2) 40 + with open(os.path.join('src', 'resources', 'main', 'data', 'levels', 'level'), 'w') as file: 41 + for i in range(16): 42 + for j in range(16): 43 + pass 44 + file.write(self.surface) 45 + 46 + self.save_button.update() 47 + self.save_button_text.update() 48 + 49 + def render(self): 50 + self.game.renderer.main_surface.fill((71, 89, 126)) 51 + 52 + self.save_button.render() 53 + self.save_button_text.render() 54 + 55 + self.game.renderer.main_surface.blit(pygame.transform.scale(self.surface, (720, 720)), (0, 0))