c
复制代码
// 模板 显示Hello World 支持c和c++ 也可以改成main.cpp
#include <stdio.h>
#include <stdlib.h>
#include "raylib.h"
#define ROWS 20
#define COLS 10
int grid[ROWS][COLS] = {0};
#define CELL_SIZE 30
#define BLOCK_TYPE_COUNT 7
const Color colors_block[BLOCK_TYPE_COUNT + 1] = {
{26,31,40,255},
{47,230,23,255},
{232,18,18,255},
{226,116,17,255},
{237,234,4,255},
{166,0,247,255},
{21,204,209,255},
{13,64,216,255},
};
typedef struct Block {
int idx;
int sz;
int shape[4][4];
int row,col;
} Block;
// 当前方块
Block curBlock;
// 所有方块
Block allBlocks[BLOCK_TYPE_COUNT] = {
{1,2,{
{1,1,0,0},
{1,1,0,0},
}},
{2,3,{
{0,0,0},
{1,1,0},
{0,1,1},
}},
{3,3,{
{0,0,0},
{0,1,1},
{1,1,0},
}},
{4,3,{
{0,1,0},
{0,1,0},
{0,1,1},
}},
{5,3,{
{0,0,0},
{1,1,1},
{0,1,0},
}},
{6,3,{
{0,1,0},
{0,1,0},
{1,1,0},
}},
{7,4,{
{0,0,1,0},
{0,0,1,0},
{0,0,1,0},
{0,0,1,0}
}}
};
// 方块下落速度
float downInterval =0.32f;
// 方块下落间隔
float curInterval = 0;
// 初始化
void init();
// 加载纹理
void loadTextures();
// 初始化游戏
void initGame();
// 处理操作
void checkOperation(float dt);
Vector2 getPosOfRowCol(int row, int col);
// 绘制网格
void drawGrid();
// 绘制方块
void drawCurBlock();
// 绘制方块下降
void driveCurBlock(float dt);
// 主函数
int main() {
init();
// 游戏循环
while (!WindowShouldClose()) {
float dt = GetFrameTime();
checkOperation(dt);
BeginDrawing();
ClearBackground((Color){44,44,127,255});
drawGrid();
drawCurBlock();
driveCurBlock(dt);
EndDrawing();
}
CloseWindow();
return 0;
}
void init() {
SetConfigFlags(FLAG_WINDOW_HIGHDPI);
InitWindow(500, 620, "Tetris");
SetTargetFPS(60);
loadTextures();
initGame();
}
void initGame() {
for(int i = 0; i < ROWS; i++) {
for(int j = 0; j < COLS; j++) {
grid[i][j] = 0;
}
}
curBlock = allBlocks[GetRandomValue(0, BLOCK_TYPE_COUNT - 1)];
curBlock.row = 0;
curBlock.col = COLS/2 - curBlock.sz/2;
}
void loadTextures() {
}
void checkOperation(float dt) {
}
Vector2 getPosOfRowCol(int row, int col) {
int x = col * CELL_SIZE + 11 + CELL_SIZE/2;
int y = row * CELL_SIZE + 11 + CELL_SIZE/2;
return (Vector2){x, y};
}
void drawGrid() {
// 先绘制所有单元格
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
Vector2 pos = getPosOfRowCol(i, j);
DrawRectangle(pos.x - CELL_SIZE/2, pos.y - CELL_SIZE/2,
CELL_SIZE - 1, CELL_SIZE - 1,
(Color){0,0,0,255});
}
}
// 然后绘制网格线
for (int i = 0; i <= ROWS; i++) {
DrawLine(11, 11 + i * CELL_SIZE,
11 + COLS * CELL_SIZE, 11 + i * CELL_SIZE,
LIGHTGRAY);
}
for (int j = 0; j <= COLS; j++) {
DrawLine(11 + j * CELL_SIZE, 11,
11 + j * CELL_SIZE, 11 + ROWS * CELL_SIZE,
LIGHTGRAY);
}
}
void drawCurBlock(){
for (int i = 0; i < curBlock.sz; i++) {
for (int j = 0; j < curBlock.sz; j++) {
if (curBlock.shape[i][j] == 1) {
int row = curBlock.row + i;
int col = curBlock.col + j;
if (row >= 0 && row < ROWS && col >= 0 && col < COLS) {
Vector2 pos = getPosOfRowCol(row, col);
// 使用标准 Raylib 函数绘制矩形
DrawRectangle(
(int)(pos.x - CELL_SIZE/2), // 左上角x
(int)(pos.y - CELL_SIZE/2), // 左上角y
CELL_SIZE - 1, // 宽度
CELL_SIZE - 1, // 高度
colors_block[curBlock.idx] // 颜色
);
}
}
}
}
}
void driveCurBlock(float dt) {
curInterval += dt;
if (curInterval >= downInterval) {
curBlock.row++;
curInterval = 0.0f;
}
}