Not quite sure where to start when it comes to drawing an array of sprites.

Started by
1 comment, last by ifthen 11 years, 4 months ago
So, I finished a pong game and upped the ante to a clone of the game Snake. Now I want the "food" to spawn at a random Vector 2 every 3 or so seconds (I'll flesh out the mechanics later). I've got two food classes, one for "good food" and one for "bad food." The random positioning works adequately, but I'm unsure how to draw more than one. I'm thinking something along the lines of (psuedo code):

if (counter <= 3)
n = rand.next (0, 100);
if (n <= 75)
goodFoodList.Add(food)
else
badFoodList.Add(badfood)
counter = 0;

My food classes each have their own draw method that I call in the game1 class draw method to keep things from getting cluttered, so the draw would look like:

foreach (Food food in goodFoodList)
Food.draw();

Am I on the right track? Is there anything I need to include in the food classes to get this working properly? Is there a better way of doing things?

Thanks in advance for the help. I really appreciate it.
Advertisement
Something like that.

In the long run, what you really want is-

foreach (Something s in everythingList)
s.draw();

Ideally, "Something" should be a Sprite, GameObject... whatever you're working with. Add all your game objects to a list, and draw them all.
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"]//init
List<GameObject> gameObjects = new ArrayList<GameObject>();
Snake snake;
gameObjects.add(snake); //OK if Snake inherits from GameObject
...
//food spawn routine
gameObjects.add(new GoodFood()); //OK if GoodFood inherits from GameObject, which it does
...
//game loop
for (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".

This topic is closed to new replies.

Advertisement