Complete Roguelike Tutorial, using python3+pysdl2, part 1

From RogueBasin
Jump to: navigation, search

This tutorial is a draft, an unfinished tutorial. Feel free to talk and edit but do it knowing that much is yet to be done.

This is part of a series of tutorials; the main page can be found here.



Showing the character on screen

Time to work with - the shiny part our game. Create it in the project's folder.

For this step we're going to need a character sprite. Don't worry, we will draw some letters in the tradition of roguelikes later on. But for now lets use an image. We're using art by David E. Gervais, available here under CC BY 3.0 license. Specifically we're using ``HalfOgreFighter3.png``, because, well, they look mighty! Note that those sprites are in 54x54 resolution. And they have a pink background. A proper sized version with transparent background is available at the project's GitHub. Create a resources folder and save the image on it. Save the license there too, so that we do not forget to give the author its deserved credits.

By now your project's folder should look like this:

  | |
  | +-davir_gervais_tileset.license
  | |
  | +-HalfOgreFighter3.png

Because we did some hard work creating our Manager, SceneBase, etc., we won't even need to import sdl2 for this part. All we need is to import those classes (and Resources) from manager:

from manager import Manager, SceneBase, Resources

Let's put Inheritance to work by subclassing SceneBase:

class RogueLike(SceneBase):
    """An aspiring Roguelike game's scene."""
    def __init__(self, **kwargs):
        # Nothing there for us but lets call super in case we implement
        # something later on, ok?
        # pass the name of the resource to the sdl2.ext.Resources instance on
        fname = Resources.get("HalfOgreFighter3.png")
        # use the pysdl2 factory to create a sprite from an image
        self.sprite = self.factory.from_image(fname)
        # set it to a position to look better on our screenshot :)
        self.sprite.position = (128, 128)
    def on_update(self):
        """Graphical logic."""
        # use the render method from manager's spriterenderer

That would be all for now. To test, at the end of the, adding the belo lines and run it:

if __name__ == '__main__':
    # create a game/Manager instance
    # we're using an arbitrary size to put our half-ogre right in the middle 
    # of the screen
    m = Manager(width=288, height=288)
    # pass our created RogueLike scene to the Manager
    # make it fly!

And now we should be able to see a mighty half-ogre in the middle of a black screen:

Roguelike tutorial pysdl2-part1-character on screen.png
Personal tools