Skip to main content
GameDev.net gamedev.net
🔒 Locked

pygame examples

Started by biggjoee5790 Dec 20, 2008 at 3:50 PM 25 replies 13.8k views
Original Post
biggjoee5790
biggjoee5790
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.
daviangel
daviangel
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]
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Oluseyi
Oluseyi
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.
Esys
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...
biggjoee5790
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.
Oluseyi
Oluseyi
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]
daviangel
daviangel
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!
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
biggjoee5790
biggjoee5790
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?
Oluseyi
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.
biggjoee5790
biggjoee5790
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.
Oluseyi
Oluseyi
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]
biggjoee5790
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. Im guessing diablo was created in a diffrent fashion? with an actual level creator software of some sort?
Oluseyi
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.
biggjoee5790
biggjoee5790
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?
shadowisadog
shadowisadog
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!
Oluseyi
Oluseyi
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.
biggjoee5790
biggjoee5790
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.
biggjoee5790
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
Oluseyi
Oluseyi
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.