新手小白,剛學到陣列,因為學的不多,所以不知道怎么向左向右跳動,所以只能勇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;
}
}
除錯后:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/343092.html
標籤:其他
上一篇:Educational Codeforces Round 116 (Rated for Div. 2) E. Arena(dp)
下一篇:資料結構與演算法---陣列
