原理:
二维数组存储每个瓦片序号
然后有一个缓冲区存储瓦片图片,
最后连续采样缓冲区,粘贴到屏幕上,
而缓冲区数据随着采样越界再重新更新
#include <graphics.h>
#include <stdio.h>
// 默认游戏地图
int map[20][20]= {
{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 a=30;
int xc=3*a;
int yc=3*a;
IMAGE c(3*a,3*a);
int see = 9;
int seex =0;
int seey=0;
IMAGE n(see*a,see*a);
int oldnx=0;
int oldny=0;
void loadmap();
void change(ExMessage msg) {
if(msg.message==WM_KEYDOWN) {
switch(msg.vkcode) {
case 0x41:
xc--;
break;
case 0x44:
xc++;
break;
case 0x57:
yc--;
break;
case 0x53:
yc++;
break;
}
}
int changeflag=0;
if(xc>2*(3*a)) {
xc=xc-(3*a);
seex+=3;
changeflag=1;
}
if(changeflag) {
SetWorkingImage(&n);
for(int i=0; i<9; i++) {
for(int j=0; j<9; j++) {
switch (map[i+seey][j+seex]) { // 解决x坐标与数组的对应关系 第二个方括号【】是列数,是横坐标x,控制第几列
case 0:
setfillcolor(BLACK);
break;
case 1:
setfillcolor(WHITE);
break;
case 2:
setfillcolor(GREEN);
break;
case 3:
setfillcolor(BLUE);
break;
}
fillrectangle(j*a,i*a,(j+1)*a,(i+1)*a);
}
}
}
SetWorkingImage(&n);
getimage(&c,xc,yc,3*a,3*a);
SetWorkingImage();
putimage(800,a,&n);
putimage(1300,a,&c);
printf("cx = %d, yc = %d\n",xc,yc);
printf("seex = %d, seey = %d\n",seex,seey);
}
int main() {
loadmap();
getimage(&n,1*a,1*a,see*a,see*a);
putimage(800,a,&n);
ExMessage msg;
while(1) {
peekmessage(&msg,EX_KEY|EX_MOUSE,true);
change(msg);
Sleep(2);
flushmessage(EX_MOUSE);
}
closegraph();
return 0;
}
void loadmap() {
// FILE* fp;
// fp=fopen("map.txt","r");
// for(int i=0; i<20; i++) {
// for(int j=0; j<20; j++) {
// fscanf(fp," %d",&map[i][j]);
// }
// fscanf(fp,"\n");
// }
//
// printf("gammap\n");
// for(int i=0; i<20; i++) {
// for(int j=0; j<20; j++) {
// printf(" %d",map[i][j]);
// }
// printf("\n");
// }
// fclose(fp);
initgraph(1800,800,1);
setbkcolor(GREEN);
cleardevice();
for(int i=0; i<20; i++) {
for(int j=0; j<20; j++) {
rectangle(i*a,j*a,(i+1)*a,(j+1)*a);
}
}
setfillcolor(WHITE);
for(int i=0; i<20; i++) {
for(int j=0; j<20; j++) {
switch (map[i][j]) {
case 0:
setfillcolor(BLACK);
break;
case 1:
setfillcolor(WHITE);
break;
case 2:
setfillcolor(GREEN);
break;
case 3:
setfillcolor(BLUE);
break;
}
fillrectangle(j*a,i*a,(j+1)*a,(i+1)*a);
}
}
}