Before you can do anything with pygame you have to set it up. So the first thing you're program needs to do is tell python that we want to use pygame. This is done using the command import pygame on its own line. Next you need to tell pygame to set itself up. This is done by calling the init method on the pygame object, with pygame.init().
Now that you have set pygame up we want to display something on the screen. So we set up the window with screen = pygame.display.set_mode((468, 640)). This tells pygame to create a screen with a width of 468 pixels and a height of 640 pixels. If you run the program a window should flash on the screen briefly. Try changing the height and width to various values and see how it effects the resulting window.
Now we need to keep the program running after the window has opened. To do this we will create an infinite loop. It python 1 is true so if we loop while 1 it will never end. Add this to the end of the program. This loop is called the main loop. However this on its own won't work as python requires that the loop does something. So we shall add a limit to how frequently the loop executes. First we need to create a pygame clock object before the loop with clock = pygame.time.Clock(). Then we need to call the clock tick method. This method will limit the number of times we execute the loop to 60 times per second by tacking more time if we call it more frequently.
Your program should now look like this:
import pygame
pygame.init()
screen = pygame.display.set_mode((468,
640))
clock = pygame.time.Clock()
while 1:
clock.tick(60)
Now at the moment we cannot close the window with the x button in the corner. We have terminate the python program using ctrl+c in the terminal. To get this to work we have to start listening for user input. Pygame represents input as events. Events are things that have happened, for example clicking somewhere, moving the mouse or pressing a key on the keyboard. So the first thing we need to do is loop through all the events that have happened since the last time we went through the loop. We do this using a for loop and the pygame.event.get method, for event in pygame.event.get():. Then we need to check if the event.type is equal to pygame.QUIT. event.type tells us what sort of event has happened and pygame.QUIT means that we've been asked to close. Usually because the user pressed the x button in the top corner.
So the program should look like:
import pygame
pygame.init()
screen = pygame.display.set_mode((468,
640))
clock = pygame.time.Clock()
while 1:
clock.tick(60)
for
event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
This is the basic structure of a pygame program.
Now the window at the moment is a black rectangle. We want to change that to a blue. So first we need to create a surface object. This represents an area we can draw to. This takes a rect representing its size. We can get this using the get_size method on our screen object. Once we have a surface object we need to construct another background object using to convert call on the one we just created. This new one is what we will be using. The convert method creates a surface object that matches the display type. We then want to fill our background with a solid colour. We can do this with the fill method. The fill method takes three parameters, each of which takes a value between 0 and 255 which says how red, blue and green the surface should be. For example to use a blue colour use background.fill((0, 0, 250)). This on its own will not draw the object to the screen. To draw the object to the screen we have to use the screens blit method, which draws it to the frame-buffer. The framebuffer is storage area where we draw the individual objects to before we draw to the screen so we draw everything to the screen at once rather than one at a time. Then we use the flip method of the display object to draw the frame-buffer to the screen. We want to this every time we loop through the main loop. You're program should now look something like this:
import pygame
pygame.init()
screen = pygame.display.set_mode((468,
640))
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 250))
clock = pygame.time.Clock()
while 1:
clock.tick(60)
for
event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
screen.blit(background, (0, 0))
pygame.display.flip()
Now we have the basic program that handles all of the minimum requirements to get the program running. You might want to play around with some different background colours by changing the parameters to background.fill((0, 0, 250)).