Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8406461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:06:59+00:00 2026-06-09T23:06:59+00:00

I am trying to program classical snake game. I have logic working (eating, growing

  • 0

I am trying to program classical snake game. I have logic working (eating, growing moving etc.). Now I want to add some nice graphics. So far I have some ugly graphics i made with this code:
(pseudocode)

void drawWorld(const std::vector<Tileable*> world_map) {
    for each Tileable {
         get position of Tileable
         get color of Tileable
         draw rectangle at position with color
    }
}

Class Tileable represents objects on Snakes map -> apples, walls even snake itself. It stores objects position, what happens when snake collides with object etc -> logic.

BUT

Woldn it be nice if Apples were circles, not rectangles?

So I want to add with class Drawable with method .draw() and derived classes DrawableSnake, DrawableApple, DrawableWall, DrawableWhatever, each having its own draw() method.
The problem is converting Tielable subclasses from collection world_map to apropriate subclass of Drawable, to have something like this:

void drawWorld(const std::vector<Tileable*> world_map) {
    for each Tileable {
         get drawable from tileable
         drawable.draw()
    }
}

I am thinking about using something like factory with RTTI: (pseudocode)

Drawable convertToDrawable(Tileable* tileable){
     if tileable is Apple return DrawableApple
     if tileable is Wall return DrawableWall
     etc.
}

Is there some nicer way to do this, without using RTTI?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:07:00+00:00Added an answer on June 9, 2026 at 11:07 pm

    This is a common problem in designing game engines. One way to solve this is to move to an entity-component based system.

    The apples, walls and the snake would all be of the same class – ‘Entity’. To each entity you would attach a list of components. In your case ‘ComponentDrawable’ could be a component and ‘ComponentTileable’ could be another. When you create the entity you initialize ComponentDrawable with the data it needs to draw that specific entity.

    You need a way to get a component of a certain type from the entity. A simple system could have each entity having an array of component pointers. Each component type would have a unique index in that array. An empty slot in the array would have a NULL value.

    template<typename T> T* getComponent( Entity *entity ) {
        entity.component_array[T::index]
    }
    

    And your modified pseudocode example would then end up looking something like this:

    void drawWorld(const std::vector<Entity*> world_map) {
        for each entity in world_map {
             ComponentDrawable *drawable = getComponent<ComponentDrawable>( entity )
             if( drawable )
                 drawable->draw()
        }
    }
    

    Some people like to keep functionality out of the components and only use them as carriers of data. If you would like to go that route then a data driven system could use the data from the ComponentDrawable to draw the correct representation of each entity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to program a game in which I have a Table class
Im trying to program a sort of game where you have 4 seconds to
I am trying to program (while learning JQuery) the following: I have a form
I'm trying to program a Roulette game for my capstone class. I've been trying
I've made a CUDA program for 2D convolution and now want to compare it
What I'm trying to program is a simple multithreaded python game server for fun.
I'm trying to program a little game for Android, and when I draw the
I'm trying to program a pyramid like score system for an ARG game and
I am trying to program in PHP, but php tags are not working: and
I have been trying to program a UNIX style shell command prompt in C.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.