Pixel Art in Python

smiley pixel art

Learning Objectives


In this blog post we are going to investigate how to use lists and list of lists with Python to create some 2D pixel art graphics.

List?


In Python, a list is used to save collection of values. For instance, to save all the different days of the week we could declare a variable called “daysOfTheWeek” and assign a list as follows:

Based on this what do you think len(daysOfTheWeek) would return?
What’s about daysOfTheWeek[0]?
Or daysOfTheWeek[3] ?

Now look at this program used to display all the days of the week:

When using lists you can use a range of methods to add or remove values in the list. For instance:

Multi-dimensional Lists?


Sometimes you need more than on dimension to a list. In Python you can achieve this by creating a list of lists!

For instance for our pixel art project we want to store each pixel on a line within a list:
e.g.:

We can then store all these lines into a list. Let’s call this list “grid”

A quicker way to do this is to do it all in one line as follows:

We can then access any cell within this grid using the following command:

Let’s apply this to our pixel art project


Challenge #1:


Change this code to create the pixel art of a space invader:
spaceInvader

Challenge #2:


Tweak this code to allow colours to be used in your pixel art.
To do so you will first create a new list called colourPalette to store a collection of colours of your choice: e.g.

More changes will be required for you to be able to recreate this pixel art:
Applepix

Share Button
Posted in Computer Science, Python - Intermediate, Python Challenges
7 comments on “Pixel Art in Python
1 Pings/Trackbacks for "Pixel Art in Python"
  1. […] our Pixel Art Challenge – Art work from our students: Scroll down to see the […]

Our Latest Book