好玩的c语言代码_做游戏,学编程(C语言) 11 2048
时间: 2024-01-24 10:05:06 浏览: 124
62套C语言小游戏源码
4星 · 用户满意度95%
好的,下面是一个基于C语言的2048小游戏代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 4
#define COL 4
void init_board(int board[][COL])
{
int i,j;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
board[i][j] = 0;
}
}
}
void print_board(int board[][COL])
{
int i,j;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
printf("%4d",board[i][j]);
}
printf("\n");
}
}
void random_number(int board[][COL])
{
int i,j;
int count = 0;
int rand_num = (rand()%2+1)*2;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
if (board[i][j] == 0)
{
count++;
}
}
}
int x = rand()%ROW;
int y = rand()%COL;
int n = rand()%count;
count = 0;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
if (board[i][j] == 0)
{
if (count == n)
{
board[i][j] = rand_num;
return;
}
count++;
}
}
}
}
int can_move(int board[][COL])
{
int i,j;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
if (board[i][j] == 0)
{
return 1;
}
if (i > 0 && board[i][j] == board[i-1][j])
{
return 1;
}
if (i < ROW-1 && board[i][j] == board[i+1][j])
{
return 1;
}
if (j > 0 && board[i][j] == board[i][j-1])
{
return 1;
}
if (j < COL-1 && board[i][j] == board[i][j+1])
{
return 1;
}
}
}
return 0;
}
void move_left(int board[][COL])
{
int i,j,k;
for (i = 0; i < ROW; i++)
{
k = 0;
for (j = 1; j < COL; j++)
{
if (board[i][j] != 0)
{
if (board[i][j] == board[i][k])
{
board[i][k] *= 2;
board[i][j] = 0;
}
else if (board[i][k] == 0)
{
board[i][k] = board[i][j];
board[i][j] = 0;
}
else
{
k++;
board[i][k] = board[i][j];
if (k != j)
{
board[i][j] = 0;
}
}
}
}
}
}
void move_right(int board[][COL])
{
int i,j,k;
for (i = 0; i < ROW; i++)
{
k = COL-1;
for (j = COL-2; j >= 0; j--)
{
if (board[i][j] != 0)
{
if (board[i][j] == board[i][k])
{
board[i][k] *= 2;
board[i][j] = 0;
}
else if (board[i][k] == 0)
{
board[i][k] = board[i][j];
board[i][j] = 0;
}
else
{
k--;
board[i][k] = board[i][j];
if (k != j)
{
board[i][j] = 0;
}
}
}
}
}
}
void move_up(int board[][COL])
{
int i,j,k;
for (j = 0; j < COL; j++)
{
k = 0;
for (i = 1; i < ROW; i++)
{
if (board[i][j] != 0)
{
if (board[i][j] == board[k][j])
{
board[k][j] *= 2;
board[i][j] = 0;
}
else if (board[k][j] == 0)
{
board[k][j] = board[i][j];
board[i][j] = 0;
}
else
{
k++;
board[k][j] = board[i][j];
if (k != i)
{
board[i][j] = 0;
}
}
}
}
}
}
void move_down(int board[][COL])
{
int i,j,k;
for (j = 0; j < COL; j++)
{
k = ROW-1;
for (i = ROW-2; i >= 0; i--)
{
if (board[i][j] != 0)
{
if (board[i][j] == board[k][j])
{
board[k][j] *= 2;
board[i][j] = 0;
}
else if (board[k][j] == 0)
{
board[k][j] = board[i][j];
board[i][j] = 0;
}
else
{
k--;
board[k][j] = board[i][j];
if (k != i)
{
board[i][j] = 0;
}
}
}
}
}
}
int main()
{
int board[ROW][COL];
char ch;
srand(time(NULL));
init_board(board);
random_number(board);
random_number(board);
print_board(board);
while (1)
{
ch = getchar();
if (ch == 'q')
{
break;
}
switch(ch)
{
case 'a':
move_left(board);
break;
case 'd':
move_right(board);
break;
case 'w':
move_up(board);
break;
case 's':
move_down(board);
break;
default:
continue;
}
if (can_move(board))
{
random_number(board);
print_board(board);
}
else
{
printf("Game over!\n");
break;
}
}
return 0;
}
```
这是一个控制台版的2048小游戏,你可以使用wasd或者方向键来移动方块,q键退出游戏。祝你玩得愉快!
阅读全文