Creating Games with Pygame

A tutorial explaining how to use Pygame, a wrapper built on Python and SDL, for game development and improved execution.
______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Seems Python really sucks for

Anonymous's picture

Seems Python really sucks for programming! This is the forth piece of code I've copied to try and run and it came up with errors etc like the other posters stated. Its either platform dependent, ie Linux stuff, or it has things that need to be installed to make a game or program to run. This makes it extremely frustrating for anyone new to programming to actually get anything other than hello world to run!

Interestingly I've not had this problem with any of the java listings I've found, so it looks like I'll be going the Java route after all.

You shouldn't be trying to do

Anonymous's picture

You shouldn't be trying to do anything with graphics unless you are comfortable with a language, you need to have all the basics in order to understand what you are doing.

Python is way simpler than Java to learn, btw. Just get a book if you have trouble googling for tutorials.

Hey

Karanveer's picture

Great stuff! Python is an awesome language to program in, and pygame is very good to beginners. I've started my own set of tutorials on my site : http://lameness-prevails.com/
Check it out and let me know if you like it!

Re: Creating Games with Pygame

Anonymous's picture

Thanks for the article! It's really helpful.

NameError: name 'main' is not defined

nitrofurano01's picture

Traceback (most recent call last):
File "Pygame_LinuxJournalExample.py", line 23, in
if __name__ == '__main__': main()
NameError: name 'main' is not defined

this appeared after trying to fix lots of indentation errors...

complex, difficult and confusing

nitrofurano01's picture

For newbies, this article is complex, difficult and confusing - and plenty of redundancies...
Would be much more easier to learn from minimal examples.
I have some similar minimal examples at:
http://nitrofurano.linuxkafe.com/python
http://nitrofurano.linuxkafe.com/sdlbasic
http://nitrofurano.linuxkafe.com/wxbasic

FTP Link

Anonymous's picture

Hi,

The file is here:
ftp://ftp.ssc.com/pub/lj/listings/Web/7694.tar.gz
("Web", not "WEB")

You could have found it yourself on the ftp server... ;-)

Dead Link

Anonymous's picture

Hi people,

Looks like the link is dead. Can someone else give it a try?

ftp://ftp.ssc.com/pub/lj/listings/Web/7694.tar.gz

Thanks,

Jim

The link

Bob's picture

The link still doesn't work with that 'Web'.

Re: Creating Games with Pygame

Anonymous's picture

Actually the link to the tarball is STILL not working- can anyone get a hold of it and mail it to mr981505@netscape.net

ta.

Re: Creating Games with Pygame

Anonymous's picture

Excellent.

Now, if we could have a similar article detailing a simple database app (PostgreSQL, MySQL or similar as the back end) with a gui front end I would seriously consider spending time on Python.

Re: Creating Games with Pygame

Anonymous's picture

Database connection is really easy in python. For Dbase connectivity you can find information on http://www.python.org/topics/database/. With the DB API 2.0, there is actually not much difference in connecting to a mysql, or a postgresql database.
For gui, you have a number of choices - wxpython, tk, gtk just to name a few. You can find info on this on www.wxpython.org . If you prefer tk, you can find info on that on http://www.python.org/topics/tkinter/.

Re: Creating Games with Pygame

Anonymous's picture

Instead of waiting for next article look at http://www.pygtk.org/pygtk2tutorial/index.html

Greetings
kamyl

Re: Creating Games with Pygame

Squallbr's picture

Very nice tutorial, i've never programmed in Python and I was able to understand a lot and even find some problems
This is mostly logic but...

in the load_sound method there's a mistake
fullname = os.path.join('data', name)
if os.path.exists(full_name) == False: # Wrong
sound = pygame.mixer.Sound(fullname)
else:
print 'File does not exist:', fullname
return No_Sound
Change the mistake for
if os.path.exists(full_name) == True:

and then it'll work :) if it exists it should be played and not the oposite :)

Congratulations for this nice tutorial

Re: Creating Games with Pygame

Anonymous's picture

Also the author confuses 'fullname' and 'full_name'.

This article is very poorly written.

Re: Creating Games with Pygame

Anonymous's picture

There is no error in the actual source file

Re: Creating Games with Pygame

Anonymous's picture

That the tutorial deviates from the source it's explaining is another serious problem.

program coding ex. color

gina alontaga's picture

how to create it using the ,

Re: Creating Games with Pygame

Anonymous's picture

The programming in the tutorial certainly had some problems, but the introduction to the topic was top notch. I was introduced to an aspect of python programming (well, programming in general) I had never been exposed to in such a way that I felt I had a handle on the basics. Sure, the sample code could have been editted better, but the tutorial served it's purpose in introducing a new package and showing off the capabilities.

Re: Creating Games with Pygame

Anonymous's picture

Even better would be just "if os.path.exists(fullname):"; usually in Python there's no guarantee that the result will be True or False, just that the result will evaluate to true or false in a boolean context (which True and False happen to do, but so do 1 and 0, or 18 and None...)

Re: Creating Games with Pygame

Anonymous's picture

Just a few nitpicks from a Pygame hacker...

RenderClear is "going away" in Pygame 1.7, coming in a few days. Group, RenderPlain, and RenderClear are now all the same group. The fourth one, RenderUpdates (not mentioned in the article), is still separate and usually gives better performance when used properly (and doesn't require double buffering).

You mention that pygame.time.delay with a constant value might be too long for slower machines; that's what pygame.time.Clock is for, you can specify an FPS to try and maintain, and it will calculate the correct time to sleep.

The call to random.seed() is not necessary; Python automatically seeds the RNG when it is not done explicitly.

Resetting the screen mode can be avoided by calling pygame.quit() at the end of the program. This shuts down all the SDL systems, including the display.

"if enemyship_sprites.sprites() == []:" is unsafe Python code in general. You don't know if sprites() is going to return a list, tuple, iterator, generator... Properly it should be written as "if len(enemyship_sprites.sprites()) == 0:", but you can also write it as "if not enemyship_sprites:" (I don't like that version). Pygame 1.7 you can just write "if len(enemyship_sprites) == 0:" which is probably the best way in the future.

-- Joe Wreschnig

not bad

Anonymous's picture

Not bad but I've seen better tutorials on pygame. Anyway, good python coder can do all of available tutorials in a week... Not much of it so good work!

--
Regards
Nazgob
www.nazgob.com

Re: Creating Games with Pygame

Anonymous's picture

The link to the tar ball on the LJ FTP site has been fixed.--LJ Editorial

Uh... no. The link is still b

Anonymous's picture

Uh... no. The link is still broken. However, the user comment above with the correct URL does work.

Re: Creating Games with Pygame

mclazarus's picture

The link to the source code and image files gives an ftp error:
"550 Can't chagne director to /pub/lj/listings/WEB/7694.tar.gz: No such file or directory"

Otherwise looks like it could be fun to play around with.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix