-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpaceInvaders.pde
178 lines (162 loc) · 4.5 KB
/
SpaceInvaders.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
//PLEASE ALLOW A FEW SECOND FOR ALL IMAGE DATA AND MP3 FILES TO LOAD BEFORE GAMEPLAY BEGINS
import processing.sound.*;
// DOWNLOAD PROCESSING FOUNDATION'S SOUND LIBRARY SKETCH > IMPORT LIBRARY > ADD LIBRARY > SOUND
Starship player;
ArrayList <Mothership> invaders;
Mothership nextMothership;
int lives;
int score;
boolean gameTime;
PImage logo;
PImage rules;
PImage gameOver;
PFont font;
PFont underTitle;
PFont scoreText;
SoundFile sound;
void setup(){
fullScreen();
lives = 3;
score = 0;
gameTime = false;
player = new Starship();
invaders = new ArrayList();
invaders.add(new Mothership());
thread("newMothershipCreator");
logo = loadImage("./images/logo.png");
gameOver = loadImage("./images/gameOver.png");
rules = loadImage("rules.png");
sound = new SoundFile(this, "sound.mp3");
sound.loop();
}
void draw(){
if(!gameTime){
titleScreen();
}
else{
theBackground();
if(!game()){
noLoop();
endScreen();
}//if
}//else
}
void titleScreen(){
textAlign(CENTER);
theBackground();
image(logo, width/2 - 600, height/2 - 500, 1200, 600);
underTitle = createFont("Silom", 40);
textFont(underTitle);
fill(#F9FF4D);
text("ARROW KEYS TO NAVIGATE SPACE BAR TO SHOOT", displayWidth/2, displayHeight/2 + 130);
underTitle = createFont("Silom", 26);
textFont(underTitle);
text("SHOOT TO SELECT", displayWidth/2, displayHeight/2 + 280);
Button play = new Button(width/2-75, height/2 + 300, "playButton2.png");
Button help = new Button(width/2+25, height/2 + 300, "helpButton2.png");
play.display();
help.display();
player.update();
if(play.isHit(player.getLaser())){
//player.getLaser().laserDestroyed();
gameTime = true;
}//if
if(help.isHit(player.getLaser())){
//player.getLaser().laserDestroyed();
fill(0);
noStroke();
rect(0,0, width, height, 0);
image(rules, width/2-500, height/2-450, 1000, 900);
delay(900);
}//if
}
void endScreen(){
theBackground();
image(gameOver, width/2-325, height/2-400);
System.out.println("Game over");
System.out.println("Your score was: " + score);
}
void newMothershipCreator(){
nextMothership = new Mothership();
}
boolean game(){
for(Mothership commandship: new ArrayList<Mothership>(invaders)){
player.update();
int ptsToAdd = 0;
try{
ptsToAdd = commandship.update(player.getLaser()); //Update mothership
if(ptsToAdd > 0)
score += ptsToAdd;
} catch(Exception ConcurrentModificationException) { //Most likely unneccessary
ptsToAdd = -1;
if(commandship.getLevel() >= 20){ //if space invaders move past ships guns minus 1000 pts
score -= 1000;
}
invaders.add(nextMothership);
thread("newMothershipCreator"); //avoids long hault in graphics during object creation
}
if(ptsToAdd == -1){
invaders.remove(commandship);
continue;
}
else{
ArrayList<Laser> activeLasers = commandship.getSpaceInvaderLasers();
for(Laser laser: activeLasers){
if(player.isHit(laser)){
lives--;
player.explode();
player.receiveDamage(laser.getDamage());
laser.laserDestroyed();
if(lives <= 0){ //Game Finished
delay(2000);
return false;
}
break;
}
}
}
}
if(frameCount%4000==0 || invaders.size() == 0){
invaders.add(nextMothership);
thread("newMothershipCreator");
}
return true;
}
void theBackground(){
//bg
fill(20);
stroke(#F9FF4D);
strokeWeight(5);
rect(0,0, width, height, 0);
//credits
font = createFont("Phosphate-Solid", 20);
textFont(font);
fill(#380862);
text("by Connor Page & John Pignato", 169,displayHeight - 10);
font = createFont("Phosphate-Solid", 24);
textFont(font);
fill(#380862);
text("CS-255 FA2020", displayWidth - 85,displayHeight - 10);
//score box
stroke(#F9FF4D);
fill(#F5AD39);
strokeWeight(2);
rect(width/2-201, height-24, 400, 50, 4);
//score tracker
scoreText = createFont("Silom", 16);
textFont(scoreText);
fill(0);
text("SCORE: LIVES:", displayWidth/2-25, displayHeight-5);
text(score, displayWidth/2-30, displayHeight-5);
text(lives, displayWidth/2+160, displayHeight-5);
}
void keyPressed(){
if(key == CODED){
if (keyCode == LEFT)
player.shiftLeft();
else if(keyCode == RIGHT)
player.shiftRight();
}
else if(key == ' ')
player.fireGun();
}