How to Program in Python With Pygame

Program in Python With Pygame 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.





[dividers style=”3″]

[notice type=”support” title=”STEP BY STEP” tag=”h4″]STEP[/notice]


Download Python.


Download Pygame.


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


Open up IDLE (say it like a car idles).


Go to File>New Window.


In the new window type this, feel free to add any “blank:” fields

  1. """[Your Title].py

  2. Author: [your name]

  3. Description: [Description Here]"""


  1. Note that there is no blank line in between the first line and the “Author” line.

  2. In order to save time loading Python only loads Pygame when it is told to load Pygame. This is standard to most programming languages.

[dividers style=”3″]

[notice type=”support” title=”Importing Pygame” tag=”h4″]Importing Pygame[/notice]


Initialize Pygame. It takes 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.

  1. pygame.init()


Code. 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.

[dividers style=”3″]

[notice type=”support” title=”The Code” tag=”h4″]The Code[/notice]

  1. """

  2. Author: Dan Grahn

  3. Description: My Hello World program

  4. """

  5. pygame.init()

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

  7. This is where we define what the screen is going to look like. In most cases you want the screen to be 640×480, 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)).

  8. 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. 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.”

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

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

  11. background = background.convert()

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

  13. clock = pygame.time.Clock()

  14. keepGoing = True

  15. while keepGoing:

  16. clock.tick(30)

  17. for event in pygame.event.get():

  18. if event.type == pygame.QUIT:

  19. keepGoing = False

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

  21. pygame.display.flip()

  22. 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.

Like it? Share it!