This repository has been archived by the owner on Jan 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpaceout.pde
251 lines (214 loc) · 6.56 KB
/
Spaceout.pde
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Breakout! (Space-Themed!)
Story:
You have three fighter ships to destroy a hostile UFO heading for Earth at lightspeed!
You have to blast the UFO away in one volley, or else it will regenerate and you will
have to try again! Because you are battling an alien starcraft at lightspeed, if your
fighter ship does not constantly return to the mothership after blasting bits of the
UFO away, it may become lost to the stars beyond, so be sure to move the mothership
around to stay close to your fighter ship.
Controls:
1.) To move the paddle use the left and right arrow keys.
2.) To pause the game, press 'p'
3.) To reset the game, press 'r' or the reset button under the play button
Gameplay:
The ball will speed up gradually over time, about once every 4.5 seconds.
Other stuff:
I threw in a couple end screens for whether or not the player destroys the UFO.
Because of this, I'd appreciate it if you could play the game by winning and losing to see
both screens. :)
(...or just fiddle with my code to force a win, that works too...)
*/
import controlP5.*;
ControlP5 cp5;
controlP5.Button play, reset, playAgain;
ControlFont font, sfont, efont;
Textlabel score, lives, endMsg, endMsg2;
boolean playing;
PShader backgroundShader;
PImage wallTiles, boxTiles;
Box[] walls, boxes;
Ball ball;
Paddle paddle;
boolean lost, won;
int UFOparts;
int numLives = 3;
float time;
void setup() {
size(500, 500, P2D); // No touchy! Size is hardcoded now
won = false;
lost = false;
UFOparts = 64;
time = 0;
playing = false;
/* Fonts! */
cp5 = new ControlP5(this);
font = new ControlFont(createFont("Modern No. 20", 50, true)); // For the buttons
sfont = new ControlFont(createFont("Modern No. 20", 30, true)); // For the score and life counter
efont = new ControlFont(createFont("Modern No. 20", 25, true)); // For the end screens
play = cp5.addButton("PLAY").setPosition(100, 198).setSize(295, 100);
play.getCaptionLabel().setFont(font);
play.setColorBackground(color(120, 10, 10));
play.setColorForeground(color(160, 10, 10));
play.setColorActive(color(160, 40, 40));
reset = cp5.addButton("RESET").setPosition(210, 302).setSize(75, 30);
reset.setFont(new ControlFont(createFont("Modern No. 20", 20, true)));
playAgain = cp5.addButton("PLAYAGAIN").setPosition(165, 302).setSize(150, 30);
playAgain.setFont(new ControlFont(createFont("Modern No. 20", 20, true)));
playAgain.getCaptionLabel().setText("Play Again?");
playAgain.hide();
score = cp5.addTextlabel("Score");
lives = cp5.addTextlabel("Lives");
endMsg = cp5.addTextlabel("End Message");
endMsg2 = cp5.addTextlabel("End Message 2");
backgroundShader = loadShader("woah.glsl");
boxTiles = loadImage("alien.jpg");
wallTiles = loadImage("starry.jpg");
walls = new Box[3];
walls[0] = new Box(0, 0, 50, 500, true, true, wallTiles);
walls[1] = new Box(450, 0, 50, 500, true, true, wallTiles);
walls[2] = new Box(50, 0, 400, 50, true, true, wallTiles);
int num = 0;
boxes = new Box[UFOparts];
for (int x = 50; x < 450; x += 50) {
for (int y = 150; y < 350; y += 25) {
boxes[num] = new Box(x, y, 50, 25, true, false, boxTiles);
num++;
}
}
paddle = new Paddle(new PVector(250, height), 200);
ball = new Ball(new PVector(251, 400), 25);
}
void startOver() {
}
void draw() {
if (!lost && !won) { // If you're still playing...
backgroundShader.set("resolution", width, height);
//backgroundShader.set("mouse", ball.c.x/(width*1.0), 1 - ball.c.y/(height*1.0));
backgroundShader.set("time", time);
filter(backgroundShader);
noStroke();
for (Box wall : walls) wall.draw();
for (Box box : boxes) box.draw();
// If not paused, move things around
if (playing) {
time += 0.01;
paddle.update();
ball.update();
ball.collisionCheck(walls);
ball.collisionCheck(boxes);
ball.collisionCheck(paddle);
}
// Draw the things that move
paddle.draw();
ball.draw();
showScore();
showLives();
// If the ball goes below the screen, start again
if (ball.c.y > height + ball.d/2) {
numLives--;
score.hide();
lives.hide();
setup();
}
// If you destroy the UFO, go to the win screen
if (ball.score == UFOparts) {
won = true;
endScreen();
}
// If all your ships are lost to space, go to the lose screen
if (numLives == 0) {
lost = true;
endScreen();
}
}
}
void showScore() {
score.setText("Parts Destroyed: " + ball.score + "/" + UFOparts);
score.setPosition(50, 10).setColorValue(color(250, 250, 0)).setFont(sfont);
}
void showLives() {
lives.setText("Ships: " + numLives);
lives.setPosition(330, 10).setColorValue(color(250, 250, 0)).setFont(sfont);
}
void endScreen() {
PImage endImg;
if (won) endImg = loadImage("earth.jpg"); // Winning endscreen
else endImg = loadImage("darksky.jpg"); // Losing endscreen
image(endImg, 0, 0);
play.hide();
score.hide();
lives.hide();
playAgain.show();
numLives = 3;
if (won) {
fill(96);
rect(125, 200, 225, 140);
endMsg.setText("You Won!").setColor(color(255, 215, 0)).setFont(font);
endMsg.setPosition(130, 200).setSize(100, 50);
endMsg2.setText("The Earth was saved.").setColor(color(255, 215, 0)).setFont(efont);
endMsg2.setPosition(130, 260).setSize(100, 50);
}
else {
endMsg.setText("Game Over").setColor(color(255, 255, 255)).setFont(font);
endMsg.setPosition(125, 200).setSize(100, 50);
endMsg2.setText("The Earth was destroyed.").setColor(color(255, 255, 255)).setFont(efont);
endMsg2.setPosition(112, 260).setSize(100, 50);
}
}
void PLAY() {
if (!lost && !won) {
play.hide();
reset.hide();
playing = true;
}
}
void RESET() {
if (!lost && !won) {
reset.hide();
score.hide();
lives.hide();
play.hide();
numLives = 3;
setup();
}
}
void PLAYAGAIN() {
reset.hide();
score.hide();
lives.hide();
play.hide();
playAgain.hide();
endMsg.hide();
endMsg2.hide();
numLives = 3;
setup();
}
void keyPressed() {
if (!lost && !won) {
if (key == 'p') {
if (playing) {
play.show();
reset.show();
playing = false;
}
else {
play.hide();
reset.hide();
playing = true;
}
}
else if (key == 'r') {
play.hide();
reset.hide();
score.hide();
lives.hide();
numLives = 3;
setup();
}
}
paddle.keyPressed();
}
void keyReleased() {
paddle.keyReleased();
}