Background Image

Introduction

Welcome to this tutorial on building Tetris with p5.js and chatGPT. In this tutorial, we will guide you step by step on how to create your own version of the classic game Tetris using the p5.js library and chatGPT for behavior driven development.

Before we start, make sure you have a basic understanding of JavaScript and HTML. Knowledge in p5.js and chatGPT is not required as we will cover them in this tutorial.

Let's get started!

Background Image

Terms and Definitions

Test-Driven Development (TDD)

TDD is a software development process where you write a test before you write your code, and then write the code to pass the test.

Behavior-Driven Development (BDD)

BDD is a software development process that encourages collaboration between developers, QA and non-technical or business participants in a software project.

Gherkin

Gherkin is a language designed to be easy to learn by non-programmers and yet structured enough to allow concise description of examples to illustrate business rules in most real-world domains.

Cucumber

Cucumber is a tool that supports BDD. It runs automated acceptance tests written in a behavior-driven development (BDD) style.

Tetris

Tetris is a popular video game that involves stacking tetrominoes to clear lines.

ChatGPT

ChatGPT is a language model developed by OpenAI. It's capable of generating human-like text based on the input it's given.

Agent

In the context of AI, an agent is anything that can perceive its environment through sensors and acts upon that environment through effectors.

Deep Learning, Machine Learning, Deep Neural Networks, Transformers, GPT

These are all terms related to AI. Deep Learning is a subset of Machine Learning that uses neural networks with many layers (hence "deep"). Transformers are a type of model used in machine learning. GPT (Generative Pretrained Transformer) is a type of transformer model developed by OpenAI.

Background Image

Building the Grid

Building the Grid

Guess What's Next!

What's the first decision we have to make about our game?

Click to reveal the answer

Given

What's the first Given we can think of as a user?

When

What's the first When we can think of as a user?

Then

What's the first Then we can think of as a user?

Hold Up!

Wait a minute, we can't assert we see a Piece on the Grid unless we assert we have a fresh Grid instance. so, let's create THAT scenario first:

Comment make sure the grid renders empty for a new game

Scenario User starts new game, sees grid

Given a fresh New Game

When I view the first frame

Then I see The Grid

In this section, we will discuss how to build the grid for our Tetris game using p5.js. The grid is a crucial component of the game as it is where the Tetris blocks will be placed and moved around.

We will be using a 2D array to represent our grid. Each cell in the array will represent a cell in the Tetris grid. We will initialize all cells to be empty at the start of the game.

We will also need to create a function to draw the grid on the screen. This function will loop through each cell in our 2D array and draw a square at the corresponding location on the screen.

Finally, we will need to create a function to update the grid. This function will be called whenever a Tetris block is moved or rotated. It will update the 2D array to reflect the new state of the grid.



Prompt Output:

Click on the prompt to copy it to clipboard

Prompt will be displayed here...
Background Image

Tetrominos

Section 2. Tetrominos

In this section, we will discuss how to create the Tetrominos for our Tetris game using p5.js. The Tetrominos are the shapes that will be falling from the top of the screen. There are seven different Tetrominos in total: I, J, L, O, S, T, and Z.

We will be using a 2D array to represent each Tetromino. Each cell in the array will represent a cell in the Tetromino. We will initialize all cells to be empty at the start of the game.

We will also need to create a function to draw the Tetromino on the screen. This function will loop through each cell in our 2D array and draw a square at the corresponding location on the screen.

Finally, we will need to create a function to update the Tetromino. This function will be called whenever a Tetromino is moved or rotated. It will update the 2D array to reflect the new state of the Tetromino.

Prompt Output:

Click on the prompt to copy it to clipboard

