My Pygame Game Engine

Merge pull request #1 from willmexe/Input-fix

Fixed input to work on mac

authored by willem and committed by GitHub 765ba23a f0b8d6c8

src/scripts/__pycache__/__init__.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/game/__pycache__/bullet.cpython-39.pyc

This is a binary file and will not be displayed.

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

This is a binary file and will not be displayed.

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.

+13 -17
src/scripts/game_manager.py
··· 10 10 from .modules.fonts import font_handler 11 11 from .modules.debug import Debug 12 12 13 - from .screens.screen_main_menu import screen_main_menu 14 - from .screens.screen_game import screen_game 15 - from .screens.screen_settings import screen_settins 16 - from .screens.screen_lose import screen_lose 17 - from .screens.screen_win import screen_win 13 + from .screens.screen_main import screen_main 18 14 from .screens.gui_test import gui_test 19 15 20 16 class Game_Manager: ··· 60 56 self.change_title(self.properties["id"]) 61 57 self.input.input_state = "general" 62 58 63 - game_screen = screen_game(self) 64 - main_menu_screen = screen_main_menu(self) 59 + main_screen = screen_main(self) 65 60 screen_gui_test = gui_test(self) 66 - settings_screen = screen_settins(self) 67 - lose_screen = screen_lose(self) 68 - win_screen = screen_win(self) 69 61 self.renderer.load_screen(screen_gui_test, 'gui_test') 70 - self.renderer.load_screen(game_screen, 'game') 71 - self.renderer.load_screen(main_menu_screen, 'main_menu') 72 - self.renderer.load_screen(settings_screen, 'settings') 73 - self.renderer.load_screen(lose_screen, 'lose') 74 - self.renderer.load_screen(win_screen, 'win') 75 - self.renderer.switch_screen('main_menu') 62 + self.renderer.load_screen(main_screen, 'main') 63 + self.renderer.switch_screen('main') 76 64 77 65 def update(self): 78 66 # Calculate Delta Time ··· 81 69 self.previous_time = now 82 70 83 71 self.input.any_key_pressed = False 72 + for i, item in enumerate(self.input.pressed_keys): 73 + self.input.pressed_keys[i] = False 74 + 75 + keys = pygame.key.get_pressed() 84 76 for event in pygame.event.get(): 85 77 if event.type == pygame.QUIT: 86 78 self.stop() 87 79 if event.type == pygame.KEYDOWN: 88 80 self.input.any_key_pressed = True 89 - 81 + for i, key in enumerate(self.input.record_keys): 82 + key_signature = 0 83 + exec("key_signature = pygame.K_" + key) 84 + self.input.pressed_keys[i] = keys[key_signature] 85 + 90 86 # Screens 91 87 self.renderer.update() 92 88
src/scripts/modules/__pycache__/colors.cpython-39.pyc

This is a binary file and will not be displayed.

src/scripts/modules/__pycache__/debug.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.

+4 -2
src/scripts/modules/debug.py
··· 25 25 26 26 self.fps = 0 27 27 28 + self.render_hud = False 29 + 28 30 def send_stats(self, update_time, render_time): 29 31 self.update_time = update_time * 1000 30 32 self.render_time = render_time * 1000 ··· 36 38 self.latest_readings += self.frame_ms 37 39 if self.fps_end_time - self.fps_start_time > 1: 38 40 self.fps_start_time = time.time() 39 - print(self.latest_readings) 40 41 self.latest_readings = 0 41 42 42 43 def render(self): 43 - self.debug_gui.render() 44 + if self.render_hud: 45 + self.debug_gui.render()
+17 -7
src/scripts/modules/input.py
··· 1 1 from re import L 2 - import pygame, keyboard 2 + import pygame 3 3 4 4 class Input: 5 5 6 6 input_state = "main" 7 7 8 + key_signature = False 9 + 8 10 record_keys = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", 9 11 "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", 10 12 "UP", "DOWN", "LEFT", "RIGHT", 11 - "SPACE", "CTRL", "SHIFT", "ALT", "RETURN", "ESCAPE", "TAB", "BACKSPACE", 12 - "period", "subtract", "'"] 13 + "SPACE", "LCTRL", "LSHIFT", "LALT", "RETURN", "ESCAPE", "TAB", "BACKSPACE", 14 + "PERIOD", "MINUS"] 13 15 14 16 def set_input_state(self, state): 15 17 self.input_state = state ··· 18 20 self.any_key_pressed = False 19 21 20 22 self.last_frame_keys = [] 23 + self.pressed_keys = [] 21 24 for i in self.record_keys: 25 + self.pressed_keys.append(False) 22 26 self.last_frame_keys.append(False) 23 27 24 28 self.mouse_button_last_frame = False 25 29 26 30 def is_pressed(self, key, input_state=""): 31 + keys = pygame.key.get_pressed() 32 + code = "self.key_signature = pygame.K_" + key 33 + exec(code) 27 34 if input_state != "": 28 - return bool(keyboard.is_pressed(key) and pygame.mouse.get_focused() and input_state == self.input_state) 35 + return bool(keys[self.key_signature] and pygame.mouse.get_focused() and input_state == self.input_state) 29 36 else: 30 - return bool(keyboard.is_pressed(key) and pygame.mouse.get_focused()) 37 + return bool(keys[self.key_signature] and pygame.mouse.get_focused()) 31 38 32 39 def check_keys(self): 33 40 for i, key in enumerate(self.record_keys): 34 41 self.last_frame_keys[i] = self.is_pressed(key) 35 42 36 43 def is_just_pressed(self, key, input_state=""): 44 + keys = pygame.key.get_pressed() 45 + key_signature = False 46 + exec("key_signature = pygame.K_" + key) 37 47 if input_state != "": 38 - return bool(keyboard.is_pressed(key) and not self.last_frame_keys[self.record_keys.index(key)] and pygame.mouse.get_focused() and input_state == self.input_state) 48 + return bool(keys[key_signature] and not self.last_frame_keys[self.record_keys.index(key)] and pygame.mouse.get_focused() and input_state == self.input_state) 39 49 else: 40 - return bool(keyboard.is_pressed(key) and not self.last_frame_keys[self.record_keys.index(key)] and pygame.mouse.get_focused()) 50 + return bool(keys[key_signature] and not self.last_frame_keys[self.record_keys.index(key)] and pygame.mouse.get_focused()) 41 51 42 52 def is_any_key_pressed(self): 43 53 return self.any_key_pressed
src/scripts/screens/__pycache__/__init__.cpython-39.pyc

This is a binary file and will not be displayed.

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

This is a binary file and will not be displayed.

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

This is a binary file and will not be displayed.

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

This is a binary file and will not be displayed.

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

This is a binary file and will not be displayed.

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

This is a binary file and will not be displayed.

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

This is a binary file and will not be displayed.

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

This is a binary file and will not be displayed.

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