-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsManager.py
80 lines (63 loc) · 2.1 KB
/
SettingsManager.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
import pygame
import sys
class InputUtils:
@staticmethod
def handle_events(input_manager):
"""Handle all events and pass them to the input manager."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
input_manager.quit_game()
elif event.type == pygame.KEYDOWN:
input_manager.key_down(event.key)
elif event.type == pygame.KEYUP:
input_manager.key_up(event.key)
class InputManager:
def __init__(self):
self.actions = {
pygame.K_w: self.move_forward,
pygame.K_s: self.move_backward,
pygame.K_a: self.move_left,
pygame.K_d: self.move_right,
pygame.K_SPACE: self.shoot,
pygame.K_e: self.interact,
pygame.K_q: self.quit_game
}
self.pressed_keys = set()
def key_down(self, key):
"""Handle key press events."""
if key in self.actions:
self.actions[key]()
self.pressed_keys.add(key)
def key_up(self, key):
"""Handle key release events."""
if key in self.pressed_keys:
self.pressed_keys.remove(key)
def move_forward(self):
print("Moving forward")
def move_backward(self):
print("Moving backward")
def move_left(self):
print("Moving left")
def move_right(self):
print("Moving right")
def shoot(self):
print("Shooting")
def interact(self):
print("Interacting")
def quit_game(self):
print("Quitting game")
pygame.quit()
sys.exit()
# Example usage
def main():
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("3rd Person Shooter Input Manager")
input_manager = InputManager()
while True:
InputUtils.handle_events(input_manager)
screen.fill((0, 0, 0)) # Clear the screen with black
# Here you would update game logic and render game objects
pygame.display.flip() # Update the full display Surface to the screen
if __name__ == "__main__":
main()