Tetris for Intro/Intermediate Programmers (Fall 2026 version)
Step 2: Creating and Drawing the Board
- Let's get started:
First, download the starter file provided for you in this week's homework writeup. Also, be sure that CMU Graphics is in the same folder as that file. - Writing the gameDimensions() and playTetris() functions:
First, we need to make sure that the runApp() function sets up a window of an appropriate size. To do this, we need to determine the starting dimensions of the game. The dimensions we care about are:- rows: the number of rows in the Tetris board. The default value is 15.
- cols: the number of columns in the Tetris board. The default value is 10.
- cellSize: the size of a cell in the Tetris board. The default value is 20.
- margin: the size of the margin around the board. The default value is 25.
- The function gameDimensions() should return a four-tuple containing these four values. The player can then change the dimensions in gameDimensions to play with a board of a different size. The file you submit should use the default values specified above, but we may also test on different values! Note: gameDimensions() should not take any parameters.
- Now you should write the playTetris() function, which will set up the window for the game.
Note that this is a little different than prior coding problems:
we do not specify the window size as an input parameter!
Instead, playTetris should calculate the width and height of the screen
using the dimensions provided by gameDimensions(), then call
runApp(width=width, height=height). Finally, we can call playTetris() at the bottom of the file to start the application! Again, playTetris() should not take any parameters. - Writing the onAppStart(app) function:
First, we'll set up the game state. First, you should call gameDimensions() in your onAppStart(app) function to set app.rows, app.cols, app.cellSize, and app.margin to the appropriate values. - For our purposes, a "board" is a 2-dimensional list of color names (strings like "red"). Our goal here is to allocate the board in the onAppStart function, and then to draw the board in our draw functions (which will be called by redrawAll).
- Now we need to allocate our board as a 2-dimensional list of names of colors, and fill it with our emptyColor ("blue"), as all cells are empty to start the game. We also need to store this board, and the emptyColor, in the app object (app.board, app.emptyColor).
- To help us test our code, we will add a few lines in the onAppStart function to pre-load a few cells in the board with some colors. This code will be removed later on. In particular, we will paint the top-left cell red, the top-right cell white, the bottom-left cell green, and bottom-right cell gray. Here is our temporary code:
# pre-load a few cells with known colors for testing purposes
app.board[0][0] = "red" # top-left is red
app.board[0][app.cols-1] = "white" # top-right is white
app.board[app.rows-1][0] = "green" # bottom-left is green
app.board[app.rows-1][app.cols-1] = "gray" # bottom-right is gray
Our main draw function, redrawAll, will draw the background of the entire game (orange). Then it will use top-down design and call a new function, drawBoard, to draw the board.

|
David Kosbie |