-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
218 lines (177 loc) · 4.87 KB
/
script.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
var room = 1;
var lives = 5;
var score = 0;
var powerups = ["lifeincrease"];
var Ball = function() {
this.x = random(30, 370);
this.y = random(150, 250);
this.xSpeed = 3;
this.ySpeed = 3;
this.color = color(random(0, 255), random(0, 255), random(0, 255));
};
var Paddle = function() {
this.x = random(10, 240);
this.y = 300;
};
var ball = new Ball();
var paddle = new Paddle();
var brickW = 15;
var brickH = 10;
var index = 0;
var brickArray = {
xPos: [],
yPos: [],
On: []
};
var setupBricks = function() {
for (var i = 0; i < 20; i++) {
for (var j = 0; j < 10; j++) {
brickArray.On[index] = true;
brickArray.xPos[index] = 1 + i * 20;
brickArray.yPos[index] = 10 + 12 * j;
index++;
}
}
};
var triggerPowerups = function() {
if (round(random(0, 600)) === 3) {
if (powerups[round(random(0, 0))] === "lifeincrease") {
lives++;
fill(0);
text("Extra life", 200, 200);
}
}
};
var gameDifficulty = function() {
if (score % 20 === 0) {
ball.ySpeed += 0.05;
}
};
var drawBricks = function() {
background(0, 255, 255);
fill(220, 0, 20);
for (var k = 0; k < index; k++) {
if (brickArray.On[k] === true) {
fill(255, 0, 0);
rect(brickArray.xPos[k], brickArray.yPos[k], brickW, brickH);
if ((brickArray.xPos[k] >= ball.x - 15 && brickArray.xPos[k] <= ball.x + 15) && (brickArray.yPos[k] >= ball.y - 15 && brickArray.yPos[k] <= ball.y + 15) && brickArray.On[k] === true) {
brickArray.On[k] = false;
ball.ySpeed = ball.ySpeed;
score++;
}
}
if (score % 100 === 0) {
brickArray.On[k] = true;
}
}
};
var drawBall = function() {
ball.y += ball.ySpeed;
ball.x -= ball.xSpeed;
fill(ball.color);
ellipse(ball.x, ball.y, 30, 30);
// if the ball hits the sides
if (ball.x <= 0 + 15 || ball.x >= 400 - 15) {
ball.xSpeed = -ball.xSpeed;
}
// if the ball hits the top
if (ball.y <= 0 + 15) {
ball.ySpeed = -ball.ySpeed;
}
//ball hits ground -- Lose a life
if (ball.y >= 400 - 15) {
lives--;
ball.x = random(30, 370);
ball.y = random(150, 250);
}
if (lives <= 0) {
room = 3;
}
};
var drawDashboard = function() {
fill(0);
textSize(20);
text("Lives:\n" + lives, 20, 370);
text("Score:\n" + score, 90, 370);
};
var drawPaddle = function() {
paddle.x = mouseX;
fill(0, 150, 255);
rect(paddle.x, 300, 150, 20);
//if ball collides with paddle
if ((ball.x - 15 >= paddle.x && ball.x + 15 <= paddle.x + 150) && (ball.y + 15 >= paddle.y && ball.y + 15 <= paddle.y + 20)) {
ball.ySpeed = -ball.ySpeed;
}
//if paddle colides with sides
if (paddle.x >= 390 - 150) {
paddle.x = 390 - 150;
}
};
var startScreen = function() {
textAlign(CORNER);
background(0);
fill(0, 150, 255);
textSize(30);
text("Retro Paddle", 100, 100);
rect(200, 300, 150, 20);
fill(0, 200, 0);
rect(160, 200, 100, 40, 10);
fill();
text("play", 180, 228);
};
setupBricks();
var playScreen = function() {
textAlign(CORNER);
background(40, 160);
drawBricks();
drawBall();
drawPaddle();
drawDashboard();
triggerPowerups();
gameDifficulty();
};
var gameoverScreen = function() {
background(0);
textSize(30);
textAlign(CENTER);
text("You Lost!\nYour score was " + score, 200, 80);
textSize(30);
fill(0, 200, 0);
rect(160, 200, 100, 40, 10);
fill();
text("play", 200, 228);
fill(0, 150, 255);
rect((sin(Date.now() / 70)*width/10)+120, 300, 150, 20);
};
var draw = function() {
if (room === 1) {
startScreen();
}
if (room === 2) {
playScreen();
}
if (room === 3) {
gameoverScreen();
}
};
var mouseClicked = function() {
if (room === 1) {
if (mouseX >= 160 && mouseX <= 260 && mouseY >= 200 && mouseY <= 240) {
room = 2;
}
}
if (room === 3) {
if (mouseX >= 160 && mouseX <= 260 && mouseY >= 200 && mouseY <= 240) {
room = 2;
//set everything back to normal
ball.x = random(30, 370);
ball.y = random(150, 250);
ball.color = color(random(255), random(255), random(255));
lives = 5;
score = 0;
ball.xSpeed = 3;
ball.ySpeed = 3;
setupBricks();
}
}
};