Space invaders
Space invaders Here's the beginnings of a Spaceinvaders-esque game. Note the use of routines, arrays, and for loops.
int [] shiparray = {
200, 570, 20, 10, 4}; // x, y, width, height, speed
boolean[] location = {
false, false, false, false, false, false }; // left, right, up, down, last pressed left?, last pressed up?
boolean trigger = true;
int[] bulletX = new int[1000];
int[] bulletY = new int[1000];
float [] bombX = new float[1000];
float [] bombY = new float[1000];
int bulletcounter = 0;
int bombcounter = 0;
int bombspeed = 2;
float bulletspeed = 4.0;
float [] targetX = {
-2, -30, -60, -100, -120, -200 };
float [] targetY = {
random(300)+ 20, random(300)+ 20, random(300)+ 20, random(300)+ 20, random(300)+ 20, random(300)+ 20 };
float [] targetSpeed = {
2, 3, 3, 2, 1, 3};
float [] targetSize = {
20, 40, 60, 15, 25, 30 };
float [] targetX2 = {
400, 410, 420};
float [] targetY2 = {
random(300)+ 20, random(300)+ 20, random(300)+ 20 };
int hittimer = 0;
void setup(){
size(400, 600);
}

void draw() {
background(200);
moveship();
movebullets(); // and collion with targets
movebombs(); // and collion with ship
moveTargets();
}

void moveTargets(){
if (bombcounter > 999){
bombcounter = 0;
}
for(int i=0; i < 5; i++) {
targetX [i] = targetX [i] + (targetSize[i] / 40);
rect( targetX [i], targetY [i], targetSize[i], targetSize[i]);
if (targetX [i] > 420){
targetX [i] = int ((random(100) + 20) * -1) ;
targetY [i] = random (300);
}
float ran = random (200);
if (int(ran) == 5) {
bombX[bombcounter] = targetX [i];
bombY[bombcounter] = targetY [i];
bombcounter ++;
}
}
}


void movebullets(){
for(int i=0; i < bulletcounter; i++) {
if (bulletY[i] > - 20) {
bulletY[i] -= bulletspeed;
}
line ( bulletX[i], bulletY[i], bulletX[i], bulletY[i] + 6);
// collision detection with target below
for(int j=0; j < 6; j++) { // a for statment inside a for statment! crazy!
if ((bulletX[i] > targetX [j]) && (bulletX[i] < targetX [j] + targetSize[j])){ // if its iside the x
if ((bulletY[i] > targetY [j]) && (bulletY[i] < targetY [j] + targetSize[j])){ // if its iside the y
// targetX [j] = - 100; // reset the target
targetX [j] = int ((random(100) + 20) * -1) ;
targetY [j] = random (300);

bulletY[i] = -40; // remove the bullet
}
}
}
}
}

void movebombs(){
for(int i=0; i < bombcounter; i++) {
if (bombY[i] < height + 20) {
bombY[i] += bombspeed;
}
stroke(255, 20, 20);
line ( bombX[i], bombY[i], bombX[i], bombY[i] + 6);
stroke(1);
if ((bombX[i] > shiparray [0]) && (bombX[i] < shiparray [0] + shiparray[2])){ // if its iside the x
if ((bombY[i] > shiparray [1]) && (bombY[i] < shiparray [1] + shiparray [3])){ // if its iside the y
println("hit");
bombX[i] = - 100; // reset the target
bombY[i] = height + 30; // remove the bullet
hittimer = millis() + 1000;
}
}
}
}



void moveship(){
if ((location[0] == true) && (location[1] == true)){ // if both buttons are down
if (location[4] == true){ // location 4 toggles on or off depending on which was lass pressed.
shiparray[0] = shiparray[0] - shiparray[4];
}
else {
shiparray[0] = shiparray[0] + shiparray[4]; // shiparray[4] is the ship's speed
}
}
else { // if only one of the two are pressed
if (location[0] == true){ // if a is pressed
shiparray[0] = shiparray[0] - shiparray[4];
}
if (location[1] == true){ // if s is pressed
shiparray[0] = shiparray[0] + shiparray[4];
}
}
if ((location[2] == true) && (location[3] == true)){ // if both buttons are down
if (location[5] == true){ // location 4 toggles on or off depending on which was lass pressed.
shiparray[1] = shiparray[1] - shiparray[4];
}
else {
shiparray[1] = shiparray[1] + shiparray[4];
}
}
else { // if only one of the two are pressed
if (location[2] == true){ // if a is pressed
shiparray[1] = shiparray[1] - shiparray[4];
}
if (location[3] == true){ // if s is pressed
shiparray[1] = shiparray[1] + shiparray[4];
}
}
if (keyPressed) {
if (key == 'a'){
location[0] = true;
location[4] = true;
}
if (key == 'd'){
location[1] = true;
location[4] = false;
}
if (key == 'w'){
location[2] = true;
location[5] = true;
}
if (key == 's'){
location[3] = true;
location[5] = false;
}
if (key == ' '){ // fire bullet
if (trigger == true){
bulletX [bulletcounter] = shiparray[0] + shiparray[2]/2;
bulletY [bulletcounter] = shiparray[1];
bulletcounter ++;
if (bulletcounter == 999){ // so it never gets bigger than the array
bulletcounter = 0;
}
trigger = false;
}
}
}
if (hittimer > millis()){
fill(255, 0, 0);
}
else{
fill(1);
}
rect(shiparray[0], shiparray[1], shiparray[2], shiparray[3]);
fill(255);
}
void keyReleased() {
if (key == 'a'){
location[0] = false;
}
if (key == 'd'){
location[1] = false;
}
if (key == 'w'){
location[2] = false;
}
if (key == 's'){
location[3] = false;
}
if (key == ' '){
trigger = true;
}
}