-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputHandler.py
38 lines (30 loc) · 1.06 KB
/
InputHandler.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
import pygame
class InputHandler:
keysDown = []
keysDownOnce = []
def isKeyDown(self, key):
return self.keysDown.count(key) > 0
def isKeyHit(self, key):
if self.keysDown.count(key) > 0:
if self.keysDownOnce.count(key) > 0:
self.keysDownOnce = self.__remove(self.keysDownOnce, key)
return True
return False
def handleEvent(self, event):
if event.type == pygame.KEYDOWN:
self.__keyDown(event.key)
elif event.type == pygame.KEYUP:
self.__keyUp(event.key)
def __keyUp(self, key):
self.keysDown = self.__remove(self.keysDown, key)
self.keysDownOnce = self.__remove(self.keysDownOnce, key)
def __keyDown(self, key):
if self.keysDown.count(key) == 0:
self.keysDown.append(key)
self.keysDownOnce.append(key)
def __remove(self, lst, key):
newKeys = []
for currentKey in lst:
if not currentKey == key:
newKeys.append(currentKey)
return newKeys