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


emil10001 said:
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.
You don't really want to be installing libraries willy nilly. The NAND has only a small and limited space, and a bunch of libraries only used by one or two applications can add up quick.
On the other hand, including these libraries in the PND file may waste a bit more space on your SD card, but those are generally going to be 4 or 8 GB at least. 100MB wasted out of 4GB because you've included the same library across multiple PND files, or 25MB out of 512 wasted for only four apps that use that library? Strictly speaking, it's a little more wasteful to include the libraries with the PND, but ultimately it's better because SD cards are larger and more dynamic: if you're done with a PND and delete it, the library goes away, but you can't as easily remove it from the NAND; if you're in a bind and really need that extra space, you can spend a couple of dollars and just get a 512MB or 1GB card, but if the NAND runs out of space because you've installed a bunch of libraries you're stuck trying to figure out which libraries to delete so you can install another one.
 
Last edited by a moderator:
emil10001 said:
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()
I didn't really check the game, but I would write that as:
Code:
completed = [False,False,False]
states = [NarWorld, TrekLand, Disapproval]
alive = True

while alive:
  i = Space.Loop()
  completed[i] = states[i].Loop()
  alive = completed[i]
  if all(completed):
    break

Finish.Loop(alive)
sys.exit()
And watch your indentation, python is quite sensitive to that.

<edit> In case Space.Loop() just returns 0,1,2, etc.. this becomes even shorter:

Code:
states = [NarWorld, TrekLand, Disapproval]

Finish.Loop(all(state.Loop() for state in states))

sys.exit()
The function 'all' short-circuits, i.e. it stops as soon as a state returns False.
</edit>
 
Last edited by a moderator:
Caine said:
And watch your indentation, python is quite sensitive to that.

Yea, it's the code blocks that screwed that up.

In case Space.Loop() just returns 0,1,2, etc.. this becomes even shorter:

Code:
 states = [NarWorld, TrekLand, Disapproval]
 
 Finish.Loop(all(state.Loop() for state in states))
 
 sys.exit()
The function 'all' short-circuits, i.e. it stops as soon as a state returns False.

But that won't do what I needed it to do. My game does not step through those functions linearly, it depends on which planet you visit, and that can be done in any order. The all function is neat though.

@WizardStan - good point, that's probably going to be the preferred solution.
 
Last edited by a moderator:
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 only ever done this for Windows, and I just followed someone else's guide and it worked just fine. Have you tried following a guide?

It doesn't actually build a binary anyway, it builds a binary that runs Python and then includes your source and resources in a zipped file. So it's not strictly a single binary (which is arguably better as it lets the user replace certain parts of the code without having to download a whole new binary). That's for py2exe at least, not sure about py2app, never used it.
 
Last edited by a moderator:
rabidpoobear said:
I've only ever done this for Windows, and I just followed someone else's guide and it worked just fine. Have you tried following a guide?

It doesn't actually build a binary anyway, it builds a binary that runs Python and then includes your source and resources in a zipped file. So it's not strictly a single binary (which is arguably better as it lets the user replace certain parts of the code without having to download a whole new binary). That's for py2exe at least, not sure about py2app, never used it.

Well, I've figured out that the problem probably isn't my setup.py script, but something obscure happening with PyGame. When I run from the command line I get the following:

Code:
pygame.error: File is not a Windows BMP file

I had taken care of this when I installed pygame by making sure that libpng was installed, and that pygame could find it. I've double-checked where my system pulls pygame from and where py2app is pulling pygame from. I've also pulled in the SDL frameworks and libpng. I have forced all output from the build into a log so that I can search it, but everything looks like it's being pulled from the right spot. I can't figure it out.
 
Last edited by a moderator:
Is pygame something that you could teach yourself on a pandora? As in can you install the tools you needto develop it, or would you need to work on a pc/laptop?

And what's a decent source for entry level basics, any good site or YouTube channel to recommend?
 
