Blog

Coding Exercise: Build Tetris in Python using Pygame

pygame is a Free and Open Source python programming language library for making multimedia applications like games built on top of the excellent SDL (Simple DirectMedia Layer) library. Simple DirectMedia Layer is a cross-platform development library designed to provide low-level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D. 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.

pygame.org (the website) welcomes all Python game, art, music, sound, video and multimedia projects.

 

History of Pygame

Pygame was originally written by Pete Shinners to replace PySDL after its development stalled. It has been a community project since 2000 and is released under the open source free software GNU Lesser General Public License.

 

Pygame Installation

Pygame requires Python. The best way to install pygame is with the pip tool (which is what python uses to install packages). Note, this comes with python in recent versions. We use the –user flag to tell it to install into the home directory, rather than globally.

Installation on Windows

For Windows, make sure you add Python 3.6.1 in the PATH, from there, you run the below command:

python3 -m pip install -U pygame –user

Installation on Ubuntu

For Ubuntu and many Linux distributions, they have their own Pygame package. Run the below command for these operating systems:

sudo apt-get install python3-pygame

Installation on Mac

There are issues pertaining in installation of Pygame on Mac. But it is available, if you start a virtual environment in your Mac system using the below command:

python3 m virtualenv anenv

Activate the virtual environment:

. ./anenv/bin/activate

The venvdotapp helps the python be a Mac ‘app’, so that the pygame window can get focus

python m pip install venvdotapp venvdotapp python m pip install pygame

 

 

 

Features of pygame

  • Silliness built in. Pygame is meant to make things fun. New silliness is added every 3.1415 seconds. Silliness is a fun feature added into pygame package, in order to denote the simplicity in coding a game using pygame.
  • Does not require OpenGL. With many people having broken OpenGL setups, requiring OpenGL exclusively will cut into your user base significantly. Pygame uses either opengl, directx, windib, X11, linux frame buffer, and many other different backends… including an ASCII art backend! OpenGL is often broken on linux systems, and also on windows systems – which is why professional games use multiple backends.
  • Multi-core CPUs can be used easily. With dual-core CPUs common, and 8 core CPUs cheaply available on desktop systems, making use of multi-core CPUs allows you to do more in your game. Selected pygame functions release the dreaded python GIL, which is something you can do from C code.
  • Uses optimized C, and Assembly code for core functions. C code is often 10-20 times faster than python code, and assembly code can easily be 100x or more times faster than python code.
  • Comes with many Operating systems. Just an apt-get, emerge, pkg_add, or yast install away.  No need to mess with installing it outside of your operating systems package manager. Comes with binary installers (and uninstallers) for Windows or MacOS X.
  • Truly portable. Supports Linux (pygame comes with most mainstream linux distributions), Windows (95,98,me,2000,XP,vista, 64bit windows etc), Windows CE, BeOS, MacOS, Mac OS X, FreeBSD, NetBSD, OpenBSD, BSD/OS, Solaris, IRIX, and QNX.
  • It’s Simple and easy to use. Kids and adults make games with pygame.
  • Does not require a GUI to use all functions. You can use pygame without a monitor – like if you want to use it just to process images, get joystick input, or play sounds.
  • A small amount of code. It does not have hundreds of thousands of lines of code for things you won’t use anyway. The core is kept simple, and extra things like GUI libraries, and effects are developed separately outside of pygame.
 

Pros of Pygame

  • Easy Python syntax
  • Pygame uses Python as its scripting language. Python is widely considered one of the easiest languages to grasp even for beginners.
  • Very easy to understand
  • The API is very straightforward.
  • Good canvas system
  • Pygame has a drawing system that allows the user to create and draw on an unlimited number of canvases.
 

Cons of Pygame

  • Pygame does not scale well with large project games, but it works well with small games and hobby projects. For small-scale game development, pygame is a good option.
  • You might have a hard time finding out the basic functions that you require and their workings, but most of the functions in pygame has been incorporated as small apps across. You can get github programs related to Pygame. All those functions and their descriptions will be given in their code.
  • As I have said before, as there is not much description about the functions and their uses, Pygame also lacks the Pygame community and Pygame developers. Those who develop their small-scale applications in Pygame, put up in Github, but the reliability of those developers is still doubtful
  • Most of the game that we play, has a certain amount of Physics, AI, networking and inputs involved. In Pygame, we won’t be having these complexities as it is not supported by Pygame.
  • Pygame uses an old version of SDL, hence it can be said to be outdated. Some of the features used by SDL2 are also not supported in Pygame. But still remember Old is Gold.
 

Let us build a Tetris game using Pygame

 

from random import randrange as rand

