Original Post
OK I'm thinking of starting my own Python/Pygame tutorial in this thread in part to help myself and others who are currently working with Python/Pygame. This came about due to the fact that I finally found a language/graphics API I can make 2D games in a reasonable amount of time and I hope to show that here. Also, it's an attempt to weed out the bugs in my personal pygame engine I dubbed gameEngine and you can see the bug here in this thread. So I'm gonna start all over from scratch without using my custom gameEngine and build up my Pirates clone in python/pgame. That was my main motivation was for learning to program in the first place to make a 2D pirates clone that I could customize to my taste. Also, by posting my progress here I will be able to keep track of how long it takes me since I will be adding little by little as I can find the time to do so. Anyways, lets get started and if anyone wants to add anything feel free to! First you need to have Python and pygame installed for your platform. I'm using Python 2.5.1 and pygame 1.7.1. I don't know if there is a 64bit version of pygame or how hard it would be to compile it from source so all this is on 32bit WinXP but if anyone knows about 64bit pygame support let me know. Now that we have Python and pygame installed the first thing you should do is make sure they are installed and working so do this: 1. Get into Python by using the command line or IDLE to get to the Python interactive shell. 2. Use the following command to import pygame into Python import pygame If pygame is installed correctly nothing will happen. If you get an error message on the other hand you pygame is not installed correctly and you'll need to fix it before you do anything else Pygame helps you easily create game in Python by providing the following: Display- a graphical display Surface- a rectangular area that contains an image Rect- used to determine size of objects and to detect whether 2 objects collide Input modules- lets the user interact with pygame using a mouse,keyboard or joystick Output modules- provides ways to send information back to the user includes audio output for background music and sound effects Utility modules- more modules to help make game programming easier like transformation functions that let you scale and rotate images, manage fonts, working with time I will use the IDEA/ALTER(dibbs to Andy Harris for this) acronoyms as a simple way to remeber all the details that go into getting a game started. Here's what it stands for: I-Import and Initialize D-Display E-Entitites A-Action A-Assign values L-Loop T-Time E-Events R-Refresh screen It's alot to remeber but onece you understand this basic framework you can use it for every game you write. Now I'm gonna break down the above even more so it makes more sense: I-Import and Initialize 1.Be sure pygame is installed on machine 2.Tell Python you gonna use pygame library import pygame 3.Initialize pygame pyame.init() D-Display 1.Create variable to represent the display screen 2.Call pygame.display.set_mode() function 3.Determine the size or resolution of screen 4.Specify any other parameters 5.Set up screen caption with display.set_caption() E-Entitites(things moving about on screen) 1.Create a variable called background to hold the background 2.Use pygame.Surface() to build a Surface object background = pygame.Surface(screen.get_size()) 3.Make the background the same size as the display 4.Convert the background to standard color format using convert() 5.Copy the converted image back to the background background = background.convert() 6.Change the color of the background background.fill((0,0,255)) # note using R,G,B values here A-Action(includes main or game loop) A-Alter Assign values to key variables 1.Create clock variable clock.pygame.time.Clock() 2.Create Boolean to manage game loop keepGoing = True L-Loop 1.Any time you want to end game set keepGoing to False while keepGoing: T-Time 1.Use tick() method to set maximum frame rate 30fps in our case clock.tick(30) E-Events 1.Get list of events that happened during this frame pygame.event.get() 2.Step through each element in the event list for event in pygame.event.get(): 3.Check the event type for now we are just looking for QUIT if event.type == pygame.QUIT: 4.Write some code for the event keepGoing = False R-Refresh screen Finally we get to see our hard work displayed to the screen in this step! To display the screen: 1.Copy the background image to screen using the screen.blit() method screen.blit(background,(0,0)) 2.Copy any foreground images to the screen using screen.blit() This means the first thing you draw will be on the bottom and successive images will appear on top 3.Flip the display - copies screen buffer to display hardware pygame.display.flip() The code for our first pygame program follows: Copy the above sourcecode into a file and save it. I saved mine as idea.py as you can see in the comments. Now run it by either double-clicking on the file and if everything is setup correctly it will automatically run. If not go to your commandline and I assume Python is in your path and just type: python idea.py You should end up with something like the following image
You can modify the following line to change the backround color background.fill((0,0,255))-will give you a blue background for example,etc : [Edited by - daviangel on July 19, 2007 8:58:20 PM]
""" idea.py
Simplest possible pygame display
Demonstrates IDEA/ALTER model
Note: Don't try to run any pygame stuff
within IDLE will lockup!
Jaime Moreno
4/2/07
"""
#I - Import and initialize
import pygame
pygame.init()
#D - Display configuration
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption("Hello, world!")
#E - Entities (just background for now)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((100,60,25))
#A - Action (broken into ALTER steps)
#A - Assign values to key variables
clock = pygame.time.Clock()
keepGoing = True
#L - Set up main loop
while keepGoing:
#T - Timer to set frame rate
clock.tick(30)
#E - Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
#R - Refresh display
screen.blit(background, (0,0))
pygame.display.flip()
# end main game loop
You can modify the following line to change the backround color background.fill((0,0,255))-will give you a blue background for example,etc : [Edited by - daviangel on July 19, 2007 8:58:20 PM]



