Complete Roguelike Tutorial, using python3+pysdl2, part 1

From RogueBasin
Revision as of 17:14, 2 December 2017 by Lukems-br (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.


Contents


Graphics

Showing the character on screen

Time to work with rl.py - 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:

+-pysdl2-roguelike-tutorial/
  |
  +-constants.py
  |
  +-manager.py
  |
  +-rl.py
  |
  +-resources/
  | |
  | +-davir_gervais_tileset.license
  | |
  | +-HalfOgreFighter3.png
  |
  +-util/
    |
    +-time.py

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):
        """Initialization."""
        # Nothing there for us but lets call super in case we implement
        # something later on, ok?
        super().__init__(**kwargs)
 
        # pass the name of the resource to the sdl2.ext.Resources instance on
        # manager.py
        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
        self.manager.spriterenderer.render(sprites=self.sprite)

That would be all for now. To test, at the end of the rl.py, 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
    m.set_scene(scene=RogueLike)
 
    # make it fly!
    m.run()

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