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 117
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

Tattoo Yourself (or Your Girlfriend)

Henna. Beautiful, fun, exotic... and best of all... Do-It-Yourself (with a little practice). Also, unlike tattoos, it's temporary. Henna generally lasts for 1-3 weeks. An ancient tradition, henna is ...

The Mother of all Ping Pong Guns - LETHAL

The lethal pneumatic ping pong gun. This thing isn't exactly wife approved. Creator Ron Kessinger built this mighty powerful ping pong launcher - "a variation on a potato cannon, built to run on ...

Old Bumper Cars Go Street Legal

Flickr user MR38 has posted a set of photos of bumper cars made street legal, as displayed at the annual Cruisin’ Grand festival in Escondido, California. This mini cars were retrofitted with 750 cc ...

Human Powered Ferris Wheel

According to Google's (albeit rough) translation from French to English: "A big wheel in India that does not work with an engine but using human power. Men throw themselves in front of the wheel ...

Make-It-Yourself LEGO Gummies

Turkey day is over, and you have this nice Friday-Saturday-Sunday stretch before it's back the daily grind. Here's a project that inspires both young and old: LeGummies brick shaped gummy candies ...

loading...