-
Notifications
You must be signed in to change notification settings - Fork 0
/
PongGame.jack
executable file
·163 lines (132 loc) · 3.81 KB
/
PongGame.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
// 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/PongGame.jack
/**
* The Pong game.
*/
class PongGame {
// The singlton
static PongGame instance;
// The bat
field Bat bat;
// The ball
field Ball ball;
// The current wall that the ball is bouncing from.
field int wall;
// True when the game is over
field boolean exit;
// The current score.
field int score;
// The last wall that the ball bounced from.
field int lastWall;
// The current width of the bat
field int batWidth;
/** Constructs a new Pong Game. */
constructor PongGame new() {
do Screen.clearScreen();
let batWidth = 50;
let bat = Bat.new(230, 229, batWidth, 7);
let ball = Ball.new(253, 222, 0, 511, 0, 229);
do ball.setDestination(400,0);
do Screen.drawRectangle(0, 238, 511, 240);
do Output.moveCursor(22,0);
do Output.printString("Score: 0");
let exit = false;
let score = 0;
let wall = 0;
let lastWall = 0;
return this;
}
/** Deallocates the object's memory. */
method void dispose() {
do bat.dispose();
do ball.dispose();
do Memory.deAlloc(this);
return;
}
/** Creates an instance of PongGame and stores it. */
function void newInstance() {
let instance = PongGame.new();
return;
}
/** Returns the single instance of PongGame. */
function PongGame getInstance() {
return instance;
}
/** Starts the game. Handles inputs from the user that control
* the bat's movement direction. */
method void run() {
var char key;
while (~exit) {
// waits for a key to be pressed.
while ((key = 0) & (~exit)) {
let key = Keyboard.keyPressed();
do bat.move();
do moveBall();
}
if (key = 130) {
do bat.setDirection(1);
}
else {
if (key = 132) {
do bat.setDirection(2);
}
else {
if (key = 140) {
let exit = true;
}
}
}
// Waits for the key to be released.
while ((~(key = 0)) & (~exit)) {
let key = Keyboard.keyPressed();
do bat.move();
do moveBall();
}
}
if (exit) {
do Output.moveCursor(10,27);
do Output.printString("Game Over");
}
return;
}
/**
* Handles ball movement, including bouncing.
* If the ball bounces from the wall, finds its new direction.
* If the ball bounces from the bat, shrinks the bat's size and
* increases the score by one.
*/
method void moveBall() {
var int bouncingDirection, batLeft, batRight, ballLeft, ballRight;
let wall = ball.move();
if ((wall > 0) & (~(wall = lastWall))) {
let lastWall = wall;
let bouncingDirection = 0;
let batLeft = bat.getLeft();
let batRight = bat.getRight();
let ballLeft = ball.getLeft();
let ballRight = ball.getRight();
if (wall = 4) {
let exit = (batLeft > ballRight) | (batRight < ballLeft);
if (~exit) {
if (ballRight < (batLeft + 10)) {
let bouncingDirection = -1;
}
else {
if (ballLeft > (batRight - 10)) {
let bouncingDirection = 1;
}
}
let batWidth = batWidth - 2;
do bat.setWidth(batWidth);
let score = score + 1;
do Output.moveCursor(22,7);
do Output.printInt(score);
}
}
do ball.bounce(bouncingDirection);
}
return;
}
}