C语言画表格

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},
};

// 初始化
void init();
// 加载纹理
void loadTextures();
// 初始化游戏
void initGame();
// 处理操作
void checkOperation(float dt);
Vector2 getPosOfRowCol(int row, int col);
// 绘制网格
void drawGrid();


// 主函数
int main() {
    init();
    // 游戏循环
    while (!WindowShouldClose()) {
        float dt = GetFrameTime();
        checkOperation(dt);
        BeginDrawing();
            ClearBackground((Color){44,44,127,255});
            drawGrid();            
        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;
        }
    }
}

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++) {
        for (int j = 0; j < COLS; j++) {
            Vector2 pos = getPosOfRowCol(i, j);
            drawRectangleCenter((float)pos.x,(float)pos.y, (float)(CELL_SIZE - 1), (float)(CELL_SIZE -1), 
            colors_block[grid[i][j]]);
        }
    }
    // 然后绘制网格线
    // 然后绘制网格线
    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);
    }
}
相关推荐
秦禹辰1 小时前
本地Docker部署开源Web相册图库Piwigo与在线远程访问实战方案
开发语言·后端·golang
the beard1 小时前
深入理解Java多线程:状态、安全、同步与通信
java·开发语言
zhysunny2 小时前
Day22: Python涡轮增压计划:用C扩展榨干最后一丝性能!
c语言·网络·python
YxVoyager2 小时前
【C标准库】详解<stdio.h>标准输入输出库
c语言·c++
lucky_lyovo2 小时前
大模型部署
开发语言·人工智能·云计算·lua
Warren983 小时前
如何在 Spring Boot 中安全读取账号密码等
java·开发语言·spring boot·后端·安全·面试·测试用例
燃尽了,可无4 小时前
C#基础编程核心知识点总结
开发语言·c#
llrraa20105 小时前
python whisper生成字幕
开发语言·python·whisper
努力努力再努力wz5 小时前
【c++进阶系列】:万字详解多态
java·linux·运维·开发语言·c++
秦亿凡5 小时前
多线程下为什么用ConcurrentHashMap而不是HashMap
java·开发语言