-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBat.jack
More file actions
executable file
·110 lines (93 loc) · 2.65 KB
/
Bat.jack
File metadata and controls
executable file
·110 lines (93 loc) · 2.65 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
// 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/Bat.jack.
/**
* A graphic Pong bat. Has a screen location, width and height.
* Has methods for drawing, erasing, moving left and right on
* the screen and changing the width.
*/
class Bat {
// The screen location
field int x, y;
// The width and height
field int width, height;
// The direction of the bat's movement
field int direction; // 1 = left, 2 = right
/** Constructs a new bat with the given location and width. */
constructor Bat new(int Ax, int Ay, int Awidth, int Aheight) {
let x = Ax;
let y = Ay;
let width = Awidth;
let height = Aheight;
let direction = 2;
do show();
return this;
}
/** Deallocates the object's memory. */
method void dispose() {
do Memory.deAlloc(this);
return;
}
/** Draws the bat on the screen. */
method void show() {
do Screen.setColor(true);
do draw();
return;
}
/** Erases the bat from the screen. */
method void hide() {
do Screen.setColor(false);
do draw();
return;
}
/** Draws the bat. */
method void draw() {
do Screen.drawRectangle(x, y, x + width, y + height);
return;
}
/** Sets the direction of the bat (0=stop, 1=left, 2=right). */
method void setDirection(int Adirection) {
let direction = Adirection;
return;
}
/** Returns the left edge of the bat. */
method int getLeft() {
return x;
}
/** Returns the right edge of the bat. */
method int getRight() {
return x + width;
}
/** Sets the width. */
method void setWidth(int Awidth) {
do hide();
let width = Awidth;
do show();
return;
}
/** Moves the bat one step in its direction. */
method void move() {
if (direction = 1) {
let x = x - 4;
if (x < 0) {
let x = 0;
}
do Screen.setColor(false);
do Screen.drawRectangle((x + width) + 1, y, (x + width) + 4, y + height);
do Screen.setColor(true);
do Screen.drawRectangle(x, y, x + 3, y + height);
}
else {
let x = x + 4;
if ((x + width) > 511) {
let x = 511 - width;
}
do Screen.setColor(false);
do Screen.drawRectangle(x - 4, y, x - 1, y + height);
do Screen.setColor(true);
do Screen.drawRectangle((x + width) - 3, y, x + width, y + height);
}
return;
}
}