左边是游戏地图编辑区,右边是地图缓冲区,解决了地图缓冲区拖动bug,成功使得缓冲区可以更新。
AWSD进行移动
鼠标左右键分别是绘制/拖动
按F1健导出为mapv3.txt
F2清空数组
打印的是游戏数组
easyx开发devcpp 5.11 easyx20220922版本
#include #include #define ROW 150// 游戏地图行 #define COLUMN 150// 游戏地图列 //#define ROW 20// 游戏地图行//#define COLUMN 20// 游戏地图列#define PIXNUM 30// 一个瓦片边长为 30 像素 #define SHOWSIZE 10// 一个视口边长为 5 个瓦片 #define BUFFERSIZE 3// 地图缓冲区边长 3 个视口 #define SHOWSPEED 4// 游戏地图背景移动速度 #define SHOWX 300// 游戏显示区域 #define SHOWY 300typedef struct area {int lx;// 左上角坐标int ly;int rx;// 右上角坐标int ry;int high;// 高度int wide;} area;typedef struct checkBackgroundBuffer {// 用于记录缓冲条件的临界数据int xmin;int ymin;int xmax;int ymax;int adjust;} bklimit;typedef struct background {// 游戏地图背景area gamepos;// 采样区相对于缓冲区的坐标area deskpos;// 改 bug 整理得到一个新结构体,把窗口坐标封装了int nowpointx;// 在整个地图数组映射形成的地图上的坐标int nowpointy;IMAGE *gamebk;} bk;typedef struct backgroundBuffer {// 游戏背景缓冲区area meshpos;int meshrow;int meshcolumn;bklimit bufferlimit;IMAGE *mapmesh;// 缓冲区,长宽各为 3*size*a 个像素} bkbuffer;int map[ROW][COLUMN]= {0};//int map[ROW][COLUMN]= {//{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,0,0,0, 0,0,0,0,0, 0,0,0,0,0},//{0,0,0,0,0, 0,0,1,0,0, 0,0,2,0,2, 0,0,0,0,0},//{0,0,0,0,0, 0,0,0,0,2, 0,0,0,2,0, 0,0,0,0,0},//{0,0,0,0,1, 0,0,0,0,0, 0,0,1,0,0, 1,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,0,0,0, 0,1,0,0,0, 0,0,0,0,0, 0,0,0,0,0},//{0,0,0,0,0, 0,0,1,0,0, 0,0,2,0,2, 0,0,0,0,0},//{0,0,0,0,0, 0,0,0,0,2, 0,0,0,2,0, 0,0,0,0,0},//{0,0,0,0,1, 0,0,0,0,0, 0,0,1,0,0, 1,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,0,0,0, 0,1,0,0,0, 0,0,0,0,0, 0,0,0,0,0},//{0,0,0,0,0, 0,0,1,0,0, 0,0,2,0,2, 0,0,0,0,0},//{0,0,0,0,0, 0,0,0,0,2, 0,0,0,2,0, 0,0,0,0,0},//{0,0,0,0,1, 0,0,0,0,0, 0,0,1,0,0, 1,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,0,0,0, 0,1,0,0,0, 0,0,0,0,0, 0,0,0,0,0},//{0,0,0,0,0, 0,0,1,0,0, 0,0,2,0,2, 0,0,0,0,0},//{0,0,0,0,0, 0,0,0,0,2, 0,0,0,2,0, 0,0,0,0,0},//{0,0,0,0,1, 0,0,0,0,0, 0,0,1,0,0, 1,0,0,0,0},//};int pixnum=PIXNUM;// 瓦片的大小正方形像素瓦片的边长int showsize=SHOWSIZE;int buffersize = BUFFERSIZE;bk bkgd;// backgroundbkbuffer bkgdbu;// backgroundbufferIMAGE b(3*showsize*pixnum,3*showsize*pixnum);// mapmesh 缓冲区IMAGE a(showsize*pixnum,showsize*pixnum);// 采样区,采样的到的图片就是屏幕上的游戏// 清除地图void clearmap();// 保存编辑完的地图void savemap();//读取txt字符表格作为背景数组void loadmap();//检测背景是否需要更新bool buchange() {bool changeflag=0;while(bkgd.gamepos.lx>bkgdbu.bufferlimit.xmax) {// if else if变 while 解决鼠标拖动,导致的多次平移问题bkgd.gamepos.lx-=bkgdbu.bufferlimit.adjust;bkgdbu.meshpos.lx+=showsize;changeflag=1;printf("死循环\n");}while(bkgd.gamepos.lxbkgdbu.bufferlimit.ymax) {bkgd.gamepos.ly-=bkgdbu.bufferlimit.adjust;bkgdbu.meshpos.ly+=showsize;changeflag=1;}while(bkgd.gamepos.ly<bkgdbu.bufferlimit.ymin) {bkgd.gamepos.ly+=bkgdbu.bufferlimit.adjust;bkgdbu.meshpos.ly-=showsize;changeflag=1;}return changeflag;}//刷新背景缓冲区void freshbuffer() {SetWorkingImage(bkgdbu.mapmesh);// 选择缓冲区为更新对象for(int i=0; i<bkgdbu.meshrow; i++) {for(int j=0; jdeskpos.lx&&m.y>deskpos.ly&&m.x<deskpos.rx&&m.y&&<){//+=flah_a;//}else{//+=flag_a;//}bkgd.gamepos.lx-=flag_a;// 左移采样区bkgd.gamepos.lx+=flag_d;// 右移采样区bkgd.gamepos.ly-=flag_w;// 上移采样区bkgd.gamepos.ly+=flag_s;// 下移采样区}//初始化地图缓冲区void initbkbuffer() {bkgdbu.mapmesh = &b;// 挂载地图缓冲区bkgdbu.meshpos.lx=0;bkgdbu.meshpos.ly=0;bkgdbu.meshrow=BUFFERSIZE*showsize;bkgdbu.meshcolumn=BUFFERSIZE*showsize;bkgdbu.bufferlimit.adjust=showsize*pixnum;// 缓冲区需要更新的临界条件bkgdbu.bufferlimit.xmax=2*showsize*pixnum;// 超过九宫格就立即更新,采样区默认在九宫格的中心bkgdbu.bufferlimit.xmin=0;bkgdbu.bufferlimit.ymax=2*showsize*pixnum;bkgdbu.bufferlimit.xmin=0;freshbuffer();SetWorkingImage();//putimage(700,pixnum,bkgdbu.mapmesh);// 缓冲区可视化}//初始化地图采样区void initbk() {bkgd.gamebk = &a;// 如果是在这里声明临时变量 a ,函数执行完毕之后,程序就会销毁变量bkgd.gamepos.lx=showsize*pixnum;// 采样区默认位置bkgd.gamepos.ly=showsize*pixnum;bkgd.nowpointx=bkgd.gamepos.lx;// 绝对位置,用于解决gamepos 范围在 0~2*showsize*pixnum 导致的笔刷绘制失败bugbkgd.nowpointy=bkgd.gamepos.ly;// 打表检测,读表时发现的绘制位置始终在一个区域内,才发现的这个bugbkgd.deskpos.lx=SHOWX;// 采样之后的粘贴位置bkgd.deskpos.ly=SHOWY;bkgd.deskpos.rx=bkgd.deskpos.lx+showsize*pixnum;// 粘贴区域的大小,用于检测鼠标指针是否在该区域内绘制bkgd.deskpos.ry=bkgd.deskpos.ly+showsize*pixnum;SetWorkingImage(bkgdbu.mapmesh);getimage(bkgd.gamebk,bkgd.gamepos.lx,bkgd.gamepos.ly,showsize*pixnum,showsize*pixnum);// 加载屏幕地图,开始采样SetWorkingImage();putimage(bkgd.deskpos.lx,bkgd.deskpos.ly,bkgd.gamebk);// 粘贴采样结果}int main() {initgraph(1800,800,1);setbkcolor(GREEN);cleardevice();loadmap();// 加载地图clearmap();initbkbuffer();initbk();ExMessage msg;while(1) {peekmessage(&msg,EX_KEY|EX_MOUSE,true);move(msg);// 键盘awsd检查draft(msg);// 鼠标右键拖动检查draw(msg);// 鼠标左键绘制if(buchange()) {freshbuffer();// 刷新九宫格缓冲区printf("九宫格已刷新\n");}if(msg.message==WM_KEYDOWN) {if(msg.vkcode==VK_F1) {savemap();} else if(msg.vkcode== VK_F2) {clearmap();}}show();// 打印缓冲区,打印新的采样结果,更新游戏背景, 结算完毕统一显示Sleep(2);}return 0;}//清楚地图 void clearmap() {for(int i=0; i<ROW; i++) {for(int j=0; j<COLUMN; j++) {if(i==0||j==0) {map[i][j]=1;} else {map[i][j]=0;}}}printf("freash gammap\n");}// 保存地图void savemap() {FILE* fp;fp=fopen("mapv3.txt","w");for(int i=0; i<ROW; i++) {for(int j=0; j<COLUMN; j++) {fprintf(fp," %d",map[i][j]);}fprintf(fp,"\n");}fclose(fp);printf("save gammap\n");for(int i=0; i<ROW; i++) {for(int j=0; j<COLUMN; j++) {printf(" %d",map[i][j]);}}printf("map 显示已完成\n");}//加载地图void loadmap() {FILE* fp;fp=fopen("mapv2.txt","r");for(int i=0; i<ROW; i++) {for(int j=0; j<COLUMN; j++) {fscanf(fp," %d",&map[i][j]);}fscanf(fp,"\n");}fclose(fp);printf("gammap\n");for(int i=0; i<ROW; i++) {for(int j=0; j<COLUMN; j++) {printf(" %d",map[i][j]);}printf("\n");}}