Prompt will be displayed here...
  1. Define Tetrominos: Create 2D arrays for each of the seven Tetromino shapes (I, J, L, O, S, T, and Z). Each cell in these arrays represents a block in the Tetromino.

    const I = [[1, 1, 1, 1]], J = [[1, 0, 0], [1, 1, 1]], L = [[0, 0, 1], [1, 1, 1]], O = [[1, 1], [1, 1]], S = [[0, 1, 1], [1, 1, 0]], T = [[0, 1, 0], [1, 1, 1]], Z = [[1, 1, 0], [0, 1, 1]];
  2. Drawing Tetrominos: Develop a function to draw a Tetromino on the screen. This function should loop through each cell in the Tetromino's 2D array and draw a square at the corresponding location.

    function drawTetromino(tetromino, x, y, size) {
                for (let i = 0; i < tetromino.length; i++) {
                for (let j = 0; j < tetromino[i].length; j++) {
                    if (tetromino[i][j] === 1) {
                    rect(x + j * size, y + i * size, size, size);
                    }
                }
                }
            }
            
  3. Updating Tetrominos: Implement a function to update the position and rotation of a Tetromino. This function should modify the Tetromino's 2D array to reflect its new state after movement or rotation.

    function updateTetromino(tetromino, direction) {
                // Rotate or move the tetromino based on the direction
                // This is a placeholder and needs to be implemented based on the game's logic
            }
            

Tetrominos are a great usecase for "Scenario Outlines" in Gherkin BDD. We can easily define a map of valid rotations, for example, and loop through them all, reusing the same logic to validate all 7 * 4 possibilities;

ChatGPT Says: Don't Forget...

Remember to always test your game thoroughly. Make sure all Tetromino rotations and movements work as expected and the game ends when it should.

Background Image

Game Loop

Game Loop

In this section, we will discuss how to create the game loop for our Tetris game using p5.js. The game loop is responsible for updating the state of the game and drawing it on the screen.

We will need to create a function to update the state of the game. This function will be called every frame and will be responsible for updating the position of the Tetromino and checking if it has collided with any other Tetrominos.

We will also need to create a function to draw the state of the game on the screen. This function will be called every frame and will be responsible for drawing the Tetromino and the grid.

Finally, we will need to create a function to handle user input. This function will be called whenever the user presses a key on the keyboard. It will be responsible for moving the Tetromino and rotating it.

Background Image

Controls

Controls

In this section, we will discuss how to create the controls for our Tetris game using p5.js. The controls are responsible for moving the Tetromino and rotating it.

We will need to create a function to handle user input. This function will be called whenever the user presses a key on the keyboard. It will be responsible for moving the Tetromino and rotating it.

We will also need to create a function to check if the Tetromino has collided with any other Tetrominos. This function will be called whenever the Tetromino is moved or rotated. It will be responsible for checking if the Tetromino has collided with any other Tetrominos.

Finally, we will need to create a function to update the state of the game. This function will be called every frame and will be responsible for updating the position of the Tetromino and checking if it has collided with any other Tetrominos.

Background Image

Mobile Considerations

Mobile Considerations

In this section, we will discuss how to make our Tetris game touch responsive using p5.js. The game will be playable on both desktop and mobile devices.

We will need to create a function to handle user input. This function will be called whenever the user presses a key on the keyboard or touches the screen. It will be responsible for moving the Tetromino and rotating it.

We will also need to create a function to check if the Tetromino has collided with any other Tetrominos. This function will be called whenever the Tetromino is moved or rotated. It will be responsible for checking if the Tetromino has collided with any other Tetrominos.

Finally, we will need to create a function to update the state of the game. This function will be called every frame and will be responsible for updating the position of the Tetromino and checking if it has collided with any other Tetrominos.

Background Image

View Final Results

View Final Results

In this section, you can view the final results of your Tetris game. You can see how the game looks and behaves after implementing all the features discussed in the tutorial.

You can interact with the game, play it, and test its features. This will give you a better understanding of how the different parts of the game work together.

Enjoy playing your Tetris game!

Background Image

Future Work

Future Work

In this section, we will discuss some ideas for future work on our Tetris game using p5.js. The game is currently playable on both desktop and mobile devices.

We could add more Tetrominos to the game. There are seven different Tetrominos in total: I, J, L, O, S, T, and Z.

We could also add more levels to the game. Currently, there is only one level.

We could also add a high score system to the game. Currently, there is no way to keep track of the player's score.

We could also consider adding a multiplayer mode to the game. This would allow players to compete against each other in real-time, adding a new level of challenge and excitement to the game.

We could also consider adding custom themes to the game. This would allow players to personalize their gaming experience and make the game more visually appealing.

Rubik's Cube Version

Coming soon...

🎉 Vote for the Next Tutorial!

We value your opinion! Please vote for the next game you'd like to see in our series:




Find this content useful? Buy Me a Coffee at ko-fi.com

by jake downs and chatgpt. Fork this project on Github! | View Source Code