Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
marvellkid
marvellkid's Profile
2
Posts
4
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
Creating Space Invaders - Can't Stop 'Game Over' Screen coming up at start
[3 Replies]
04-May-2011 08:53 PM
Forum:
Programming Questions
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;
}
}
}
}
}
expecting EOF, found 'class'
[6 Replies]
04-May-2011 09:45 AM
Forum:
Programming Questions
Hi,
I'm fairly new to processing, and my only problem is finding and solving errors. If anyone can help, I will be really grateful.
Ship ship;
bullet bullet;
Alien alien0;
Alien alien1;
Alien alien2;
Alien alien3;
Alien alien4;
Alien alien5;
Alien alien6;
Alien alien7;
Alien0Bullet alien0bullet;
Alien1Bullet alien1bullet;
Alien2Bullet alien2bullet;
Alien3Bullet alien3bullet;
Alien4Bullet alien4bullet;
Alien5Bullet alien5bullet;
Alien6Bullet alien6bullet;
Alien7Bullet alien7bullet;
int score = 0; //inititlises score
int totalscore; //total score
boolean gameplay = false;
void setup () {
size (1000,600); //size of window
smooth ();
//create ship and bullet
ship = new Ship ();
bullet = new bullet ();
//create aliens
alien0 = new Alien (35, 150);
alien1 = new Alien (105, 150);
alien2 = new Alien (175, 150);
alien3 = new Alien (245, 150);
alien4 = new Alien (35, 220);
alien5 = new Alien (105, 220);
alien6 = new Alien (175, 220);
alien7 = new Alien (245, 220);
//create bullets
alien0bullet = new Alien0Bullet (35,150);
alien1bullet = new Alien1Bullet (105,150);
alien2bullet = new Alien2Bullet (175,150);
alien3bullet = new Alien3Bullet (245,150);
alien4bullet = new Alien4Bullet (35,220);
alien5bullet = new Alien5Bullet (105,220);
alien6bullet = new Alien6Bullet (175,220);
alien7bullet = new Alien7Bullet (245,220);
}
void draw () {
//fire alien bullets randomly
float t = random (0,450);
float r = random (0,450);
background (0); //black background
//display title when game is active
if (ship.GameOver == false); {
loadFont ("ArialMT-16.vlw");
fill (255);
text ("Space Invaders (c) 2011 - Paul Marvell (100088293)",10, 20);
text ("seconds",500,20);
text ("health:",950,20);
fill (0);
}
if (ship.AlienCount <= 0) {
totalscore = totalscore;
}
if(spaceship.AlienCount > 0) {
totalscore=(spaceship.score*100000/millis());
}
stroke (255); //white line
line (0,25,width+1,25); //underline title
//basic shields
line(100,500,150,500);
line(350,500,400,500);
line(600,500,650,500);
line(850,500,900,500);
//display user ship
ship.display();
if (ship.ShipHP <= 0) {
ship.GameOver = true; //if ship HP less than or equal to 0, Game Over
}
if (ship.GameOver == true) {
ship.lose(); //if Game Over true, lose
}
if (ship.AlienCount <= 0) {
if (ship.shipHP > O) {
ship.win(); //if ship HP more than 0 and 0 aliens on screen, win.
}
}
if (spaceship.GameOver == false) {
bullet.display ();
bullet.move (); //if not Game Over display and move user ship
}
//display aliens
alien0.move();
alien0.display();
alien1.move();
alien1.display();
alien2.move();
alien2.display();
alien3.move();
alien3.display();
alien4.move();
alien4.display();
alien5.move();
alien5.display();
alien6.move();
alien6.display();
alien7.move();
alien7.display();
//if alien has health enable alien bullet display and move
if (alien0.AlienHealth >0) {
alien0bullet.display ();
alien0bullet.move ();
alien0bullet.fire ();
}
if (alien1.AlienHealth >0) {
alien1bullet.display ();
alien1bullet.move ();
alien1bullet.fire ();
}
if (alien2.AlienHealth >0) {
alien2bullet.display ();
alien2bullet.move ();
alien2bullet.fire ();
}
if (alien3.AlienHealth >0) {
alien3bullet.display ();
alien3bullet.move ();
alien3bullet.fire ();
}
if (alien4.AlienHealth >0) {
alien4bullet.display ();
alien4bullet.move ();
alien4bullet.fire ();
}
if (alien5.AlienHealth >0) {
alien5bullet.display ();
alien5bullet.move ();
alien5bullet.fire ();
}
if (alien6.AlienHealth >0) {
alien6bullet.display ();
alien6bullet.move ();
alien6bullet.fire ();
}
if (alien7.AlienHealth >0) {
alien7bullet.display ();
alien7bullet.move ();
alien7bullet.fire ();
}
//destroy alien on bullet
if (alien0.intersect(bullet)) {
alien0.hp();
}
if (alien1.intersect(bullet)) {
alien1.hp();
}
if (alien2.intersect(bullet)) {
alien2.hp();
}
if (alien3.intersect(bullet)) {
alien3.hp();
}
if (alien4.intersect(bullet)) {
alien4.hp();
}
if (alien5.intersect(bullet)) {
alien5.hp();
}
if (alien6.intersect(bullet)) {
alien6.hp();
}
if (alien7.intersect(bullet)) {
alien7.hp();
}
//harm user ship with alien bullet
if (alien0bullet.intersect (ship)) {
ship.HP ();
alien0bullet.bulletfired = false;
alien0bullet.bullethit = true;
}
if (alien1bullet.intersect (ship)) {
ship.HP ();
alien1bullet.bulletfired = false;
alien1bullet.bullethit = true;
}
if (alien2bullet.intersect (ship)) {
ship.HP ();
alien2bullet.bulletfired = false;
alien2bullet.bullethit = true;
}
if (alien3bullet.intersect (ship)) {
ship.HP ();
alien3bullet.bulletfired = false;
alien3bullet.bullethit = true;
}
if (alien4bullet.intersect (ship)) {
ship.HP ();
alien4bullet.bulletfired = false;
alien4bullet.bullethit = true;
}
if (alien5bullet.intersect (ship)) {
ship.HP ();
alien5bullet.bulletfired = false;
alien5bullet.bullethit = true;
}
if (alien6bullet.intersect (ship)) {
ship.HP ();
alien6bullet.bulletfired = false;
alien6bullet.bullethit = true;
}
if (alien7bullet.intersect (ship)) {
ship.HP ();
alien7bullet.bulletfired = false;
alien7bullet.bullethit = true;
}
}
//mouse press fires bullet
void mousePressed () {
{
if ((bullet.bulletHit == true) && (bullet.bulletFired == false));
{
bullet.bulletFired = true;
bullet.bulletHit = flase;
}
}
}
«Prev
Next »
Moderate user : marvellkid
Forum