ping-pong 4th VARIABLE LEFT-BASE-POS VARIABLE RIGHT-BASE-POS VARIABLE

  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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
\ ping-pong.4th
VARIABLE LEFT-BASE-POS
VARIABLE RIGHT-BASE-POS
VARIABLE BAll-X
VARIABLE BALL-Y
VARIABLE BALL-DIRECTION ( 0 -- LT, 1 -- RT, 3 -- RB, 2 -- LB )
VARIABLE FIRST-PLAYER-SCORE
VARIABLE SECOND-PLAYER-SCORE
: DEFAULT-VARIABLES
9 LEFT-BASE-POS !
9 RIGHT-BASE-POS !
40 BAll-X !
12 BALL-Y !
0 BALL-DIRECTION !
0 FIRST-PLAYER-SCORE !
0 SECOND-PLAYER-SCORE !
;
: DRAW-BORDERS
80 0 DO I 0 AT-XY ." *" LOOP \ top horisontal
80 0 DO I 25 AT-XY ." *" LOOP \ bottom horisontal
25 1 DO 0 I AT-XY ." *" LOOP \ left vertical
25 1 DO 79 I AT-XY ." *" LOOP \ right vertical
4 25 AT-XY ." [ 0 ]"
70 25 AT-XY ." [ 0 ]"
;
: DRAW-LEFT-BASE
2 LEFT-BASE-POS @ 5 0 DO 2DUP I + AT-XY ." |" LOOP 2DROP
;
: DRAW-RIGHT-BASE
77 RIGHT-BASE-POS @ 5 0 DO 2DUP I + AT-XY ." |" LOOP 2DROP
;
: REDRAW-BASE ( x y delta -- )
1 = IF
2DUP
AT-XY SPACE
5 + AT-XY ." |"
ELSE
2DUP
1 - AT-XY ." |"
4 + AT-XY SPACE
THEN
0 0 AT-XY
;
: REDRAW-LEFT-BASE
DUP
2 LEFT-BASE-POS @ ROT REDRAW-BASE
LEFT-BASE-POS @ + LEFT-BASE-POS !
;
: REDRAW-RIGHT-BASE
DUP
77 RIGHT-BASE-POS @ ROT REDRAW-BASE
RIGHT-BASE-POS @ + RIGHT-BASE-POS !
;
: LEFT-BASE-DOWN
LEFT-BASE-POS @ 18 <> IF 1 REDRAW-LEFT-BASE THEN
;
: LEFT-BASE-UP
LEFT-BASE-POS @ 1 <> IF -1 REDRAW-LEFT-BASE THEN
;
: RIGHT-BASE-DOWN
RIGHT-BASE-POS @ 18 <> IF 1 REDRAW-RIGHT-BASE THEN
;
: RIGHT-BASE-UP
RIGHT-BASE-POS @ 1 <> IF -1 REDRAW-RIGHT-BASE THEN
;
: FLIP-HORISONTAL ( dir -- dir )
BALL-DIRECTION @
DUP 0 = IF
2
ELSE
DUP 1 = IF
3
ELSE
DUP 3 = IF
1
ELSE
0
THEN
THEN
THEN
BALL-DIRECTION !
DROP
;
: FLIP-VERTICAL ( dir -- dir)
BALL-DIRECTION @
DUP 0 = IF
1
ELSE
DUP 1 = IF
0
ELSE
DUP 2 = IF
3
ELSE
2
THEN
THEN
THEN
BALL-DIRECTION !
DROP
;
: REDRAW-BALL
DUP
BALL-X @ BALL-Y @ AT-XY SPACE
BALL-X @ 1 ROT 1 AND IF + ELSE - THEN BALL-X !
BALL-Y @ 1 ROT 2 AND IF + ELSE - THEN BALL-Y !
BALL-X @ BALL-Y @ AT-XY ." O"
;
: LEFT-BASE-CATCH?
BALL-Y @ LEFT-BASE-POS @ >=
BALL-Y @ LEFT-BASE-POS @ 5 + <=
AND
;
: RIGHT-BASE-CATCH?
BALL-Y @ RIGHT-BASE-POS @ >=
BALL-Y @ RIGHT-BASE-POS @ 5 + <=
AND
;
: FIRST-PLAYER-FAIL
FIRST-PLAYER-SCORE @ DUP
1 + FIRST-PLAYER-SCORE !
6 25 AT-XY .
;
: SECOND-PLAYER-FAIL
SECOND-PLAYER-SCORE @ DUP
1 + SECOND-PLAYER-SCORE !
72 25 AT-XY .
;
: MOVE-BALL
BALL-X @ 3 = IF
LEFT-BASE-CATCH? IF
FLIP-VERTICAL
THEN
THEN
BALL-X @ 76 = IF
RIGHT-BASE-CATCH? IF
FLIP-VERTICAL
THEN
THEN
BALL-X @ 1 = IF
FIRST-PLAYER-FAIL
FLIP-VERTICAL
THEN
BALL-X @ 78 = IF
SECOND-PLAYER-FAIL
FLIP-VERTICAL
THEN
BALL-Y @ DUP
1 = SWAP 22 = OR IF
FLIP-HORISONTAL
THEN
BALL-DIRECTION @ REDRAW-BALL
0 0 AT-XY
;
: PROCESS-KEY
DUP 113 ( Q ) = IF PAGE QUIT THEN
DUP 119 ( W ) = IF LEFT-BASE-UP THEN
DUP 115 ( S ) = IF LEFT-BASE-DOWN THEN
DUP 111 ( O ) = IF RIGHT-BASE-UP THEN
DUP 108 ( L ) = IF RIGHT-BASE-DOWN THEN
;
: INIT-DRAW
PAGE
DRAW-BORDERS
DRAW-LEFT-BASE
DRAW-RIGHT-BASE
;
: START
DEFAULT-VARIABLES
INIT-DRAW
BEGIN
MOVE-BALL
KEY? IF
KEY PROCESS-KEY
THEN
KEY? IF
KEY PROCESS-KEY
THEN
50 MS
AGAIN
;
START