-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.jack
More file actions
executable file
·237 lines (203 loc) · 5.66 KB
/
Ball.jack
File metadata and controls
executable file
·237 lines (203 loc) · 5.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/11/Pong/Ball.jack
/**
* A graphic ball. Has a screen location and distance of last destination.
* Has methods for drawing, erasing and moving on the screen.
* The ball's dimensions are 6X6 pixels.
*/
class Ball {
// The ball's screen location (in pixels)
field int x, y;
// Distance of last destination
field int lengthx, lengthy;
// Used for straight line movement computation
field int d, straightD, diagonalD;
field boolean invert, positivex, positivey;
// wall locations
field int leftWall, rightWall, topWall, bottomWall;
// last wall that the ball was bounced from
field int wall;
/** Constructs a new Ball with a given initial location
* and the locations of the walls. */
constructor Ball new(int Ax, int Ay, int AleftWall, int ArightWall, int AtopWall, int AbottomWall) {
let x = Ax;
let y = Ay;
let leftWall = AleftWall;
let rightWall = ArightWall - 6; // -6 for ball size
let topWall = AtopWall;
let bottomWall = AbottomWall - 6; // -6 for ball size
let wall = 0;
do show();
return this;
}
/** Deallocates the object's memory. */
method void dispose() {
do Memory.deAlloc(this);
return;
}
/** Draws the ball on the screen. */
method void show() {
do Screen.setColor(true);
do draw();
return;
}
/** Erases the ball from the screen. */
method void hide() {
do Screen.setColor(false);
do draw();
return;
}
/** Draws the ball. */
method void draw() {
do Screen.drawRectangle(x, y, x + 5, y + 5);
return;
}
/** Returns the left edge of the ball. */
method int getLeft() {
return x;
}
/** Returns the right edge of the ball. */
method int getRight() {
return x + 5;
}
/** Sets the destination of the ball. */
method void setDestination(int destx, int desty) {
var int dx, dy, temp;
let lengthx = destx - x;
let lengthy = desty - y;
let dx = Math.abs(lengthx);
let dy = Math.abs(lengthy);
let invert = (dx < dy);
// scan should be on Y-axis
if (invert) {
let temp = dx; // swap dx, dy
let dx = dy;
let dy = temp;
let positivex = (y < desty);
let positivey = (x < destx);
}
else {
let positivex = (x < destx);
let positivey = (y < desty);
}
let d = (2 * dy) - dx;
let straightD = 2 * dy;
let diagonalD = 2 * (dy - dx);
return;
}
/**
* Moves the ball one unit towards its destination.
* Returns 0 if the ball has not reached a wall.
* If it did, returns a value according to the wall:
* 1-left wall, 2-right wall, 3-top wall, 4-bottom wall.
*/
method int move() {
do hide();
if (d < 0) {
let d = d + straightD;
}
else {
let d = d + diagonalD;
if (positivey) {
if (invert) {
let x = x + 4;
}
else {
let y = y + 4;
}
}
else {
if (invert) {
let x = x - 4;
}
else {
let y = y - 4;
}
}
}
if (positivex) {
if (invert) {
let y = y + 4;
}
else {
let x = x + 4;
}
}
else {
if (invert) {
let y = y - 4;
}
else {
let x = x - 4;
}
}
if (~(x > leftWall)) {
let wall = 1;
let x = leftWall;
}
if (~(x < rightWall)) {
let wall = 2;
let x = rightWall;
}
if (~(y > topWall)) {
let wall = 3;
let y = topWall;
}
if (~(y < bottomWall)) {
let wall = 4;
let y = bottomWall;
}
do show();
return wall;
}
/**
* Bounces from the current wall: sets the new destination
* of the ball according to the ball's angle and the given
* bouncing direction (-1/0/1=left/center/right or up/center/down).
*/
method void bounce(int bouncingDirection) {
var int newx, newy, divLengthx, divLengthy, factor;
// dividing by 10 first since results are too big
let divLengthx = lengthx / 10;
let divLengthy = lengthy / 10;
if (bouncingDirection = 0) {
let factor = 10;
}
else {
if (((~(lengthx < 0)) & (bouncingDirection = 1)) | ((lengthx < 0) & (bouncingDirection = (-1)))) {
let factor = 20; // bounce direction is in ball direction
}
else {
let factor = 5; // bounce direction is against ball direction
}
}
if (wall = 1) {
let newx = 506;
let newy = (divLengthy * (-50)) / divLengthx;
let newy = y + (newy * factor);
}
else {
if (wall = 2) {
let newx = 0;
let newy = (divLengthy * 50) / divLengthx;
let newy = y + (newy * factor);
}
else {
if (wall = 3) {
let newy = 250;
let newx = (divLengthx * (-25)) / divLengthy;
let newx = x + (newx * factor);
}
else { // assumes wall = 4
let newy = 0;
let newx = (divLengthx * 25) / divLengthy;
let newx = x + (newx * factor);
}
}
}
do setDestination(newx, newy);
return;
}
}