-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.py
More file actions
121 lines (108 loc) · 3.43 KB
/
pong.py
File metadata and controls
121 lines (108 loc) · 3.43 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import sys, keyboard, time, random
sys.stdout.write("\033[2J")
screeny, screenx = 40, 80
screen = [["0" for i in range(screenx)] for i in range(screeny)]
class Player:
def __init__(self, y):
self.y = y
self.score = 0
def go_up(self):
if self.y[0] > 0:
for i in range(3):
self.y[i] -= 1
def go_down(self):
if self.y[2] < screeny-1:
for i in range(3):
self.y[i] += 1
class Ball:
def __init__(self):
self.y = screeny/2
self.x = screenx/2
self.direction = 1
self.flap = False
def go(self):
if self.y == 0:
self.y += 1
if self.direction == 1:
self.direction = 3
elif self.direction == 7:
self.direction = 5
elif self.y == screeny-1:
self.y -= 1
if self.direction == 3:
self.direction = 1
elif self.direction == 5:
self.direction = 7
elif self.x == 0:
player_two.score += 1
self.y = screeny/2
self.x = screenx/2
self.direction = random.choice([1, 3, 5, 7])
elif self.x == screenx-1:
player_one.score += 1
self.y = screeny/2
self.x = screenx/2
self.direction = random.choice([1, 3, 5, 7])
elif self.x == 3 and self.y in player_one.y:
self.x += 1
if self.direction == 1:
self.direction = 7
elif self.direction == 3:
self.direction = 5
elif self.x == (screenx-4) and self.y in player_two.y:
self.x -= 1
if self.direction == 5:
self.direction = 3
elif self.direction == 7:
self.direction = 1
else:
self.flap = not self.flap
if self.direction == 1:
if self.flap:
self.x -= 1
else:
self.y -= 1
elif self.direction == 3:
if self.flap:
self.x -= 1
else:
self.y += 1
elif self.direction == 5:
if self.flap:
self.x += 1
else:
self.y += 1
elif self.direction == 7:
if self.flap:
self.x += 1
else:
self.y -= 1
player_one = Player([18, 19, 20])
player_two = Player([18, 19, 20])
ball = Ball()
while True:
ball.go()
if keyboard.is_pressed("w"):
player_one.go_up()
if keyboard.is_pressed("s"):
player_one.go_down()
if keyboard.is_pressed("up"):
player_two.go_up()
if keyboard.is_pressed("down"):
player_two.go_down()
for i in range(screeny):
for j in range(screenx):
if i == ball.y and j == ball.x:
screen[i][j] = "7"
else:
screen[i][j] = "0"
if i in player_one.y:
screen[i][2] = "7"
if i in player_two.y:
screen[i][screenx-3] = "7"
sys.stdout.write("\033[H")
print(f"\t\033[1mPlayer 1: {player_one.score}\tPlayer 2: {player_two.score}\033[m")
for i in screen:
for count, j in enumerate(i):
print(f"\033[4{j}m \033[m", end="\n" if count == screenx-1 else "")
time.sleep(0.02)