基于粤嵌开发板的c语言游戏2048源代码
时间: 2023-12-26 08:23:42 浏览: 101
以下是基于粤嵌开发板的C语言游戏2048的简单源代码,仅供参考:
```c
#include "gd32f30x.h"
#include "systick.h"
#include "lcd.h"
#include "touch.h"
#include "usart.h"
#include "stdlib.h"
#include "stdio.h"
#define ROWS 4
#define COLS 4
#define WIDTH 60
#define HEIGHT 60
#define BOARD_X 20
#define BOARD_Y 80
#define SCORE_X 30
#define SCORE_Y 30
int board[ROWS][COLS]; // 存储方块数字
int score = 0; // 得分
// 绘制方块
void draw_block(int x, int y, int num) {
char buf[10];
int width = WIDTH - 10;
int height = HEIGHT - 10;
lcd_draw_rect(x, y, width, height, BLACK, BLACK);
if (num > 0) {
lcd_draw_fill_rect(x + 5, y + 5, width, height, WHITE);
sprintf(buf, "%d", num);
lcd_draw_string(x + width / 2 - 10, y + height / 2 - 10, buf, BLACK, WHITE);
}
}
// 绘制分数
void draw_score() {
char buf[20];
sprintf(buf, "SCORE: %d", score);
lcd_draw_string(SCORE_X, SCORE_Y, buf, BLUE, WHITE);
}
// 游戏初始化
void init_game() {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
board[i][j] = 0;
}
}
score = 0;
draw_score();
}
// 随机生成数字方块
void generate_block() {
int row, col;
do {
row = rand() % ROWS;
col = rand() % COLS;
} while (board[row][col] != 0);
board[row][col] = rand() % 2 + 1; // 生成1或2
}
// 绘制游戏界面
void draw_board() {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
draw_block(BOARD_X + j * WIDTH, BOARD_Y + i * HEIGHT, board[i][j]);
}
}
}
// 检查游戏是否结束
int check_gameover() {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (board[i][j] == 0) {
return 0; // 还有空位,游戏未结束
}
if (i + 1 < ROWS && board[i][j] == board[i + 1][j]) {
return 0; // 横向上还有相同数字,游戏未结束
}
if (j + 1 < COLS && board[i][j] == board[i][j + 1]) {
return 0; // 纵向上还有相同数字,游戏未结束
}
}
}
return 1; // 游戏结束
}
// 移动方块
void move_block(int sx, int sy, int dx, int dy) {
int temp = board[sy][sx];
board[sy][sx] = 0;
board[dy][dx] = temp;
}
// 合并方块
void merge_block(int sx, int sy, int dx, int dy) {
board[dy][dx] *= 2;
board[sy][sx] = 0;
score += board[dy][dx];
}
// 处理向左移动
void move_left() {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (board[i][j] == 0) {
continue;
}
int x, y;
for (x = j + 1; x < COLS; x++) {
if (board[i][x] != 0) {
if (board[i][j] == board[i][x]) {
merge_block(x, i, j, i);
} else {
if (board[i][j] == 0) {
move_block(x, i, j, i);
j--;
}
}
break;
}
}
}
}
}
// 处理向右移动
void move_right() {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = COLS - 1; j >= 0; j--) {
if (board[i][j] == 0) {
continue;
}
int x, y;
for (x = j - 1; x >= 0; x--) {
if (board[i][x] != 0) {
if (board[i][j] == board[i][x]) {
merge_block(x, i, j, i);
} else {
if (board[i][j] == 0) {
move_block(x, i, j, i);
j++;
}
}
break;
}
}
}
}
}
// 处理向上移动
void move_up() {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (board[i][j] == 0) {
continue;
}
int x, y;
for (y = i + 1; y < ROWS; y++) {
if (board[y][j] != 0) {
if (board[i][j] == board[y][j]) {
merge_block(j, y, j, i);
} else {
if (board[i][j] == 0) {
move_block(j, y, j, i);
i--;
}
}
break;
}
}
}
}
}
// 处理向下移动
void move_down() {
int i, j;
for (i = ROWS - 1; i >= 0; i--) {
for (j = 0; j < COLS; j++) {
if (board[i][j] == 0) {
continue;
}
int x, y;
for (y = i - 1; y >= 0; y--) {
if (board[y][j] != 0) {
if (board[i][j] == board[y][j]) {
merge_block(j, y, j, i);
} else {
if (board[i][j] == 0) {
move_block(j, y, j, i);
i++;
}
}
break;
}
}
}
}
}
// 处理触摸事件
void handle_touch_event(touch_event_t event) {
int x = event.x - BOARD_X;
int y = event.y - BOARD_Y;
if (x >= 0 && x < WIDTH * COLS && y >= 0 && y < HEIGHT * ROWS) {
int col = x / WIDTH;
int row = y / HEIGHT;
if (board[row][col] == 0) {
generate_block();
draw_board();
}
}
}
int main(void) {
systick_config();
lcd_config();
touch_config();
usart_config();
lcd_clear(WHITE);
lcd_draw_string(BOARD_X, BOARD_Y - 50, "2048 GAME", BLUE, WHITE);
lcd_draw_rect(BOARD_X - 5, BOARD_Y - 5, WIDTH * COLS + 10, HEIGHT * ROWS + 10, BLACK, BLACK);
init_game();
generate_block();
generate_block();
draw_board();
while (1) {
touch_event_t event = get_touch_event();
if (event.type == TOUCH_EVENT_PRESS) {
handle_touch_event(event);
} else if (event.type == TOUCH_EVENT_SWIPE_RIGHT) {
move_right();
if (!check_gameover()) {
generate_block();
}
draw_board();
} else if (event.type == TOUCH_EVENT_SWIPE_LEFT) {
move_left();
if (!check_gameover()) {
generate_block();
}
draw_board();
} else if (event.type == TOUCH_EVENT_SWIPE_UP) {
move_up();
if (!check_gameover()) {
generate_block();
}
draw_board();
} else if (event.type == TOUCH_EVENT_SWIPE_DOWN) {
move_down();
if (!check_gameover()) {
generate_block();
}
draw_board();
}
}
}
```
这是一个简单的实现,仅供参考,如果需要更完整的代码,建议自行搜索。
阅读全文