I just see that vid in the spoiler by ED and answers my question, you can do it all on the pandora, sweet, I will give this pygame a try I think, see if I can pick it up, still post any good basic reference if you know a good one please though
 
Fzero said:
Is pygame something that you could teach yourself on a pandora? As in can you install the tools you needto develop it, or would you need to work on a pc/laptop?

And what's a decent source for entry level basics, any good site or YouTube channel to recommend?
Sure, don't see why not. Typing might be a bit easier on a PC, but the Pandora should work just fine. ED was editing some python stuff in a video as well.

You don't really need a lot of tools for this. A text editor, python and pygame should be sufficient.

Teaching resources depends on if you already have programming skills or not.
Python beginners guide seems like a reasonable place to start. For pygame in specific, you could check out the pygame tutorials page.
 
Last edited by a moderator:
Fzero said:
Is pygame something that you could teach yourself on a pandora? As in can you install the tools you needto develop it, or would you need to work on a pc/laptop?

And what's a decent source for entry level basics, any good site or YouTube channel to recommend?
You could code in python/pygame on the Pandora, but you'll probably find it much easier on a desktop.
It's easy enough to install anyway.

I hear good things about Dive Into Python, although I haven't actually gone through it myself.
Not really sure youtube would really work for learning to code, since you need lots of code examples.

EDIT: Once you get the hang of the basics, pretty much everything else you need to know is in the standard library reference: http://docs.python.org/library/
 
Last edited by a moderator:
Fzero said:
I just see that vid in the spoiler by ED and answers my question, you can do it all on the pandora, sweet, I will give this pygame a try I think, see if I can pick it up, still post any good basic reference if you know a good one please though

