推箱子游戏
- 项目名称:推箱子
- 所用知识点:C/C++ 基础知识
- 所用工具:vs2013&Easyx
程序思路分析
(效果图)
程序思路:
第一步:创建窗口
#include<graphics.h>
#include<stdio..h>
int main()
{
initgraph(400, 400);//创建400px * 400px的窗口
system ("pause");
return 0;
}
第二步:贴图(贴图三部曲:定义图片,加载图片,显示图片)
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int main()
{
initgraph(400, 400);//创建400px * 400px的窗口
IMAGE b;//定义图片
loadimage(&b, "b.bmp", 50, 50);//加载图片
putimage(j * 50, i * 50, &b);//显示图片
system ("pause");
return 0;
}`
第三步:箱子的移动(图片的移动)
{
int x, y;//表示人物所在位置的下标
for (int j = 0; j < 8; j++)
{
for (int i = 0; i < 8; i++)
{
if (Map[j][i] == 5 || Map[j][i] == 8)
{
x = j;
y = i;
}
}
}
char key = _getch();
//Sleep(30);
switch (key)//获取按键
{
//向上
case 72:
case 'w':
case 'W':
if (Map[x - 1][y] == 0 || Map[x - 1][y] == 3)
{
Map[x - 1][y] += 5;
Map[x][y] -= 5;
}
else if (Map[x - 1][y] == 4 || Map[x - 1][y] == 7)
if (Map[x - 2][y] == 0 || Map[x - 1][y] == 3)
{
Map[x - 2][y] += 4;
Map[x - 1][y] += 1;
Map[x][y] -= 5;
}
break;
case 80:
case 's':
case 'S':
if (Map[x + 1][y] == 0 || Map[x + 1][y] == 3)
{
Map[x + 1][y] += 5;
Map[x][y] -= 5;
}
else if (Map[x + 1][y] == 4 || Map[x + 1][y] == 7)
if (Map[x + 2][y] == 0 || Map[x + 1][y] == 3)
{
Map[x + 2][y] += 4;
Map[x + 1][y] += 1;
Map[x][y] -= 5;
}
break;
case 77:
case 'd':
case 'D':
if (Map[x][y + 1] == 0 || Map[x][y + 1] == 3)
{
Map[x][y + 1] += 5;
Map[x][y] -= 5;
}
else if (Map[x][y + 1] == 4 || Map[x][y + 1] == 7)
if (Map[x][y + 2] == 0 || Map[x][y + 1] == 3)
{
Map[x][y + 2] += 4;
Map[x][y + 1] += 1;
Map[x][y] -= 5;
}
break;
case 75:
case 'a':
case 'A':
if (Map[x][y - 1] == 0 || Map[x][y - 1] == 3)
{
Map[x][y - 1] += 5;
Map[x][y] -= 5;
}
else if (Map[x][y - 1] == 4 || Map[x][y - 1] == 7)
if (Map[x][y - 2] == 0 || Map[x][y - 1] == 3)
{
Map[x][y - 2] += 4;
Map[x][y - 1] += 1;
Map[x][y] -= 5;
}
break;
}
程序的源代码
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
IMAGE a, aby, b, c, d, dby, e, f;
void InitImg()
{
//1.加载背景
loadimage(&f, "f.jpg", 400, 400);
//加载小鸟
loadimage(&a, "a.bmp", 50, 50);
loadimage(&aby, "aby.bmp", 50, 50);
loadimage(&b, "b.bmp", 50, 50);
loadimage(&c, "c.bmp", 50, 50);
loadimage(&d, "d.bmp", 50, 50);
loadimage(&dby, "dby.bmp", 50, 50);
loadimage(&e, "e.bmp", 50, 50);
}
int Map[8][8] = {
{ 0, 0, 1, 1, 1, 0, 0, 0 },
{ 0, 0, 1, 3, 1, 0, 0, 0 },
{ 0, 0, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 4, 0, 4, 3, 1 },
{ 1, 3, 0, 4, 5, 1, 1, 1 },
{ 1, 1, 1, 1, 4, 1, 0, 0 },
{ 0, 0, 0, 1, 3, 1, 0, 0 },
{ 0, 0, 0, 1, 1, 1, 0, 0 },
};
void DrawMap()
{
putimage(0, 0, &f);
int i, j;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++) {
switch (Map[i][j]) {
case 0:printf(" ");
break;
case 1:printf("@");
putimage(j * 50, i * 50, &b);
break;
case 3:printf("#");
putimage(j * 50, i * 50, &d, SRCAND);
putimage(j * 50, i * 50, &dby, SRCPAINT);
break;
case 4:printf("$");
putimage(j * 50, i * 50, &c);
break;
case 5:printf("%");
putimage(j * 50, i * 50, &a, SRCAND);
putimage(j * 50, i * 50, &aby, SRCPAINT);
break;
case 7:
printf("^");
putimage(j * 50, i * 50, &e);
break;
}
}
printf("\n");
}
}
void key_Down()
{
int x, y;//表示人物所在位置的下标
for (int j = 0; j < 8; j++)
{
for (int i = 0; i < 8; i++)
{
if (Map[j][i] == 5 || Map[j][i] == 8)
{
x = j;
y = i;
}
}
}
char key = _getch();
//Sleep(30);
switch (key)//获取按键
{
//向上
case 72:
case 'w':
case 'W':
if (Map[x - 1][y] == 0 || Map[x - 1][y] == 3)
{
Map[x - 1][y] += 5;
Map[x][y] -= 5;
}
else if (Map[x - 1][y] == 4 || Map[x - 1][y] == 7)
if (Map[x - 2][y] == 0 || Map[x - 1][y] == 3)
{
Map[x - 2][y] += 4;
Map[x - 1][y] += 1;
Map[x][y] -= 5;
}
break;
case 80:
case 's':
case 'S':
if (Map[x + 1][y] == 0 || Map[x + 1][y] == 3)
{
Map[x + 1][y] += 5;
Map[x][y] -= 5;
}
else if (Map[x + 1][y] == 4 || Map[x + 1][y] == 7)
if (Map[x + 2][y] == 0 || Map[x + 1][y] == 3)
{
Map[x + 2][y] += 4;
Map[x + 1][y] += 1;
Map[x][y] -= 5;
}
break;
case 77:
case 'd':
case 'D':
if (Map[x][y + 1] == 0 || Map[x][y + 1] == 3)
{
Map[x][y + 1] += 5;
Map[x][y] -= 5;
}
else if (Map[x][y + 1] == 4 || Map[x][y + 1] == 7)
if (Map[x][y + 2] == 0 || Map[x][y + 1] == 3)
{
Map[x][y + 2] += 4;
Map[x][y + 1] += 1;
Map[x][y] -= 5;
}
break;
case 75:
case 'a':
case 'A':
if (Map[x][y - 1] == 0 || Map[x][y - 1] == 3)
{
Map[x][y - 1] += 5;
Map[x][y] -= 5;
}
else if (Map[x][y - 1] == 4 || Map[x][y - 1] == 7)
if (Map[x][y - 2] == 0 || Map[x][y - 1] == 3)
{
Map[x][y - 2] += 4;
Map[x][y - 1] += 1;
Map[x][y] -= 5;
}
break;
}
}
int main()
{
initgraph(400, 400);
InitImg();
DrawMap();
while (1)
{
DrawMap();
key_Down();
system("cls");
}
getchar();
system("pause");
}