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.
Page Index Toggle Pages: 1
Polymorphism? (Read 520 times)
Polymorphism?
Mar 25th, 2010, 8:00am
 
Hi all, my first post asks a question about how to implement polymorphism in a game sketch that I am working on that moment. Its a very rough sketch atm, so any other help that you would like to offer is also appreciated.

Basically, I am reading the tutorials and stumbled on to inheritance and subclasses etc. A concept that I find facinating yet a head scratcher at the same time.

To be brief (my code will follow): I am trying to implement a subclass (balls2), this will inherit everything about the class B but the balls will move faster and be different sizes and appear at different rates.

Now - I saw that you can make an array, but instead of populating the array with just one object you can put lots of things in it.

I want to say that if the frameRate%aTimePeriod put ball1 in array, if the frameRate%anotherTimePeriod then put ball2 in array. This will make the game a bit more fun (hopefully).

What I want to know, is what code should I use to do this and more importantly why? Thanks in advance people!

B[] myB = new B[999];
Obstacles[] obsT = new Obstacles[999];
int enemyNum;
int ballCount=0;
int obstCount=0;
int ballZCount=0;
int time=0;
int lives;
int timerCount = frameCount/25;
float pos1 = 350;
float obstXX;
float obstYY;

void setup() {
 size (400,400);
 smooth();
 PFont font;
 frameRate(25);
 font=loadFont("Calibri-24.vlw");
 textFont(font);
 lives=1;
}

void draw(){
 background(0);
 println (obstCount);
 if(lives==1){
 drawPlayer(pos1,70.0);
 drawBalls();
 drawObst();
 time++;
 if(time%100==0){ballHELLO();}
 if(time%100==0){obstHELLO();}
 }
 
 else{
 end();
 if(mousePressed)
 {begin();}
 
 }
}

void begin(){
 lives=1;
 ballCount=0;
 obstCount=0;
}


void drawPlayer(float y, float q)
{
 float Ypos = y;
 float siZe = q;
 fill(255,255,255);
 rectMode(CENTER);
 rect(mouseX,Ypos,siZe,10);
}

void ballHELLO()
{
   myB[ballCount] = new B(random(0,400),random(0,200),20,random(1.5,1.8),random(1.5,1.8));
   ballCount++;
}

void obstHELLO()
{
   obsT[obstCount] = new Obstacles(random(0,width),random(0,100));
   obstCount++;
}



void drawBalls() //pretty sure its here where i need to implement the polymorphism
{
 for(int i=0;i< ballCount;i++)
 {  
     myB[i].display();
     myB[i].gravity(mouseX,pos1);
 }

}

void drawObst()
{
 for(int j=0;j< obstCount;j++)
 {  
     obsT[j].show();
 }

}


void timer()
{
 fill(255);
 text(frameRate/25,random(0,400),random(0,400));

}

void end()
{
 text("you lasted: "+timerCount,200,200);
 text("seconds, well done!",200,250);
 
}

class B {
 //Hopefully a superclass
 float globalX;
 float globalY;
 float diameter;
 float speedX;
 float speedY;
 float speedFactor;

 B (float global_X, float global_Y, float diamtr, float speed_X, float speed_Y) {
   globalX = global_X;
   globalY = global_Y;
   diameter = diamtr;
   speedX = speed_X;
   speedY = speed_Y;
   speedFactor = speedY*speedY;

 }

 void gravity (float u, float v){

   globalX = globalX + (speedX * speedFactor);
   globalY = globalY + (speedY * speedFactor);

   if((globalX > width)||(globalX < 0))
   {
     speedX = speedX* -1;
   }
   
   if((globalY < 0)||(dist(u,v,globalX,globalY) <= 20))
   {
     speedY = speedY* -1;
   }

   constrain (globalX, 0, width);
   constrain (globalY, 0, height);
   
   lose();

 }


 void display(){
   ellipseMode (CENTER);
   ellipse (globalX,globalY,diameter,diameter);
   fill (255,255,255);

 }

 void lose()
 {
   if(globalY>height)
   {
     lives-=1;
     
   }
}

}

class balls2 extends B {
 
 balls2() {
   super(6,5,4,3,2); //some dummy numbers this will change
 }
 
}

class Obstacles {
 
 float ObstX;
 float ObstY;
 
Obstacles(float h, float d){
           ObstX = h;
           ObstY = d;
}
 
  void show(){

    beginShape();
    fill(255);
    vertex(ObstX,ObstY);
    vertex(ObstX,ObstY+10);
    vertex(ObstX+10,ObstY+10);
    vertex(ObstX+10,ObstY);
    vertex(ObstX,ObstY);
    endShape();
}

}


Again apologies if my code is very rough.


Re: Polymorphism?
Reply #1 - Mar 25th, 2010, 9:45am
 
You will not need a sub-object class for what you want to achieve.
You can make your "ball" class as it is, but keep the "speed/ size" etc. all as a creation parameter, say of your object. Then, depending on your "timing" you add ball-objects of different starting paramters to your array.

You will need subclasses only if you want to achieve a kind of hierachy in programming. Say a "super" class "Objects" which will return a position as size etc. Then a "subclass" Balls which in additon have function to roll the ball around and f.e. another subclass "goal" which would have a function to recognize events etc.
The point is, that BOTH those subclasses would have the basic propertie of being able to report their "position" because this has already been done in the super-class.
As long as you will only have "one type" of object, there is absolutely no need to make super and subclasses. (Except you want to uses the same superclass in a different sketch and like "deriving" more than "rewriting" code.)
Just make the class do what you need.
In a way it boild down to:
If a specific function-call (say "draw") always does the SAME thing just with different parameters (size, color, etc.) everything can go into one classe. If the specific function call should do completely different things (f.e. one draws and animates a ball while another draws some completely different shape), you might want to create two different (sub)classes of one super class...
Does it make sense?
I think it is mainly a matter of "style" and one can program with a lot of classes and inherited classes, or with just "one" class which contains all functionality...
Page Index Toggle Pages: 1