19.04.18--推箱子(graphics图形库)(内置可视化地图编辑器//还未加入读取写入文件)...

#include<stdio.h>
#include<graphics.h>
#include<time.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
#include <conio.h>
/
#define N 10
#define Window_width 800
#define Window_height 600
#define GAMEbutton_x 300
#define GAMEbutton_y 250
#define Button_line_x 10
#define Button_line_y 5
#define Button_width 200
#define Button_height 50
#define Button_gap 70
/
HWND hWnd;
int win;
int map[12][10] = { //用不同的数字去表示不同的元素 打印出界面.
 {0,0,1,1,1,1,0,0},
 {0,0,1,3,3,1,0,0},
 {0,1,1,0,3,1,1,0},
 {0,1,0,0,4,3,1,0},
 {1,1,0,4,0,0,1,1},
 {1,0,0,1,4,4,0,1},
 {1,0,5,0,0,0,0,1},
 {1,1,1,1,1,1,1,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,0,0} };
int gameinit();
void game();
void edit();
void drawmap();
void draw_block(int,int);
void draw_wall(int,int);
void draw_goal(int,int);
void draw_carrier(int,int);
void draw_box(int,int);
void draw_box_goal(int,int);
void draw_road(int,int);
void draw_carrier_goal(int,int);
void draw_magic(int,int);
void draw_special(int,int);
//--------------------------------主函数部分 函数入口----------------------------------------------//
int main() {
 int x=0;
 srand((unsigned)time(NULL));
 do
 {
  x=gameinit();
  if(x==1)
   game();
  else if(x==2)
   edit();
  break;
 } while (1);
 closegraph();
 //getchar();
 return 0;
}
int gameinit()
{
 char s1[]="开始游戏",s2[]="地图编辑器",s3[]="关于",s4[]="推箱子";
 MOUSEMSG msg;
 int p=0;
 initgraph(Window_width,Window_height);
// setbkcolor(WHITE);
// setbkmode(OPAQUE);
 setfillcolor(WHITE);
 fillrectangle(0,0,Window_width,Window_height);
 draw_wall(50,100);
 draw_goal(100,100);
 draw_carrier(150,100);
 draw_box(200,100);
 draw_magic(250,100);
 draw_special(300,150);
 setfillcolor(BLACK);
 for(int i=0;i<4;i++)fillrectangle(GAMEbutton_x,GAMEbutton_y+i*Button_gap,GAMEbutton_x+Button_width,GAMEbutton_y+i*Button_gap+Button_height);
 setfillcolor(WHITE);
 for(i=0;i<4;i++)fillrectangle(GAMEbutton_x+Button_line_x,GAMEbutton_y+Button_line_y+i*Button_gap,GAMEbutton_x-Button_line_x+Button_width,GAMEbutton_y-Button_line_y+i*Button_gap+Button_height);
 setbkcolor(WHITE);
 settextcolor(BLACK);
 settextstyle(50, 25, "锐字云字库行楷体1.0");
 outtextxy(Window_width/2-64,Window_height/6,s4);
 settextstyle(20, 10, "锐字云字库行楷体1.0");
 outtextxy(GAMEbutton_x+Button_width/2-38,GAMEbutton_y+Button_height/2-8,s1);
 outtextxy(GAMEbutton_x+Button_width/2-46,GAMEbutton_y+Button_gap+Button_height/2-8,s2);
 outtextxy(GAMEbutton_x+Button_width/2-21,GAMEbutton_y+2*Button_gap+Button_height/2-8,s3);
 while(1){
  msg=GetMouseMsg();
  switch(msg.uMsg){
  case WM_MOUSEMOVE://鼠标移动
   char s[12];
   sprintf(s, "(%d,%d)", msg.x, msg.y);
   outtextxy(0, 0, s);
   p=rand()%5;
   for(i=0;i<5;i++)
   {
    setlinecolor(WHITE);
    setfillcolor(WHITE);
    fillrectangle(50+i*50,150,150+i*50,200);
    if(i==p)draw_special(50+i*50,150);
   }
   setfillcolor(RED);
   setlinecolor(WHITE);
   if(msg.x>GAMEbutton_x&&msg.y>GAMEbutton_y&&msg.x<GAMEbutton_x+Button_width&&msg.y<GAMEbutton_y+Button_height)
    fillcircle(GAMEbutton_x+40,GAMEbutton_y+Button_height/2,10);
   else if(msg.x>GAMEbutton_x&&msg.y>GAMEbutton_y+Button_gap&&msg.x<GAMEbutton_x+Button_width&&msg.y<GAMEbutton_y+Button_gap+Button_height)
    fillcircle(GAMEbutton_x+40,GAMEbutton_y+Button_gap+Button_height/2,10);
   else if(msg.x>GAMEbutton_x&&msg.y>GAMEbutton_y+2*Button_gap&&msg.x<GAMEbutton_x+Button_width&&msg.y<GAMEbutton_y+2*Button_gap+Button_height)
    fillcircle(GAMEbutton_x+40,GAMEbutton_y+2*Button_gap+Button_height/2,10);
   else
   {
    setfillcolor(WHITE);
    fillcircle(GAMEbutton_x+40,GAMEbutton_y+Button_height/2,10);
    fillcircle(GAMEbutton_x+40,GAMEbutton_y+Button_gap+Button_height/2,10);
    fillcircle(GAMEbutton_x+40,GAMEbutton_y+2*Button_gap+Button_height/2,10);
   }
   break;
  case WM_LBUTTONDOWN://左键按下
   setlinecolor(BLACK);
   if(msg.x>GAMEbutton_x&&msg.y>GAMEbutton_y&&msg.x<GAMEbutton_x+Button_width&&msg.y<GAMEbutton_y+Button_height)
   {
    rectangle(GAMEbutton_x-10,GAMEbutton_y-10,GAMEbutton_x+10+Button_width,GAMEbutton_y+10+Button_height);
    return 1;
   }
   else if(msg.x>GAMEbutton_x&&msg.y>GAMEbutton_y+Button_gap&&msg.x<GAMEbutton_x+Button_width&&msg.y<GAMEbutton_y+Button_gap+Button_height)
   {
    rectangle(GAMEbutton_x-10,GAMEbutton_y-10+Button_gap,GAMEbutton_x+10+Button_width,GAMEbutton_y+10+Button_gap+Button_height);
    return 2;
   }
   else if(msg.x>GAMEbutton_x&&msg.y>GAMEbutton_y+2*Button_gap&&msg.x<GAMEbutton_x+Button_width&&msg.y<GAMEbutton_y+2*Button_gap+Button_height)
   {
    rectangle(GAMEbutton_x-10,GAMEbutton_y-10+2*Button_gap,GAMEbutton_x+10+Button_width,GAMEbutton_y+10+2*Button_gap+Button_height);
    return 3;
   }
   setlinecolor(WHITE);
   break;
  }
 }
}
void game()
{
 //
 char input;
 int i, j; //循环控制变量
 int peoR, peoC;  //人的下标
 setfillcolor(WHITE);
 fillrectangle(0,0,Window_width,Window_height);
 drawmap();
 while (1)
 {
  //确定人的位置
  for (i = 0; i < 8; i++)
  {
   for (j = 0; j < 8; j++)
   {
    if (map[i][j] == 5 || map[i][j] == 8) //5人
    {
     peoR = i;
     peoC = j;
    }
   }
  }
  printf("(%d %d)", peoR, peoC);
  input = getch();
  //对不同的键盘输入采取不同的处理 //分支结构
  switch (input)
  {
  case 'W':  //上  WASD  上下左右键控制方向
  case 'w':
  case 72:  //键值
      //判断人的前面是空地或者目的地  //改变两个位置
   if (map[peoR][peoC - 1] == 0 || map[peoR][peoC - 1] == 3)
   {
    map[peoR][peoC - 1] += 5;
    draw_block(peoR,peoC - 1);//直接绘图
    map[peoR][peoC] -= 5;
    draw_block(peoR,peoC);//直接绘图
   }
   //改变三个位置 箱子
   else if (map[peoR][peoC - 1] == 4 || map[peoR][peoC - 1] == 7)
   {
    if (map[peoR][peoC - 2] == 0 || map[peoR][peoC - 2] == 3)
    {
     map[peoR][peoC - 2] += 4;
     draw_block(peoR,peoC-2);//直接绘图
     map[peoR][peoC - 1] += 1;
     draw_block(peoR,peoC-1);//直接绘图
     map[peoR][peoC] -= 5;
     draw_block(peoR,peoC);//直接绘图
    }
   }

   
   break;
  case 'S':  //下
  case 's':
  case 80:  //map[peoR + 1][peoC]
      //判断人的前面是空地或者目的地  //改变两个位置
   if (map[peoR][peoC + 1] == 0 || map[peoR][peoC + 1] == 3)
   {
    map[peoR][peoC + 1] += 5;
    draw_block(peoR,peoC+1);//直接绘图
    map[peoR][peoC] -= 5;
    draw_block(peoR,peoC);//直接绘图
   }
   //改变三个位置 箱子
   else if (map[peoR][peoC + 1] == 4 || map[peoR][peoC + 1] == 7)
   {
    if (map[peoR][peoC + 2] == 0 || map[peoR][peoC + 2] == 3)
    {
     map[peoR][peoC + 2] += 4;
     draw_block(peoR,peoC+2);//直接绘图
     map[peoR][peoC + 1] += 1;
     draw_block(peoR,peoC+1);//直接绘图
     map[peoR][peoC] -= 5;
     draw_block(peoR,peoC);//直接绘图
    }
   }
   
   break;
  case 'A':  //map[peoR][peoC - 1]
  case 'a':
  case 75:
   //判断人的前面是空地或者目的地  //改变两个位置
   if (map[peoR - 1][peoC] == 0 || map[peoR - 1][peoC] == 3)
   {
    map[peoR - 1][peoC] += 5;
    draw_block(peoR-1,peoC);//直接绘图
    map[peoR][peoC] -= 5;
    draw_block(peoR,peoC);//直接绘图
   }
   //改变三个位置 箱子
   else if (map[peoR - 1][peoC] == 4 || map[peoR - 1][peoC] == 7)
   {
    if (map[peoR - 2][peoC] == 0 || map[peoR - 2][peoC] == 3)
    {
     map[peoR - 2][peoC] += 4;
     draw_block(peoR-2,peoC);//直接绘图
     map[peoR - 1][peoC] += 1;
     draw_block(peoR-1,peoC);//直接绘图
     map[peoR][peoC] -= 5;
     draw_block(peoR,peoC);//直接绘图
    }
   }
   break;
  case 'D':
  case 'd':
  case 77:
   //判断人的前面是空地或者目的地  //改变两个位置
   if (map[peoR + 1][peoC] == 0 || map[peoR + 1][peoC] == 3)
   {
    map[peoR + 1][peoC] += 5;
    draw_block(peoR+1,peoC);//直接绘图
    map[peoR][peoC] -= 5;
    draw_block(peoR,peoC);//直接绘图
   }
   //改变三个位置 箱子
   else if (map[peoR + 1][peoC] == 4 || map[peoR + 1][peoC] == 7)
   {
    if (map[peoR + 2][peoC] == 0 || map[peoR + 2][peoC] == 3)
    {
     map[peoR + 2][peoC] += 4;
     draw_block(peoR+2,peoC);//直接绘图
     map[peoR + 1][peoC] += 1;
     draw_block(peoR+1,peoC);//直接绘图
     map[peoR][peoC] -= 5;
     draw_block(peoR,peoC);//直接绘图
    }
   }
   break;
  }
 }
}
void edit()
{
 MOUSEMSG msg;
 int block=0,update_x=0,update_y=0;
 for(int i=0;i<12;i++)for(int j=0;j<10;j++)map[i][j]=0;
 setfillcolor(WHITE);
 fillrectangle(0,0,Window_width,Window_height);
 drawmap();
 line(0,Window_height/4*3,Window_width/4*3,Window_height/4*3);
 line(Window_width/4*3,0,Window_width/4*3,Window_height/4*3);
 line(Window_width/4*3+10,0,Window_width/4*3+10,Window_height/4*3);
 line(Window_width/4*3+10,Window_height/4*3,Window_width,Window_height/4*3);
 line(0,Window_height/4*3+10,Window_width,Window_height/4*3+10);
 /*方块位置*/
 draw_wall(0,Window_height/4*3+20);
 draw_goal(50,Window_height/4*3+20);
 draw_carrier(100,Window_height/4*3+20);
 draw_box(150,Window_height/4*3+20);
 draw_magic(200,Window_height/4*3+20);
 draw_road(250,Window_height/4*3+20);
 /*方块位置*/
 while(1){
  msg=GetMouseMsg();
  if(block==1)
   outtextxy(Window_width/4*3+20,20,"墙    ");
  else if(block==2)
   outtextxy(Window_width/4*3+20,20,"目的地");
  else if(block==3)
   outtextxy(Window_width/4*3+20,20,"搬运工");
  else if(block==4)
   outtextxy(Window_width/4*3+20,20,"箱子  ");
  else if(block==5)
   outtextxy(Window_width/4*3+20,20,"魔法阵");
  else if(block==6)
   outtextxy(Window_width/4*3+20,20,"路    ");
  switch(msg.uMsg){
  case WM_MOUSEMOVE://鼠标移动
   setfillcolor(RED);
   setlinecolor(WHITE);
  /* for(i=0;i<5;i++)
   {
    if(msg.x>i*50&&msg.y>Window_height/4*3+20&&msg.x<50+i*50&&msg.y<Window_height/4*3+70)
    {
     if(block!=i+1)fillcircle(25,Window_height/4*3+95,10);
    }
   }*/
   if(msg.x>0&&msg.y>Window_height/4*3+20&&msg.x<50&&msg.y<Window_height/4*3+70)
    fillcircle(25,Window_height/4*3+95,10);
   else if(msg.x>50&&msg.y>Window_height/4*3+20&&msg.x<100&&msg.y<Window_height/4*3+70)
    fillcircle(75,Window_height/4*3+95,10);
   else if(msg.x>100&&msg.y>Window_height/4*3+20&&msg.x<150&&msg.y<Window_height/4*3+70)
    fillcircle(125,Window_height/4*3+95,10);
   else if(msg.x>150&&msg.y>Window_height/4*3+20&&msg.x<200&&msg.y<Window_height/4*3+70)
    fillcircle(175,Window_height/4*3+95,10);
   else if(msg.x>200&&msg.y>Window_height/4*3+20&&msg.x<250&&msg.y<Window_height/4*3+70)
    fillcircle(225,Window_height/4*3+95,10);
   else if(msg.x>250&&msg.y>Window_height/4*3+20&&msg.x<300&&msg.y<Window_height/4*3+70)
    fillcircle(275,Window_height/4*3+95,10);
   else
   {
    setfillcolor(WHITE);
    for(i=0;i<6;i++)if(block!=i+1)fillcircle(25+i*50,Window_height/4*3+95,10);
   }
   if(msg.x>0&&msg.y>0&&msg.x<Window_width/4*3&&msg.y<Window_height/4*3)
   {
    draw_block(update_x,update_y);
    update_x=msg.x/50;
    update_y=msg.y/50;
    if(block==1)
     draw_wall(msg.x/50*50,msg.y/50*50);
    else if(block==2)
     draw_goal(msg.x/50*50,msg.y/50*50);
    else if(block==3)
     draw_carrier(msg.x/50*50,msg.y/50*50);
    else if(block==4)
     draw_box(msg.x/50*50,msg.y/50*50);
    else if(block==5)
     draw_magic(msg.x/50*50,msg.y/50*50);
    else if(block==6)
     draw_road(msg.x/50*50,msg.y/50*50);
   }
   break;
  case WM_LBUTTONDOWN://左键按下
  /* for(i=0;i<5;i++)
   {
    if(msg.x>i*50&&msg.y>Window_height/4*3+20&&msg.x<50+i*50&&msg.y<Window_height/4*3+70)
    {
     block=i+1;
     draw_special(i*50,Window_height/4*3+70);
    }
   }*/
   if(msg.x>0&&msg.y>Window_height/4*3+20&&msg.x<50&&msg.y<Window_height/4*3+70)
   {
    block=1;
    fillcircle(25,Window_height/4*3+95,10);
   }
   else if(msg.x>50&&msg.y>Window_height/4*3+20&&msg.x<100&&msg.y<Window_height/4*3+70)
   {
    block=2;
    fillcircle(75,Window_height/4*3+95,10);
   }
   else if(msg.x>100&&msg.y>Window_height/4*3+20&&msg.x<150&&msg.y<Window_height/4*3+70)
   {
    block=3;
    fillcircle(125,Window_height/4*3+95,10);
   }
   else if(msg.x>150&&msg.y>Window_height/4*3+20&&msg.x<200&&msg.y<Window_height/4*3+70)
   {
    block=4;
    fillcircle(175,Window_height/4*3+95,10);
   }
   else if(msg.x>200&&msg.y>Window_height/4*3+20&&msg.x<250&&msg.y<Window_height/4*3+70)
   {
    block=5;
    fillcircle(225,Window_height/4*3+95,10);
   }
   else if(msg.x>250&&msg.y>Window_height/4*3+20&&msg.x<300&&msg.y<Window_height/4*3+70)
   {
    block=6;
    fillcircle(275,Window_height/4*3+95,10);
   }
   else if(msg.x>0&&msg.y>0&&msg.x<Window_width/4*3&&msg.y<Window_height/4*3)
   {
    if(block==1)
     map[msg.x/50][msg.y/50]=1;
    else if(block==2)
     map[msg.x/50][msg.y/50]=3;
    else if(block==3)
     map[msg.x/50][msg.y/50]=5;
    else if(block==4)
     map[msg.x/50][msg.y/50]=4;
    else if(block==5)
     map[msg.x/50][msg.y/50]=9;
    else if(block==6)
     map[msg.x/50][msg.y/50]=0;
   }
   else
   {
    setfillcolor(WHITE);
    block=0;
    for(i=0;i<6;i++)fillrectangle(i*50,Window_height/4*3+70,i*50+50,Window_height/4*3+120);
   }
  /* if(i>=5)
   {
    block=0;
    setfillcolor(WHITE);
    setlinecolor(WHITE);
    for(i=0;i<5;i++)
    {
     fillrectangle(i*50,Window_height/4*3+70,i*50+50,Window_height/4*3+120);
    }
   }*/
   break;
  }
 }
}
//画图
void drawmap(){
 //for循环遍历二维数组
 for (int i = 0; i < 12; i++)
 {
  for (int j = 0; j < 9; j++)
  {
   switch (map[i][j])
   {
   case 0:  //空地
    draw_road(i*50,j*50);
    break;
   case 1:
    draw_wall(i*50,j*50);
    break;
   case 3:
    draw_goal(i*50,j*50);
    break;
   case 4:
    draw_box(i*50,j*50);
    break;
   case 5:
    draw_carrier(i*50,j*50);
    break;
   case 7:    //箱子在目的地里面
    draw_box_goal(i*50,j*50);
    break;
   case 8:
    draw_carrier_goal(i*50,j*50);
    break;
   case 9:
    draw_magic(i*50,j*50);
    break;
   }
  }
 }
}
void draw_block(int i,int j)
{
 setfillcolor(WHITE);
 fillrectangle(i*50,j*50,i*50+48,j*50+48);
 switch (map[i][j])
 {
 case 0:  //空地
  draw_road(i*50,j*50);
  break;
 case 1:
  draw_wall(i*50,j*50);
  break;
 case 3:
  draw_goal(i*50,j*50);
  break;
 case 4:
  draw_box(i*50,j*50);
  break;
 case 5:
  draw_carrier(i*50,j*50);
  break;
 case 7:    //箱子在目的地里面
  draw_box_goal(i*50,j*50);
  break;
 case 8:
  draw_carrier_goal(i*50,j*50);
  break;
 case 9:
  draw_magic(i*50,j*50);
  break;
 }
}
void draw_wall(int x,int y)
{
 setlinecolor(BLACK);
 rectangle(x,y,x+48,y+48);
 for(int i=0;i<4;i++)
 {
  if(i>0)line(x,y+12*i,x+48,y+12*i);
  for(int j=0;j<(i+1)%2+2;j++)line(x+16+16*j-(i+1)%2*8,y+i*12,x+16+16*j-(i+1)%2*8,y+i*12+12);
 }
}
void draw_goal(int x,int y)
{
 setfillcolor(BLUE);
 rectangle(x,y,x+48,y+48);
 fillcircle(x+24,y+24,10);
}
void draw_carrier(int x,int y)
{
 setlinecolor(BLACK);
 circle(x+24,y+10,7);
 line(x+24,y+17,x+24,y+34);
 line(x+24,y+22,x+12,y+30);
 line(x+24,y+22,x+36,y+30);
 line(x+24,y+34,x+14,y+44);
 line(x+24,y+34,x+34,y+44);
}
void draw_box(int x,int y)
{
 setlinecolor(BLACK);
 rectangle(x,y,x+48,y+48);
 rectangle(x,y,x+12,y+12);
 rectangle(x+36,y+36,x+48,y+48);
 rectangle(x+36,y,x+48,y+12);
 rectangle(x,y+36,x+12,y+48);
 line(x+12,y+10,x+36,y+10);
 line(x+12,y+38,x+36,y+38);
 line(x+10,y+12,x+10,y+36);
 line(x+38,y+12,x+38,y+36);
 for(int i=1;i<4;i++)line(x+10,y+10+7*i,x+38,y+10+7*i);
}
void draw_box_goal(int x,int y)
{
 setlinecolor(BLACK);
 rectangle(x,y,x+48,y+48);
 rectangle(x,y,x+12,y+12);
 rectangle(x+36,y+36,x+48,y+48);
 rectangle(x+36,y,x+48,y+12);
 rectangle(x,y+36,x+12,y+48);
 line(x+12,y+10,x+36,y+10);
 line(x+12,y+38,x+36,y+38);
 line(x+10,y+12,x+10,y+36);
 line(x+38,y+12,x+38,y+36);
 for(int i=1;i<4;i++)line(x+10,y+10+7*i,x+38,y+10+7*i);
 setfillcolor(BLACK);
 fillcircle(x+24,y+24,10);
}
void draw_road(int x,int y)
{
 setfillcolor(WHITE);
 setlinecolor(BLACK);
 fillrectangle(x,y,x+48,y+48);
}
void draw_carrier_goal(int x,int y)
{
 setlinecolor(BLACK);
 circle(x+24,y+10,7);
 line(x+24,y+17,x+24,y+34);
 line(x+24,y+22,x+12,y+30);
 line(x+24,y+22,x+36,y+30);
 line(x+24,y+34,x+14,y+44);
 line(x+24,y+34,x+34,y+44);
 setfillcolor(BLUE);
 fillcircle(x+24,y+24,10);
}
void draw_magic(int x,int y)
{
 setlinecolor(BLACK);
 rectangle(x,y,x+48,y+48);
 circle(x+24,y+24,20);
 line(x+24,y+4,x+7,y+34);
 line(x+24,y+4,x+41,y+34);
 line(x+41,y+34,x+7,y+34);
 line(x+24,y+44,x+7,y+14);
 line(x+24,y+44,x+41,y+14);
 line(x+41,y+14,x+7,y+14);
}
void draw_special(int x,int y)
{
 setlinecolor(BLACK);
 line(x+24,y+4,x+20,y+30);
 line(x+24,y+4,x+28,y+30);
 line(x+20,y+30,x+28,y+30);
 line(x+24,y+24,x+24,y+44);
 setfillcolor(BLUE);
 fillcircle(x+24,y+44,4);
}

转载于:https://www.cnblogs.com/redyellowblue/p/10733576.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值