We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Space Invaders questions from a new programmer
Page Index Toggle Pages: 1
Space Invaders questions from a new programmer (Read 963 times)
Space Invaders questions from a new programmer
Feb 23rd, 2010, 8:59pm
 
After successfully programming Pong, I've decided to undertake Space Invaders. This time, I want to implement classes and objects into my sketch.

So far I have the "mothership", which the player controls. In attempting to create three enemy aliens to move back and forth, I discovered that they didn't move in a uniform manner. What I mean is, once one creature (a rectangle at this point) hits, let's say, the right hand wall, instead of having the other two move over at the same time, only that one that hit it moves over. The other two continue moving along until they hit, causing my creatures to overlap.

I have an inkling to create three separate classes for my aliens instead of just one, as I did before. I think the solution to my problem may lie in having all three classes use the same speed variable, but different location  variables. Yet I'm not sure how to go about setting a variable in one class that can be used by another class as well.

I'm only up to the chapter on Objects in Daniel Shiffman's book (chapter eight). Below is my code so far for the mothership (just a simple rectangle moving along, controlled by the mouse). In my sketch I have the Ship class in another tab, but to make it all visible, I'll copy and paste it to my main sketch tab and paste the full code.

Code:

Ship motherShip;

void setup() {
size(800,600);
smooth();
motherShip=new Ship(550,150,20);
}

void draw() {
background(255);
motherShip.display();
}

class Ship {
float ypos;
float wrect;
float hrect;

Ship(float tempYpos, float tempWrect, float tempHrect) {
ypos=tempYpos;
wrect=tempWrect;
hrect=tempHrect;
}

void display() {
fill(0);
rectMode(CENTER);
rect(mouseX,ypos,wrect,hrect);
}
}


I know it's nothing much (yet). Can anyone give me some pointers on how to make this code cleaner, or is it pretty good so far? I understand I very well could have achieved the same outcome in less than five lines, but I'm wanting to use objects and classes to enhance my project.

Thanks very much!  Smiley
Re: Space Invaders questions from a new programmer
Reply #1 - Feb 23rd, 2010, 9:44pm
 
Hey, that code looks good.

As far as the aliens hitting the wall and reversing goes:  Didn't *all* the aliens in Space Invaders change direction as a unit?  I'd use a global variable called "alienDir" or something.  In the update function for the alien class, I'd say "if I'm hitting the wall, reverse the alienDir."  That way the entire block should move more or less together.
Re: Space Invaders questions from a new programmer
Reply #2 - Mar 1st, 2010, 5:13pm
 
Thanks Ben. I'm going crazy here. Below is my current code. I attempted to creature an Enemy class with only one creature so far to make it simple. I can't get it to run as it says there is no Ship class, although I can perfectly see one. It'd be more helpful if my professor lectured about each chapter instead of choosing questions from the quiz to answer in class. Err.

Main code:
Code:
Ship motherShip;
Enemy creature1;

void setup() {
size(800,600);
smooth();
motherShip=new Ship(550,150,20);
creature1=new Enemy(400,200,75,75);
}

void draw() {
background(255);
motherShip.displayMotherShip();
creature1.displayCreature1();
}


Enemy class:
Code:
class Enemy {
float xposcreature1;
float yposcreature1;
float wcreature1;
float hcreature1;

Enemy(float tempXposcreature1, float tempYposcreature1, float tempWcreature1, float tempHcreature1) {
xposcreature1=tempXposcreature1;
yposcreature1=tempYposcreature1;
wcreature1=tempWcreature1;
hcreature1=tempHcreature1;
}

void displayCreature1() {
fill(0);
rectMode(CENTER);
rect(xposcreature1,yposcreature1,wcreature1,hcreature1);
}


Ship class:
Code:
class Ship {
float yposmothership;
float wrectmothership;
float hrectmothership;

Ship(float tempYposmothership, float tempWrectmothership, float tempHrectmothership) {
yposmothership=tempYposmothership;
wrectmothership=tempWrectmothership;
hrectmothership=tempHrectmothership;
}

void displayMotherShip() {
fill(0);
rectMode(CENTER);
rect(mouseX,yposmothership,wrectmothership,hrectmothership);
}
}
}
Re: Space Invaders questions from a new programmer
Reply #3 - Mar 1st, 2010, 7:40pm
 
Check the curly brackets in your Ship class - you have 3 opening, but four closing. Whenever classes "disappear" like this in Processing, it always seems to be due to a curly brackets issue (if it's not where I said it will be there somewhere - count and check them all).
Re: Space Invaders questions from a new programmer
Reply #4 - Mar 1st, 2010, 7:42pm
 
Ah I see...you need to move one of those closing brackets up to close the "enemy" class.... that's why it is not seeing the Ship class.....
Page Index Toggle Pages: 1