Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 wtfmates   Members   -  Reputation: 109

Like
0Likes
Like

Posted 26 November 2012 - 12:29 PM

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.

Sponsor:

#2 willpowered   Members   -  Reputation: 479

Like
1Likes
Like

Posted 26 November 2012 - 01:50 PM

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.

#3 ifthen   Members   -  Reputation: 820

Like
3Likes
Like

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 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".

Edited by ifthen, 26 November 2012 - 03:01 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS