Difference between revisions of "Complete Roguelike Tutorial, using python+libtcod"

From RogueBasin
Jump to navigation Jump to search
m
Line 43: Line 43:
* ''SDL.dll'' on Windows, ''SDLlib.so'' on Linux
* ''SDL.dll'' on Windows, ''SDLlib.so'' on Linux
* A font from the ''fonts'' folder. We chose ''celtic_garamond_10x10_gs_tc.png''.
* A font from the ''fonts'' folder. We chose ''celtic_garamond_10x10_gs_tc.png''.
== Showing the @ on screen ==
This first part will be a bit of a crash-course. The reason is that you need a few lines of boilerplate code that will initialize and handle the basics of a libtcod window. And though there are many options, we won't explain them all or this part will really start to drag out. Fortunately the code involved is not as much as in many other libraries!
First we import the library. The name ''libtcodpy'' is a bit funky (sorry Jice!) so we'll rename it to just ''libtcod''.
<pre>import libtcodpy as libtcod</pre>
Then, a couple of important values. It's good practice to define special numbers that might get reused. Many people capitalize them to distinguish from variables that may change.
<pre>SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50
LIMIT_FPS = 20</pre>
Now, something libtcod-specific: we're going to use a custom font! It's pretty easy. libtcod comes bundled with a few fonts that are usable right out of the box. Remember however that they can be in different '''formats''', and you'll need to tell it about this. This one is "grayscale" and using the "tcod layout", that's why it ends with ''_gs_tc''. If you wanna use a font with a different layout or make your own, the [http://doryen.eptalys.net/data/libtcod/doc/1.4.2/console/console_set_bitmap_font_size.html docs on the subject] are really informative. You can worry about that at a later time though. Notice that the size of a font is automatically detected.
<pre>libtcod.console_set_custom_font('celtic_garamond_10x10_gs_tc.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)</pre>
This is probably the most important call, initializing the window. We're specifying its size, the title (change it now if you want to), and the last parameter tells it if it should be fullscreen or not.
<pre>libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)</pre>
For a real-time roguelike, you wanna limit the speed of the game (frames-per-second or FPS). If you want it to be turn-based, ignore this line.
<pre>libtcod.sys_set_fps(LIMIT_FPS)</pre>
Now the main loop. It will keep running the logic of your game as long as the window is not closed.
<pre>while not libtcod.console_is_window_closed():</pre>
For each iteration we'll want to print something useful to the window. If your game is turn-based each iteration is a turn; if it's real-time, each one is a frame. Here we're setting the text color to be white. [http://doryen.eptalys.net/data/libtcod/doc/1.4.2/color/index.html There's a good list of colors you can use here], along with some info about mixing them and all that. The zero is the console we're printing to, in this case the screen; more on that later.
<pre>    libtcod.console_set_foreground_color(0, libtcod.white)</pre>
Don't forget the indentation at the beginning of the line, it's extra-important in Python. '''Make sure you don't mix tabs with spaces for indentation!''' This comes up often if you copy-and-paste code from the net, and you'll see an error telling you something about the indentation (that's a pretty big clue right there!). Choose one option and stick with it.
Now print a string, left-aligned, to the coordinates (1,1). Once more the first zero specifies the console, which is the screen in this case. Can you guess what that string is? No, it doesn't move yet!
<pre>    libtcod.console_print_left(0, 1, 1, libtcod.BKGND_NONE, '@')</pre>
At the end of the main loop you'll always need to present the changes to the screen. This is called ''flushing'' the console and is done with the following line.
<pre>    libtcod.console_flush()</pre>
Ta-da! You're done. Run that code and give yourself a pat on the back!




Line 51: Line 88:


----
----
<center><h1>'''Missing sections'''</h1></center>
<center><h1>'''Missing sections'''</h1></center>



Revision as of 21:45, 6 December 2009

Hi there!


This is a work-in-progress collab effort by a small group of developers to create a Python+libtcod tutorial.




Complete Roguelike Tutorial, using Python+libtcod

Short introduction

Welcome!

Welcome to this tutorial! As you probably guessed, the goal is to have a one-stop-shop for all the info you need on how to build a good Roguelike from scratch. We hope you find it useful! But first, some quick Q&A.


Why Python?

Anyone familiar with this language will tell you it's fun! This tutorial would probably be much harder without it. We recommend that you install Python 2.6 and go through at least the first parts of the Python Tutorial. This tutorial will be much easier if you experimented with the language first. Remember that the Python Library Reference is your friend -- the standard library has everything you might need and when programming you should be ready to search it for help on any unknown function you might encounter.


