基于easyX的<颜色侵略>小游戏

是挺久以前做的一个东西,突然想到放上来分享一下俺的拙作,纯原创。

利用二维数组对齐进行划分,并讲状态分为被侵略与未被侵略两种状态来记录。 在旧版的easyX可以运行。

源码及exe下载 

头文件:

  1 #include <graphics.h> 

  2 #include <iostream> 

 3 const int color_num=6;
 4 static int steps=14;
 5 static int List[10][10];//绘图数组
 6 static int vid[10][10];//存储状态数组
 7 static char in[3];
 8 static int a=0;
 9 class color_flood
10 {
11      void Image1(int i,int j);//被侵略方块图像绘制
12      void Image2(int i,int j);//未被侵略图像绘制
13 public:
14      color_flood();//数据初始化
15      void Grph();//图像初始化
16      void Update();//对玩家的操作的相应更新
17      int Iswin();
18      void chooseimage();//玩家做出操作的相应
18      void Output();//调用绘图函数

20};

 .cpp文件:

   0 #include "FLOOD_IT_v1.0.h"

  1 color_flood::color_flood()

 2     {
 3         memset(vid,0,sizeof(vid));
 4         memset(List,0,sizeof(List));
 5         randomize();
 6         for(int i=1;i<9;++i)
 7             for(int j=1;j<9;++j)
 8                 List[i][j]=random(6)+1;//随机颜色    
 9 
10         vid[1][1]=1;
11         a=List[1][1];
12         steps=14;
13     }
 1 void color_flood:: Grph()
 2     {
 3         memset(vid,0,sizeof(vid));
 4         memset(List,0,sizeof(List));
 5         randomize();
 6         for(int i=1;i<9;++i)
 7             for(int j=1;j<9;++j)
 8                 List[i][j]=random(6)+1;//随机颜色    
 9 
10         vid[1][1]=1;
11         a=List[1][1];
12         steps=14;
13         initgraph(800,600);
14         setbkcolor(WHITE);
15         setcolor(BLACK);
16          setlinestyle(PS_DASH,1,6);
17         rectangle(143,148,428,432);
18         rectangle(30,100,770,570);
19         line(560,100,560,569);
20         setfillstyle(DARKGRAY);
21         bar(600,120,730,200);
22         rectangle(600,120,730,200);
23         setlinestyle(PS_DASH,1,1);
24         line(560,230,770,230);
25         line(560,360,770,360);
26         line(560,450,770,450);
27         setfont(25,0,"Arial Black");
28         outtextxy(620,250,"STEPS:");
29         //outtextxy(620,380,"SCORE:");
30         setfontbkcolor(DARKGRAY);
31         outtextxy(610,140,"NEW GAME");
32         setfont(55,0,"Arial Black");
33         outtextxy(100,40,"FLOOD IT!"); 

34 } 

 1     void color_flood:: Update()
 2     {
 3         for(int i=1;i<9;++i)
 4         {    for(int j=1;j<9;++j)
 5             {
 6                 if(vid[i][j]==1)
 7                     continue;
 8                 else
 9                 {
10                     if((vid[i][j-1]==1||vid[i-1][j]==1||vid[i][j+1]==1||vid[i+1][j]==1)&&List[i][j]==a)
11                         vid[i][j]=1;
12                 }
13             }
14         }
15     }

 

 1 void color_flood:: Image1(int i,int j)
 2     {
 3         switch(a)
 4         {
 5         case 1:{setfillstyle(RED);break;}
 6         case 2:{setfillstyle(BLUE);break;}
 7         case 3:{setfillstyle(GREEN);break;}
 8         case 4:{setfillstyle(YELLOW);break;}
 9         case 5:{setfillstyle(MAGENTA);break;}
10         case 6:{setfillstyle(BROWN);break;}
11         }
12         bar(i*35+145,j*35+150,(i+1)*35+145,(j+1)*35+150);

13         } 

 1     void color_flood:: Image2(int i,int j)
 2     {
 3         switch(List[i+1][j+1])
 4         {
 5         case 1:{setfillstyle(RED);break;}
 6         case 2:{setfillstyle(BLUE);break;}
 7         case 3:{setfillstyle(GREEN);break;}
 8         case 4:{setfillstyle(YELLOW);break;}
 9         case 5:{setfillstyle(MAGENTA);break;}
10         case 6:{setfillstyle(BROWN);break;}
11         }
12         bar(i*35+145,j*35+150,(i+1)*35+145,(j+1)*35+150);
13     }

 

 1     void color_flood:: Output()
 2     {
 3         for(int i=1;i<9;++i)
 4         {
 5             for(int j=1;j<9;++j)
 6             {
 7                 if(vid[i][j]==0)
 8                     Image2(i-1,j-1);
 9                 else
10                     Image1(i-1,j-1);
11             }
12         }
13 
14         //*str=steps;

15         } 

 1 int color_flood:: Iswin()
 2     {
 3         int n=0;
 4         for(int i=1;i<9;++i)
 5             for(int j=1;j<9;++j)
 6                 if(vid[i][j]==1)
 7                 {
 8                     n++;
 9                 }
10             return n;
11     }

 


 1 void color_flood:: chooseimage() 

 2     {
 3         for(int i=0;i<6;i++)
 4         {
 5                 switch(i)
 6                 {
 7                 case 0:{setfillstyle(RED);break;}
 8                 case 1:{setfillstyle(BLUE);break;}
 9                 case 2:{setfillstyle(GREEN);break;}
10                 case 3:{setfillstyle(YELLOW);break;}
11                 case 4:{setfillstyle(MAGENTA);break;}
12                 case 5:{setfillstyle(BROWN);break;}
13                 }
14             bar(90+i*40+i*30,490,130+i*40+i*30,530);
15         }
16         MOUSEMSG msg;
17         msg=GetMouseMsg();
18         while(1)
19         {
20             if(MouseHit!=0)
21             {
22                 if(msg.uMsg==WM_LBUTTONUP)
23                 {
24         
25                     if(msg.y>490&&msg.y<530)
26                     {
27                         if(msg.x>90&&msg.x<130)
28                             {a=1;--steps;}
29                         if(msg.x>160&&msg.x<200)
30                             {a=2;--steps;}
31                         if(msg.x>230&&msg.x<270)
32                             {a=3;--steps;}
33                         if(msg.x>300&&msg.x<340)
34                             {a=4;--steps;}
35                         if(msg.x>370&&msg.x<410)
36                             {a=5;--steps;}
37                         if(msg.x>440&&msg.x<480)
38                             {a=6;--steps;}
39                     }
40                     if(msg.x>600&&msg.x<730&&msg.y>120&&msg.y<200)
41                     {
42                         Grph();
43                     }
44                     msg.mkLButton=0;        
45 
46                 }
47 
48                 break;
49             }
50             else
51                 continue;
52         }
53     }

  1 int main()

 2     {
 3         color_flood img;
 4         img.Grph();
 5         int b;
 6         while(1)
 7         {        
 8             img.Fre();
 9             img.Output();
10             img.chooseimage();
11             img.Update();
12             if(steps==-1)
13             {
14                 setbkcolor(WHITE);
15                 setfont(30,0,"Arial Black");
16                 outtextxy(100,300,"You Lose!  Press any key to New Game");
17                 getch();
18                 img.Grph();
19             }
20             b=img.Iswin();
21             if(b==64)
22             {
23                 img.Output();
24                 setbkcolor(WHITE);
25                 setfont(30,0,"Arial Black");
26                 outtextxy(100,300,"You Win! Press any key to New Game");
27                 getch();
28                 img.Grph();
29             }
30         }
31         getch();
32         closegraph();
33         return 0;
34     }

 

 

posted @ 2012-04-06 22:30  XeCet  阅读(658)  评论(0编辑  收藏  举报