新手小白,刚学到数组,因为学的不多,所以不知道怎么向左向右跳动,所以只能勇q、e代替。 因为地图太大了,就不想写了,所以地图只有开头那一段,实现方式可能比较笨,不要笑我
操作方式:w 跳跃;a 左移;d 右移;q 向左跳;e 向右跳。
马里奥长这个样:&
#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
#define ROW 10
#define COL 24
void menu()//菜单
{
printf("#######################\n");
printf("####1、play 2、exit####\n");
printf("#######################\n");
printf("#######################\n");
}
void InitBoard(char board[ROW][COL], int row, int col)//初始化
{
int i = 0;
int j = 0;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
board[i][j] = ' ';
}
}
}
void DisplayBoard(char board[ROW][COL], int row, int col)//打印
{
int i = 0;
int j = 0;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
printf("%c", board[i][j]);
}
printf("\n");
}
}
void Map(char board[ROW][COL], int row, int col)// 地图
{
int i = 0;
//第一行(ROW=0)
for (i = 0; i < COL; i++)
{
board[0][i] = ' ';
}
printf("\n");
//第二行(ROW=1)
for (i = 0; i < COL; i++)
{
board[1][i] = ' ';
}
printf("\n");
//第三行(row=2)
for (i = 0; i < COL; i++)
{
board[2][i] = ' ';
}
printf("\n");
//di4hang(row =3)
for (i = 0; i < 5; i++)
{
board[3][i] = ' ';
}
for (i = 5; i < 9; i++)
{
board[3][i] = '#';
}
for (i = 9; i < COL; i++)
{
board[3][i] = ' ';
}
printf("\n");
//di5 hang(ROW=4)
for (i = 0; i < 12; i++)
{
board[4][i] = ' ';
}
for (i = 12; i < 14; i++)
{
board[4][i] = '#';
}
for (i = 14; i < 16; i++)
{
board[4][i] = ' ';
}
for (i = 16; i < 18; i++)
{
board[4][i] = '#';
}
for (i = 18; i < COL; i++)
{
board[4][i] = ' ';
}
printf("\n");
//di 6 hang(ROW=5)
for (i = 0; i < 10; i++)
{
board[5][i] = ' ';
}
for (i = 9; i < 11; i++)
{
board[5][i] = '#';
}
for (i = 11; i < 12; i++)
{
board[5][i] = ' ';
}
for (i = 12; i < 14; i++)
{
board[5][i] = '#';
}
for (i = 14; i < 16; i++)
{
board[5][i] = ' ';
}
for (i = 16; i < 18; i++)
{
board[5][i] = '#';
}
for (i = 18; i < COL; i++)
{
board[5][i] = ' ';
}
printf("\n");
//di 7 hang(ROW=6)
//di 6 hang(ROW=5)
for (i = 0; i < 10; i++)
{
board[6][i] = ' ';
}
for (i = 9; i < 11; i++)
{
board[6][i] = '#';
}
for (i = 11; i < 12; i++)
{
board[6][i] = ' ';
}
for (i = 12; i < 14; i++)
{
board[6][i] = '#';
}
for (i = 14; i < 16; i++)
{
board[6][i] = ' ';
}
for (i = 16; i < 18; i++)
{
board[6][i] = '#';
}
for (i = 18; i < COL; i++)
{
board[6][i] = ' ';
}
printf("\n");
//di 8 hang(ROW=7)
for (i = 0; i < 20; i++)
{
board[7][i] = '#';
}
for (i = 20; i < 22; i++)
{
board[7][i] = ' ';
}
for (i = 22; i < COL; i++)
{
board[7][i] = '#';
}
//di 9 hang
for (i = 0; i < 20; i++)
{
board[8][i] = '#';
}
for (i = 20; i < 22; i++)
{
board[8][i] = ' ';
}
for (i = 22; i < COL; i++)
{
board[8][i] = '#';
}
}
void test()//游戏
{
int x = ROW - 4;
int y = 0;
char role = '&';//角色
char board[ROW][COL] = { 0 };
InitBoard(board, ROW, COL);
system("cls");
DisplayBoard(board, ROW, COL);
Map(board, ROW, COL);
board[x][y] = role;
system("cls");
DisplayBoard(board, ROW, COL);
while (1)
{
//方向键开始
int move = _getch();
//move left
if (move == 'a')
{
if (board[x][y - 1] != '#')
{
board[x][y] = board[x][y - 1];
y--;
board[x][y] = role;
system("cls");
DisplayBoard(board, ROW, COL);
Sleep(80);
}
//gravity 重力
while (board[x + 1][y] != '#')
{
board[x][y] = board[x + 1][y];
x++;
board[x][y] = role;
}
system("cls");
DisplayBoard(board, ROW, COL);
}
//move right
if (move == 'd')
{
if (board[x][y + 1] != '#')
{
board[x][y] = board[x][y + 1];
y++;
board[x][y] = role;
system("cls");
DisplayBoard(board, ROW, COL);
Sleep(80);
}
//gravity 重力
while (board[x + 1][y] != '#')
{
board[x][y] = board[x + 1][y];
x++;
board[x][y] = role;
}
system("cls");
DisplayBoard(board, ROW, COL);
}
//jump
if (move == 'w')
{
if (board[x - 1][y] != '#' && board[x - 2][y] != '#')
{
board[x][y] = board[x - 2][y];
x -= 2;
board[x][y] = role;
system("cls");
DisplayBoard(board, ROW, COL);
Sleep(80);
//gravity 重力
while (board[x + 1][y] != '#')
{
board[x][y] = board[x + 1][y];
x++;
board[x][y] = role;
}
system("cls");
DisplayBoard(board, ROW, COL);
}
if (board[x - 1][y] != '#' && board[x - 2][y] == '#')
{
board[x][y] = board[x - 1][y];
x -= 1;
board[x][y] = role;
system("cls");
DisplayBoard(board, ROW, COL);
Sleep(80);
//gravity 重力
while (board[x + 1][y] != '#')
{
board[x][y] = board[x + 1][y];
x++;
board[x][y] = role;
}
system("cls");
DisplayBoard(board, ROW, COL);
}
}
//jump right
if (move == 'e')
{
if (board[x - 1][y] != '#' && board[x - 2][y + 1] != '#' && board[x - 1][y + 2] != '#' && board[x][y + 3] != '#')
{
board[x][y] = board[x - 1][y];
x--;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
board[x][y] = board[x - 1][y + 1];
x--;
y++;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
board[x][y] = board[x + 1][y + 1];
x++;
y++;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
board[x][y] = board[x + 1][y + 1];
x++;
y++;
board[x][y] = role;
system("cls");
DisplayBoard(board, ROW, COL);
}
else if (board[x - 1][y] != '#' && board[x - 2][y + 1] != '#' && board[x - 1][y + 2] != '#' && board[x][y + 3] == '#')
{
board[x][y] = board[x - 1][y];
x--;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
board[x][y] = board[x - 1][y + 1];
x--;
y++;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
board[x][y] = board[x + 1][y + 1];
x++;
y++;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
}
else if (board[x - 1][y] != '#' && board[x - 2][y + 1] != '#' && board[x - 1][y + 2] == '#')
{
board[x][y] = board[x - 1][y];
x--;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
board[x][y] = board[x - 1][y + 1];
x--;
y++;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
}
else if (board[x - 1][y] != '#' && board[x - 2][y + 1] == '#')
{
board[x][y] = board[x - 1][y];
x--;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
}
else if (board[x - 1][y] != '#')
{
;
}
while (board[x + 1][y] != '#')
{
board[x][y] = board[x + 1][y];
x++;
board[x][y] = role;
}
system("cls");
DisplayBoard(board, ROW, COL);
}
//jump left
if (move == 'q')
{
if (board[x - 1][y] != '#' && board[x - 2][y - 1] != '#' && board[x - 1][y - 2] != '#' && board[x][y - 3] != '#')
{
board[x][y] = board[x - 1][y];
x--;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
board[x][y] = board[x - 1][y - 1];
x--;
y--;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
board[x][y] = board[x + 1][y - 1];
x++;
y--;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
board[x][y] = board[x + 1][y - 1];
x++;
y--;
board[x][y] = role;
system("cls");
DisplayBoard(board, ROW, COL);
}
else if (board[x - 1][y] != '#' && board[x - 2][y - 1] != '#' && board[x - 1][y - 2] != '#' && board[x][y - 3] == '#')
{
board[x][y] = board[x - 1][y];
x--;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
board[x][y] = board[x - 1][y - 1];
x--;
y--;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
board[x][y] = board[x + 1][y - 1];
x++;
y--;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
}
else if (board[x - 1][y] != '#' && board[x - 2][y - 1] != '#' && board[x - 1][y - 2] == '#')
{
board[x][y] = board[x - 1][y];
x--;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
board[x][y] = board[x - 1][y - 1];
x--;
y--;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
}
else if (board[x - 1][y] != '#' && board[x - 2][y - 1] == '#')
{
board[x][y] = board[x - 1][y];
x--;
board[x][y] = role;
Sleep(20);
system("cls");
DisplayBoard(board, ROW, COL);
}
else if (board[x - 1][y] != '#')
{
;
}
while (board[x + 1][y] != '#')
{
board[x][y] = board[x + 1][y];
x++;
board[x][y] = role;
}
system("cls");
DisplayBoard(board, ROW, COL);
}
//方向键结束
if (board[x][y] == board[6][COL - 1])//到达游戏最后一列,游戏结束
{
break;
}
}
Sleep(200);
system("cls");
printf(" \n");
printf(" WIN! \n");
printf(" \n");
printf(" \n");
}
int main()
{
menu();
int input;
again:
scanf_s("%d", &input);
switch (input)
{
case 1:
test();
break;
case 2:
break;
default:
goto again;
}
}
调试后: