Difference between revisions of "Complete Roguelike Tutorial, using python3+pysdl2, part 1"
(removed non-related content) |
|||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C" width="60%"><tr><td><center> | |||
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. | |||
</center></td></tr></table></center> | |||
<center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center> | <center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center> | ||
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+pysdl2|here]]. | This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+pysdl2|here]]. | ||
Line 10: | Line 15: | ||
== Showing the character on screen == | == Showing the character on screen == | ||
Time to work with ''rl.py'' - the shiny part our game. Create it in the project's folder. | 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. | 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 [http://pousse.rapiere.free.fr/tome/tiles/DO/tome-domonsterstiles.htm here] under [https://creativecommons.org/licenses/by/3.0/ CC BY 3.0 license]. Specifically we're using ``HalfOgreFighter3.png``, because, well, they look mighty! | We're using art by David E. Gervais, available [http://pousse.rapiere.free.fr/tome/tiles/DO/tome-domonsterstiles.htm here] under [https://creativecommons.org/licenses/by/3.0/ 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 [https://github.com/LukeMS/pysdl2-roguelike-tutorial/blob/master/resources/HalfOgreFighter3.PNG proper sized version with transparent background] is available at the project's GitHub. | Note that those sprites are in 54x54 resolution. And they have a pink background. A [https://github.com/LukeMS/pysdl2-roguelike-tutorial/blob/master/resources/HalfOgreFighter3.PNG 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 [https://github.com/LukeMS/pysdl2-roguelike-tutorial/raw/master/resources/davir_gervais_tileset.license 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/ | +-pysdl2-roguelike-tutorial/ | ||
| | | | ||
Line 28: | Line 32: | ||
+-resources/ | +-resources/ | ||
| | | | | | ||
| +-davir_gervais_tileset.license | | +-[https://github.com/LukeMS/pysdl2-roguelike-tutorial/raw/master/resources/davir_gervais_tileset.license davir_gervais_tileset.license] | ||
| | | | | | ||
| +-HalfOgreFighter3.png | | +-[https://github.com/LukeMS/pysdl2-roguelike-tutorial/raw/master/resources/HalfOgreFighter3.png HalfOgreFighter3.png] | ||
| | | | ||
+-util/ | +-util/ | ||
| | | | ||
+-time.py | +-time.py | ||
Line 43: | Line 45: | ||
</syntaxhighlight></div> | </syntaxhighlight></div> | ||
Let's put Inheritance to work by subclassing ''SceneBase'': | |||
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> | <div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> | ||
Line 70: | Line 73: | ||
self.manager.spriterenderer.render(sprites=self.sprite) | self.manager.spriterenderer.render(sprites=self.sprite) | ||
</syntaxhighlight></div> | </syntaxhighlight></div> | ||
That would be all for now. | |||
To test, at the end of the ''rl.py'', adding the belo lines and run it: | |||
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> | <div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> | ||
if __name__ == '__main__': | if __name__ == '__main__': | ||
Line 90: | Line 93: | ||
[[File:Roguelike_tutorial_pysdl2-part1-character_on_screen.png|center]] | [[File:Roguelike_tutorial_pysdl2-part1-character_on_screen.png|center]] | ||
[[Category:Developing]] | [[Category:Developing]] |
Latest revision as of 16:14, 2 December 2017
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. |
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: