• Advertisement
Sign in to follow this  

pygame examples

This topic is 3344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys I finally feel that Im ready to begin learning pygame. I am very comfortable with python and Tkinter. I had a python class this entire semester that added to all of the knowledge I gained through my own studies. Im using the book "Beginning Game Development with Python and Pygame". Its an awesome book and its helping me a lot. Do you guys know where I can find some example game source code to learn from and possibly get ideas for my own practice games? Just 2d for now because Im nowhere near opengl yet.

Share this post


Link to post
Share on other sites
Advertisement
I wrote a beginners tutorial called pygame easy as py not long ago that should be around here somewhere.
Lemme know if it works out for you.
But yes as you can see from my tutorial and the comments,etc it's a lot easier going(motivation) when you have an end goal in mind(in my case a sid meiers pirates clone) instead of just checking out other people's code and not learning as much.


[Edited by - daviangel on December 20, 2008 4:05:29 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
Do you guys know where I can find some example game source code to learn from and possibly get ideas for my own practice games?

You're doing it wrong.

I'm sure you have ideas, even if it's just incremental changes on games you've played. I'm also sure you can think of simple games that would at least be exercise to implement - Pong, Tetris, Connect4, Solitaire, Minesweeper, Nemesis... The beauty of Python and PyGame is that they take care of a lot of complex details so you can just focus on getting your game working. Instead of looking for code to essentially copy from, I want you to try to solve the problems on your own, then turn to other people's code and this forum for help if you get stuck.

You can do it, so do it.

Share this post


Link to post
Share on other sites
I love how that "tutorial" has copyrighted code that is essentially taken right out of "The L Line: The Express Line to Learning" by Andy Harris. Especially the IDEA and ALTER concepts...

Share this post


Link to post
Share on other sites
thanks alot guys. My goal as of now is to make a diablo copy. Starting small with one or 2 character types and maybe a map or two. Then expanding and making it bigger and bigger. I know I need to make some smaller 2d games first but thats my goal as of now.

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
thanks alot guys. My goal as of now is to make a diablo copy. Starting small with one or 2 character types and maybe a map or two. Then expanding and making it bigger and bigger. I know I need to make some smaller 2d games first but thats my goal as of now.

That's a good, healthy goal. I've been thinking about making a Gauntlet style game myself, just for fun. Keep us informed; maybe we'll swap notes and war stories. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by Esys
I love how that "tutorial" has copyrighted code that is essentially taken right out of "The L Line: The Express Line to Learning" by Andy Harris. Especially the IDEA and ALTER concepts...

Yes excellent book I recommend it all the time;)
Glad I'm not the only one that's actually read it.
And yeah after seeing how easy it is to use pygame using the IDEA and ALTER concepts you wonder why someone didn't think of it before or at least putting it in a book so help other new pygame programmers?

p.s. the pygame easy as py pun was all my idea though so give credit where it's due!

Share this post


Link to post
Share on other sites
ya thanks I think its a decent goal... not too farfetched. Im pretty sure I know what skills I would need to create such a game. Some pre rendered 3d models, some sprites, textures, good programming skills, pyopengl knowledge, etc. Only thing I have absolutely no idea about is creating levels.. like the actual maps. I guess Id use some sort of isometric level editor correct?

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
Only thing I have absolutely no idea about is creating levels.. like the actual maps. I guess I'd use some sort of isometric level editor correct?

That's really up to you. An isometric tiles game basically requires that you have map data that defines the level layout, with different values/symbols/definitions for each tile that indicates whether it's a barrier or a passable surface, and what kind of barrier or surface, etc. For relatively simple setups, the data can be as simple as an ASCII text file where each character represents a different type of tile. In that case, your level editor can be Notepad, or you can write an application to help you graphically place the tiles.

You can use an existing level editor, but then you have to make your game conform to the level data definition used by that editor. It's a tradeoff of features vs development effort.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by biggjoee5790
Only thing I have absolutely no idea about is creating levels.. like the actual maps. I guess I'd use some sort of isometric level editor correct?

That's really up to you. An isometric tiles game basically requires that you have map data that defines the level layout, with different values/symbols/definitions for each tile that indicates whether it's a barrier or a passable surface, and what kind of barrier or surface, etc. For relatively simple setups, the data can be as simple as an ASCII text file where each character represents a different type of tile. In that case, your level editor can be Notepad, or you can write an application to help you graphically place the tiles.

You can use an existing level editor, but then you have to make your game conform to the level data definition used by that editor. It's a tradeoff of features vs development effort.


Ok so let me understand this better. Lets say you were making a diablo copy.. or any isometric rpg for that matter. How would YOU yourself go about creating the levels for it? lets say your using python, pygame, and pyopengl.

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
Ok so let me understand this better. Lets say you were making a diablo copy.. or any isometric rpg for that matter. How would YOU yourself go about creating the levels for it? lets say your using python, pygame, and pyopengl.

Scrolling Games. It's not complete yet - ironically, the next segment I need to write is precisely on tiling in isometric/axonometric views. The data layout is the same, though, for the simple case: plaintext in rows with each character representing a different tile.

When you start looking for more elaborate objects like entrances to buildings/caverns, and blended tile modes, you need a more complex tiling system. I've never really bothered to think about that; for the most part, I focus on demonstrating the "90% solution" using simple techniques. Plus, I'm a fan of sports games [smile]

Share this post


Link to post
Share on other sites
Ok so you wouldnt use a level editor you would actually create the level with code in python.. moving the background with key strokes. Im guessing diablo was created in a diffrent fashion? with an actual level creator software of some sort?

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
Ok so you wouldnt use a level editor you would actually create the level with code in python.. moving the background with key strokes.

You're not getting it.

A "level" is just data, initially external to the program, which is used to create runtime objects in the language of your choice. It's important to note that in languages like Python the distinction between code and data is quite fluid, but it's still a question of reading in data, creating (or recreating) runtime objects from the data, and then manipulating it at runtime based on user input.

The sample in the Scrolling Games article uses a simple ASCII text file to define the level, with an integer representing each tile. (For simplicity's sake, I had the names of the image files containing the tiles match the data identifier - tile 1 is in file 1.jpg - but the mapping can be as arbitrary and complex as you like.) Remember, the article states that the code is illustrative, not production-quality.

Level editors just create data, like Maya creates data, or Photoshop creates data, or ProTools creates data. The data is stored in different formats, so your application has to know how to load it, but it's up to your application how you represent it at runtime and how you manipulate it once you have.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by biggjoee5790
Ok so you wouldnt use a level editor you would actually create the level with code in python.. moving the background with key strokes.

You're not getting it.

A "level" is just data, initially external to the program, which is used to create runtime objects in the language of your choice. It's important to note that in languages like Python the distinction between code and data is quite fluid, but it's still a question of reading in data, creating (or recreating) runtime objects from the data, and then manipulating it at runtime based on user input.

The sample in the Scrolling Games article uses a simple ASCII text file to define the level, with an integer representing each tile. (For simplicity's sake, I had the names of the image files containing the tiles match the data identifier - tile 1 is in file 1.jpg - but the mapping can be as arbitrary and complex as you like.) Remember, the article states that the code is illustrative, not production-quality.

Level editors just create data, like Maya creates data, or Photoshop creates data, or ProTools creates data. The data is stored in different formats, so your application has to know how to load it, but it's up to your application how you represent it at runtime and how you manipulate it once you have.


You're right.. I wasnt getting it.. haha. Now I gotcha. So a Level editor software is just like say.. 3DS Max. Where in 3ds max you only create the model.. but your program must import it and use it correctly.. a level editor just creates an area for your characters or whatever to move on, and your program must import it and have the objects in the game interact with the level accordingly? So a level editor does nothing more than create the level.. there are no scripts or functions in a level editor that interact with your code?

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
You're right.. I wasnt getting it.. haha. Now I gotcha. So a Level editor software is just like say.. 3DS Max. Where in 3ds max you only create the model.. but your program must import it and use it correctly.. a level editor just creates an area for your characters or whatever to move on, and your program must import it and have the objects in the game interact with the level accordingly? So a level editor does nothing more than create the level.. there are no scripts or functions in a level editor that interact with your code?



You can also have other things your editor can do to "interact with your code" such as adding triggers. Usually triggers are implemented when certain conditions are met (player enters zone, certain number of kills met, certain task achieved) that cause scripted events to happen in your game. You could implement all this by using a scripting language (I suppose Python could be used as the scripting language for the game ;)) that would define code external to the actual exe.

From experience I am making a game where I have animation triggers built into my level editor. On certain events I can trigger certain character animations using my trigger system.

For a really nice (but sorta old I guess) example of triggers I would check out Starcraft and the map editing program that goes with it. I suggest Starcraft because the editor comes with the game and the game is 2d in nature.

I would assume triggers and scripts would be quite useful in an RPG style game where you have various quests. You could then script a zone where if entered you trigger a cut scene, seal off the exit and unleash a horde of monsters for instance.

Of course you have a lot of other wonderful stuff to deal with such as isometric layering and if your doing a click to move style game you have to mess with A* path finding as well. Diablo (I assume a single player version) would be hard to make so you have your work cut out for you.

Anyway for now just focus on getting your tiles and scrolling working. Don't worry about triggers for now. In the future you might also want to consider using a structure for each tile.

Good luck!

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
So a level editor does nothing more than create the level.. there are no scripts or functions in a level editor that interact with your code?

Scripts and functions are a form of data, too. A script is (usually) a block of text that is meaningful when interpreted by a particular parser. There's no reason your level editor can't create scripts and embed triggers in the level, as shadowisadog pointed out.

Depending on how closely you want to marry your game engine (an engine is a pre-integrated collection of objects and function calls prepared to make certain types of games) to your level editor, you can even expose objects, methods and functions from the engine in the editor, which can then indicate them as "responders" to events within the level.

Languages like Python happen to be introspective and reflective - they are capable of examining the language properties of their own objects as well as generating new objects at runtime that interact with those defined at compile time. In simpler terms, you could conceivably put Python source code in your level editor and then have your game engine run that code in response to certain evens (using eval()) - though you should take steps to sanitize/verify/validate the code before you execute it!

The key point here is that in dynamic languages - Perl, Python, PHP, Ruby, ActionScript, JavaQcript, LISP, etc - code and data are interchangeable.

Share this post


Link to post
Share on other sites
Thanks a lot you guys. That was very helpful. Ive messed around with starcraft type level editors before so I see what your saying. And ya a single player diablo copy will be quite an undertaking, but Ive been wanting to do it for as long as I can remember. Im going to make a bunch of smaller simpler games first to get better. Thanks again guys.

Share this post


Link to post
Share on other sites
Im curious as to how I would go about creating a GUI within my game. Do I use Tkinter within pygame or do I use the font module in pygame to perform this? For instance to make a main menu for a game, or an inventory for a character... etc

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
Im curious as to how I would go about creating a GUI within my game. Do I use Tkinter within pygame or do I use the font module in pygame to perform this? For instance to make a main menu for a game, or an inventory for a character... etc

You're going to need to draw your GUI elements manually. Tkinter won't render on a PyGame surface. There are a couple of PyGame GUI libraries out there, too, but still no definitive solution, in my opinion.

Share this post


Link to post
Share on other sites
eshh really.. Well ok lets say I was going to make a simple menu in a game.. like a GUI that had choices like new game, difficulty, quit, high scores, etc that popped up when the game started and when you pause it. Would I have to draw the polygon to hold the choices and then put the choices inside of it? Is there a pygame class that has methods that act like a button or a radio buttion in Tkinter?

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
eshh really.. Well ok lets say I was going to make a simple menu in a game.. like a GUI that had choices like new game, difficulty, quit, high scores, etc that popped up when the game started and when you pause it. Would I have to draw the polygon to hold the choices and then put the choices inside of it?

PyGame is a 2D API, so it doesn't draw polygons per se. It draws rectangles. Or images. You can load a bunch of images for each of your menu options, then draw them in the appropriate positions:
(the following is just a code fragment, not a complete program!)
menuNewGame = image.load(newGameMenuImage)
menuDifficulty = image.load(difficultyMenuImage)
menuHighScore = image.load(highScoreImage)
menuQuit = image.load(quitImage)

menu = [menuNewGame, menuDifficulty, menuHighScore, menuQuit]
# create menu image positions as a list rects
menu = zip(menu, rect)
selected = 0

for evt in event.get():
if evt.type == REDRAW:
for i, (m, r) in enumerate(menu):
if i == selected:
screen.fill(menuHighlightColor, r.inflate(5, 5))
screen.blit(m, r)

elif evt.type == KEYDOWN:
if evt.key == K_UP:
selected -= 1
selected %= len(menu)
elif evt.key == K_DOWN:
selected += 1
selected %= len(menu)
elif evt.key == K_RETURN:
# use selected to determine what event to generate or function to call



Quote:
Is there a pygame class that has methods that act like a button or a radio buttion in Tkinter?

No. You'll need to write your own. It's not hard, just tedious.

Share this post


Link to post
Share on other sites
ahh ok I gotcha now. So I would have to create an area that would act as a button, and have it respond by getting a mouse click event that occurs only within the area the "button" covers? Thats what Im understanding. If thats the case is there a way to have the "button" be raised or lowered? like when you click it have the button go in and come out when you release.. like in Tkinter?

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
If thats the case is there a way to have the "button" be raised or lowered? like when you click it have the button go in and come out when you release.. like in Tkinter?

Yes. It's called hit testing, and it's a bunch of inconvenience to implement manually, especially if you want it to be as robust as regular GUI toolkits. You need to receive the mousedown event, check to see if the mousepoint is within the rectangle of any of your buttons and then set its state to depressed - but not fire the click event until the button in subsequently released. That allows you to move your mousepoint out of the button while holding the mouse button down and avoid dispatching the event connected to the button.

(As usual, the following is just a code fragment, not a complete program!)


# we use this to check, quickly, if the mouse was depressed over a button by
# storing a reference to the button
mouseInButton = 0

if evt.type == MOUSEBUTTONDOWN:
if evt.button == 1: # left mouse button
# evt.pos is a tuple representing the x,y coordinate of the mouse cursor
# you need to test it against your various buttons
for b in buttons:
r = b.get_rect()
if r.collidepoint(evt.pos):
b.state = BUTTONSTATE_PRESSED
mouseInButton = b

elif evt.type == MOUSEMOTION:
if mouseInButton:
r = mouseInButton.get_rect()
if r.collidepoint(evt.pos):
mouseInButton.state = BUTTONSTATE_PRESSED
elif:
mouseInButton.state = BUTTONSTATE_RELEASED

elif evt.type == MOUSEBUTTONUP:
if mouseInButton:
r = mouseInButton.get_rect()
# now we test to see if the mouse button is still within the button's area
if r.collidepoint(evt.pos):
b = mouseInButton
mouseInButton = 0
b.dispatch()

Share this post


Link to post
Share on other sites
thanks so much Oluseyi .. youve really been a huge help. So GUIs in pygame are pretty tedious from what I can tell. Ill have to practice a lot. Im looking to eventually make a small action rpg with an isometric style level. RPGs are full of menus and GUIs.. skill trees, inventories, character profiles, etc.. so I wanted to make sure there was a clear way to implement these things in pygame, thanks alot man. I appreciate it

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement