-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.py
66 lines (52 loc) · 1.53 KB
/
pong.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
#console.log("\033[0;34mINX\033[3DFO\033[0m\n");
import sys, tty, termios
import threading
import time
def main():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
player_one = [0]
player_two = [0]
try:
tty.setraw(sys.stdin.fileno())
def read_user_input():
while True:
ch = sys.stdin.read(1)
if ch == "q":
return
elif ch == "n":
player_one[0] += 1
elif ch == "j":
player_one[0] -= 1
elif ch == "m":
player_two[0] += 1
elif ch == "k":
player_two[0] -= 1
def draw_loop():
while True:
draw(player_one[0], player_two[0])
time.sleep(0.1)
thread = threading.Thread(target=draw_loop)
thread.daemon = True
thread.start()
read_user_input()
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
HEIGHT = 25
WIDTH = 80
PADDLE_HEIGHT = 3
def draw(position_one, position_two):
view = new_view()
for i in range(PADDLE_HEIGHT):
view[position_one + i][0] = "x"
view[position_two + i][WIDTH - 1] = "x"
sys.stdout.write("\033[" + str(HEIGHT) + "A")
sys.stdout.write("".join(["".join(row) + "\r\n" for row in view]))
def new_view():
view = []
for h in range(HEIGHT):
row = [" "] * WIDTH
view.append(row)
return view
if __name__ == "__main__":
main()