My Pygame Game Engine
at main 3.6 kB view raw
1## Imports 2 3from re import L 4import pygame 5 6## Class 7 8class Input: 9 10 ## Start vars 11 12 input_state = "main" 13 14 key_signature = False 15 16 # All keys recognized by the script 17 18 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", 19 "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", 20 "UP", "DOWN", "LEFT", "RIGHT", 21 "SPACE", "LCTRL", "LSHIFT", "LALT", "RETURN", "ESCAPE", "TAB", "BACKSPACE", 22 "PERIOD", "MINUS"] 23 24 ## Setter for the input state 25 26 def set_input_state(self, state): 27 self.input_state = state 28 29 ## Initialize 30 31 def __init__(self): 32 self.any_key_pressed = False 33 34 # Create the last_frame_keys and pressed_keys using a for loop for the length of self.record_keys 35 36 self.last_frame_keys = [] 37 self.pressed_keys = [] 38 for i in self.record_keys: 39 self.pressed_keys.append(False) 40 self.last_frame_keys.append(False) 41 42 self.mouse_button_last_frame = False 43 44 ## Returns if a keys is pressed 45 46 def is_pressed(self, key, input_state=""): 47 48 # Gets the key signature in pygame using this terribleness 49 # I wish i could just use the keyboard module but that doesnt work on unix based operating systems ;-; 50 51 keys = pygame.key.get_pressed() 52 code = "self.key_signature = pygame.K_" + key 53 exec(code) 54 55 # Returns a bool wether the key is pressed the window is focused and based on the func params if the input state is the current one 56 57 if input_state != "": 58 return bool(keys[self.key_signature] and pygame.mouse.get_focused() and input_state == self.input_state) 59 else: 60 return bool(keys[self.key_signature] and pygame.mouse.get_focused()) 61 62 ## Check what keys are pressed this frame 63 64 def check_keys(self): 65 for i, key in enumerate(self.record_keys): 66 self.last_frame_keys[i] = self.is_pressed(key) 67 68 ## Returns true if a key was pressed this frame not if its held 69 70 def is_just_pressed(self, key, input_state=""): 71 72 # Same terribleness as in is_pressed() 73 74 keys = pygame.key.get_pressed() 75 code = "self.key_signature = pygame.K_" + key 76 exec(code) 77 78 # Same thing as in is_pressed() except now theres a check for wether or not it was pressed last frame too in which it returns false 79 80 if input_state != "": 81 return bool(keys[self.key_signature] and not self.last_frame_keys[self.record_keys.index(key)] and pygame.mouse.get_focused() and input_state == self.input_state) 82 else: 83 return bool(keys[self.key_signature] and not self.last_frame_keys[self.record_keys.index(key)] and pygame.mouse.get_focused()) 84 85 ## Returns if any key is pressed 86 87 def is_any_key_pressed(self): 88 return self.any_key_pressed 89 90 ## Returns if mb_left is pressed 91 92 def is_mouse_button_pressed(self, button=0, input_state=""): 93 if input_state == "": 94 return pygame.mouse.get_pressed()[button] 95 else: 96 return pygame.mouse.get_pressed()[button] and self.input_state == input_state 97 98 ## Returns if mb_left was pressed this frame 99 100 def is_mouse_button_just_pressed(self, input_state=""): 101 if input_state == "": 102 return pygame.mouse.get_pressed()[0] and not self.mouse_button_last_frame 103 else: 104 return pygame.mouse.get_pressed()[0] and not self.mouse_button_last_frame and self.input_state == input_state