RhysyNoob
YaBB Newbies
Offline
Posts: 1
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.