I Made A Simple 2D Game Using Pygame, Source Included


emil10001

Active Member
Joined
May 19, 2008
Messages
669
Last weekend was the first ever Reddit Game Jam, and I decided to participate. I figured that it would be fun to write a game from scratch, and to learn PyGame while I was at it. The game consists of two basic parts; first is outer-space (Space.py) where you fly your ship to a planet, top-down style. The other part is on a planet's surface, trying to survive it's inhabitants (NarWorld.py, TrekLand.py and Disapproval.py), in the style of a platformer. Main.py ties everything together, and Start.py and Finish.py are the start and finish screens. Ground.py is not really used, but the three ground levels are based on what I wrote there.

As I said before, I built this game in one weekend, about 17 hours of development time. There is next to no polish, and the art sucks. This is not intended to be a full game for release or anything like that, but it should provide enough information to hack up into your own pygame based game. Please feel free to reuse this code, and consider the code to be under the BSD license.

My overall impression of PyGame is a very good one. It was very easy to develop with, and quick to get things up and running. Each of the two styles took me about 6 hours each to code. The rest of the time was spent on drawing the graphics, a bit of cleanup and putting it all together. If you're looking for a language/toolset to use for your first game, I would recommend Python/PyGame. (I wrote my first game in Java.) I'm not really sure if I will move forward with this project, clean it up and extend it. I may, but I may just move on to something else. Feedback here will probably determine that.


Blog post with download link (includes source):
http://sites.google.com/site/ejfeig1/blog/finishedthegamejamgame

Reddit contest submission:
http://www.reddit.com/r/RedditGameJam/comments/c4w27/reddit_game_jam_01_final_submissions_thread/c0q71s1

A good PyGame tutorial:
http://richard.cgpublisher.com/product/pub.84/prod.11
 
