- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.py
More file actions
Latest commit
executable file
·82 lines (74 loc) · 1.66 KB
/
Copy pathpong.py
File metadata and controls
executable file
·82 lines (74 loc) · 1.66 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
#!/usr/bin/env python3
importos, time, msvcrt
W, H=45, 23
TH=os.get_terminal_size()[1]
CONT=0
WIN=1
LOSE=2
QUIT=-1
defupdate():
globalbx, by, vx, vy, p1, p2, score, paused
ifmsvcrt.kbhit():
k=msvcrt.getch()
ifmsvcrt.kbhit():
k+=msvcrt.getch()
ifk==b'\xe0K'andnotpaused:
p1-=2
elifk==b'\xe0M'andnotpaused:
p1+=2
elifk==b'p':
paused=notpaused
elifkin (b'\x1b', b'q'):
returnQUIT
ifpaused:
returnCONT
p1=max(4, min(W-5, p1))
p2=max(4, min(W-5, bx))
bx+=vx
by+=vy
ifbx==0orbx==W-1:
vx=-vx
ifby==1andabs(bx-p2) <5:
vy=-vy
ifby==H-2andabs(bx-p1) <5:
vy=-vy
score+=1
ifby<0:
returnWIN
ifby>=H:
returnLOSE
returnCONT
defdraw():
print('\n'*(TH-H), end='')
a= [[' ']*Wforiinrange(H)]
a[by][bx] ='O'
a[H-1][p1-4:p1+5] = ['=']*9
a[0][p2-4:p2+5] = ['=']*9
forrina:
print('|'+''.join(r) +'|')
print('score:', score, end='', flush=True)
defmain():
globalbx, by, vx, vy, p1, p2, score, paused
bx=12
by=10
vx=1
vy=1
p1=22
p2=bx
score=0
paused=False
print('\n'*1000, end='')
st=CONT
whilest==CONT:
draw()
time.sleep(.1)
st=update()
os.system('cls')
ifst==LOSE:
print('\nYou lose!')
elifst==WIN:
print('\nYou win!')
ifst!=QUIT:
print('\nYour score was:', score)
if__name__=='__main__':
main()