Noob, please help with Space Invaders code
in
Programming Questions
•
3 years ago
Hey I am very new to Processing and am trying to build something like Space Invaders for a class I'm taking. I'm sure much of this code is executed pretty clunkily but I've got two ArrayLists for bullets and and Array for the aliens. I'm happy with how this stage works but this is the crux of my problem -- I want multiple stages but cant figure out how to reinitialize the array of aliens into their original starting positions after all of these are cleared. I also want to alter the speed and color when they are reinstated. Any help anyone could give me would truly be appreciated.
//EARTH DEFENSE FORCE 1953 FINAL
Spaceship mySpaceship;
ArrayList bullets;
ArrayList bombs;
int cols= 10;
int rows =5;
int cols_select;
int rows_select;
Alien[][] aliens = new Alien[cols][rows];
Timer timer;
boolean gameOn=true;
int deadAliens=0;
int lastGuy=0;
void setup(){
size(1300,1000);
colorMode(HSB,360,360,360);
smooth();
//initialize the Spaceship object, call for constructor;
mySpaceship = new Spaceship(200,200,200,500,900);
//initialize the bulletArrayList
bullets = new ArrayList();
bombs = new ArrayList();
//initialize the AlienArray
for (int i=0; i< cols; i++){
for (int j=0; j<rows; j++){
aliens[i][j] = new Alien(140, 300, 300, 360, i*100,j*80,1);
}
}
//initialize the Timer
timer = new Timer(200);
timer.start();
}
void draw(){
fill(0,50);
rect(0,0,width,height);
mySpaceship.display();
mySpaceship.move();
mySpaceship.limit_x();
mySpaceship.lastGuy();
for (int i=0; i< cols; i++){
for(int j=0; j<rows; j++){
aliens[i][j].display();
aliens[i][j].move();
aliens[i][j].shift();
aliens[i][j].exitStageLeft();
}
}
for (int i=0; i < bullets.size(); i++) {
Bullet b = (Bullet) bullets.get(i);
b.display();
b.fire();
if(b.finished()){
bullets.remove(b);
}
for (int k=0; k< cols; k++){
for(int j=0; j<rows; j++){
if(b.intersect(aliens[k][j])){
bullets.remove(b);
aliens[k][j].destroyed();
deadAliens=deadAliens+1;
}
}
}
}
for (int i=0; i < bombs.size(); i++) {
Bomb b = (Bomb) bombs.get(i);
b.display();
b.fire();
if(b.finished()){
bombs.remove(b);
}
}
for (int i=0; i< cols; i++){
for(int j=0; j<rows; j++){
int cols_select=int(random(10));
int rows_select=int(random(5));
if(timer.isFinished() && aliens[cols_select][rows_select].imaDeadAlien==false){
bombs.add(new Bomb(140,300,300,aliens[cols_select][rows_select].true_xpos,aliens[cols_select][rows_select].true_ypos,-10));
timer.start();
}
}
}
}
//end draw
void mousePressed(){
bullets.add(new Bullet(int(random(180,220)),200,200,mySpaceship.xpos,880,10));
}
//end mousePressed
class Alien {
color h;
color s;
color b;
float a;
float xpos;
float true_xpos;
float true_ypos;
float ypos;
float xspeed;
float d;
float r;
boolean imaDeadAlien=false;
Alien(color h_, color s_, color b_, float a_, float xpos_, float ypos_, float xspeed_){
h= h_;
s= s_;
b= b_;
a=a_;
xpos = xpos_;
ypos = ypos_;
xspeed = xspeed_;
}
void display(){
noStroke();
fill(h,s,b,a);
r=40;
true_xpos=xpos+200;
true_ypos=ypos+200;
ellipse(true_xpos, true_ypos, r,r);
}
void move() {
xpos = xpos + xspeed;
}
void shift() {
if(xpos+200 >= width-100){
ypos=ypos+40;
xspeed=(xspeed*(-1));
}
if(xpos+200<= 100){
ypos=ypos+40;
xspeed=xspeed*(-1);
}
}
void destroyed(){
imaDeadAlien=true;
}
void exitStageLeft(){
if(imaDeadAlien==true){
xpos=-2000;
xspeed=0;
}
}
}
class Bomb{
int h;
int s;
int b;
float xpos;
float ypos;
float yspeed;
Bomb(int temp_h, int temp_s, int temp_b, float temp_xpos, float temp_ypos, float temp_yspeed){
h= temp_h;
s= temp_s;
b= temp_b;
xpos = temp_xpos;
ypos = temp_ypos;
yspeed = temp_yspeed;
}
void display(){
fill(h,s,b);
rect(xpos, ypos, 4, 10);
}
void fire(){
ypos=ypos-yspeed;
}
boolean finished(){
if (ypos-yspeed < 50 || ypos-yspeed > 950) return true;
else return false;
}
boolean intersect(Alien a){
float distance = dist(xpos,ypos,a.true_xpos,a.true_ypos);
if(distance < 20){
return true;
} else {
return false;
}
}
}
class Bullet{
int h;
int s;
int b;
float xpos;
float ypos;
float yspeed;
Bullet(int temp_h, int temp_s, int temp_b, float temp_xpos, float temp_ypos, float temp_yspeed){
h= temp_h;
s= temp_s;
b= temp_b;
xpos = temp_xpos;
ypos = temp_ypos;
yspeed = temp_yspeed;
}
void display(){
fill(h,s,b);
rect(xpos, ypos, 4, 10);
}
void fire(){
ypos=ypos-yspeed;
}
boolean finished(){
if (ypos-yspeed < 50 || ypos-yspeed > 950) return true;
else return false;
}
boolean intersect(Alien a){
float distance = dist(xpos,ypos,a.true_xpos,a.true_ypos);
if(distance < 20){
return true;
} else {
return false;
}
}
}
class Spaceship{
//declare variables
int h;
int s;
int b;
float xpos;
float ypos;
//constructor
Spaceship(int temp_h, int temp_s, int temp_b, float temp_xpos, float temp_ypos){
h=temp_h;
s=temp_s;
b=temp_b;
xpos=temp_xpos;
ypos=temp_ypos;
}
void display(){
noStroke();
fill(h,s,b);
ellipse(xpos,ypos,50,50);
}
void move(){
xpos=mouseX;
noCursor();
}
void limit_x(){
if(mouseX<100){
xpos=100;
}
if(mouseX>(width-100)){
xpos=(width-100);
}
}
void lastGuy(){
println(deadAliens);
if(deadAliens==49){
lastGuy=1000;
} else { lastGuy=0;
}
}
}
class Timer {
int savedTime;
int totalTime;
Timer(int tempTotalTime){
totalTime=tempTotalTime;
}
void start(){
savedTime= millis();
}
boolean isFinished(){
int passedTime = millis()-savedTime-(lastGuy);
if(passedTime > totalTime){
return true;
} else {return false;
}
}
}
//EARTH DEFENSE FORCE 1953 FINAL
Spaceship mySpaceship;
ArrayList bullets;
ArrayList bombs;
int cols= 10;
int rows =5;
int cols_select;
int rows_select;
Alien[][] aliens = new Alien[cols][rows];
Timer timer;
boolean gameOn=true;
int deadAliens=0;
int lastGuy=0;
void setup(){
size(1300,1000);
colorMode(HSB,360,360,360);
smooth();
//initialize the Spaceship object, call for constructor;
mySpaceship = new Spaceship(200,200,200,500,900);
//initialize the bulletArrayList
bullets = new ArrayList();
bombs = new ArrayList();
//initialize the AlienArray
for (int i=0; i< cols; i++){
for (int j=0; j<rows; j++){
aliens[i][j] = new Alien(140, 300, 300, 360, i*100,j*80,1);
}
}
//initialize the Timer
timer = new Timer(200);
timer.start();
}
void draw(){
fill(0,50);
rect(0,0,width,height);
mySpaceship.display();
mySpaceship.move();
mySpaceship.limit_x();
mySpaceship.lastGuy();
for (int i=0; i< cols; i++){
for(int j=0; j<rows; j++){
aliens[i][j].display();
aliens[i][j].move();
aliens[i][j].shift();
aliens[i][j].exitStageLeft();
}
}
for (int i=0; i < bullets.size(); i++) {
Bullet b = (Bullet) bullets.get(i);
b.display();
b.fire();
if(b.finished()){
bullets.remove(b);
}
for (int k=0; k< cols; k++){
for(int j=0; j<rows; j++){
if(b.intersect(aliens[k][j])){
bullets.remove(b);
aliens[k][j].destroyed();
deadAliens=deadAliens+1;
}
}
}
}
for (int i=0; i < bombs.size(); i++) {
Bomb b = (Bomb) bombs.get(i);
b.display();
b.fire();
if(b.finished()){
bombs.remove(b);
}
}
for (int i=0; i< cols; i++){
for(int j=0; j<rows; j++){
int cols_select=int(random(10));
int rows_select=int(random(5));
if(timer.isFinished() && aliens[cols_select][rows_select].imaDeadAlien==false){
bombs.add(new Bomb(140,300,300,aliens[cols_select][rows_select].true_xpos,aliens[cols_select][rows_select].true_ypos,-10));
timer.start();
}
}
}
}
//end draw
void mousePressed(){
bullets.add(new Bullet(int(random(180,220)),200,200,mySpaceship.xpos,880,10));
}
//end mousePressed
class Alien {
color h;
color s;
color b;
float a;
float xpos;
float true_xpos;
float true_ypos;
float ypos;
float xspeed;
float d;
float r;
boolean imaDeadAlien=false;
Alien(color h_, color s_, color b_, float a_, float xpos_, float ypos_, float xspeed_){
h= h_;
s= s_;
b= b_;
a=a_;
xpos = xpos_;
ypos = ypos_;
xspeed = xspeed_;
}
void display(){
noStroke();
fill(h,s,b,a);
r=40;
true_xpos=xpos+200;
true_ypos=ypos+200;
ellipse(true_xpos, true_ypos, r,r);
}
void move() {
xpos = xpos + xspeed;
}
void shift() {
if(xpos+200 >= width-100){
ypos=ypos+40;
xspeed=(xspeed*(-1));
}
if(xpos+200<= 100){
ypos=ypos+40;
xspeed=xspeed*(-1);
}
}
void destroyed(){
imaDeadAlien=true;
}
void exitStageLeft(){
if(imaDeadAlien==true){
xpos=-2000;
xspeed=0;
}
}
}
class Bomb{
int h;
int s;
int b;
float xpos;
float ypos;
float yspeed;
Bomb(int temp_h, int temp_s, int temp_b, float temp_xpos, float temp_ypos, float temp_yspeed){
h= temp_h;
s= temp_s;
b= temp_b;
xpos = temp_xpos;
ypos = temp_ypos;
yspeed = temp_yspeed;
}
void display(){
fill(h,s,b);
rect(xpos, ypos, 4, 10);
}
void fire(){
ypos=ypos-yspeed;
}
boolean finished(){
if (ypos-yspeed < 50 || ypos-yspeed > 950) return true;
else return false;
}
boolean intersect(Alien a){
float distance = dist(xpos,ypos,a.true_xpos,a.true_ypos);
if(distance < 20){
return true;
} else {
return false;
}
}
}
class Bullet{
int h;
int s;
int b;
float xpos;
float ypos;
float yspeed;
Bullet(int temp_h, int temp_s, int temp_b, float temp_xpos, float temp_ypos, float temp_yspeed){
h= temp_h;
s= temp_s;
b= temp_b;
xpos = temp_xpos;
ypos = temp_ypos;
yspeed = temp_yspeed;
}
void display(){
fill(h,s,b);
rect(xpos, ypos, 4, 10);
}
void fire(){
ypos=ypos-yspeed;
}
boolean finished(){
if (ypos-yspeed < 50 || ypos-yspeed > 950) return true;
else return false;
}
boolean intersect(Alien a){
float distance = dist(xpos,ypos,a.true_xpos,a.true_ypos);
if(distance < 20){
return true;
} else {
return false;
}
}
}
class Spaceship{
//declare variables
int h;
int s;
int b;
float xpos;
float ypos;
//constructor
Spaceship(int temp_h, int temp_s, int temp_b, float temp_xpos, float temp_ypos){
h=temp_h;
s=temp_s;
b=temp_b;
xpos=temp_xpos;
ypos=temp_ypos;
}
void display(){
noStroke();
fill(h,s,b);
ellipse(xpos,ypos,50,50);
}
void move(){
xpos=mouseX;
noCursor();
}
void limit_x(){
if(mouseX<100){
xpos=100;
}
if(mouseX>(width-100)){
xpos=(width-100);
}
}
void lastGuy(){
println(deadAliens);
if(deadAliens==49){
lastGuy=1000;
} else { lastGuy=0;
}
}
}
class Timer {
int savedTime;
int totalTime;
Timer(int tempTotalTime){
totalTime=tempTotalTime;
}
void start(){
savedTime= millis();
}
boolean isFinished(){
int passedTime = millis()-savedTime-(lastGuy);
if(passedTime > totalTime){
return true;
} else {return false;
}
}
}
1