Monday, September 20, 2010

Creating a 2D RPG using pygame: Part 1

A as small intro to what this blog series will be about, I'd like to start of by saying I am learning this as I go. I assume that since you are reading this you also do not have much experience in programing games but that you have programmed before. This is basically the documentation of my learning expirence and I hope that it will provide some good examples and a detailed thought process of how a game can be designed and implemented. If you have any ideas or ways to make things more efficient, please leave me a comment or send me an email. Any and all help is appreciated.

Just a reminder, this is not an introduction to python. You can't really expect to understand what is going on in the code without understanding the basics first. There are plenty of well put together python tutorials that can introduce you to the concepts of OO programming and the syntax of python. I would recommend starting here

I'll start off with what you will need to begin following along and some tools that I've found useful in helping me to develop my program(s) up to this point.


**Required**

  • Python 2.6 or greater:  If you want to actually run the scripts, you can download the version to your respective OS here. But if you're running anything that's not windows its probably already installed by default.
  • Text editor / IDE: I prefer to use eclipse with pydev if I'm feeling heavy weight, but I will also use geany when I just need to write a script really quick.

**Recommended**

  • IPython: It is a more feature rich python shell that is an invaluable tool. Currently I can not tell you how to use it to its full potential, but maybe a few more posts down the way I'll have a better understanding of the power it holds.
  • Type into your python interpreter "import this". (I love python)

Getting Started

OK, so the first thing we'll need is a way to initialize the pygame modules and create a screen to display everything. We'll also need a way to exit that screen and quit pygame after we are sure our code works.
import pygame, sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((640,480))

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit(0)
        else:
            pass
First, we need to initialize pygame. That is taken care of with one simple function pygame.init(). It is an easy function used to get everything started but it is not necessary for the program over all. You also have the option of starting only the modules you will be using instead of initializing them all.

Next, we need to create the surface that all our graphics will be displayed on. That is what screen = pygame.display.set_mode((640,480)) does. The variable screen is a surface object and is the only surface object that can be written to and have the results displayed. For a really good explanation of what a surface is object go here

The rest of the code is basically where all the animation of our game would happen. In this infinite loop, all the graphics will be rendered on the screen and the speed through this loop basically sets the frame rate. You can control the fram rate with other libraries in pygame, but I have not looked into how that works yet. The QUIT variable comes from where we imported * from pygame.locals at the beginning of our script.

Well that's great! If we run this script now, we'll have a 640x480 black screen. not very exciting. Next we'll add a character to the screen and make him move around using the arrow keys.

Another very good tutorial that covers what I just did here and in a little more detail can be found here:
http://www.switchonthecode.com/tutorials/python-and-pygame-a-primer

continue to part 2...

No comments:

Post a Comment