Yes, also take another look at the first post. I posted a link to a good tutorial that explains the basic elements needed for a game, and I also posted source code (yes, it's sloppy and could be much improved upon) to the game that I made over last weekend. The reason that I posted the source was so that people who were just starting out could take a simple game, that runs, and learn from it by screwing around with the source.

A really good exercise would be to fix the scrolling movement of the background, and make the enemies and items move in relation to that scrolling. Another thing is the laser firing in TrekLand is broken and could be fixed. The timing of the animations could be worked on to slow them down a bit.

If you already know Python, then PyGame is pretty easy. Otherwise, I think this book is a good start to Python. It's short, easy to understand and free to download.
 
Last edited by a moderator:
Nice one guys, I am going to try get into this, I just watched 10 parts of a 16 part video tutorial, I understood the basics of setting a screen, an image for a fixed background and how to draw a circle that a user can move with the mouse or keys... I don't have any python knowledge at all, and far as programming goes nothing really, for work iv had to edit some simple basic script but I don't do much, and I know some web scripts/language, so I understand functions and variables so it's not 'all' alien, the vids I was watching were pretty good as it goes, I'm going to d/l pygame so I can follow along and actually do it myself.
Will d/l your sample game too, good to try break down what does what I suppose.
Will check that free book too.
 
Aninhumer said:
You could code in python/pygame on the Pandora, but you'll probably find it much easier on a desktop.
It's easy enough to install anyway.

I hear good things about Dive Into Python, although I haven't actually gone through it myself.

I would NOT recommend Dive into Python.
In fact I have not read it either and in the past I have recommended it but have always put the same disclaimer that I haven't read it.
I've always heard it's not a good beginner reference but after reading this article: Dive into Python Must Die I no longer recommend it even for those familiar with programming.


Emil's reference of How to Think like a Computer Scientist is a better book, as is Invent with Python (also a free book online). Both of those I have actually looked at, and they seem quite nice. Personally I learned using the tutorial by Guido, and then just writing a TON of code. Python has been my main 'fun' language since I was in high school. Now I've got a master's in Comp Sci and program professionally in many different languages, and I STILL use Python for everything I write myself. So guys, don't get the idea that Python is just a toy language that you learn to play around with until you are ready to move on to C++. There are very few things most people would want to program that Python cannot handle, and it will save you a ton of time versus a lot of other languages.

The book Game Programming with Python and Pygame by Will McGugan is good too. He makes a lot of references to sushi and his writing style is just a touch annoying at times, but he knows his stuff and he presents it in a pretty understandable way. It's not a great introduction to Python but it's a good one to Pygame. Some stuff is omitted but what is there is good. It's non-free though.
[disclaimer - I was sent a free review copy of this book by the publisher]

I would also recommend anyone who is really motivated to learn Python to join the Python Tutor mailing list. You can find it off the main Python website. I've been a contributor there for about 6 years now. I don't think I've ever seen a question go unanswered for more than half a day or so. It's incredibly active and full of really knowledgeable people. It's one of the friendliest and quickest-responding e-mail lists I've been part of. Just make sure that when you ask questions, you have actually TRIED to solve the problem, and you can tell us what you've tried, why you think it should work (or why you think it is broken) and what you've tried to fix it. Include source code (if it's lots of code, throw it up on pastebin or attach it) - try to trim it down to relevant parts, but if you aren't sure if a part is relevant, leave it in - and whatever exceptions you're getting (the full traceback) or, if you're not getting an exception, what it's doing that is not what you thought you were telling it to do.

The pygame mailing list is also good, and nearly as active as Tutor. I'm on both of them, and I hope to see you guys there as well!
 
Last edited by a moderator:
^ I completely agree with the above. Python has been my main hobby language since I finished high school, and I too recently got my masters in CS (SW eng. main). I also own the pygame book (I bought it just to see if there was some basics I have missed), and I can recommend it for learning the basics. I also have the luxury of writing python at work :p (in addition to several other languages, but mainly python)

EDIT: last statement
 
rabidpoobear said:
I would NOT recommend Dive into Python.
In fact I have not read it either and in the past I have recommended it but have always put the same disclaimer that I haven't read it.
I've always heard it's not a good beginner reference but after reading this article: Dive into Python Must Die I no longer recommend it even for those familiar with programming.
Thanks for that link, I won't recommend it again. :p

Personally I learned using the tutorial by Guido, and then just writing a TON of code. Python has been my main 'fun' language since I was in high school. Now I've got a master's in Comp Sci and program professionally in many different languages, and I STILL use Python for everything I write myself. So guys, don't get the idea that Python is just a toy language that you learn to play around with until you are ready to move on to C++. There are very few things most people would want to program that Python cannot handle, and it will save you a ton of time versus a lot of other languages.
I think I learned from Guido's tutorial too, but then I'd learned BASIC before I did, so I wasn't sure if it was aimed at beginners or not.
Definitely Python is still my favourite language. Its only flaw is that it makes most other languages I try seem messy and complicated. :p
I had a play with Google's Go too at one point. It's strange in that it has one of the best concurrency implementations I've seen, and yet the language sucks in so many other ways. (No exceptions? Really?)
And yes I know about Stackless Python, I gave up on that when its suggested way to reimplement sleep() was to use a spinlock. <_<
 
Last edited by a moderator:
B-ZaR said:
^ I completely agree with the above. Python has been my main hobby language since I finished high school, and I too recently got my masters in CS (SW eng. main). I also own the pygame book (I bought it just to see if there was some basics I have missed), and I can recommend it for learning the basics. I also have the luxury of writing python at work :p (in addition to several other languages, but mainly python)

EDIT: last statement
Congrats on the masters. It's a bitch, eh?

Hah, writing Python for work sounds dreamy. I just took the best job I got offered (benefits / pay-wise). Python jobs tend to be rather scarce so one has to look around for them, and they also seem to quite often be at startups, which can be both a good and a bad thing, depending on your POV. I rushed through school, I just turned 21 a few months ago. I'm not one of those people who is real critical about their jobs - I'd prefer to do Python programming but I don't mind anything else - heck, I did 6 months as a C# programmer and I thought WPF was super-cool! I still have fantasies about Expression Blend - Python GUI design can't even come CLOSE to C# tools. (unless you use IronPython... shh!) I am seeking to never be unemployed for long, at least while I've got school loans. In fact my previous job and my next job are even overlapping this time!

I just really love programming, so whatever language or software it is doesn't really matter to me. I would like a Python job but if that means being unemployed for 6 months while I find one that suits me and pays well, I'd rather not. I didn't think it was a good move to be picky about a job straight out of college - I need experience before I start demanding what I want! Also I have a few loans left over from school and I'd like to be out of debt within the next 6 months (they're subsidized so if I pay them off before then, I don't have to pay any interest - thanks, gov't!) I don't really have any expenses except food and shelter and lots and lots of electricity so it should be a piece of cake! Having to work to pay for college has taught me how to spend as little money as possible - writing those fat checks to the college always makes me go :( . That's my life goal - to have enough financial security that I'm not ever forced to do anything I don't want to do. I'm not quite there yet, but I'm getting close. I'll get that Python job once I'm there. (My other life goal was to have enough certifications that I could teach at a community college - which was sort of contrary to my first life goal of not going into debt - so I made an exception :)
 
Last edited by a moderator:
Aninhumer said:
Definitely Python is still my favourite language. Its only flaw is that it makes most other languages I try seem messy and complicated. :p
I had a play with Google's Go too at one point. It's strange in that it has one of the best concurrency implementations I've seen, and yet the language sucks in so many other ways. (No exceptions? Really?)
And yes I know about Stackless Python, I gave up on that when its suggested way to reimplement sleep() was to use a spinlock. <_<
Agreed that Python's a little too good :)

I guess with Go they were intending to improve upon C/C++ in areas that mattered to Google (concurrency), not create a higher-level language.
Is stackless really that bad? I never used it, haven't had much use for concurrency so far so I haven't looked into available options. I assumed since Eve Online used it that it's at least viable, if not the best approach... are there any other decent alternatives?
 
Last edited by a moderator:
rabidpoobear said:
I guess with Go they were intending to improve upon C/C++ in areas that mattered to Google (concurrency), not create a higher-level language.
Is stackless really that bad? I never used it, haven't had much use for concurrency so far so I haven't looked into available options. I assumed since Eve Online used it that it's at least viable, if not the best approach... are there any other decent alternatives?
Thing is, their standard coding practice was to include an error return variable (it has multiple returns, like returning tuples in python) in every function. They might as well have just made it automatic (maybe overridable) and integrated it as a language feature.

Stackless isn't terrible for what it was intended for, but I was coming from Go and expecting full concurrent I/O handling (which I never actually got to work in Go, but I'm pretty sure it was supposed to, it handled sleep() alright anyway). Apparently Eve extended it to do I/O properly IIRC, so that could be why they didn't have any trouble.

The tasklets/channels model is really nice to work with though, even without I/O. :)
Tasklets are lightweight enough that you can just throw them around without worrying about memory or task switching constraints.
The example they give is a prime sieve made of a chain of tasklets, with a new section added for each new prime number that comes out of the end.
 
Last edited by a moderator:
Just wanted to jump in and attest to Dive's unfriendliness towards somebody trying to learn the language. I picked it up a three years ago and got about half-way into it before giving up. It was a really confusing book that made it look like python was confusing and hard to write. I didn't really touch it for a while after that. This summer, I wanted to give Python another try, so I read through the book that I posted. Actually, I just skimmed it because the layout was conducive to that, and I had enough background knowledge to skip the beginners explanations of everything. But that book got me up and writing python code in a week.

I'm not sure if I will use PyGame for my next game, but I think that I will probably use it for the next big game that I do on the pandora (after I finish Reign of Brains). But I'd also like to try writing an HTML5 game. PyGame was really pleasant to use, it doesn't get in your way. There were maybe a couple of things that it took me a bit to figure out, but once I had those, writing everything else was very straightforward.

@rabidpoobear I interviewed for a Python job at this company a few weeks ago: http://www.a-bb.net . The interview didn't go that well, so I didn't get the position. It's an all python shop, about 10 people and they do initial hires on a contract basis. It's also in Boston.
 
Back
Top