C语言+easyX图形库的推箱子实现
发布日期:2021-05-09 00:16:56 浏览次数:10 分类:博客文章

本文共 5033 字,大约阅读时间需要 16 分钟。

main函数
#include
#include
#include
#include
#include"box_1.h"#include
IMAGE images[7];//存储6个图片,用图形库IMAGE的类struct pos MAN_position;//使用二维数组来初始化地图/* 0表示墙,1表示地板,2表示箱子的目的地 3表示人物 4表示箱子 5表示箱子成功命中目标*/int map[MAP_ROW][MAP_COL] = {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },{ 0, 1, 4, 1, 0, 2, 1, 0, 2, 1, 0, 0 },{ 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0 },{ 0, 1, 0, 2, 0, 1, 1, 4, 1, 1, 1, 0 },{ 0, 1, 1, 1, 0, 3, 1, 1, 1, 4, 1, 0 },{ 0, 1, 2, 1, 1, 4, 1, 1, 1, 1, 1, 0 },{ 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },};void ChangeMap(pos* pos, map_unit uint){ //将pos的位置下对应的数据和更改的数据进行交换并且在对应位置输出对应的图片 map[pos->x][pos->y] = uint; putimage(pos->y * 61, pos->x * 61, &images[uint]);}void GameInit(void){ //初始化第一个框架图 initgraph(LENGTH, WIDTH); //画地图 //用数组来储存对应的二维数组的图片 //开始加载图片到数组中 loadimage(images + 0, _T("res/wall_right.bmp")); loadimage(images + 1, _T("res/floor.bmp")); loadimage(images + 2, _T("res/des.bmp")); loadimage(images + 3, _T("res/man.bmp")); loadimage(images + 4, _T("res/box.bmp")); loadimage(images + 5, _T("res/box.bmp")); loadimage(images + 6, _T("res/sm7.jpg")); //存储背景图片输出 //开始画地图 for (int i = 0; i < MAP_ROW; i++)//行 { for (int j = 0; j < MAP_COL; j++)//列 { if (map[i][j] == MAN) //读取枚举列表中的man { MAN_position.x = i; MAN_position.y = j; } int k = map[i][j]; putimage(j * 61,i * 61, images+k); } printf("\n"); }}void GameControl(char ch){ //局部渲染 //在局部位置重新渲染 //一次操作可能会改变三个位置 struct pos next_position = MAN_position; struct pos next_next_position = MAN_position; switch (ch) { case 'W': case 'w': //上移 next_position.x--; next_next_position.x -= 2; break; case 'A': case 'a' : //左移 next_position.y--; next_next_position.y -= 2; break; case 'D': case 'd' : //右移 next_position.y++; next_next_position.y += 2; break; case 'S': case 's' : next_position.x++; next_next_position.x += 2; break; } //使用宏函数来检查下一个位置是否合法 if (ISVALID(next_next_position) == NULL)//进行边界检查 { return; } //分情况进行局部渲染 //如果下一个位置是地板,下一个位置是箱子,下一个位置是墙壁三种情况 //下一个位置是地板 if (map[next_position.x][next_position.y] == FLOOR) { ChangeMap(&next_position, MAN);//把地板的位置换成人的位置并且输出图片 ChangeMap(&MAN_position, FLOOR);//把人的位置换成地板并且输出图片 MAN_position = next_position;//把人的位置坐标移到下一个位置的坐标上去 } //如果下一个位置是箱子 else if (map[next_position.x][next_position.y] == BOX) { //当箱子的下一个位置是合理位置时就可以推动下下个位置是地板或者目的地就可以推动 if (map[next_next_position.x][next_next_position.y] == FLOOR)//如果下下个位置的地板 { ChangeMap(&MAN_position, FLOOR); ChangeMap(&next_position, MAN); ChangeMap(&next_next_position, BOX); MAN_position = next_position; } //如果下一个位置是箱子的目标地址 else if (map[next_next_position.x][next_next_position.y] == BOX_DES) { ChangeMap(&MAN_position, FLOOR); ChangeMap(&next_position, MAN); ChangeMap(&next_next_position, HIT); MAN_position = next_position; } } //如果下一个位置是墙直接不进行操作 else if (map[next_position.x][next_position.y] == WALL) { }}int IsGameOver(){ int flag; for (int i = 0; i < MAP_ROW; i++) { for (int j = 0; j < MAP_COL; j++) { if (map[i][j] == BOX_DES) { flag =0; return flag; } } } return 1;}void GameOverScrene(){ //表示游戏已经结束,更换背景图和字体 settextcolor(RED);//设置字体颜色 settextstyle(80, 0, _T("微软雅黑"));//设置字体 setbkmode(TRANSPARENT);//设置字体背景透明 //字体的写出区域 RECT rec = { 0,0,LENGTH,WIDTH }; drawtext(_T("恭喜过关"), &rec, DT_CENTER | DT_VCENTER);}void GamePlay(void){ int gameOver = 1; char way; while (gameOver==1) { if (_kbhit()) { way = _getch(); if (way == 'q') { gameOver = 0; } else { GameControl(way); } if (IsGameOver()) { GameOverScrene(); gameOver = 0; } } //每次输入后都检查游戏是否结束 Sleep(50); }}int main(void){ GameInit(); GamePlay(); system("pause"); closegraph(); return 0;}

 

 

 

 

.h头文件

1 #pragma once 2 #define LENGTH 1200*0.61 3 #define WIDTH 900*0.61 4 #define MAP_ROW        9 5 #define MAP_COL     12 6 #define ISVALID(a) ((a.x >= 0 && a.x < LENGTH) && (a.y >= 0 && a.y < WIDTH)) 7  8  9 struct pos {10     int x;11     int y;12 };13 14 enum map_unit {15     WALL,16     FLOOR,17     BOX_DES,18     MAN,19     BOX,20     HIT,21     MAP_UNIT_COUNT22 };
View Code

 

这个项目的环境是在visual stduio和easyX图形上,所以需要自行配置环境

 

 

完整:

 

上一篇:sscanf()的使用
下一篇:纯C语言贪吃蛇demo

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2025年04月17日 12时23分08秒