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.
Here's a basic implementation of Tetris in C using the raylib library. This code creates a Tetris game with basic functionality, including moving and rotating tetrominoes, line clearing, scoring, and game over handling.
// Simple Tetris Game in C using raylib
#include "raylib.h"
#include <stdlib.h>
#include <time.h>
#define SCREEN_WIDTH 400
#define SCREEN_HEIGHT 800
#define ROWS 20
#define COLS 10
#define BLOCK_SIZE 40
typedef struct {
int x, y;
} Point;
typedef struct {
Point blocks[4];
Color color;
} Tetromino;
int field[ROWS][COLS] = {0};
Tetromino tetrominoes[7] = {
// I
{{{0, -1}, {0, 0}, {0, 1}, {0, 2}}, SKYBLUE},
// J
{{{-1, 0}, {0, 0}, {0, 1}, {0, 2}}, BLUE},
// L
{{{1, 0}, {0, 0}, {0, 1}, {0, 2}}, ORANGE},
// O
{{{0, 0}, {1, 0}, {0, 1}, {1, 1}}, YELLOW},
// S
{{{0, 0}, {1, 0}, {-1, 1}, {0, 1}}, GREEN},
// T
{{{-1, 0}, {0, 0}, {1, 0}, {0, 1}}, PURPLE},
// Z
{{{-1, 0}, {0, 0}, {0, 1}, {1, 1}}, RED},
};
Tetromino current;
int currentX = COLS / 2, currentY = 0;
float timer = 0;
float delay = 0.5f;
bool gameover = false;
int score = 0;
void SpawnTetromino() {
current = tetrominoes[rand() % 7];
currentX = COLS / 2;
currentY = 0;
}
void RotateTetromino() {
for (int i = 0; i < 4; i++) {
int x = current.blocks[i].x;
int y = current.blocks[i].y;
current.blocks[i].x = -y;
current.blocks[i].y = x;
}
}
bool CheckCollision(int dx, int dy) {
for (int i = 0; i < 4; i++) {
int x = currentX + current.blocks[i].x + dx;
int y = currentY + current.blocks[i].y + dy;
if (x < 0 || x >= COLS || y >= ROWS) {
return true;
}
if (y >= 0 && field[y][x]) {
return true;
}
}
return false;
}
void MergeTetromino() {
for (int i = 0; i < 4; i++) {
int x = currentX + current.blocks[i].x;
int y = currentY + current.blocks[i].y;
if (y >= 0 && y < ROWS && x >= 0 && x < COLS) {
field[y][x] = current.color.r << 16 | current.color.g << 8 | current.color.b;
}
}
}
void ClearLines() {
for (int y = ROWS - 1; y >= 0; ) {
bool full = true;
for (int x = 0; x < COLS; x++) {
if (!field[y][x]) {
full = false;
break;
}
}
if (full) {
for (int i = y; i > 0; i--) {
for (int x = 0; x < COLS; x++) {
field[i][x] = field[i - 1][x];
}
}
for (int x = 0; x < COLS; x++) {
field[0][x] = 0;
}
score += 100;
} else {
y--;
}
}
}
int main() {
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tetris in C using raylib");
SetTargetFPS(60);
srand(time(NULL));
SpawnTetromino();
while (!WindowShouldClose()) {
float dt = GetFrameTime();
timer += dt;
if (!gameover) {
if (IsKeyPressed(KEY_LEFT) && !CheckCollision(-1, 0)) currentX--;
if (IsKeyPressed(KEY_RIGHT) && !CheckCollision(1, 0)) currentX++;
if (IsKeyPressed(KEY_DOWN)) timer = delay;
if (IsKeyPressed(KEY_UP)) {
RotateTetromino();
if (CheckCollision(0, 0)) { // If collision after rotation, rotate back
for (int i = 0; i < 3; i++) RotateTetromino();
}
}
if (timer > delay) {
if (!CheckCollision(0, 1)) {
currentY++;
} else {
MergeTetromino();
ClearLines();
SpawnTetromino();
if (CheckCollision(0, 0)) {
gameover = true;
}
}
timer = 0;
}
} else {
if (IsKeyPressed(KEY_ENTER)) {
// Reset game
memset(field, 0, sizeof(field));
gameover = false;
score = 0;
SpawnTetromino();
}
}
BeginDrawing();
ClearBackground(BLACK);
// Draw field
for (int y = 0; y < ROWS; y++) {
for (int x = 0; x < COLS; x++) {
if (field[y][x]) {
Color color = {
(field[y][x] >> 16) & 0xFF,
(field[y][x] >> 8) & 0xFF,
field[y][x] & 0xFF,
255
};
DrawRectangle(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE - 1, BLOCK_SIZE - 1, color);
} else {
DrawRectangleLines(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, DARKGRAY);
}
}
}
// Draw current tetromino
if (!gameover) {
for (int i = 0; i < 4; i++) {
int x = currentX + current.blocks[i].x;
int y = currentY + current.blocks[i].y;
if (y >= 0) {
DrawRectangle(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE - 1, BLOCK_SIZE - 1, current.color);
}
}
}
// Draw Score
DrawText(TextFormat("Score: %d", score), 10, 10, 20, WHITE);
// Game over message
if (gameover) {
DrawText("Game Over!", SCREEN_WIDTH / 2 - MeasureText("Game Over!", 40) / 2, SCREEN_HEIGHT / 2 - 40, 40, WHITE);
DrawText("Press ENTER to restart", SCREEN_WIDTH / 2 - MeasureText("Press ENTER to restart", 20) / 2, SCREEN_HEIGHT / 2, 20, WHITE);
}
EndDrawing();
}
CloseWindow();
return 0;
}
Explanation:
SpawnTetromino() selects a random tetromino and resets its position.RotateTetromino() rotates the tetromino 90 degrees.CheckCollision() checks if the tetromino collides with walls or existing blocks.MergeTetromino() merges the current tetromino into the field when it can't move down further.ClearLines() checks for and clears any full lines, updating the score.Controls:
Compilation Instructions:
To compile and run this code, you'll need to have the raylib library installed on your system. Here's how you can compile the program:
gcc -o tetris tetris.c -lraylib -lm -lpthread -ldl -lrt -lGL -lX11
Make sure to replace tetris.c with the actual filename if it's different.
Note: The compilation flags (-lraylib -lm -lpthread -ldl -lrt -lGL -lX11) may vary depending on your operating system. Please refer to the raylib installation guide for detailed instructions tailored to your setup.
Once compiled, run the executable to start the game:
./tetris
Enjoy playing Tetris!
Not yet included in a published release.