--- /dev/null
+#include "tetris.h"
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+// ÓÎϷ״̬
+TetrisGame game;
+
+// ·½¿éÐÎ×´¶¨Òå (7ÖÖÐÎ×´£¬Ã¿ÖÖ4¸öÐýת״̬)
+const uint8_t tetrominoes[7][4][4][4] = {
+ // IÐÍ
+ {
+ {{0,0,0,0}, {1,1,1,1}, {0,0,0,0}, {0,0,0,0}},
+ {{0,0,1,0}, {0,0,1,0}, {0,0,1,0}, {0,0,1,0}},
+ {{0,0,0,0}, {1,1,1,1}, {0,0,0,0}, {0,0,0,0}},
+ {{0,0,1,0}, {0,0,1,0}, {0,0,1,0}, {0,0,1,0}}
+ },
+ // OÐÍ
+ {
+ {{2,2}, {2,2}, {0,0}, {0,0}},
+ {{2,2}, {2,2}, {0,0}, {0,0}},
+ {{2,2}, {2,2}, {0,0}, {0,0}},
+ {{2,2}, {2,2}, {0,0}, {0,0}}
+ },
+ // TÐÍ
+ {
+ {{0,3,0}, {3,3,3}, {0,0,0}, {0,0,0}},
+ {{0,3,0}, {0,3,3}, {0,3,0}, {0,0,0}},
+ {{0,0,0}, {3,3,3}, {0,3,0}, {0,0,0}},
+ {{0,3,0}, {3,3,0}, {0,3,0}, {0,0,0}}
+ },
+ // LÐÍ
+ {
+ {{0,0,4}, {4,4,4}, {0,0,0}, {0,0,0}},
+ {{0,4,0}, {0,4,0}, {0,4,4}, {0,0,0}},
+ {{0,0,0}, {4,4,4}, {4,0,0}, {0,0,0}},
+ {{4,4,0}, {0,4,0}, {0,4,0}, {0,0,0}}
+ },
+ // JÐÍ
+ {
+ {{5,0,0}, {5,5,5}, {0,0,0}, {0,0,0}},
+ {{0,5,5}, {0,5,0}, {0,5,0}, {0,0,0}},
+ {{0,0,0}, {5,5,5}, {0,0,5}, {0,0,0}},
+ {{0,5,0}, {0,5,0}, {5,5,0}, {0,0,0}}
+ },
+ // SÐÍ
+ {
+ {{0,6,6}, {6,6,0}, {0,0,0}, {0,0,0}},
+ {{0,6,0}, {0,6,6}, {0,0,6}, {0,0,0}},
+ {{0,0,0}, {0,6,6}, {6,6,0}, {0,0,0}},
+ {{6,0,0}, {6,6,0}, {0,6,0}, {0,0,0}}
+ },
+ // ZÐÍ
+ {
+ {{7,7,0}, {0,7,7}, {0,0,0}, {0,0,0}},
+ {{0,0,7}, {0,7,7}, {0,7,0}, {0,0,0}},
+ {{0,0,0}, {7,7,0}, {0,7,7}, {0,0,0}},
+ {{0,7,0}, {7,7,0}, {7,0,0}, {0,0,0}}
+ }
+};
+
+// RGB565ÑÕÉ«¶¨Òå
+const uint16_t colors[8] = {
+ 0x0000, // ºÚÉ« (±³¾°)
+ 0x07E0, // ÂÌÉ« (IÐÍ)
+ 0xFFFF, // »ÆÉ« (OÐÍ)
+ 0x7F00, // רɫ (TÐÍ)
+ 0xFFA5, // ³ÈÉ« (LÐÍ)
+ 0x1F00, // À¶É« (JÐÍ)
+ 0x001F, // ºìÉ« (SÐÍ)
+ 0x780F // ×ÏÉ« (ZÐÍ)
+};
+
+// ³õʼ»¯ÓÎÏ·
+void tetris_init() {
+ // ³õʼ»¯Ëæ»úÊýÖÖ×Ó
+ srand(time(NULL));
+
+ // Çå¿ÕÓÎÏ·ÇøÓò
+ memset(game.board, 0, sizeof(game.board));
+
+ // ³õʼ»¯ÓÎϷ״̬
+ game.score = 0;
+ game.level = 1;
+ game.game_over = false;
+
+ // Éú³ÉµÚÒ»¸ö·½¿é
+ tetris_spawn_piece();
+}
+
+// Éú³Éз½¿é
+void tetris_spawn_piece() {
+ // Ëæ»úÑ¡Ôñ·½¿éÀàÐÍ
+ game.current_piece.type = rand() % 7;
+ game.current_piece.rotation = 0;
+ game.current_piece.x = BOARD_WIDTH / 2 - 2;
+ game.current_piece.y = 0;
+
+ // ¼ì²éÊÇ·ñÓÎÏ·½áÊø
+ if (!tetris_is_valid_position(game.current_piece.x, game.current_piece.y, game.current_piece.type, game.current_piece.rotation)) {
+ game.game_over = true;
+ return;
+ }
+
+ // Éú³ÉÏÂÒ»¸ö·½¿é
+ game.next_piece.type = rand() % 7;
+ game.next_piece.rotation = 0;
+}
+
+// ¼ì²éλÖÃÊÇ·ñÓÐЧ
+bool tetris_is_valid_position(int x, int y, int type, int rotation) {
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ if (tetrominoes[type][rotation][i][j] != 0) {
+ int board_x = x + j;
+ int board_y = y + i;
+
+ // ¼ì²é±ß½ç
+ if (board_x < 0 || board_x >= BOARD_WIDTH || board_y >= BOARD_HEIGHT) {
+ return false;
+ }
+
+ // ¼ì²éÊÇ·ñÓëÒÑÓз½¿éÅöײ
+ if (board_y >= 0 && game.board[board_y][board_x] != 0) {
+ return false;
+ }
+ }
+ }
+ }
+ return true;
+}
+
+// ÒÆ¶¯·½¿é
+bool tetris_move_piece(int dx, int dy) {
+ int new_x = game.current_piece.x + dx;
+ int new_y = game.current_piece.y + dy;
+
+ if (tetris_is_valid_position(new_x, new_y, game.current_piece.type, game.current_piece.rotation)) {
+ game.current_piece.x = new_x;
+ game.current_piece.y = new_y;
+ return true;
+ }
+ return false;
+}
+
+// Ðýת·½¿é
+bool tetris_rotate_piece() {
+ int new_rotation = (game.current_piece.rotation + 1) % 4;
+
+ if (tetris_is_valid_position(game.current_piece.x, game.current_piece.y, game.current_piece.type, new_rotation)) {
+ game.current_piece.rotation = new_rotation;
+ return true;
+ }
+ return false;
+}
+
+// ·½¿éÏÂÂäÒ»¸ñ
+bool tetris_drop_piece() {
+ return tetris_move_piece(0, 1);
+}
+
+// ·½¿éÖ±½ÓÂäµ½µ×²¿
+void tetris_hard_drop() {
+ while (tetris_drop_piece());
+ tetris_lock_piece();
+}
+
+// Ëø¶¨·½¿éµ½ÓÎÏ·°å
+void tetris_lock_piece() {
+ // ½«·½¿é¹Ì¶¨µ½ÓÎÏ·°å
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ if (tetrominoes[game.current_piece.type][game.current_piece.rotation][i][j] != 0) {
+ int board_x = game.current_piece.x + j;
+ int board_y = game.current_piece.y + i;
+ if (board_y >= 0) {
+ game.board[board_y][board_x] = tetrominoes[game.current_piece.type][game.current_piece.rotation][i][j];
+ }
+ }
+ }
+ }
+
+ // ¼ì²é²¢Ïû³ýÍêÕûµÄÐÐ
+ tetris_clear_lines();
+
+ // Éú³Éз½¿é
+ tetris_spawn_piece();
+}
+
+// Ïû³ýÍêÕûµÄÐÐ
+void tetris_clear_lines() {
+ int lines_cleared = 0;
+
+ for (int i = BOARD_HEIGHT - 1; i >= 0; i--) {
+ bool line_complete = true;
+
+ // ¼ì²éÐÐÊÇ·ñÍêÕû
+ for (int j = 0; j < BOARD_WIDTH; j++) {
+ if (game.board[i][j] == 0) {
+ line_complete = false;
+ break;
+ }
+ }
+
+ // Èç¹ûÐÐÍêÕû£¬Ïû³ý²¢ÏÂÒÆÉÏÃæµÄÐÐ
+ if (line_complete) {
+ for (int k = i; k > 0; k--) {
+ memcpy(game.board[k], game.board[k-1], sizeof(game.board[0]));
+ }
+ memset(game.board[0], 0, sizeof(game.board[0]));
+ lines_cleared++;
+ i++; // ¼ì²éÐÂÏÂÒÆµÄÐÐ
+ }
+ }
+
+ // ¸üзÖÊýºÍµÈ¼¶
+ if (lines_cleared > 0) {
+ // ·ÖÊý¼ÆË㣺1ÐÐ100·Ö£¬2ÐÐ300·Ö£¬3ÐÐ600·Ö£¬4ÐÐ1000·Ö
+ int line_scores[] = {0, 100, 300, 600, 1000};
+ game.score += line_scores[lines_cleared] * game.level;
+
+ // ÿÏû³ý10ÐÐÉýÒ»¼¶
+ game.level = game.score / 1000 + 1;
+ }
+}
+
+// ¸üÐÂÓÎϷ״̬
+void tetris_update() {
+ if (game.game_over) {
+ return;
+ }
+
+ // ³¢ÊÔÏÂÂä·½¿é
+ if (!tetris_drop_piece()) {
+ // ·½¿éÎÞ·¨ÏÂÂä£¬Ëø¶¨µ½ÓÎÏ·°å
+ tetris_lock_piece();
+ }
+}
+
+// »æÖÆÓÎÏ·
+void tetris_draw(uint16_t *framebuffer) {
+ // Çå¿ÕÆÁÄ»
+ memset(framebuffer, 0, SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(uint16_t));
+
+ // ¼ÆËã·½¿é´óС
+ int cell_size = (SCREEN_WIDTH - 20) / (BOARD_WIDTH + 6); // 6ÁÐÓÃÓÚÏÂÒ»¸ö·½¿éÔ¤ÀÀ
+
+ // »æÖÆÓÎÏ·ÇøÓò±ß¿ò
+ tetris_draw_rect(framebuffer, 10, 10, BOARD_WIDTH * cell_size + 10, BOARD_HEIGHT * cell_size + 10, 0xFFFF);
+
+ // »æÖÆÓÎÏ·°å
+ for (int i = 0; i < BOARD_HEIGHT; i++) {
+ for (int j = 0; j < BOARD_WIDTH; j++) {
+ if (game.board[i][j] != 0) {
+ tetris_draw_cell(framebuffer, 10 + j * cell_size + 1, 10 + i * cell_size + 1, cell_size - 1, cell_size - 1, colors[game.board[i][j]]);
+ }
+ }
+ }
+
+ // »æÖƵ±Ç°·½¿é
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ if (tetrominoes[game.current_piece.type][game.current_piece.rotation][i][j] != 0) {
+ int x = game.current_piece.x + j;
+ int y = game.current_piece.y + i;
+ if (y >= 0) {
+ tetris_draw_cell(framebuffer, 10 + x * cell_size + 1, 10 + y * cell_size + 1, cell_size - 1, cell_size - 1, colors[tetrominoes[game.current_piece.type][game.current_piece.rotation][i][j]]);
+ }
+ }
+ }
+ }
+
+ // »æÖÆÏÂÒ»¸ö·½¿éÔ¤ÀÀÇøÓò
+ int preview_x = 10 + BOARD_WIDTH * cell_size + 20;
+ int preview_y = 10;
+ tetris_draw_rect(framebuffer, preview_x, preview_y, preview_x + 6 * cell_size, preview_y + 6 * cell_size, 0xFFFF);
+
+ // »æÖÆÏÂÒ»¸ö·½¿é
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ if (tetrominoes[game.next_piece.type][game.next_piece.rotation][i][j] != 0) {
+ tetris_draw_cell(framebuffer, preview_x + (j + 1) * cell_size + 1, preview_y + (i + 1) * cell_size + 1, cell_size - 1, cell_size - 1, colors[tetrominoes[game.next_piece.type][game.next_piece.rotation][i][j]]);
+ }
+ }
+ }
+
+ // »æÖÆ·ÖÊýºÍµÈ¼¶
+ tetris_draw_text(framebuffer, "Score:", preview_x, preview_y + 7 * cell_size, 0xFFFF);
+ tetris_draw_number(framebuffer, game.score, preview_x + 40, preview_y + 7 * cell_size, 0xFFFF);
+
+ tetris_draw_text(framebuffer, "Level:", preview_x, preview_y + 8 * cell_size, 0xFFFF);
+ tetris_draw_number(framebuffer, game.level, preview_x + 40, preview_y + 8 * cell_size, 0xFFFF);
+
+ // Èç¹ûÓÎÏ·½áÊø£¬»æÖÆÓÎÏ·½áÊøÐÅÏ¢
+ if (game.game_over) {
+ tetris_draw_rect(framebuffer, SCREEN_WIDTH / 2 - 60, SCREEN_HEIGHT / 2 - 30, SCREEN_WIDTH / 2 + 60, SCREEN_HEIGHT / 2 + 30, 0x0000);
+ tetris_draw_rect(framebuffer, SCREEN_WIDTH / 2 - 62, SCREEN_HEIGHT / 2 - 32, SCREEN_WIDTH / 2 + 62, SCREEN_HEIGHT / 2 + 32, 0xFFFF);
+ tetris_draw_text(framebuffer, "GAME OVER", SCREEN_WIDTH / 2 - 40, SCREEN_HEIGHT / 2 - 10, 0xFFFF);
+ tetris_draw_text(framebuffer, "Score:", SCREEN_WIDTH / 2 - 30, SCREEN_HEIGHT / 2 + 10, 0xFFFF);
+ tetris_draw_number(framebuffer, game.score, SCREEN_WIDTH / 2 + 10, SCREEN_HEIGHT / 2 + 10, 0xFFFF);
+ }
+}
+
+// »æÖƾØÐÎ
+void tetris_draw_rect(uint16_t *framebuffer, int x1, int y1, int x2, int y2, uint16_t color) {
+ for (int y = y1; y < y2; y++) {
+ for (int x = x1; x < x2; x++) {
+ if (x >= 0 && x < SCREEN_WIDTH && y >= 0 && y < SCREEN_HEIGHT) {
+ framebuffer[y * SCREEN_WIDTH + x] = color;
+ }
+ }
+ }
+}
+
+// »æÖƵ¥Ôª¸ñ
+void tetris_draw_cell(uint16_t *framebuffer, int x, int y, int width, int height, uint16_t color) {
+ // »æÖƵ¥Ôª¸ñÖ÷Ìå
+ tetris_draw_rect(framebuffer, x, y, x + width, y + height, color);
+
+ // »æÖƵ¥Ôª¸ñ±ß¿ò
+ uint16_t border_color = color > 0x7FFF ? color - 0x1010 : color + 0x1010;
+ tetris_draw_line(framebuffer, x, y, x + width - 1, y, border_color);
+ tetris_draw_line(framebuffer, x, y, x, y + height - 1, border_color);
+
+ // »æÖƵ¥Ôª¸ñ¸ß¹â
+ uint16_t highlight_color = color + 0x2020;
+ tetris_draw_line(framebuffer, x + width - 1, y + 1, x + width - 1, y + height - 1, highlight_color);
+ tetris_draw_line(framebuffer, x + 1, y + height - 1, x + width - 1, y + height - 1, highlight_color);
+}
+
+// »æÖÆÏß¶Î
+void tetris_draw_line(uint16_t *framebuffer, int x1, int y1, int x2, int y2, uint16_t color) {
+ // BresenhamÖ±ÏßËã·¨
+ int dx = abs(x2 - x1);
+ int dy = abs(y2 - y1);
+ int sx = x1 < x2 ? 1 : -1;
+ int sy = y1 < y2 ? 1 : -1;
+ int err = dx - dy;
+
+ while (1) {
+ if (x1 >= 0 && x1 < SCREEN_WIDTH && y1 >= 0 && y1 < SCREEN_HEIGHT) {
+ framebuffer[y1 * SCREEN_WIDTH + x1] = color;
+ }
+
+ if (x1 == x2 && y1 == y2) {
+ break;
+ }
+
+ int e2 = 2 * err;
+ if (e2 > -dy) {
+ err -= dy;
+ x1 += sx;
+ }
+ if (e2 < dx) {
+ err += dx;
+ y1 += sy;
+ }
+ }
+}
+
+// »æÖÆÎı¾
+void tetris_draw_text(uint16_t *framebuffer, const char *text, int x, int y, uint16_t color) {
+ // ¼òµ¥µÄÎı¾»æÖƺ¯Êý£¬Ö»Ö§³ÖÊý×ֺʹóд×Öĸ
+ const uint8_t font[] = {
+ // 0
+ 0x3E, 0x41, 0x41, 0x41, 0x3E,
+ // 1
+ 0x00, 0x21, 0x7F, 0x01, 0x00,
+ // 2
+ 0x21, 0x43, 0x45, 0x49, 0x31,
+ // 3
+ 0x22, 0x41, 0x49, 0x49, 0x36,
+ // 4
+ 0x0C, 0x14, 0x24, 0x7F, 0x04,
+ // 5
+ 0x72, 0x51, 0x51, 0x51, 0x4E,
+ // 6
+ 0x1E, 0x29, 0x49, 0x49, 0x06,
+ // 7
+ 0x40, 0x47, 0x48, 0x50, 0x60,
+ // 8
+ 0x36, 0x49, 0x49, 0x49, 0x36,
+ // 9
+ 0x30, 0x49, 0x49, 0x4A, 0x3C,
+ // A
+ 0x1E, 0x21, 0x21, 0x21, 0x1E,
+ // B
+ 0x3F, 0x49, 0x49, 0x49, 0x26,
+ // C
+ 0x1E, 0x21, 0x21, 0x21, 0x21,
+ // D
+ 0x3F, 0x21, 0x21, 0x21, 0x1E,
+ // E
+ 0x3F, 0x49, 0x49, 0x49, 0x41,
+ // F
+ 0x3F, 0x48, 0x48, 0x48, 0x40,
+ // G
+ 0x1E, 0x21, 0x29, 0x29, 0x3E,
+ // H
+ 0x3F, 0x08, 0x08, 0x08, 0x3F,
+ // I
+ 0x00, 0x41, 0x3F, 0x41, 0x00,
+ // J
+ 0x06, 0x01, 0x41, 0x3F, 0x00,
+ // K
+ 0x3F, 0x08, 0x14, 0x22, 0x41,
+ // L
+ 0x3F, 0x01, 0x01, 0x01, 0x01,
+ // M
+ 0x3F, 0x20, 0x10, 0x20, 0x3F,
+ // N
+ 0x3F, 0x20, 0x10, 0x08, 0x3F,
+ // O
+ 0x1E, 0x21, 0x21, 0x21, 0x1E,
+ // P
+ 0x3F, 0x48, 0x48, 0x48, 0x30,
+ // Q
+ 0x1E, 0x21, 0x25, 0x22, 0x1D,
+ // R
+ 0x3F, 0x48, 0x4C, 0x4A, 0x31,
+ // S
+ 0x26, 0x49, 0x49, 0x49, 0x32,
+ // T
+ 0x40, 0x40, 0x7F, 0x40, 0x40,
+ // U
+ 0x3F, 0x01, 0x01, 0x01, 0x3F,
+ // V
+ 0x3F, 0x02, 0x04, 0x02, 0x3F,
+ // W
+ 0x3F, 0x10, 0x0C, 0x10, 0x3F,
+ // X
+ 0x21, 0x12, 0x0C, 0x12, 0x21,
+ // Y
+ 0x21, 0x12, 0x0C, 0x04, 0x04,
+ // Z
+ 0x41, 0x43, 0x45, 0x49, 0x41
+ };
+
+ int char_width = 6;
+ int char_height = 5;
+
+ while (*text) {
+ char c = *text++;
+ int index = -1;
+
+ if (c >= '0' && c <= '9') {
+ index = (c - '0') * 5;
+ } else if (c >= 'A' && c <= 'Z') {
+ index = (c - 'A' + 10) * 5;
+ } else if (c == ':') {
+ index = 35 * 5; // ðºÅµÄλÖÃ
+ }
+
+ if (index >= 0) {
+ for (int i = 0; i < char_height; i++) {
+ uint8_t row = font[index + i];
+ for (int j = 0; j < 5; j++) {
+ if (row & (1 << (4 - j))) {
+ if (x + j < SCREEN_WIDTH && y + i < SCREEN_HEIGHT) {
+ framebuffer[(y + i) * SCREEN_WIDTH + (x + j)] = color;
+ }
+ }
+ }
+ }
+ }
+
+ x += char_width;
+ }
+}
+
+// »æÖÆÊý×Ö
+void tetris_draw_number(uint16_t *framebuffer, int number, int x, int y, uint16_t color) {
+ char buffer[10];
+ sprintf(buffer, "%d", number);
+ tetris_draw_text(framebuffer, buffer, x, y, color);
+}