C语言写一个推箱子小游戏,全当练习。本项目使用了EasyX 库,为什么要用这个第三方库呢?这个库是用来干什么的呢?
许多学编程的都是从 C 语言开始入门的,有些学校以 Turbo C 为环境讲 C 语言,只是 Turbo C 的环境实在太老了,复制粘贴都很不方便,所以有些学校直接拿 VC 来讲 C 语言,因为 VC 的编辑和调试环境都很优秀,并且 VC 有适合教学的免费版本。可惜在 VC 下只能做一些文字性的练习题,想画条直线画个圆都很难,还要注册窗口类、建消息循环等等。还有计算机图形学,这门课程的重点是绘图算法,而不是 Windows 编程。所以,许多老师不得不用 TC 教学,因为 Windows 绘图太复杂了,会偏离教学的重点。所以,为了VC 方便的开发平台和 TC 简单的绘图功能,EasyX 库出现了。
界面展示(部分)
源码展示(部分)
MOUSEMSG v;
while (true)
{
loadimage(NULL, path1, 450, 450);
v = GetMouseMsg();
if (v.uMsg == WM_LBUTTONDOWN)
{
settextcolor(BLUE);
settextstyle(20, 10, 0);
outtext(_T("W为向上,S为向下,A为向左,D为向右"));
char t, o;//b,c确定小人的位置 x,y,p,q,r,u确定星号的位置
int n = 1, aa, bb, j, i, b, c, x = 0, y = 0, r = 0, u = 0, p = 0, q = 0, a[11][11] = { 0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,1,1,1,0,0,0,0,
0,0,0,0,1,2,1,0,0,0,0,
0,0,0,0,1,0,1,1,1,1,0,
0,0,1,1,1,3,0,3,2,1,0,
0,0,1,2,0,3,4,1,1,1,0,
0,0,1,1,1,1,3,1,0,0,0,
0,0,0,0,0,1,2,1,0,0,0,
0,0,0,0,0,1,1,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,0 },
f[11][11] = { 0,0,0,0,0,0,0,0,0,0,0,
0,1,1,1,1,1,0,0,0,0,0,
0,1,4,0,0,1,0,0,0,0,0,
0,1,0,3,3,1,0,1,1,1,0,
0,1,0,3,0,1,0,1,2,1,0,
0,1,1,1,0,1,1,1,2,1,0,
0,0,1,1,0,0,0,0,2,1,0,
0,0,1,0,0,0,1,0,0,1,0,
0,0,1,0,0,0,1,1,1,1,0,
0,0,1,1,1,1,1,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,0 },
g[11][11] = { 0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,0,0,0,
0,0,1,4,0,1,1,1,0,0,0,
0,0,1,0,3,0,0,1,0,0,0,
0,1,1,1,0,1,0,1,1,0,0,
0,1,2,1,0,1,0,0,1,0,0,
0,1,2,3,0,0,1,0,1,0,0,
0,1,2,0,0,0,3,0,1,0,0,
0,1,1,1,1,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0 };
abc:
settextstyle(40, 20, 0);
for (i = 0, aa = 0; i < 11; i++, aa += 40)
{
for (j = 0, bb = 0; j < 11; j++, bb += 40)
{
if (a[i][j] == 0)
putimage(bb, aa, &tupian[0]);
if (a[i][j] == 1)
putimage(bb, aa, &tupian[1]);
if (a[i][j] == 2)
putimage(bb, aa, &tupian[2]);
if (a[i][j] == 3)
putimage(bb, aa, &tupian[3]);
if (a[i][j] == 4)
{
putimage(bb, aa, &tupian[4]);
b = i;
c = j;
}
}
}
t = _getch();
while (1)
{
cleardevice();
if (t == 27)
{
loadimage(NULL, path2), 450, 450);
Sleep(2000);
exit(0);
}
if (t == 'w' || t == 'W')
{
if (a[b - 1][c] == 0)
{
a[b][c] = 0;
a[b - 1][c] = 4;
}
if (a[b - 1][c] == 2)
{
if ((b == r && c == u) || (b == p && c == q) || (b == x && c == y))
{
a[b - 1][c] = 4;
a[b][c] = 2;
}
else
{
a[b][c] = 0;
a[b - 1][c] = 4;
}
}
if (a[b - 1][c] == 3)
{
if (a[b - 2][c] == 0 || a[b - 2][c] == 2)
{
a[b - 1][c] = 4;
a[b - 2][c] = 3;
a[b][c] = 0;
}
if ((b == r && c == u) || (b == p && c == q) || (b == x && c == y))
{
if (a[b - 1][c] != 1 && a[b - 1][c] != 3)
{
a[b - 1][c] = 4;
a[b][c] = 2;
}
}
}
}
if (t == 's' || t == 'S')
{
if (a[b + 1][c] == 0 || a[b + 1][c] == 2)
{
a[b][c] = 0;
a[b + 1][c] = 4;
}
if ((b == r && c == u) || (b == p && c == q) || (b == x && c == y))
{
if (a[b + 1][c] != 3 && a[b + 1][c] != 1)
{
a[b][c] = 2;
a[b + 1][c] = 4;
}
}
if (a[b + 1][c] == 3)
{
if (a[b + 2][c] == 1)
break;
if (a[b + 2][c] == 0 || a[b + 2][c] == 2)
{
a[b + 1][c] = 4;
a[b + 2][c] = 3;
a[b][c] = 0;
}
}
}
if (t == 'a' || t == 'A')
{
if (a[b][c - 1] == 2 || a[b][c - 1] == 0)
{
a[b][c - 1] = 4;
a[b][c] = 0;
}
if ((b == x && c == y) || (b == r && c == u) || (b == p && c == q))
{
if (a[b][c - 1] != 3 && a[b][c - 1] != 1)
{
a[b][c] = 2;
a[b][c - 1] = 4;
}
}
if (a[b][c - 1] == 3)
{
if (a[b][c - 2] == 0 || a[b][c - 2] == 2)
{
a[b][c - 1] = 4;
a[b][c - 2] = 3;
a[b][c] = 0;
}
}
}
if (t == 'd' || t == 'D')
{
if (a[b][c + 1] == 0 || a[b][c + 1] == 2)
{
a[b][c] = 0;
a[b][c + 1] = 4;
}
if ((b == r && c == u) || (b == x && c == y) || (b == p && c == q))
{
if (a[b][c + 1] != 3 && a[b][c + 1] != 1)
{
a[b][c] = 2;
a[b][c + 1] = 4;
}
}
if (a[b][c + 1] == 3)
{
if (a[b][c + 2] == 1)
break;
if (a[b][c + 2] == 0 || a[b][c + 2] == 2)
{
a[b][c + 1] = 4;
a[b][c + 2] = 3;
a[b][c] = 0;
}
}
}
break;
}
if (n == 1)
{
if (a[3][5] == 3 && a[6][3] == 3 && a[8][6] == 3 && a[5][8] == 3)
{
loadimage(NULL, path3, 450, 450);
Sleep(2000);
n++;
for (i = 0; i < 11; i++)
{
for (j = 0; j < 11; j++)
a[i][j] = f[i][j];
}
p = 4, q = 8, r = 5, u = 8, x = 6, y = 8;
cleardevice();
goto abc;
}
else
goto abc;
}
if (n == 2)
{
if (a[4][8] == 3 && a[5][8] == 3 && a[6][8] == 3)
{
loadimage(NULL, path4, 450, 450);
Sleep(2000);
n++;
for (i = 0; i < 11; i++)
{
for (j = 0; j < 11; j++)
a[i][j] = g[i][j];
}
p = 6, q = 2, r = 7, u = 2, x = 8, y = 2;
cleardevice();
goto abc;
}
else
goto abc;
}
if (n == 3)
{
if (a[6][2] == 3 && a[7][2] == 3 && a[8][2] == 3)
{
loadimage(NULL, path5, 450, 450);
Sleep(4000);
exit(0);
}
else
goto abc;
}
}
}