Skip to content

Commit bd320c8

Browse files
committed
added shell support to simulator
1 parent 35b2b90 commit bd320c8

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ For arduino:
3434
1. - On Windows, get python 3 from [here]( https://www.python.org/downloads/) and **mark the option to add python 3 to PATH, on the installer**.
3535
- On GNU/Linux, run `sudo apt install python3 python3-pip`.
3636
2. For either system, after installing python run `sudo pip3 install pygame`.
37-
3. Run `python3 simulator.py` from the "server" folder, connect your program to port 53777 and go!
37+
3. Run `python3 simulator.py` from the "server" folder.
38+
4. In another terminal run the client with `python3 -m client -i shell`. This connects your program to port 53777 and you are ready to go!

client/input/shell.py

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
1+
from sys import stdin
2+
from msvcrt import getch
3+
14
class Shell:
25
def __init__(self, input_queue):
3-
pass
6+
self.input_queue = input_queue
7+
#self.bindings = ['Select', 'Start', 'Up', 'Down', 'Left', 'Right', 'A', 'B']
8+
self.bindings = {'K': 'Select', 'L': 'Start', 'W': 'Up', 'S': 'Down', 'A': 'Left', 'D':'Right', 'I': 'A', 'O': 'B'}
9+
10+
# no need to find the keyboard (i hope)
11+
12+
self.loop()
13+
14+
def read(self):
15+
# with this function you have to press enter everytime you give an input
16+
while True:
17+
d = stdin.readline().rstrip().upper() # this is not necessary because is a keyboard input, but why not?
18+
if d and d in self.bindings:
19+
#print("got: " + d)
20+
return d[0], self.bindings[d[0]]
21+
22+
def read2(self):
23+
# with this function you DONT have to press enter everytime you give an input
24+
# just press and go! (but have the problem of exiting the program NEEDS FIX)
25+
while True:
26+
raw_d = getch()
27+
28+
if raw_d == b'\x03': # keyboard interruption
29+
exit()
30+
31+
d = raw_d.decode("utf-8").upper()
32+
#print(d)
33+
if d in self.bindings:
34+
return d[0], self.bindings[d[0]]
35+
36+
37+
38+
def loop(self):
39+
while True:
40+
act, btn = self.read2()
41+
#print("btn is: " + btn)
42+
if act == 'R':
43+
continue
44+
self.input_queue.put(btn)

0 commit comments

Comments
 (0)