GAMR1520: Markup languages and scripting

python logo

Appendix A: Optional extras

Back to home

In this set of exercises, we will build a python/tkinter implementation of the puzzle game known as 2048.

For research purposes, it may help to play a few rounds online.

This is a fairly complex project, so we will take our time. In each exercise we will add more functionality and capability to our code, gradually building in complexity.

The game consists of a 4 × 4 grid of tiles. Tiles are empty by default, but can contain numbers. The game starts with two randomly selected tiles set to the number 2.

final game

The rules of the 2048 game are fairly simple:

Don’t worry if this doesn’t make complete sense right now. The details of the rules will become clear as we implement the necessary algorithms.

Lab exercises

Lab A.1: Unit testing

In this exercise we will introduce the unittest module, develop a data model for our game and start creating tests for the core logic.

Lab A.2: Merging left

OK, so we have a function that will stack the elements to the left. The next thing we will create is a function that will take the stacked elements and merge any pairs from right to left.

Lab A.4: A rudimentary game

In this exercise we create a rough draft of a working game and (hopefully) by the end of this session we will have our first (very basic) playable version.

Lab A.6: Scoring

In this exercise, we will add a scoring mechanism and print the score at each step when we print the grid.

Lab A.8: Refining the GUI

In this exercise we will make our 2048 GUI more attractive using better colours. We will also implement "game over" and "well done" messages and allow the user to restart.