-
Notifications
You must be signed in to change notification settings - Fork 0
/
controlling.py
91 lines (82 loc) · 2.31 KB
/
controlling.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
81
82
83
84
85
86
87
88
89
90
91
import sys
import select
import tty
from bullet import Bullet
from player import Player
class Controlling():
'''
The controlling class.
'''
def __init__(self, leds):
'''
Set the user input keys:
* LEFT = a
* RIGHT = d
* FIRE = h
:param leds: The led's count
'''
self.leds = leds
self.LEFT = "a"
self.RIGHT = "d"
self.NOTHING = ""
self.FIRE = "h"
tty.setraw(sys.stdin.fileno())
def getUserInput(self):
'''
Get the user input.
'''
while sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
sys.stdout.flush()
char = sys.stdin.read(1)
if char:
print char
if(char == self.RIGHT):
return self.RIGHT
elif(char == self.LEFT):
return self.LEFT
elif(char == self.FIRE):
return self.FIRE
elif(char == "p"):
exit()
else:
return self.NOTHING
else:
return self.NOTHING
def userInput(self, gameObject):
'''
Interpret the user input.
:param gameObject: The game object for the user input actions.
'''
userInput = self.getUserInput()
if(userInput == self.LEFT):
if(gameObject.hasWeapon == True and gameObject.weapon.fired == False):
gameObject.pos -= (gameObject.speed * gameObject.direction)
elif(gameObject.hasWeapon == False):
gameObject.pos -= (gameObject.speed * gameObject.direction)
if(gameObject.pos < 0):
gameObject.pos = 0
elif(userInput == self.RIGHT):
if(gameObject.hasWeapon == True and gameObject.weapon.fired == False):
gameObject.pos += (gameObject.speed * gameObject.direction)
elif(gameObject.hasWeapon == False):
gameObject.pos += (gameObject.speed * gameObject.direction)
if(gameObject.pos > (self.leds) - 1):
gameObject.pos = self.leds - 1
elif(userInput == self.FIRE):
if(gameObject.hasWeapon == True):
gameObject.firedWeapon = True
gameObject.weapon.fired = True
def moveObject(self, gameObject):
'''
Move the game object.
:param gameObject: The game object.
'''
if(gameObject.movingCount == 0):
gameObject.pos += (gameObject.speed * gameObject.direction)
if(gameObject.pos == 0 and gameObject.direction == -1):
gameObject.pos = self.leds - 1
elif(gameObject.pos == self.leds - 1 and gameObject.direction == 1):
gameObject.pos = 0
gameObject.movingCount = gameObject.resetMovingCount
else:
gameObject.movingCount -= 1