Will take a look when I have more time.
I too have had a look at pygame but sadly I've not coded for 5 months.
It was easy to learn and rather powerful.
I just need to be convinced the Pandora has the guts to run a complex game (or a badly written game I'm likely to come up with).
 
may88 said:
Will take a look when I have more time.
I too have had a look at pygame but sadly I've not coded for 5 months.
It was easy to learn and rather powerful.
I just need to be convinced the Pandora has the guts to run a complex game (or a badly written game I'm likely to come up with).

I wasn't sure if it was going to be viable myself until I saw ED running a PyGame game on one of his videos (inside spoiler). I was also very surprised with how speedy things were running on my very sloppily coded game. I had to add some statements to slow certain actions down, and I could have done a lot more of that.

.
 
Last edited by a moderator:
THIS IS GREAT!
Also, Thank you for the link!
I've been looking for a way to advance a little more into Python (for Maemo then eventually Pandora).

Thank you!
I'm going to look into developing using this.
 
kingoddball said:
THIS IS GREAT!
Also, Thank you for the link!
I've been looking for a way to advance a little more into Python (for Maemo then eventually Pandora).

Thank you!
I'm going to look into developing using this.

On my projects page I have another python app that might be worth looking at. It deals with the sqlite interface.
 
Last edited by a moderator:
Yeah, python/pygame is pretty badass. I'll take a look at the code and comment if I have time. I'm in the midst of moving / starting a new job right now :)
 
Well...
Not too bad for a 24-hr game. Some stuff didn't make sense but is probably because you're unfamiliar with pygame (eg. the bacon scrolls in about 5px increments but the people and the reddit guy move in 1px increments)
A lot of the code is a little hairy and I don't know if I'd necessarily recommend that people look at this as a first introduction to Pygame, but if they're already working through a tutorial
and just want to see an example of a game you can throw together real quick, it's not too bad.
(eg.
Code:
for i in win:
            if i == True:
                total += 1
                if total == 3:
                    Finish.Loop(True)
                    sys.exit()
could just be
Code:
if False not in win:
    Finish.Loop(True)
    sys.exit()

There are many other instances of such things but again it's understandable for a game programming contest. You should see some of the code I've written for game programming contests!
 
rabidpoobear said:
Well...
Not too bad for a 24-hr game. Some stuff didn't make sense but is probably because you're unfamiliar with pygame (eg. the bacon scrolls in about 5px increments but the people and the reddit guy move in 1px increments)
A lot of the code is a little hairy and I don't know if I'd necessarily recommend that people look at this as a first introduction to Pygame, but if they're already working through a tutorial
and just want to see an example of a game you can throw together real quick, it's not too bad.
(eg.
Code:
 for i in win:
             if i == True:
                 total += 1
                 if total == 3:
                     Finish.Loop(True)
                     sys.exit()
could just be
Code:
 if False not in win:
     Finish.Loop(True)
     sys.exit()

There are many other instances of such things but again it's understandable for a game programming contest. You should see some of the code I've written for game programming contests!

Wow, that fixed version is a lot nicer looking than mine.

Yes, like I said, it's pretty sloppy. But, it runs and it should give people an idea of what's needed to build a game using PyGame and a couple of different examples of how to make something work. I didn't bother to go back and fix up the scrolling because I didn't have time. That code example was one of the last things that I did when I had less than an hour to go and still didn't have opening and closing screens.

The tutorial that I linked was what I was looking at while I was working, and that explains the necessary bits.
 
Last edited by a moderator:
emil10001 said:
Wow, that fixed version is a lot nicer looking than mine.
Heh, well I've been writing Python code for about 8 years :)
In fact that snipped I posted is not how I would have done it, I would've redone that whole section in Main from this:
Code:
running = True
win = [False,False,False]
Start.Loop()
while running:
    planet = Space.Loop()
    if planet == 1:
        running = NarWorld.Loop()
        if running:
            win[0] = True
    elif planet == 2:
        running = TrekLand.Loop()
        if running:
            win[1] = True
    elif planet == 3:
        running = Disapproval.Loop()
        if running:
            win[2] = True
    total = 0
    if not running:
        Finish.Loop(False)
        sys.exit()
    for i in win:
        if i == True:
            total += 1
            if total == 3:
                Finish.Loop(True)
                sys.exit()
to this:
Code:
states = [Start, Space, NarWorld, TrekLand, Disapproval]
win = True
for state in states:
   if not state.Loop(): win = False

Finish.Loop(win)
sys.exit()
Note I made the additional assumption that Start.Loop() returns True when it is done.
[edit]
oops I didn't realize that you have a transition from Space to one of the other 3 items, not to play them all linearly. Never mind, disregard my code sample, that's for if you want to have a set of loops linearly. The way I'd modify this is to probably to have each Loop() method return the next state that should be processed.[/edit]
emil10001 said:
Yes, like I said, it's pretty sloppy. But, it runs and it should give people an idea of what's needed to build a game using PyGame and a couple of different examples of how to make something work. I didn't bother to go back and fix up the scrolling because I didn't have time. That code example was one of the last things that I did when I had less than an hour to go and still didn't have opening and closing screens.
Sure, I completely understand. I just didn't want anyone getting the idea that this was the One True Way to write a Python/Pygame program; in fact there are many approaches. Pygame's really flexible and so is Python.

Also, I mentioned this before but no one took me up on it: If you guys want to start a python / pygame topic where you ask questions about Python/Pygame (and eventually how Python/Pygame should be programmed on the Pandora) I'm happy to give free advice :)
Btw Gruso's going to be handling Pandora tutorials once the thing ships, so I'll have some Python/Pygame/Pandora tutorials there eventually.
 
Last edited by a moderator:
Nicely done! I, too, enjoy my Python and Pygame. Once made a game for a competition; had a month to develop it and it ended up with about the same amount of playtime as yours :p

Potentially useful info: when I got to play with skeezix's Pandora however long ago, I checked; it included Pygame, but not Numpy or Numeric, so Pygame's Surfarray and Sndarray modules may not be usable in the default install (of course, this could change by release, or even in later firmware images). I don't know about other optional dependencies (eg. the Font module requires SDL_ttf); I should have checked with pygame.init(), but hopefully we'll be able to find out for ourselves soon enough.
 
Really nice work, its great to see this happening .. and I hope we will see more PyGame hacks on Pandora in the coming weeks/months/years .. ;)
 
I had never used or liked Python until very recently (about a month ago) and now I'm hooked!
 
rabidpoobear said:
Code:
  states = [Start, Space, NarWorld, TrekLand, Disapproval]
  win = True
  for state in states:
     if not state.Loop(): win = False
  
  Finish.Loop(win)
  sys.exit()

Sure, I completely understand. I just didn't want anyone getting the idea that this was the One True Way to write a Python/Pygame program; in fact there are many approaches. Pygame's really flexible and so is Python.

Also, I mentioned this before but no one took me up on it: If you guys want to start a python / pygame topic where you ask questions about Python/Pygame (and eventually how Python/Pygame should be programmed on the Pandora) I'm happy to give free advice :)
Btw Gruso's going to be handling Pandora tutorials once the thing ships, so I'll have some Python/Pygame/Pandora tutorials there eventually.

Maybe something like this would work? (I should try it out and see)

Code:
  states = [NarWorld, TrekLand, Disapproval]
 space = Space.Loop()
  win = True
  while win:
     if not states[space].Loop(): win = False
  
  Finish.Loop(win)
  sys.exit()

Regarding your offer of a PyGame/Python thread, maybe that would go well in conjunction with a really good wiki page/tutorial on the subject? Especially if it has some pandora specific info. I would definitly be interested in a PyGame/Pandora cheat sheet page, telling us what we need to watch out for in order to make something run well on the Pandora.

@Tempel: Numpy should be easy to install, probably using easy_install. Numeric is depreciated, AFAIK. I have read several times that we should have SDL support on the Pandora. The easy hack to get around the font thing is just to make an image of the text to display. Not a great way to do it, but it would work in a pinch.

EDIT:

You need to be able to win the game:

Code:
completed = [False,False,False]
  states = [NarWorld, TrekLand, Disapproval]
  win = True
  while win:
    i = Space.Loop()
    completed[i] = states[i].Loop()
     if not completed[i]: 
        win = False
    if False not in completed:
        Finish.Loop(win)
        sys.exit()
    
  Finish.Loop(win)
  sys.exit()
 
Last edited by a moderator:
The old Gp2x does a nice work running games in Pygame, even if you flip() the whole screen (and that's something that you shouldn't do if you care about efficiency), so Pygame in the Pandora should be more than perfect :)

From my point of view, Pygame is a wonderful introduction to the SDL library. Most SDL functions are directly mapped into Pygame functions, so if you learn Pygame then you are half way to learn C+SDL. And if you are coding a simple game where efficiency is not a concern, like most puzzles or even shooters, Pygame is perfectly ok.
 
@rabidpoobear - I actually do have a PyGame related question. Have you ever successfully built a PyGame based binary using either py2app or py2exe? If so, would you mind posting your setup.py file? I spent the better part of Monday trying to get this thing done up with py2app only to end up with a gigantic (100MB) file that didn't do anything. Here's what I have currently for my setup.py:

Code:
"""
This is a setup.py script generated by py2applet

Usage:
    python setup.py py2app
"""

from setuptools import setup

APP = ['Main.py']
DATA_FILES = ['data','Dissaproval.py','Finish.py','NarWorld.py','TrekLand.py','Space.py','Start.py',]
OPTIONS = {'argv_emulation': True,
 'includes': ['pygame', 'os'],
 'site_packages': True,
 'strip': True,
 'use_pythonpath': True}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

Also trying this one now:

Code:
from distutils.core import setup
import os
import sys
import platform


file_dir_list = ['data','Dissaproval.py','Finish.py','NarWorld.py','TrekLand.py','Space.py','Start.py',]

start_script = "Main.py"

terse_plat_string = platform.platform(aliased = True, terse = True)

if 'Darwin' in terse_plat_string:
    import py2app

    sys.argv[1:] = ["py2app"]
    setup(
        app = [start_script],
        data_files=file_dir_list,
        options=dict(py2app=dict(argv_emulation=True))
    )
else: 
    print 'ERROR: your platform',platform.platform(aliased = True, terse = True),'has no build configuration written'
 
emil10001 said:
@Tempel: Numpy should be easy to install, probably using easy_install. Numeric is depreciated, AFAIK. I have read several times that we should have SDL support on the Pandora. The easy hack to get around the font thing is just to make an image of the text to display. Not a great way to do it, but it would work in a pinch.
Yes, either will be easy to install through opkg, but the point is that it makes distribution more troublesome. If your code relys on Numpy, then other people can't use it unless they also install Numpy (or you include Numpy in your package). And yes, SDL support is present, but some of its components (like SDL_ttf) are optional and could be left out if we're unlucky (again, I mean by default; it'll be easy to install). Anyways, my point is to watch out for the optional bits because it might cause problems for distributing.


emil10001 said:
@rabidpoobear - I actually do have a PyGame related question. Have you ever successfully built a PyGame based binary using either py2app or py2exe? If so, would you mind posting your setup.py file? I spent the better part of Monday trying to get this thing done up with py2app only to end up with a gigantic (100MB) file that didn't do anything.
I've never used py2exe or py2app, but when I did that competition, one guy was nice enough to share a really convenient Windows package. If you download my game, you can replace the contents of the "code" folder with your own Python code (and rewrite "test.py" to initiate your own stuff). Then running "runner.exe" will run everything without having to use py2exe every time. Unfortunately, I don't have an equivalent package for Mac. Did those instructions make sense?
 
Last edited by a moderator:
Tempel said:
emil10001 said:
Yes, either will be easy to install through opkg, but the point is that it makes distribution more troublesome. If your code relys on Numpy, then other people can't use it unless they also install Numpy (or you include Numpy in your package). And yes, SDL support is present, but some of its components (like SDL_ttf) are optional and could be left out if we're unlucky (again, I mean by default; it'll be easy to install). Anyways, my point is to watch out for the optional bits because it might cause problems for distributing.

Ah, well, we could probably hack up a little auto-run.sh file that informs the user that they don't have xyz lib and asks if they'd like us to install it. Then we grab it and install it for them. I've never used opkg before, so I don't know the syntax, but the fact that we're distributing to a standardized platform is going to help a lot.

I suggest that the first person to build one of these auto-run.sh scripts post it to the pandora wiki somewhere. That's what I will do if I get to it first.

I've never used py2exe or py2app, but when I did that competition, one guy was nice enough to share a really convenient Windows package. If you download my game, you can replace the contents of the "code" folder with your own Python code (and rewrite "test.py" to initiate your own stuff). Then running "runner.exe" will run everything without having to use py2exe every time. Unfortunately, I don't have an equivalent package for Mac. Did those instructions make sense?

Makes sense. I'm focusing on getting the thing running for the Mac right now, but once that gets done, I'm going to want to be able to package for windows too. So thanks for that!

As for py2app, I've been able to get the setup.py to run, build a binary, and I can run the binary. What it won't do is load the stuff that I wrote when the binary runs. Basically, I click the binary, the screen goes blank, then I press a key and it exits. I'm not sure what I'm doing wrong. I should probably head over to the python mailing list for help.
 
Last edited by a moderator:
Back
Top