Introduction to Pygame Basics — Part 1 | Daily Python #20

Ajinkya Sonawane
Daily Python
Published in
4 min readJan 26, 2020

This article is an introduction to the Pygame module basics — Image load, movement, and key presses.

Requirements:

  1. Python 3.0
  2. Pip

Install the following packages:

  1. pygame — Library for making multimedia applications like games
pip install pygame

What is Pygame?

is a Free and Open Source python programming language library for making multimedia applications like games built on top of the excellent SDL library. Like SDL, pygame is highly portable and runs on nearly every platform and operating system. Millions of people have downloaded pygame itself, which is a whole lot of bits flying across the interwebs.

Is Python suitable for gaming?

It depends on the game. Python is actually quite capable of running games. It will likely even surprise you how much is possible in under 30 milliseconds. Still, it is not hard to reach the ceiling once your game begins to get more complex. Any game running in realtime will be making full use of the computer.

Over the past several years there has been an interesting trend in game development, the move towards higher-level languages. Usually, a game is split into two major parts. The game engine, which must be as fast as possible, and the game logic, which makes the engine actually do something. It wasn’t long ago when the engine of a game was written in assembly, with portions are written in C. Nowadays, C has moved to the game engine, while often the game itself is written in higher-level scripting languages. Games like Quake3 and Unreal run these scripts as portable bytecode. More recently, Python has been used in a variety of games like Freedom Force, and Humungous’ Backyard Sports Series.

Pygame is fairly low-level when it comes to writing games. You’ll quickly find yourself needing to wrap common functions into your own game environment. The great thing about this is there is nothing inside pygame to get in your way. Your program is in full control of everything. The side effect of that is you will find yourself borrowing a lot of code to get a more advanced framework put together. You’ll need a better understanding of what you are doing.

Source: pygame.org

Article’s Goal: Ball Control using Arrow Keys

Ball Control using Arrow Keys

Let’s import the required pygame module and initialize it

import pygame
pygame.init()

Let’s store some required measurements in variables

size = width,height = 500,500
color = (0,0,0)
vel = [2,2]
title = "First Py-Game"

Initialize the window/screen which will run the game

window = pygame.display.set_mode(size)
pygame.display.set_caption(title)

Download a ball icon with a transparent background

Import the ball and map it with a pygame’s rectangle object

ball = pygame.image.load("ball.png")
ballRect = ball.get_rect()

pygame.image.load(“ball.png”) — function returns us a Surface with the ball data. The Surface will keep any color-key or alpha transparency from the file. After loading the ball image we create a variable named ‘ballRect’. Pygame comes with a convenient utility object type named Rect, which represents a rectangular area.

Now we define an infinite loop which will keep the window open till we don’t explicitly exit

run = True
while run:
#Reset the pos
pos = 0,0
pygame.time.delay(100)
print(pygame.event.get())
#Handle Arrow Key event
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
pos = -10,0
if keys[pygame.K_RIGHT]:
pos = 10,0
if keys[pygame.K_UP]:
pos = 0,-10
if keys[pygame.K_DOWN]:
pos = 0,10
#Break the loop when ESCAPE is pressed
if keys[pygame.K_ESCAPE]:
run = False
#Move the rectangle
ballRect = ballRect.move(pos)
window.fill(color)
window.blit(ball,ballRect)
pygame.display.flip()
pygame.quit()

This article is a basic introduction to pygame and does not consider the boundaries where the ball can be moved. Boundaries and obstacles will be covered in future articles.

I hope this article was helpful, do leave some claps if you liked it.

Follow the Daily Python Challenge here:

--

--