Kodeclik Logo

Our Programs

Learn More

Schedule

Kodeclik Blog

Pygame Tutorial

In this blogpost, we will create a simple game in pygame called “Paint” where the objective is to paint the full screen in the fewest number of moves (steps) possible. For more details about pygame, checkout our blogpost on pygame and how it works.

You can follow the below steps in your Python IDE of choice such as Visual Studio Code or with a web-based tool like repl.it.

One of the first things we will need to do is to import the pygame library and other utility functions and variables:

import pygame
from pygame.locals import *

We then initialize the screen display, set it up to be of size 600 by 400, fill it up with a white color and give it a title:

pygame.init()
screen = pygame.display.set_mode((600,200))
screen.fill((255,255,255))
pygame.display.set_caption("Painting Game")

In the above code, the values (255,255,255) refer to the (red, blue, green) components of the color and, in this case, render a white background.

We then setup some initial values of parameters:

move_step_size=5
rect_size=25
num_presses = 0

delay=30
myclock=pygame.time.Clock()

top_left_x=0
top_left_y=0

As discussed in our blogpost on pygame, the main crux of the program is a loop that checks for events and responds accordingly. First, we draw a rectangle at the top left corner and look for events. If it is a QUIT event, we will exit accordingly. If not, and it is a keypress event, we look to see if one of the arrow keys are present. If they are present, we move our rectangle accordingly which will give a painting effect across the screen. Note that we also introduce a delay as without it the user will not witness an interactive experience.

while True:
    pygame.draw.rect(screen, (255,0,0), 
      pygame.Rect(top_left_x, top_left_y, rect_size, rect_size))
    pygame.display.flip()

    for event in pygame.event.get():
        if (event.type == pygame.QUIT):
            pygame.quit()
            
    keypress = pygame.key.get_pressed()   
    if keypress[pygame.K_LEFT]:
        top_left_x -= move_step_size
        if (top_left_x < 0):
          top_left_x = 0
    if keypress[pygame.K_UP]:
        top_left_y -= move_step_size
        if (top_left_y < 0):
          top_left_y = 0
    if keypress[pygame.K_RIGHT]:
        top_left_x += move_step_size
        if (top_left_x >= xlimit-rect_size):
          top_left_x = xlimit-rect_size
    if keypress[pygame.K_DOWN]:
        top_left_y += move_step_size
        if (top_left_y >= ylimit-rect_size):
          top_left_y =ylimit-rect_size
    pygame.display.update()
    myclock.tick(delay)

As an example, here is what the output looks like while the user is painting the screen.

Note that you can press (right, up) keys simultaneously or (left, down) simultaneously and paint in a diagonal manner. In reality, these keys are being used alternately (and not really simultaneously) but this provides the necessary effect:

Interesting in more Pygame content? Checkout our Pygame FAQ. Also checkout the Kodeclik Python Game contest.

Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.