How are sprite/images used to associate with a character?

Started by
4 comments, last by WildCreature 9 years, 12 months ago

I'm trying to create a small game as a learning tool and wasn't really sure how to handle images.. Usually when developing a game (small, large, etc), how are the images associated with a certain character/class?

Like for example, a player chooses the hero type1 character and the image associated with that appears. How is this done in coding?

Advertisement

You can do it however way you want, within the confines of programming, math and your time. No, that doesn't tell you much, but let's just begin by saying that there's alot of ways good and bad, established and novel that could result in an apparent connection between a sprite and a character. Apparent, as in apparent to you, or anyone else playing.

You could:

1. Have hero sprites and heroes. Very specific programming, but straightforward too. Heroes can select a specific hero sprite, so that when you render heroes, you select the hero sprite from an index provided by the hero. (index into an array)

2. Have the heroes be able to link directly to a sprite, and the sprite can draw itself. (reference / pointer)

3. Have the heroes be able to draw themselves, creating a draw() member function. (draw() member function)

4. Have the heroes inherit drawing capabilities, which you override and implement, enabling them to draw themselves. (draw() inherited from Drawable class)

5. Same as #4, except you are running a script instead, allowing both you and your colleagues/team to manage many aspects from the outside using a simple text-editor.

and so on smile.png

It's not a complete list, but I tried to cover the most common ways.

Number #3 and #4 is very common.

Assosciation example:

A hero could have all the information needed both directly and indirectly, forming a decently well-encapsulated class.

The hero class itself contains information needed to identify specifics about the character.

Let's have Hero inherit from Drawable, telling us that this hero can in fact be drawn, on a screen. All it means is that you promise that your hero can be drawn on a screen.

That way we can have a custom hero class, say KnightHero which inherits the draw() call, and write his draw() function which has access to all KnightHeros information.

Now that draw() function needs to be implemented in such a way that the hero is represented on screen. :)

In your renderer, you can now add KnightHero to a list of candidates that can be drawn. Loop through that list and simply call draw().

That is the basic OOP ways, I believe.

A sprite is just an image, that you can place on screen. It can be super-small, or be the background itself. It could also be an animated character. As such, it is only a concept, and you must implement using programming somehow.

The image is stored in the character class, loaded, and printed. The pseudo-code.


class Character(object):

    def __init__(self, image, position):
        self.image = image
        self.position = position

    def render(self):
        # Draw the image on the character's location.
        drawing_function(self.image, self.position)

    def update(self):
        # Do some stuff like updating the character's location, etc.
        some_functions()
        self.render()

# The instantiation.

hero = Character('images/hero_img.png', (29, -36))

# The game loop.
while 1:
    hero.update()

Basically it's how it's done. In the OOP sense, it's said, "Character has an image." The character is updated every game loop, and while updating, it also draws itself on the screen. That said, the hero might be walking, and it has its position changed every second, and it needs to draw itself accordingly.

Sometimes the image file is loaded first and then passed to the object so you won't have to re-load the file every game loop, or you can load the file during the instantiation. Whatever, really. The image paths can be stored in database, files, etc, and usually a character has more than one images for various states it has, like idle, dying, attacking, and casting spells. Well, even a state might need more than one image, like when your character is attacking, it needs a series of images to make the attacking animation.

The idea is that the character has some images and position, and they're drawn onto the screen according to their position. The image drawn is determined by the character's current state (dying, attacking, etc), so you can set the character class to have an array of images, and a current_image variable.

Just have ur hero object have a x and y position. When time comes to draw ur image just draw it at the hero's x and y position.

I'm trying to create a small game as a learning tool and wasn't really sure how to handle images.. Usually when developing a game (small, large, etc), how are the images associated with a certain character/class?

Like for example, a player chooses the hero type1 character and the image associated with that appears. How is this done in coding?

The player is an object with a set of data

The icon is a lump of data (also an object) which can be identified with a pointer pointing to it

It is one of a number of icons, which will be associate to a player with a certain type (usually identified as an ordinal value)

You can do this statically -- on creation of the player some value/variable will determine the players 'type' and the matching icon will be

looked up from a table of 'player types) and the pointer to the correct icon will be added to the player 'object' for later use

When rendered in the game the drawing code will get the player type icon pointer from the player object and use it for drawing

A dynamic mechanis may be where some other player attibute is used to display variations of the player icon (like being injured, or dead, or with some modal state like being on foot vs being on horseback). Variables in the players object will determine whjich icon variation should be displayed and it either can be determined every cycle using if-then logic or only when it is changed (I get off a horse then the code as part of that action changes my icon back to 'on-foot' and sets my current icon pointer)

Each of those different states would have their own different icon data, and the list of all player icons would have added organization to contain the pointers to the icon data representing each of those different looks selectable for the player (elf/dwarf/human/orc) each with its list of pointers to (on-foot, on-horse,injured,dead,etc...).

If you have 10 different 'base' faces and 10 variations (matching the state/modes for each) then the program would need to have loaded 100 different icons to be able display all those variations.

--------------------------------------------[size="1"]Ratings are Opinion, not Fact

You can start with the very beginning. For example i liked this book http://www.packtpub.com/learning-libgdx-game-development/book

This topic is closed to new replies.

Advertisement