import pygame, sys

import getpass

 

# The configuration

cell_size =    18

cols =    10

rows =    22

maxfps =     30

 

colors = [

(0,   0, 0  ),

(255, 85,  85),

(100, 200, 115),

(120, 108, 245),

(255, 140, 50 ),

(50,  120, 52 ),

(146, 202, 73 ),

(150, 161, 218 ),

(35,  35, 35)

]

 

# Define the shapes of the single parts

tetris_shapes = [

   [[1, 1, 1],

    [0, 1, 0]],

   

   [[0, 2, 2],

    [2, 2, 0]],

   

   [[3, 3, 0],

    [0, 3, 3]],

   

   [[4, 0, 0],

    [4, 4, 4]],

   

   [[0, 0, 5],

    [5, 5, 5]],

   

   [[6, 6, 6, 6]],

   

   [[7, 7],

    [7, 7]]

]

 

def rotate_clockwise(shape):

   return [ [ shape[y][x]

   for y in range(len(shape)) ]

   for x in range(len(shape[0]) – 1, -1, -1) ]

 

def check_collision(board, shape, offset):

   offx, offy = offset

   for cy, row in enumerate(shape):

   for cx, cell in enumerate(row):

   try:

   if cell and board[ cy + offy ][ cx + offx ]:

   return True

   except IndexError:

   return True

   return False

 

def remove_row(board, row):

   del board[row]

   return [[0 for i in range(cols)]] + board

   

def joinmatrixes(mat1, mat2, mat2off):

   offx, offy = mat2_off

   for cy, row in enumerate(mat2):

   for cx, val in enumerate(row):

   mat1[cy+offy-1    ][cx+offx] += val

   return mat1

 

def new_board():

   board = [ [ 0 for x in range(cols) ]

   for y in range(rows) ]

   board += [[ 1 for x in range(cols)]]

   return board

 

