Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualifthen

Posted 26 November 2012 - 03:01 PM

You should take a look at inheritance in C#. Look at classes and interfaces. You could do something like
[source lang="csharp"]interface GameObject { void draw();}abstract class Food : GameObject { protected boolean isEaten = false; abstract protected void eatenResponse(Snake eater); void eaten(Snake eater) { isEaten = true; eatenResponse(eater); }}class GoodFood : Food { void draw() { if (!isEaten) { //draw a sprite of good food } } protected void eatenResponse(Snake eater) { eater.grow(3); //the snake grows a lot and gets a score from it in his own function }}class GoodFood : Food { void draw() { //... } protected void eatenResponse(Snake eater) { eater.kill(); //poison is good for you }}[/source]

That way, you can just put every drawable object in a loop and do something like
[source lang="java"]//initList<GameObject> gameObjects = new ArrayList<GameObject>();Snake snake;gameObjects.add(snake); //OK if Snake inherits from GameObject...//food spawn routinegameObjects.add(new GoodFood()); //OK if GoodFood inherits from GameObject, which it does...//game loopfor (GameObject gameObject : gameObjects) { gameObject.draw(); //every GameObject has a draw() method, no need to cast if (snakeSomehowTouchedTheObject(snake, gameObject)) { //you can handle eating this way Food food = gameObject as Food; //you can do this if (food != null) { //is gameObject of type / inheriting type Food? food.eaten(snake); } //or even better this way (if you define the functions and modify the classes accordingly) for (GameObject gameObject2 : gameObject) if (gameObject.touches(gameObject2)) { gameObject.touched(gameObject2); gameObject2.touched(gameObject); } }}[/source]

Be warned, however, that inheritance, although making many things easier, is not a principle that can be used everywhere seamlessly (eg. networking). Professional programmers know at least functional and OOP programming well, you should learn both of these code styles if you want to "go with the wave".

#2ifthen

Posted 26 November 2012 - 02:56 PM

You should take a look at inheritance in C#. Look at classes and interfaces. You could do something like
[source lang="csharp"]interface GameObject { void draw();}abstract class Food : GameObject { protected boolean isEaten = false; abstract protected void eatenResponse(Snake eater); void eaten(Snake eater) { isEaten = true; eatenResponse(eater); }}class GoodFood : Food { void draw() { if (!isEaten) { //draw a sprite of good food } } protected void eatenResponse(Snake eater) { eater.grow(3); //the snake grows a lot and gets a score from it in his own function }}class GoodFood : Food { void draw() { //... } protected void eatenResponse(Snake eater) { eater.kill(); //poison is good for you }}[/source]

That way, you can just put every drawable object in a loop and do something like
[source lang="java"]//initList<GameObject> gameObjects = new ArrayList<GameObject>()Snake snake;gameObjects.add(snake); //OK if Snake inherits from GameObject...//food spawn routinegameObjects.add(new GoodFood()); //OK if GoodFood inherits from GameObject, which it does...//game loopforeach (GameObject gameObject in gameObjects) { gameObject.draw(); //every GameObject has a draw() method, no need to cast if (snakeSomehowTouchedTheObject(snake, gameObject)) { //you can handle eating this way Food food = gameObject as Food; //you can do this if (food != null) { //is gameObject of type / inheriting type Food? food.eaten(snake); } //or even better this way (if you define the function in GameObject) if (snakeSomehowTouchedTheObject(snake, gameObject)) { gameObject.touchedSnake(snake); } }}[/source]

Be warned, however, that inheritance, although making many things easier, is not a principle that can be used everywhere seamlessly (eg. networking). Professional programmers know at least functional and OOP programming well, you should learn both of these code styles if you want to "go with the wave".

#1ifthen

Posted 26 November 2012 - 02:56 PM

You should take a look at inheritance in C# (i suppose that you are programming in C#, so correct me if I am wrong). Take a look at classes and interfaces. You could do something like
[source lang="csharp"]interface GameObject { void draw();}abstract class Food : GameObject { protected boolean isEaten = false; abstract protected void eatenResponse(Snake eater); void eaten(Snake eater) { isEaten = true; eatenResponse(eater); }}class GoodFood : Food { void draw() { if (!isEaten) { //draw a sprite of good food } } protected void eatenResponse(Snake eater) { eater.grow(3); //the snake grows a lot and gets a score from it in his own function }}class GoodFood : Food { void draw() { //... } protected void eatenResponse(Snake eater) { eater.kill(); //poison is good for you }}[/source]

That way, you can just put every drawable object in a loop and do something like
[source lang="java"]//initList<GameObject> gameObjects = new ArrayList<GameObject>()Snake snake;gameObjects.add(snake); //OK if Snake inherits from GameObject...//food spawn routinegameObjects.add(new GoodFood()); //OK if GoodFood inherits from GameObject, which it does...//game loopforeach (GameObject gameObject in gameObjects) { gameObject.draw(); //every GameObject has a draw() method, no need to cast if (snakeSomehowTouchedTheObject(snake, gameObject)) { //you can handle eating this way Food food = gameObject as Food; //you can do this if (food != null) { //is gameObject of type / inheriting type Food? food.eaten(snake); } //or even better this way (if you define the function in GameObject) if (snakeSomehowTouchedTheObject(snake, gameObject)) { gameObject.touchedSnake(snake); } }}[/source]

Be warned, however, that inheritance, although making many things easier, is not a principle that can be used everywhere seamlessly (eg. networking). Professional programmers know at least functional and OOP programming well, you should learn both of these code styles if you want to "go with the wave".

PARTNERS