Write Tetris in C + raylib.
- User
Write Tetris in C + raylib.
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Write Tetris in C + raylib.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Creating a Tetris game using C and raylib is a fantastic way to learn game development fundamentals. Below is a complete implementation of Tetris, along with explanations and instructions to get you started.
Install raylib: Make sure you have raylib installed on your system. You can follow the official installation guide.
C Compiler: Ensure you have a C compiler installed (e.g., gcc).
Here's a complete Tetris game implemented in C using raylib. The code is well-commented to help you understand each part.
// tetris.c
#include "raylib.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Define screen dimensions
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 640
// Define grid dimensions
#define GRID_WIDTH 10
#define GRID_HEIGHT 20
#define BLOCK_SIZE 32
// Define game colors
Color gridColor = DARKGRAY;
// Tetromino shapes
typedef struct {
int size;
int shape[4][4];
} Tetromino;
// Define Tetromino shapes
Tetromino tetrominoes[7] = {
// I
{4, {
{0,0,0,0},
{1,1,1,1},
{0,0,0,0},
{0,0,0,0}
}},
// J
{3, {
{2,0,0},
{2,2,2},
{0,0,0}
}},
// L
{3, {
{0,0,3},
{3,3,3},
{0,0,0}
}},
// O
{2, {
{4,4},
{4,4}
}},
// S
{3, {
{0,5,5},
{5,5,0},
{0,0,0}
}},
// T
{3, {
{0,6,0},
{6,6,6},
{0,0,0}
}},
// Z
{3, {
{7,7,0},
{0,7,7},
{0,0,0}
}}
};
// Game grid
int grid[GRID_HEIGHT][GRID_WIDTH] = {0};
// Current Tetromino
typedef struct {
int type;
int rotation;
int x, y;
} CurrentTetromino;
CurrentTetromino current;
// Next Tetromino
int nextType;
// Game states
bool gameOver = false;
// Timing
float fallSpeed = 1.0f;
float fallTimer = 0.0f;
// Function prototypes
void InitGame();
void UpdateGame(float delta);
void DrawGame();
bool CheckCollision(int newX, int newY, int newRotation);
void RotateTetromino();
void PlaceTetromino();
void ClearLines();
void SpawnTetromino();
void DrawGrid();
void DrawTetromino(int type, int rotation, int posX, int posY);
int main(void)
{
// Initialization
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tetris with raylib");
InitGame();
SetTargetFPS(60);
// Main game loop
while (!WindowShouldClose())
{
float delta = GetFrameTime();
if (!gameOver)
{
UpdateGame(delta);
}
BeginDrawing();
ClearBackground(BLACK);
DrawGame();
EndDrawing();
}
CloseWindow();
return 0;
}
void InitGame()
{
srand(time(NULL));
SpawnTetromino();
nextType = rand() % 7;
}
void UpdateGame(float delta)
{
// Handle input
if (IsKeyPressed(KEY_LEFT))
{
if (CheckCollision(current.x - 1, current.y, current.rotation))
current.x -= 1;
}
if (IsKeyPressed(KEY_RIGHT))
{
if (CheckCollision(current.x + 1, current.y, current.rotation))
current.x += 1;
}
if (IsKeyPressed(KEY_UP))
{
RotateTetromino();
}
if (IsKeyPressed(KEY_DOWN))
{
if (CheckCollision(current.x, current.y + 1, current.rotation))
current.y += 1;
}
// Handle falling
fallTimer += delta;
if (fallTimer >= fallSpeed)
{
fallTimer = 0.0f;
if (CheckCollision(current.x, current.y + 1, current.rotation))
{
current.y += 1;
}
else
{
PlaceTetromino();
ClearLines();
SpawnTetromino();
if (!CheckCollision(current.x, current.y, current.rotation))
{
gameOver = true;
}
}
}
}
void DrawGame()
{
DrawGrid();
// Draw current Tetromino
DrawTetromino(current.type, current.rotation, current.x, current.y);
// Optionally, draw next Tetromino
// You can implement a "next" display if desired
if (gameOver)
{
DrawText("GAME OVER", SCREEN_WIDTH/2 - MeasureText("GAME OVER", 20)/2, SCREEN_HEIGHT/2 - 10, 20, RED);
}
}
bool CheckCollision(int newX, int newY, int newRotation)
{
// Get current Tetromino shape
Tetromino piece = tetrominoes[current.type];
int size = piece.size;
// Rotate the piece
int rotated[4][4] = {0};
for (int y = 0; y < size; y++)
for (int x = 0; x < size; x++)
rotated[y][x] = piece.shape[(x - newRotation + size) % size][y];
// Check for collision
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
if (rotated[y][x] != 0)
{
int gridX = newX + x;
int gridY = newY + y;
if (gridX < 0 || gridX >= GRID_WIDTH || gridY < 0 || gridY >= GRID_HEIGHT)
return false;
if (grid[gridY][gridX] != 0)
return false;
}
}
}
return true;
}
void RotateTetromino()
{
int newRotation = (current.rotation + 1) % 4;
if (CheckCollision(current.x, current.y, newRotation))
current.rotation = newRotation;
}
void PlaceTetromino()
{
Tetromino piece = tetrominoes[current.type];
int size = piece.size;
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
if (piece.shape[y][x] != 0)
{
int gridX = current.x + x;
int gridY = current.y + y;
if (gridX >= 0 && gridX < GRID_WIDTH && gridY >= 0 && gridY < GRID_HEIGHT)
grid[gridY][gridX] = piece.shape[y][x];
}
}
}
}
void ClearLines()
{
for (int y = GRID_HEIGHT -1; y >=0 ; y--)
{
bool full = true;
for (int x = 0; x < GRID_WIDTH; x++)
{
if (grid[y][x] == 0)
{
full = false;
break;
}
}
if (full)
{
// Move all lines above down
for (int ty = y; ty > 0; ty--)
{
for (int x = 0; x < GRID_WIDTH; x++)
{
grid[ty][x] = grid[ty-1][x];
}
}
// Clear the top line
for (int x = 0; x < GRID_WIDTH; x++)
{
grid[0][x] = 0;
}
y++; // Check the same line again
}
}
}
void SpawnTetromino()
{
current.type = nextType;
nextType = rand() % 7;
current.rotation = 0;
current.x = GRID_WIDTH / 2 - 2;
current.y = 0;
if (!CheckCollision(current.x, current.y, current.rotation))
{
gameOver = true;
}
}
void DrawGrid()
{
// Draw settled blocks
for (int y = 0; y < GRID_HEIGHT; y++)
{
for (int x = 0; x < GRID_WIDTH; x++)
{
if (grid[y][x] != 0)
{
Color color;
switch(grid[y][x])
{
case 1: color = CYAN; break;
case 2: color = BLUE; break;
case 3: color = ORANGE; break;
case 4: color = YELLOW; break;
case 5: color = LIME; break;
case 6: color = PURPLE; break;
case 7: color = RED; break;
default: color = WHITE; break;
}
DrawRectangle(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, color);
DrawRectangleLines(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, gridColor);
}
}
}
// Draw grid lines
for (int x = 0; x <= GRID_WIDTH; x++)
{
DrawLine(x * BLOCK_SIZE, 0, x * BLOCK_SIZE, GRID_HEIGHT * BLOCK_SIZE, gridColor);
}
for (int y = 0; y <= GRID_HEIGHT; y++)
{
DrawLine(0, y * BLOCK_SIZE, GRID_WIDTH * BLOCK_SIZE, y * BLOCK_SIZE, gridColor);
}
}
void DrawTetromino(int type, int rotation, int posX, int posY)
{
Tetromino piece = tetrominoes[type];
int size = piece.size;
// Rotate the piece
int rotated[4][4] = {0};
for (int y = 0; y < size; y++)
for (int x = 0; x < size; x++)
rotated[y][x] = piece.shape[(x - rotation + size) % size][y];
// Draw the Tetromino
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
if (rotated[y][x] != 0)
{
int gridX = posX + x;
int gridY = posY + y;
if (gridX >=0 && gridX < GRID_WIDTH && gridY >=0 && gridY < GRID_HEIGHT)
{
Color color;
switch(rotated[y][x])
{
case 1: color = CYAN; break;
case 2: color = BLUE; break;
case 3: color = ORANGE; break;
case 4: color = YELLOW; break;
case 5: color = LIME; break;
case 6: color = PURPLE; break;
case 7: color = RED; break;
default: color = WHITE; break;
}
DrawRectangle(gridX * BLOCK_SIZE, gridY * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, color);
DrawRectangleLines(gridX * BLOCK_SIZE, gridY * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, gridColor);
}
}
}
}
}
Definitions and Structures:
Initialization (InitGame):
Main Loop (main):
Game Update (UpdateGame):
Collision Detection (CheckCollision):
Rotation (RotateTetromino):
Placing Tetromino (PlaceTetromino):
Clearing Lines (ClearLines):
Spawning Tetromino (SpawnTetromino):
Drawing Functions:
To compile the code, ensure you have raylib installed and use the following command:
gcc tetris.c -o tetris -lraylib -lm -lpthread -ldl -lrt -lX11
Note: The exact linking flags may vary based on your operating system. The above command works for Linux. For Windows or macOS, refer to the raylib official documentation for the correct compilation instructions.
After successful compilation, run the game using:
./tetris
This implementation provides a basic Tetris game. Here are some ideas to enhance it:
Feel free to modify and expand upon this code to create a more feature-rich Tetris game!
Not yet included in a published release.
DrawGame: Draws the grid, current Tetromino, and game over text if applicable.DrawGrid: Renders the settled blocks and grid lines.DrawTetromino: Renders the current falling Tetromino.