Program in Python With Pygame

This article was provided by wikiHow, a wiki building the world's largest, highest quality how-to manual. Please edit this article and find author credits at the original wikiHow article on how to program in python with pygame. Content on wikiHow can be shared under a Creative Commons License.

Grade C Views 134
Last edited 2 months ago

In this article we will attempt to teach you to become a "programmer", not a "Python programmer". This article will be looking at the ideas behind the code, instead of the code itself.

Step 1  

Download Python from python.org.

Step 2  

Download Pygame from pygame.org.

Step 3  

Install both Python and Pygame in the same order as they were mentioned.

Step 4  

Open up IDLE (say it like a car idles)

Step 5  

Go to File>New Window.

Step 6  

In the new window type this, feel free to add any "blank:" fields.

Step 7  

"""[Your Title].py.

Step 8  

Author: [your name]

Step 9  

Description: [Description Here]

Step 10  

"""

Step 11  

(Note: There is no blank line in between the first line and the "Author" line)

Step 12  

In order to save time loading Python only loads Pygame when it is told to load Pygame. This is standard to most programming languages. So our next step is to import Pygame.

Step 13  

import pygame.

Step 14  

Our next step is to initialize Pygame. It take Pygame and loads it into memory for you to reference in your program. It's almost like a contractor who brings all his materials to the building site, and then builds the house (only in this case, you don't have an option). The () marks and the end of the code "pass" information to Pygame, this will come up a lot so don't leave it out. Think of the passing like telling a contractor to bring materials, but not telling him what materials to bring. You don't have to put anything in the () here, but you will in the future.

Step 15  

pygame.init()

Step 16  

For our first example we will just display a colored screen, this is where it gets technical, so if you lose me, just reread the section.

Step 17  

This is the code for what we will be doing.

Step 18  

""" helloWorld.py.

Step 19  

Author: Dan Grahn.

Step 20  

Description: My Hello World program.

Step 21  

"""

Step 22  

import pygame.

Step 23  

pygame.init()

Step 24  

screen = pygame.display.set_mode((640,480))

Step 25  

pygame.display.set_caption("Hello, world!")

Step 26  

background = pygame.Surface(screen.get_size())

Step 27  

background = background.convert()

Step 28  

background.fill((44,255,44))

Step 29  

clock = pygame.time.Clock()

Step 30  

keepGoing = True.

Step 31  

while keepGoing:

Step 32  

clock.tick(30)

Step 33  

for event in pygame.event.get():

Step 34  

if event.type == pygame.QUIT:

Step 35  

keepGoing = False.

Step 36  

screen.blit(background, (0,0))

Step 37  

pygame.display.flip()

Step 38  

If you want to, you can paste this into IDLE and save it on your computer. Then double-clicking on the file will run the program.

Step 39  

This is where we define what the screen is going to look like. In most cases you want the screen to be 640x480, which is just about the right size. In this code we are saying that we want the "variable" (something that can contain information) to "get" (not equals, but gets) pygame.display.set_mode((640,480)).

Step 40  

The "pygame" part says that we are referencing "pygame" which we importer earlier. The ".display" part says that we are referencing withing pygame "display". Also, the ".set_mode" says that we now want to set the screen size and mode. Remember the concept of "passing"? If you don't read step 8. In this set of () we are passing pygame the resolution (the same as the size for now) of the screen. Specifically we are telling it to set the resolution to (640,480). Now you may be asking why there is another set of () inside of the ones we already had. The reason is that instead of passing pygame two numbers we are passing it a "tuple" (essentially a list, but not the same) containing two numbers. In fact, all the () that we have seen so far are "tuples" I just haven't told you so.

Step 41  

screen = pygame.display.set_mode((640,480))

Step 42  

pygame.display.set_caption("Hello, world!")

Step 43  

background = pygame.Surface(screen.get_size())

Step 44  

background = background.convert()

Step 45  

background.fill((44,255,44))

Step 46  

clock = pygame.time.Clock()

Step 47  

keepGoing = True.

Step 48  

while keepGoing:

Step 49  

clock.tick(30)

Step 50  

for event in pygame.event.get():

Step 51  

if event.type == pygame.QUIT:

Step 52  

keepGoing = False.

Step 53  

screen.blit(background, (0,0))

Step 54  

pygame.display.flip()

Via wikihow

$44K Street Legal Wooden SuperCar

Beautifully constructed wooden SuperCar Maniwa manufactured by Sada-Kenbi. The wooden Japanese car goes for $44,000. Wow. That's a lot for a toy. However, this baby is street legal and can go up to ...

Stomach Churning Taxidermy Fashion

Uh...yuck. Fun, but definitely yuck. Terrifying taxidermied jewelry and accessories by Reid Peppard. That's right, dead rodent cufflinks, change purses, bowties, bracelets... and a guinea pig hair ...

Eat Your Way Through the Periodic Table (Yum! Cupcakes)

Science geeks behold, the perfect dessert. The cupcake Periodic Table. Pretty simple to do-it-yourself. Start with basic cupcakes in a square mold and decorate! Previously, Knit the Periodic Table ...

Freehand Circle Drawing World Champion

Wait for it, wait for it... it happens really fast... at right about 1:05. Okay, maybe he's not a world champion, but a very cool party trick nonetheless. Here's one way to do it (not quite as perfect ...

Launch Cars Off Cliffs For Fun

More redneck fun. Bob Moravitz plus family and friends have created an 8 hour event of propelling vehicles off cliffs. Held yearly, admission is free but donations are encouraged (to cover insurance ...

loading...