Why libtcod?

If you haven't seen it in action yet, check out the features and some projects where it was used successfully. It's extremely easy to use and has tons of useful functions specific to RLs.


Graphics

Setting it up

Ok, now that we got that out of our way let's get our hands dirty! If you haven't yet, download and install Python 2.6. Other versions may work but then you'd have to smite any incompatibilities (though they shouldn't be too many). Then download libtcod and extract it somewhere. If you're on Windows, the choice between the Visual Studio and Mingw version shouldn't matter since we're using Python.

Now to create your project's folder. Create an empty file with a name of your choice, like firstrl.py. The easiest way to use libtcod is to copy the following files to your project's folder:

  • libtcodpy.py
  • libtcod-mingw.dll or libtcod-VS.dll on Windows, libtcod.so on Linux
  • SDL.dll on Windows, SDLlib.so on Linux
  • A font from the fonts folder. We chose celtic_garamond_10x10_gs_tc.png.

Showing the @ on screen

This first part will be a bit of a crash-course. The reason is that you need a few lines of boilerplate code that will initialize and handle the basics of a libtcod window. And though there are many options, we won't explain them all or this part will really start to drag out. Fortunately the code involved is not as much as in many other libraries!

First we import the library. The name libtcodpy is a bit funky (sorry Jice!) so we'll rename it to just libtcod.

import libtcodpy as libtcod

Then, a couple of important values. It's good practice to define special numbers that might get reused. Many people capitalize them to distinguish from variables that may change.

SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50
LIMIT_FPS = 20

Now, something libtcod-specific: we're going to use a custom font! It's pretty easy. libtcod comes bundled with a few fonts that are usable right out of the box. Remember however that they can be in different formats, and you'll need to tell it about this. This one is "grayscale" and using the "tcod layout", that's why it ends with _gs_tc. If you wanna use a font with a different layout or make your own, the docs on the subject are really informative. You can worry about that at a later time though. Notice that the size of a font is automatically detected.

libtcod.console_set_custom_font('celtic_garamond_10x10_gs_tc.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)

This is probably the most important call, initializing the window. We're specifying its size, the title (change it now if you want to), and the last parameter tells it if it should be fullscreen or not.

libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)

For a real-time roguelike, you wanna limit the speed of the game (frames-per-second or FPS). If you want it to be turn-based, ignore this line.

libtcod.sys_set_fps(LIMIT_FPS)

Now the main loop. It will keep running the logic of your game as long as the window is not closed.

while not libtcod.console_is_window_closed():

For each iteration we'll want to print something useful to the window. If your game is turn-based each iteration is a turn; if it's real-time, each one is a frame. Here we're setting the text color to be white. There's a good list of colors you can use here, along with some info about mixing them and all that. The zero is the console we're printing to, in this case the screen; more on that later.

    libtcod.console_set_foreground_color(0, libtcod.white)

Don't forget the indentation at the beginning of the line, it's extra-important in Python. Make sure you don't mix tabs with spaces for indentation! This comes up often if you copy-and-paste code from the net, and you'll see an error telling you something about the indentation (that's a pretty big clue right there!). Choose one option and stick with it.

Now print a string, left-aligned, to the coordinates (1,1). Once more the first zero specifies the console, which is the screen in this case. Can you guess what that string is? No, it doesn't move yet!

    libtcod.console_print_left(0, 1, 1, libtcod.BKGND_NONE, '@')

At the end of the main loop you'll always need to present the changes to the screen. This is called flushing the console and is done with the following line.

    libtcod.console_flush()

Ta-da! You're done. Run that code and give yourself a pat on the back!


Levels

The code includes a simple algorithm, it's just a sequence of rooms, each one connected to the next through a tunnel. The overlaps make it look more complex than may be apparent at first though.



Missing sections

Here are some quick guidelines for the next sections. Remember the goal is to create a RL that feels complete, but with minimal fluff so anyone can do it. The sections are not set in stone, they're open to debate and will surely go through many changes.

Stats

HP/Attack/Defense, for both the player and every monster. (I'm sure this is one of those areas where a beginner would love to tinker and it's pretty easy to add other stats.)


Items

Additive HP/Attack/Defense modifiers when worn. A string determines its class. Can equip one item of every class (weapon, armor, helmet...). Item screen with drop and use options (use equips/dequips stuff). (Should be relatively easy in python at least, where list support is awesome.)


Combat

Damage = Attack - Defense, or something. Would be cool to have a special graphical effect tied to wands and staffs (which would just be weapons with different names).


AI

Cast ray to player, if unblocked move towards, if near it, attack.