-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pong.jack
190 lines (179 loc) · 5.2 KB
/
Pong.jack
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
class Pong {
field int bounces;
field int botCord_i, botCord_j;
field int playerCord_i, playerCord_j;
field int ballCord_i, ballCord_j;
field int ballVec_i, ballVec_j;
field String endText;
constructor Pong new(){
let bounces = 0;
let botCord_i = 1;
let botCord_j = 1;
let playerCord_i = 1;
let playerCord_j = 235;
let ballCord_i = 3;
let ballCord_j = 120;
let ballVec_i = 1;
let ballVec_j = 1;
return this;
}
method void drawInfo(){
do Output.moveCursor(22, 50);
do Output.printString("ODBIJANJA: ");
do Output.moveCursor(22, 61);
do Output.printInt(bounces);
return;
}
method void redrawBounces(){
do Screen.setColor(0);
do Screen.drawRectangle(80, 241, 100, 254);
do Output.moveCursor(22, 61);
do Output.printInt(bounces);
return;
}
method void drawBall(){
do Screen.setColor(1);
do Screen.drawRectangle(ballCord_i, ballCord_j, ballCord_i+4, ballCord_j+4);
return;
}
method void eraseBall(){
do Screen.setColor(0);
do Screen.drawRectangle(ballCord_i, ballCord_j, ballCord_i+4, ballCord_j+4);
return;
}
method void drawPlayer(){
do Screen.setColor(1);
do Screen.drawRectangle(playerCord_i, playerCord_j, playerCord_i+50, playerCord_j+4);
return;
}
method void drawBot(){
do Screen.setColor(1);
do Screen.drawRectangle(botCord_i, botCord_j, botCord_i+50, botCord_j+4);
return;
}
method void movePlayer(int d){
if(d = -1){
if(playerCord_i - 1 > 0){
do Screen.setColor(0);
do Screen.drawRectangle(playerCord_i+50, playerCord_j, playerCord_i+50, playerCord_j+4);
let playerCord_i = playerCord_i - 1;
do Screen.setColor(1);
do Screen.drawRectangle(playerCord_i, playerCord_j, playerCord_i, playerCord_j+4);
}
} else {
if(playerCord_i + 1 + 50 < 511){
do Screen.setColor(0);
do Screen.drawRectangle(playerCord_i, playerCord_j, playerCord_i, playerCord_j+4);
let playerCord_i = playerCord_i + 1;
do Screen.setColor(1);
do Screen.drawRectangle(playerCord_i+50, playerCord_j, playerCord_i+50, playerCord_j+4);
}
}
return;
}
method void moveBot(){
var int d;
if((ballCord_i > (botCord_i + 10)) & (ballCord_i < (botCord_i + 40))){
return;
}
if(ballCord_i - 27 < botCord_i){
let d = -1;
} else {
let d = 1;
}
if(d = -1){
if(botCord_i - 1 > 0){
do Screen.setColor(0);
do Screen.drawRectangle(botCord_i+50, botCord_j, botCord_i+50, botCord_j+4);
let botCord_i = botCord_i - 1;
do Screen.setColor(1);
do Screen.drawRectangle(botCord_i, botCord_j, botCord_i, botCord_j+4);
}
} else {
if(botCord_i + 1 + 50 < 511){
do Screen.setColor(0);
do Screen.drawRectangle(botCord_i, botCord_j, botCord_i, botCord_j+4);
let botCord_i = botCord_i + 1;
do Screen.setColor(1);
do Screen.drawRectangle(botCord_i+50, botCord_j, botCord_i+50, botCord_j+4);
}
}
return;
}
method bool moveBall(){
do eraseBall();
if(ballVec_j < 0){
if(ballVec_j + ballCord_j - 1 - 4 < botCord_j){
if((botCord_i < ballCord_i) & (botCord_i + 50 > ballCord_i)){
let ballVec_j = 1;
let bounces = bounces + 1;
do redrawBounces();
if(ballCord_i - botCord_i < 10){
let ballVec_i = -2;
} else {
if(ballCord_i - botCord_i < 20){
let ballVec_i = -1;
} else {
if(ballCord_i - botCord_i < 30){
let ballVec_i = 0;
} else {
if(ballCord_i - botCord_i < 40){
let ballVec_i = 1;
} else {
let ballVec_i = 2;
}
}
}
}
} else {
let endText = "GAME OVER, YOU WIN";
return true;
}
}
} else {
if(ballVec_j + ballCord_j + 4 + 1 > playerCord_j){
if((playerCord_i < ballCord_i) & (playerCord_i + 50 > ballCord_i)){
let ballVec_j = -1;
let bounces = bounces + 1;
do redrawBounces();
if(ballCord_i - playerCord_i < 10){
let ballVec_i = -2;
} else {
if(ballCord_i - playerCord_i < 20){
let ballVec_i = -1;
} else {
if(ballCord_i - playerCord_i < 30){
let ballVec_i = 0;
} else {
if(ballCord_i - playerCord_i < 40){
let ballVec_i = 1;
} else {
let ballVec_i = 2;
}
}
}
}
} else {
let endText = "GAME OVER, YOU LOSE";
return true;
}
}
}
if(ballCord_i + ballVec_i < 1){
let ballVec_i = ballVec_i * -1;
}
if(ballCord_i + ballVec_i + 4 > 510){
let ballVec_i = ballVec_i * -1;
}
let ballCord_i = ballCord_i + ballVec_i;
let ballCord_j = ballCord_j + ballVec_j;
do drawBall();
return false;
}
method void endScreen(){
do Screen.clearScreen();
do Output.moveCursor(11,22);
do Output.printString(endText);
return;
}
}