class TetrisApp(object):

   def init(self):

   pygame.init()

   pygame.key.set_repeat(250,25)

   self.width = cell_size*(cols+6)

   self.height = cell_size*rows

   self.rlim = cell_size*cols

   self.bground_grid = [[ 8 if x%2==y%2 else 0 for x in range(cols)] for y in range(rows)]

  

   self.default_font =  pygame.font.Font(

   pygame.font.getdefaultfont(), 12)

  

   self.screen = pygame.display.set_mode((self.width, self.height))

   pygame.event.set_blocked(pygame.MOUSEMOTION) # We do not need

                                             # mouse movement

                                             # events, so we

                                             # block them.

   self.nextstone = tetrisshapes[rand(len(tetris_shapes))]

   self.init_game()

   

   def new_stone(self):

   self.stone = self.next_stone[:]

   self.nextstone = tetrisshapes[rand(len(tetris_shapes))]

   self.stone_x = int(cols / 2 – len(self.stone[0])/2)

   self.stone_y = 0

  

   if check_collision(self.board,

                   self.stone,

                   (self.stonex, self.stoney)):

   self.gameover = True

   

   def init_game(self):

   self.board = new_board()

   self.new_stone()

   self.level = 1

   self.score = 0

   self.lines = 0

   pygame.time.set_timer(pygame.USEREVENT+1, 1000)

   

   def disp_msg(self, msg, topleft):

   x,y = topleft

   for line in msg.splitlines():

   self.screen.blit(

   self.default_font.render(

   line,

   False,

   (255,255,255),

   (0,0,0)),

   (x,y))

   y+=14

   

   def center_msg(self, msg):

   for i, line in enumerate(msg.splitlines()):

   msgimage =  self.defaultfont.render(line, False,

   (255,255,255), (0,0,0))

  

   msgimcenterx, msgimcentery = msgimage.getsize()

   msgimcenterx //= 2

   msgimcentery //= 2

  

   self.screen.blit(msg_image, (

     self.width // 2-msgimcenterx,

     self.height // 2-msgimcentery+i*22))

   

   def draw_matrix(self, matrix, offset):

   offx, offy  = offset

   for y, row in enumerate(matrix):

   for x, val in enumerate(row):

   if val:

   pygame.draw.rect(

   self.screen,

   colors[val],

   pygame.Rect(

   (off_x+x) *

     cell_size,

   (off_y+y) *

     cell_size,

   cell_size,

   cell_size),0)

   

   def addcllines(self, n):

   linescores = [0, 40, 100, 300, 1200]

   self.lines += n

   self.score += linescores[n] * self.level

   if self.lines >= self.level*6:

   self.level += 1

   newdelay = 1000-50*(self.level-1)

   newdelay = 100 if newdelay < 100 else newdelay

   pygame.time.set_timer(pygame.USEREVENT+1, newdelay)

   

   def move(self, delta_x):

   if not self.gameover and not self.paused:

   newx = self.stonex + delta_x

   if new_x < 0:

   new_x = 0

   if new_x > cols – len(self.stone[0]):

   new_x = cols – len(self.stone[0])

   if not check_collision(self.board,

                       self.stone,

                       (newx, self.stoney)):

   self.stonex = newx

   def quit(self):

   self.center_msg(“Exiting…”)

   pygame.display.update()

   sys.exit()

   

   def drop(self, manual):

   if not self.gameover and not self.paused:

   self.score += 1 if manual else 0

   self.stone_y += 1

   if check_collision(self.board,

                   self.stone,

                   (self.stonex, self.stoney)):

   self.board = join_matrixes(

     self.board,

     self.stone,

     (self.stonex, self.stoney))

   self.new_stone()

   cleared_rows = 0

   while True:

   for i, row in enumerate(self.board[:-1]):

   if 0 not in row:

   self.board = remove_row(

     self.board, i)

   cleared_rows += 1

   break

   else:

   break

   self.addcllines(cleared_rows)

   return True

   return False

   

   def insta_drop(self):

   if not self.gameover and not self.paused:

   while(not self.drop(True)):

   pass

   

   def rotate_stone(self):

   if not self.gameover and not self.paused:

   newstone = rotateclockwise(self.stone)

   if not check_collision(self.board,

                       new_stone,

                       (self.stonex, self.stoney)):

   self.stone = new_stone

   

   def toggle_pause(self):

   self.paused = not self.paused

   

   def start_game(self):

   if self.gameover:

   self.init_game()

   self.gameover = False

   

   def run(self):

   key_actions = {

   ‘ESCAPE’:    self.quit,

   ‘LEFT’:    lambda:self.move(-1),

   ‘RIGHT’:    lambda:self.move(+1),

   ‘DOWN’:    lambda:self.drop(True),

   ‘UP’:    self.rotate_stone,

   ‘p’:    self.toggle_pause,

   ‘SPACE’:    self.start_game,

   ‘RETURN’:    self.insta_drop

   }

  

   self.gameover = False

   self.paused = False

  

   dontburnmy_cpu = pygame.time.Clock()

   while 1:

   self.screen.fill((0,0,0))

   if self.gameover:

   self.center_msg(“””Game Over!\nYour score: %d

Press space to continue””” % self.score)

   else:

   if self.paused:

   self.center_msg(“Paused”)

   else:

   pygame.draw.line(self.screen,

   (255,255,255),

   (self.rlim+1, 0),

   (self.rlim+1, self.height-1))

   self.disp_msg(“Next:”, (

   self.rlim+cell_size,

   2))

   self.disp_msg(“Score: %d\n\nLevel: %d\

\nLines: %d” % (self.score, self.level, self.lines),

   (self.rlim+cellsize, cellsize*5))

   self.drawmatrix(self.bgroundgrid, (0,0))

   self.draw_matrix(self.board, (0,0))

   self.draw_matrix(self.stone,

   (self.stonex, self.stoney))

   self.drawmatrix(self.nextstone,

   (cols+1,2))

   pygame.display.update()

  

   for event in pygame.event.get():

   if event.type == pygame.USEREVENT+1:

   self.drop(False)

   elif event.type == pygame.QUIT:

   self.quit()

   elif event.type == pygame.KEYDOWN:

   for key in key_actions:

   if event.key == eval(“pygame.K_”

   +key):

   key_actionskey

  

   dontburnmy_cpu.tick(maxfps)

 

if name == ‘main‘:

 

   s = getpass.getpass()

   App = TetrisApp()

  App.run()

Snapshots of Tetris Game

build tetris in pygame

Running the Python Tetris program from the Ubuntu terminal

 

The Tetris game will open in the new window, which will allow us to use the arrow keys of the keyboard

The game continues… So does the fun.

Happy Gaming!

Thanks for the comment
No Comments

Other Suggested Reads

  • A Beginner’s Guide to Python Web Frameworks

    A web framework is a collection of packages or modules which allow developers to write web applications or services quickly, without getting into the details of network protocols or network socket pro...
  • How To Build a Keyword Dictionary using Python

    What is a Keywords Dictionary? A Keywords Dictionary is a set of words put together based on a common theme. Consider this example, you are managing a bank and you want to improve your customer servi...
  • Primer: Python First Class Functions and Decorators

    Python functions are a lot more flexible than you would think. They are much more than just code generation specifications for a compiler. They are full-blown objects stored in pieces of memory as suc...