Creating Space Invaders - Can't Stop 'Game Over' Screen coming up at start
in
Programming Questions
•
2 years ago
I have a almost finished Space Invaders Game. But when the game starts, it thinks that Game Over has Occurred.
I'm getting no error code. Also any slight tweek's are welcomed if they are noted.
If you can help, thank you very much.
Sorry about the Long code, but Invader1Bullet class is basically repeated in the corresponding numbers.
Code:
- SpaceShip spaceship;
- bullet bullet;
- Invader invader1;
- Invader invader2;
- Invader invader3;
- Invader invader4;
- Invader invader5;
- Invader invader6;
- Invader invader7;
- Invader invader8;
- Inv1Bullet inv1bullet;
- Inv2Bullet inv2bullet;
- Inv3Bullet inv3bullet;
- Inv4Bullet inv4bullet;
- Inv5Bullet inv5bullet;
- Inv6Bullet inv6bullet;
- Inv7Bullet inv7bullet;
- Inv8Bullet inv8bullet;
- boolean gameplay = false;
- int startingTime;
- void setup () {
- size (1000,600);
- smooth ();
- //Create SpaceShip & Bullet
- spaceship = new SpaceShip ();
- bullet = new bullet ();
- //Create 8 Invaders
- invader1 = new Invader (65,100);
- invader2 = new Invader (165,100);
- invader3 = new Invader (265,100);
- invader4 = new Invader (365,100);
- invader5 = new Invader (65,190);
- invader6 = new Invader (165,190);
- invader7 = new Invader (265,190);
- invader8 = new Invader (365,190);
- //Create 8 Bullets for Invaders
- inv1bullet = new Inv1Bullet (65,100);
- inv2bullet = new Inv2Bullet (165,100);
- inv3bullet = new Inv3Bullet (265,100);
- inv4bullet = new Inv4Bullet (365,100);
- inv5bullet = new Inv5Bullet (65,190);
- inv6bullet = new Inv6Bullet (165,190);
- inv7bullet = new Inv7Bullet (265,190);
- inv8bullet = new Inv8Bullet (365,190);
- //Start Timer at 0sec
- startingTime = millis ();
- }
- void draw () {
- game ();
- }
- void game () {
- //Variables to randomly fire Invader Bullet
- float t = random (0,400);
- float r = random (0,400);
- //Set background to Black
- background (0);
- //Clock or Timer
- int m = millis () - startingTime;
- int seconds = m /1000;
- //Display information if Game hasn't been Lost
- if (spaceship.GameOver == false); {
- loadFont ("ArialMT-16.vlw");
- fill (255);
- textAlign (LEFT);
- text ("Invaders From Space (c)2011 - Paul Marvell",20,20);
- text ("Time:",935,20);
- text (seconds,970,20);
- text (spaceship.ShipHP,960,580);
- }
- //Draw White title underline
- stroke (255);
- line (0,30,width+1,30);
- //Draw Shields
- noStroke ();
- fill (0,255,0);
- rectMode (CENTER);
- rect (140,500,width/10,height/20);
- rect (380,500,width/10,height/20);
- rect (620,500,width/10,height/20);
- rect (860,500,width/10,height/20);
- //Display Spaceship
- spaceship.display ();
- //GameOver
- if (spaceship.ShipHP <= 0) {
- spaceship.GameOver = true;
- }
- if (spaceship.GameOver = true) {
- spaceship.lose ();
- }
- //Winning Game
- if (spaceship.InvCount <= 0) {
- if (spaceship.ShipHP > 0) {
- spaceship.win ();
- }
- }
- //Bullet Displayed and able to Move
- if (spaceship.GameOver == false) {
- bullet.display ();
- bullet.move ();
- }
- //Invader Displayed and able to Move
- invader1.display ();
- invader1.move ();
- invader2.display ();
- invader2.move ();
- invader3.display ();
- invader3.move ();
- invader4.display ();
- invader4.move ();
- invader5.display ();
- invader5.move ();
- invader6.display ();
- invader6.move ();
- invader7.display ();
- invader7.move ();
- invader8.display ();
- invader8.move ();
- //Invader Bullets
- if (invader1.InvHP > 0) {
- inv1bullet.display ();
- inv1bullet.move ();
- inv1bullet.fire ();
- }
- if (invader2.InvHP > 0) {
- inv2bullet.display ();
- inv2bullet.move ();
- inv2bullet.fire ();
- }
- if (invader3.InvHP > 0) {
- inv3bullet.display ();
- inv3bullet.move ();
- inv3bullet.fire ();
- }
- if (invader4.InvHP > 0) {
- inv4bullet.display ();
- inv4bullet.move ();
- inv4bullet.fire ();
- }
- if (invader5.InvHP > 0) {
- inv5bullet.display ();
- inv5bullet.move ();
- inv5bullet.fire ();
- }
- if (invader6.InvHP > 0) {
- inv6bullet.display ();
- inv6bullet.move ();
- inv6bullet.fire ();
- }
- if (invader7.InvHP > 0) {
- inv7bullet.display ();
- inv7bullet.move ();
- inv7bullet.fire ();
- }
- if (invader8.InvHP > 0) {
- inv8bullet.display ();
- inv8bullet.move ();
- inv8bullet.fire ();
- }
- //Bullet hits Invaders - Lose 1HP (destroyed)
- if (invader1.intersect (bullet)) {
- invader1.HP ();
- }
- if (invader2.intersect (bullet)) {
- invader2.HP ();
- }
- if (invader3.intersect (bullet)) {
- invader3.HP ();
- }
- if (invader4.intersect (bullet)) {
- invader4.HP ();
- }
- if (invader5.intersect (bullet)) {
- invader5.HP ();
- }
- if (invader6.intersect (bullet)) {
- invader6.HP ();
- }
- if (invader7.intersect (bullet)) {
- invader7.HP ();
- }
- if (invader8.intersect (bullet)) {
- invader8.HP ();
- }
- //Invader Bullet hits Spaceship - Lose 1HP
- if (inv1bullet.intersect (spaceship)) {
- spaceship.HP ();
- inv1bullet.bulletFired = false;
- inv1bullet.bulletHit = true;
- }
- if (inv2bullet.intersect (spaceship)) {
- spaceship.HP ();
- inv2bullet.bulletFired = false;
- inv2bullet.bulletHit = true;
- }
- if (inv3bullet.intersect (spaceship)) {
- spaceship.HP ();
- inv3bullet.bulletFired = false;
- inv3bullet.bulletHit = true;
- }
- if (inv4bullet.intersect (spaceship)) {
- spaceship.HP ();
- inv4bullet.bulletFired = false;
- inv4bullet.bulletHit = true;
- }
- if (inv5bullet.intersect (spaceship)) {
- spaceship.HP ();
- inv5bullet.bulletFired = false;
- inv5bullet.bulletHit = true;
- }
- if (inv6bullet.intersect (spaceship)) {
- spaceship.HP ();
- inv6bullet.bulletFired = false;
- inv6bullet.bulletHit = true;
- }
- if (inv7bullet.intersect (spaceship)) {
- spaceship.HP ();
- inv7bullet.bulletFired = false;
- inv7bullet.bulletHit = true;
- }
- if (inv8bullet.intersect (spaceship)) {
- spaceship.HP ();
- inv8bullet.bulletFired = false;
- inv8bullet.bulletHit = true;
- }
- }
- //Bullet fired on Mouse Press
- void mousePressed () {
- if ((bullet.bulletHit == true) && (bullet.bulletFired == false)); {
- bullet.bulletFired = true;
- bullet.bulletHit = false;
- }
- }
- //create Invader class
- class Invader {
- //Invader Variables
- float InvXpos;
- float InvYpos;
- float InvXspd;
- float InvYspd;
- int InvHP;
- float InvXconst;
- float InvXconstRev;
- int lastMillis = 0;
- boolean InvGameOver = false;
- boolean InvDead = false;
- float InvBulletX;
- float InvBulletY;
- int InvBulletLength = 10;
- int InvBulletSpd = 40;
- //Invader Constructor
- Invader (float _xpos,float _ypos) {
- InvXpos = _xpos;
- InvYpos = _ypos;
- InvHP = 1;
- InvXspd = 4;
- InvXconst = _xpos +120; //change 65 to see difference
- InvXconstRev = _xpos -120; //think its to stop InvBullets going too far in X direction
- }
- //Invader
- void display () {
- if (InvHP > 0) {
- fill (0,0,255);
- noStroke ();
- smooth ();
- ellipse (InvXpos,InvYpos,50,50);
- }
- }
- //Invader hit by Bullet
- boolean intersect (bullet a) {
- float distance = dist (InvXpos,InvYpos,bullet.bulletX,bullet.bulletY-bullet.bulletLength /2);
- if (distance < 10) {
- return true;
- } else {
- return false;
- }
- }
- void HP () {
- if (InvHP > 0) {
- InvHP = InvHP -1;
- spaceship.InvCount = spaceship.InvCount -1;
- bullet.bulletFired = false;
- bullet.bulletHit = true;
- } else {
- InvDead = true;
- }
- }
- //Invader Movement - side to side and then move down
- void move () {
- if (spaceship.GameOver == false) {
- InvXpos = InvXpos + InvXspd;
- if (InvXpos > InvXconst) InvXspd = InvXspd *-1;
- if (InvXpos < InvXconstRev) InvXspd = InvXspd *-1;
- if (millis () > lastMillis + 20000) {
- lastMillis = millis ();
- InvYpos = InvYpos +50;
- InvXspd = InvXspd *1.25;
- }
- }
- //When Invaders reach Shields, Game Over
- if (InvYpos > 475) spaceship.GameOver = true;
- }
- }
- //create Invader Bullet 1 class
- class Inv1Bullet {
- //Invader Bullet Variables
- float InvBulletX;
- float InvBulletY;
- int InvBulletLength = 10;
- int InvBulletSpd = 40;
- float InvBulletXspd;
- float InvBulletYspd;
- float InvBulletXconst;
- float InvBulletXconstRev;
- int lastMillis = 0;
- boolean bulletFired;
- boolean bulletHit;
- float r = random (100,5000);
- //Invader 1 Bullet constuctor
- Inv1Bullet (float _xpos, float _ypos) {
- bulletFired = false;
- bulletHit = true;
- InvBulletX = _xpos;
- InvBulletY = _ypos + InvBulletLength/2;
- InvBulletXspd = 2;
- InvBulletXconst = _xpos +50;
- InvBulletXconstRev = _xpos -50;
- }
- //Invader 1 Bullet
- void display () {
- if ((spaceship.GameOver == false) && (invader1.InvDead == false)) {
- if (bulletFired == false) {
- InvBulletX = invader1.InvXpos;
- InvBulletY = invader1.InvYpos;
- fill (0,0,255);
- }
- if (bulletFired == true) {
- fill (255,166,0);
- }
- line (InvBulletX,InvBulletY,InvBulletX,InvBulletY + InvBulletLength);
- strokeWeight (3);
- }
- }
- //Invader 1 Bullet Hits Spaceship
- boolean intersect (SpaceShip a) {
- float distance2 = dist (constrain (mouseX,40,560),constrain (mouseY,575,575),InvBulletX,InvBulletY);
- if (distance2 <20){
- return true;
- } else {
- return false;
- }
- }
- //Invader 1 Bullet Movement
- void move () {
- if (spaceship.GameOver == false) {
- InvBulletX = InvBulletX + InvBulletXspd;
- if (InvBulletX > InvBulletXconst) InvBulletXspd = InvBulletXspd *-1;
- if (InvBulletX < InvBulletXconstRev) InvBulletXspd = InvBulletXspd *-1;
- if (millis () > lastMillis + 20000) {
- lastMillis = millis ();
- InvBulletY = InvBulletY +50;
- InvBulletXspd = InvBulletXspd *1.25;
- }
- if ((bulletHit == true) && (bulletFired == false)); {
- if (millis () > r) {
- lastMillis = millis ();
- bulletFired = true;
- bulletHit = false;
- float t = random (5000,10000);
- r = r+t;
- }
- }
- }
- }
- //Invader 1 Bullet Random Firing
- void fire () {
- if (bulletFired == true) {
- InvBulletY = InvBulletY +20;
- }
- if (InvBulletY > height) {
- bulletFired = false;
- bulletHit = true;
- }
- if (InvBulletY > 475) {
- if (InvBulletX > 90) {
- if(InvBulletX < 190) {
- bulletFired = false;
- bulletHit = true;
- }
- }
- if (InvBulletX > 330) {
- if(InvBulletX < 430) {
- bulletFired = false;
- bulletHit = true;
- }
- }
- if (InvBulletX > 570) {
- if(InvBulletX < 670) {
- bulletFired = false;
- bulletHit = true;
- }
- }
- if (InvBulletX > 810) {
- if(InvBulletX < 910) {
- bulletFired = false;
- bulletHit = true;
- }
- }
- }
- }
- }
- //create SpaceShip class
- class SpaceShip {
- //Spaceship Variables
- float ShipXpos;
- float ShipYpos;
- int ShipHP;
- int InvCount;
- boolean win = false;
- boolean loose = false;
- boolean GameOver = false;
- boolean Dead = false;
- //SpaceShip Constructor
- SpaceShip () {
- ShipHP = 5;
- InvCount = 8;
- }
- //Spaceship
- void display () {
- if (ShipHP > 0) {
- fill (255,0,0);
- smooth();
- if (GameOver == false) {
- noStroke ();
- triangle(ShipXpos,ShipYpos-25,ShipXpos-40,ShipYpos,ShipXpos+40,ShipYpos);
- float ShipXpos = constrain (mouseX,40,560);
- float ShipYpos = constrain (mouseY,575,575);
- }
- }
- }
- //SpaceShip hit by Invader Bullet
- void HP () {
- if (ShipHP > 0) {
- ShipHP = ShipHP -1;
- }
- if (ShipHP < 1) {
- GameOver = true;
- }
- }
- //Game Won
- void win () {
- win = true;
- gameplay = false;
- loadFont ("ArialMT-16.vlw");
- fill (255);
- textAlign (CENTER);
- text ("Well Done, You have Destroyed All Invaders",width/2,height/2);
- }
- //Game Lost
- void lose () {
- gameplay = false;
- loadFont ("ArialMT-16.vlw");
- fill (255);
- textAlign (CENTER);
- text ("GAME OVER",width/2,width/2);
- }
- }
- //create bullet class
- class bullet {
- //bullet variables
- float bulletX;
- float bulletY;
- int bulletSpd;
- boolean bulletFired;
- boolean bulletHit;
- int bulletLength;
- float xpos;
- float ypos;
- //bullet constructor
- bullet () {
- bulletFired = false;
- bulletHit = true;
- bulletLength = 10;
- bulletSpd = 0;
- }
- //Bullet
- void display () {
- if (spaceship.GameOver == false) {
- line (bulletX,bulletY,bulletX,bulletY-bulletLength);
- fill (255,0,0);
- strokeWeight (3);
- if (bulletFired == false) {
- bulletX = constrain (mouseX,40,560);
- bulletY = constrain (mouseY,575,575);
- }
- }
- }
- //Bullet Movement
- void move () {
- if (bulletFired == true) {
- bulletY = bulletY -20;
- }
- if (bulletY <= 30) {
- bulletFired = false;
- bulletHit = true;
- }
- if (bulletY < 525){
- if (bulletX > 90) {
- if (bulletX < 190) {
- bulletFired = false;
- bulletHit = true;
- }
- }
- if (bulletX > 330) {
- if (bulletX < 430) {
- bulletFired = false;
- bulletHit = true;
- }
- }
- if (bulletX > 670) {
- if (bulletX < 770) {
- bulletFired = false;
- bulletHit = true;
- }
- }
- if (bulletX > 810) {
- if (bulletX < 910) {
- bulletFired = false;
- bulletHit = true;
- }
- }
- }
- }
- }
1