帮我用C语言写一个推箱子的游戏
时间: 2023-04-03 12:03:43 浏览: 99
好的,我可以为您提供一个推箱子游戏的 C 语言代码。以下是代码:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define ROW 10
#define COL 10
int map[ROW][COL] = {
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
int player_x = 1;
int player_y = 1;
int box_x = 3;
int box_y = 3;
void draw_map()
{
int i, j;
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COL; j++)
{
if(map[i][j] == 1)
{
printf("#");
}
else if(i == player_y && j == player_x)
{
printf("P");
}
else if(i == box_y && j == box_x)
{
printf("B");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
void move_player(int x, int y)
{
if(map[y][x] == 0)
{
player_x = x;
player_y = y;
}
}
void move_box(int x, int y)
{
if(map[y][x] == 0)
{
box_x = x;
box_y = y;
}
}
int main()
{
char input;
while(1)
{
system("cls");
draw_map();
input = getch();
switch(input)
{
case 'w':
move_player(player_x, player_y - 1);
if(player_x == box_x && player_y == box_y - 1)
{
move_box(box_x, box_y - 1);
}
break;
case 'a':
move_player(player_x - 1, player_y);
if(player_x == box_x - 1 && player_y == box_y)
{
move_box(box_x - 1, box_y);
}
break;
case 's':
move_player(player_x, player_y + 1);
if(player_x == box_x && player_y == box_y + 1)
{
move_box(box_x, box_y + 1);
}
break;
case 'd':
move_player(player_x + 1, player_y);
if(player_x == box_x + 1 && player_y == box_y)
{
move_box(box_x + 1, box_y);
}
break;
case 'q':
exit(0);
break;
}
}
return 0;
}
阅读全文