-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path11.1.3 Breakout.js
196 lines (167 loc) · 3.77 KB
/
11.1.3 Breakout.js
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
var PADDLE_WIDTH = 80;
var PADDLE_HEIGHT = 15;
var PADDLE_OFFSET = 10;
var BALL_RADIUS = 15;
var NUM_ROWS = 8;
var BRICK_TOP_OFFSET = 10;
var BRICK_SPACING = 2;
var NUM_BRICKS_PER_ROW = 8;
var BRICK_HEIGHT = 10;
var BRICK_WIDTH = (getWidth() - (NUM_BRICKS_PER_ROW + 1)
* BRICK_SPACING) / NUM_BRICKS_PER_ROW;
var paddle, ball;
var vx;
var vy = 8;
var gameOver = false;
var NUM_TURNS = 3;
var turnsLeft = NUM_TURNS;
var bricksLeft = NUM_ROWS * NUM_BRICKS_PER_ROW;
function setupBall(){
ball = new Circle(BALL_RADIUS);
ball.setPosition(getWidth()/2, getHeight()/2);
add(ball);
}
function setupPaddle(){
paddle = new Rectangle(PADDLE_WIDTH, PADDLE_HEIGHT);
paddle.setPosition(getWidth()/2 - paddle.getWidth()/2,
getHeight() - paddle.getHeight() - PADDLE_OFFSET);
add(paddle);
}
function getColorForRow(rowNum){
rowNum = rowNum % 8;
if(rowNum <= 1){
return Color.red;
}else if(rowNum > 1 && rowNum <= 3){
return Color.orange;
}else if(rowNum > 3 && rowNum <= 5){
return Color.green;
}else{
return Color.blue;
}
}
function drawBrick(x, y, color){
var brick = new Rectangle(BRICK_WIDTH, BRICK_HEIGHT);
brick.setPosition(x, y);
brick.setColor(color);
add(brick);
}
function drawRow(rowNum, yPos){
var xPos = BRICK_SPACING;
for(var i = 0; i < NUM_BRICKS_PER_ROW; i++){
drawBrick(xPos, yPos, getColorForRow(rowNum));
xPos += BRICK_WIDTH + BRICK_SPACING;
}
}
function drawBricks(){
var yPos = BRICK_TOP_OFFSET;
for(var i = 0; i < NUM_ROWS; i++){
drawRow(i, yPos);
yPos += BRICK_HEIGHT + BRICK_SPACING;
}
}
function setSpeeds(){
vx = Randomizer.nextInt(2, 7);
if(Randomizer.nextBoolean())
vx = -vx;
}
function setup(){
drawBricks();
setupPaddle();
setupBall();
setSpeeds();
}
function checkWalls(){
if(ball.getX() - ball.getRadius() < 0 ||
ball.getX() + ball.getRadius() > getWidth()){
vx = -vx;
}
if(ball.getY() - ball.getRadius() < 0){
vy = -vy;
}
if(ball.getY() + ball.getRadius() > getHeight()){
gameOver = true;
}
}
function getCollidingObject(){
var left = ball.getX() - ball.getRadius();
var right = ball.getX() + ball.getRadius();
var top = ball.getY() - ball.getRadius();
var bottom = ball.getY() + ball.getRadius();
var topLeft = getElementAt(left, top);
if(topLeft) return topLeft;
var topRight = getElementAt(right, top);
if(topRight) return topRight;
var bottomLeft = getElementAt(left, bottom);
if(bottomLeft) return bottomLeft;
var bottomRight = getElementAt(right, bottom);
if(bottomRight) return bottomRight;
}
function checkObjects(){
var elem = getCollidingObject();
if(elem != null){
if(elem != paddle){
remove(elem);
vy = -vy;
bricksLeft--;
}else{
vy = -Math.abs(vy);
}
}
}
function drawGameOver(){
var text = new Text("Game over", "25pt Arial");
text.setPosition(getWidth()/2 - text.getWidth()/2, getHeight()/2);
add(text);
}
function drawGameWon(){
var text = new Text("You Win!", "25pt Arial");
text.setPosition(getWidth()/2 - text.getWidth()/2, getHeight()/2);
add(text);
}
function checkWin(){
if(bricksLeft == 0){
stopTimer(draw);
drawGameWon();
}
}
function checkLose(){
/* This is unfortunately confusing now to
play multiple times... */
if(gameOver){
turnsLeft--;
remove(ball);
if(turnsLeft == 0){
stopTimer(draw);
drawGameOver();
}
else{
stopTimer(draw);
waitForClick();
setTimer(draw, 40);
setupBall();
setSpeeds();
gameOver = false;
}
}
}
function draw(){
checkWalls();
checkObjects();
ball.move(vx, vy);
checkWin();
checkLose();
}
function myMove(event){
var x = event.getX() - paddle.getWidth()/2;
if(x < 0)
x = 0;
if(x + paddle.getWidth() > getWidth())
x = getWidth() - paddle.getWidth();
paddle.setPosition(x, paddle.getY());
}
function start(){
setup();
//waitForClick();
setTimer(draw, 25);
mouseMoveMethod(myMove);
}