大一课设 消灭星星

Posted luoru

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大一课设 消灭星星相关的知识,希望对你有一定的参考价值。

 
技术图片
   1 #include<iostream>
   2 #include<graphics.h>
   3 #include<windows.h>
   4 #include<cstring>
   5 #include<cmath>
   6 #include<cstring>
   7 #include<cstdlib>
   8 #include<cstdio>
   9 #include<time.h>
  10 #include <pthread.h>
  11 #include<mmsystem.h>
  12 #pragma comment(lib,"winmm.lib")
  13 
  14 using namespace std;
  15 
  16 /**************************************************************/
  17 
  18 /**地图参数说明
  19 0 空白,四边加了一圈,为了方便处理
  20 1 2 3 4 5 五种颜色的星星
  21 6 当前被选中的星星
  22 */
  23 int mp[13][12];
  24 int mp1[13][12] =
  25 
  26     0,0,0,0,0,0,0,0,0,0,0,0,
  27     0,1,1,2,3,1,1,1,5,1,1,0,
  28     0,3,2,2,1,5,1,1,1,5,5,0,
  29     0,5,1,4,3,1,4,1,5,1,2,0,
  30     0,5,1,1,3,5,1,4,1,1,5,0,
  31     0,2,4,3,3,1,4,1,5,4,1,0,
  32     0,1,1,4,4,1,1,2,3,4,1,0,
  33     0,1,1,2,1,4,1,4,1,3,3,0,
  34     0,4,4,5,1,1,3,1,3,3,2,0,
  35     0,4,1,1,4,1,5,4,1,2,1,0,
  36     0,1,3,4,1,1,4,1,1,2,1,0,
  37     0,1,2,4,1,4,1,5,5,1,1,0,
  38     0,0,0,0,0,0,0,0,0,0,0,0
  39 ;
  40 
  41 int mp2[13][12] =
  42 
  43     0,0,0,0,0,0,0,0,0,0,0,0,
  44     0,1,1,2,3,1,1,1,5,1,1,0,
  45     0,3,2,2,1,5,1,1,1,5,5,0,
  46     0,5,1,4,3,1,4,1,5,1,2,0,
  47     0,5,1,1,3,5,3,4,1,1,5,0,
  48     0,2,1,3,3,1,4,1,5,4,1,0,
  49     0,1,3,4,4,2,1,2,3,4,1,0,
  50     0,1,3,3,1,5,4,4,1,3,3,0,
  51     0,4,1,5,1,1,3,1,3,3,2,0,
  52     0,4,1,1,4,1,5,4,1,2,1,0,
  53     0,1,3,4,1,1,4,1,1,2,1,0,
  54     0,1,2,4,1,4,1,5,5,1,1,0,
  55     0,0,0,0,0,0,0,0,0,0,0,0
  56 ;
  57 
  58 int mp3[13][12] =
  59 
  60     0,0,0,0,0,0,0,0,0,0,0,0,
  61     0,1,1,2,3,1,1,1,5,1,1,0,
  62     0,3,2,2,1,5,1,1,1,5,5,0,
  63     0,5,1,4,3,1,4,1,5,1,2,0,
  64     0,5,5,1,3,5,1,4,1,1,5,0,
  65     0,2,1,3,2,1,4,1,5,4,1,0,
  66     0,1,5,4,4,1,1,2,3,3,1,0,
  67     0,1,1,2,1,4,1,4,1,4,3,0,
  68     0,4,1,5,1,1,3,1,3,3,2,0,
  69     0,4,5,1,4,1,5,4,1,2,1,0,
  70     0,1,3,4,1,1,4,1,1,2,1,0,
  71     0,1,2,4,1,4,1,5,5,1,1,0,
  72     0,0,0,0,0,0,0,0,0,0,0,0
  73 ;
  74 
  75 int mp4[13][12] =
  76 
  77     0,0,0,0,0,0,0,0,0,0,0,0,
  78     0,1,1,2,3,1,1,1,5,1,1,0,
  79     0,3,2,2,1,5,1,1,1,5,5,0,
  80     0,5,1,4,3,1,4,1,5,1,2,0,
  81     0,5,1,1,3,5,1,4,1,1,5,0,
  82     0,2,1,2,3,2,3,2,5,4,1,0,
  83     0,1,1,4,4,4,1,2,3,4,1,0,
  84     0,1,1,2,1,4,1,4,1,2,3,0,
  85     0,4,1,5,1,2,3,1,3,3,2,0,
  86     0,4,1,1,4,1,5,4,1,2,1,0,
  87     0,1,3,4,1,1,4,1,1,2,1,0,
  88     0,1,2,4,1,4,1,5,2,1,1,0,
  89     0,0,0,0,0,0,0,0,0,0,0,0
  90 ;
  91 
  92 int mp5[13][12] =
  93 
  94     0,0,0,0,0,0,0,0,0,0,0,0,
  95     0,1,1,2,3,1,1,1,5,1,1,0,
  96     0,3,2,2,1,5,1,1,1,5,5,0,
  97     0,5,1,4,3,1,4,1,5,1,2,0,
  98     0,5,1,1,3,5,1,4,1,1,5,0,
  99     0,2,1,2,5,1,4,1,5,4,1,0,
 100     0,1,1,4,4,1,1,2,3,4,1,0,
 101     0,1,1,2,1,4,1,4,1,3,3,0,
 102     0,4,1,5,1,1,3,1,3,3,2,0,
 103     0,4,1,1,4,1,5,4,1,2,1,0,
 104     0,1,3,4,1,1,4,1,1,2,1,0,
 105     0,1,2,4,1,4,1,5,5,1,1,0,
 106     0,0,0,0,0,0,0,0,0,0,0,0
 107 ;
 108 
 109 int mp6[13][12] =
 110 
 111     0,0,0,0,0,0,0,0,0,0,0,0,
 112     0,1,1,2,3,1,1,1,5,1,1,0,
 113     0,3,2,2,1,5,1,1,1,5,5,0,
 114     0,5,1,4,3,1,4,1,5,1,2,0,
 115     0,5,4,4,3,5,1,4,1,1,5,0,
 116     0,2,4,3,3,1,4,1,5,4,1,0,
 117     0,1,1,4,4,1,1,2,3,4,1,0,
 118     0,1,1,2,1,4,1,4,1,3,3,0,
 119     0,4,1,5,1,1,3,1,3,3,2,0,
 120     0,4,1,1,4,5,5,4,1,2,1,0,
 121     0,1,3,4,1,5,4,1,1,2,1,0,
 122     0,1,2,4,1,4,1,5,5,1,1,0,
 123     0,0,0,0,0,0,0,0,0,0,0,0
 124 ;
 125 
 126 int mp7[13][12] =
 127 
 128     0,0,0,0,0,0,0,0,0,0,0,0,
 129     0,1,1,2,3,1,1,1,5,1,1,0,
 130     0,3,2,2,1,5,1,2,4,4,5,0,
 131     0,5,1,4,3,1,2,2,5,1,2,0,
 132     0,5,1,1,3,5,1,4,1,1,5,0,
 133     0,2,5,3,3,1,4,1,5,4,1,0,
 134     0,1,5,5,4,1,1,2,3,4,1,0,
 135     0,1,5,2,1,4,1,4,1,3,3,0,
 136     0,4,1,5,1,1,3,1,3,3,2,0,
 137     0,4,1,1,4,1,5,4,1,2,1,0,
 138     0,1,3,4,1,1,4,1,1,2,1,0,
 139     0,1,2,4,1,4,1,5,5,1,1,0,
 140     0,0,0,0,0,0,0,0,0,0,0,0
 141 ;
 142 
 143 int mp8[13][12] =
 144 
 145     0,0,0,0,0,0,0,0,0,0,0,0,
 146     0,1,1,2,3,1,1,1,5,1,1,0,
 147     0,3,2,2,1,5,1,1,1,5,5,0,
 148     0,5,1,4,3,1,4,1,5,1,2,0,
 149     0,5,1,1,3,5,1,4,1,1,5,0,
 150     0,2,1,3,3,1,4,1,5,4,1,0,
 151     0,1,1,4,4,1,1,2,3,4,1,0,
 152     0,1,1,2,1,4,1,4,1,3,3,0,
 153     0,4,1,5,1,1,3,1,3,3,2,0,
 154     0,4,1,1,4,1,5,4,1,2,1,0,
 155     0,1,3,4,1,1,4,1,1,2,1,0,
 156     0,1,2,4,1,4,1,5,5,1,1,0,
 157     0,0,0,0,0,0,0,0,0,0,0,0
 158 ;
 159 
 160 int mp9[13][12] =
 161 
 162     0,0,0,0,0,0,0,0,0,0,0,0,
 163     0,1,1,2,3,1,1,1,5,1,1,0,
 164     0,3,2,2,1,5,1,1,1,5,5,0,
 165     0,5,1,4,3,1,4,1,5,1,2,0,
 166     0,5,1,1,3,5,1,4,1,1,5,0,
 167     0,2,4,3,1,3,4,1,5,4,1,0,
 168     0,1,4,4,4,1,1,2,3,4,1,0,
 169     0,1,1,2,1,4,1,4,1,3,3,0,
 170     0,4,1,5,1,1,3,1,3,3,2,0,
 171     0,4,2,1,4,1,5,4,1,2,1,0,
 172     0,1,3,4,1,1,4,1,1,2,1,0,
 173     0,1,2,4,1,4,1,5,5,1,1,0,
 174     0,0,0,0,0,0,0,0,0,0,0,0
 175 ;
 176 
 177 int mp10[13][12] =
 178 
 179     0,0,0,0,0,0,0,0,0,0,0,0,
 180     0,1,1,2,3,1,1,1,5,1,1,0,
 181     0,3,2,2,1,5,1,1,1,5,5,0,
 182     0,5,1,4,3,1,4,1,5,1,2,0,
 183     0,5,1,1,3,5,1,4,1,1,5,0,
 184     0,2,1,3,3,1,4,1,5,4,1,0,
 185     0,1,1,4,4,1,1,2,3,4,1,0,
 186     0,1,1,2,1,4,1,4,1,3,3,0,
 187     0,4,1,5,1,1,3,1,3,3,2,0,
 188     0,4,1,1,4,1,5,4,1,2,1,0,
 189     0,1,3,4,1,1,4,1,1,2,1,0,
 190     0,1,2,4,1,4,1,5,5,1,1,0,
 191     0,0,0,0,0,0,0,0,0,0,0,0
 192 ;
 193 /************************************************************************/
 194 
 195 //星星图片
 196 PIMAGE menubackground;//菜单背景
 197 PIMAGE background01;//游戏背景
 198 PIMAGE ranks;//排行榜背景
 199 PIMAGE star1;
 200 PIMAGE star2;
 201 PIMAGE star3;
 202 PIMAGE star4;
 203 PIMAGE star5;
 204 PIMAGE bomb1;//炸弹
 205 PIMAGE bomb2;
 206 PIMAGE bomb3;
 207 PIMAGE bomb4;
 208 PIMAGE bomb5;
 209 PIMAGE blank0;//空白
 210 PIMAGE number1;//排行榜前三徽标
 211 PIMAGE number2;
 212 PIMAGE number3;
 213 PIMAGE gameover;
 214 PIMAGE button01;
 215 PIMAGE label;
 216 
 217 /**************************************************************************/
 218 typedef struct Ranks
 219 
 220     char name[100];
 221     int score;
 222     Ranks *nxt;
 223 
 224  Ranks;
 225 //排行榜节点
 226 
 227 Ranks *head;
 228 //排行榜头指针
 229 
 230 int star_number = 0;
 231 //选中的同色星星的数量
 232 
 233 int tmp;
 234 //当前选中的星星的颜色
 235 
 236 char str[10];
 237 //临时字符串
 238 
 239 char name[100];
 240 //当前游戏的用户名
 241 
 242 int customs=0;
 243 //当前游戏关卡
 244 
 245 int score = 0;
 246 //当前游戏得分
 247 
 248 int live = 0;
 249 //道具是否激活
 250 
 251 int scores[35]= 0,0,10,15,22,31,42,55,70,87,105,126,149,174,201,230,261,294,329,366,405,446,489,534,581,630,682,740,800,862,924;
 252 //消灭星星数的得分
 253 
 254 int goal[20]= 0,600,1400,2400,3600,5000,6600,8400,10400,12600,15000;
 255 //目标得分
 256 
 257 int addscore[110]= 44,32,22,14,8,4,2;
 258 //剩余星星加分
 259 
 260 //搜索同色星星时的方向
 261 int fx[4][2] = 1,0,0,1,-1,0,0,-1;
 262 
 263 /*=================================函数声明====================================*/
 264 
 265 void menu();
 266 //游戏菜单
 267 
 268 void start();
 269 //开始游戏
 270 
 271 void help();
 272 //游戏帮助
 273 /*
 274 游戏开始会比较简单,开局会有很多相同颜色的星星出现,
 275 这时候就需要观察哪种颜色比较多,相连的比较近。
 276 尽量把最多颜色的所有星星都连在一起!在进行消除,
 277 这样会得到很多分数哦!!越玩到后面需要的分数会更高哦!
 278 */
 279 
 280 void init();
 281 //初始化素材
 282 
 283 void update();
 284 //更新界面
 285 
 286 void findstart(int x,int y);
 287 //查找与点击的星星的相同颜色的星星快
 288 //主要通过深度优先搜索实现
 289 
 290 void drawstars();
 291 //放置星星
 292 
 293 void breakstar();
 294 //消灭星星,通过更改矩阵对应的值为0实现
 295 
 296 void restore();
 297 //恢复状态,当点击的星星星星相邻的星星中没有与它颜色相同的,恢复矩阵的状态
 298 
 299 void slowdown();
 300 //星星下落一格
 301 
 302 bool checkslowdown();
 303 //判断星星是否还可以下落
 304 
 305 void tomiddle();
 306 //星星往中间移动
 307 
 308 bool checktomiddle();
 309 //判断星星是否还能往中间移动
 310 
 311 bool isend();
 312 //判断游戏是否结束
 313 
 314 void copymp();
 315 //复制地图
 316 
 317 void next();
 318 //下一关
 319 
 320 void readdata();
 321 //读取数据
 322 
 323 void savadata();
 324 //保存游戏得分
 325 
 326 void showranks();
 327 //显示排行榜
 328 
 329 void sortrank();
 330 //链表排序
 331 
 332 int surplus();
 333 //计算剩余的星星
 334 
 335 void drawprop();
 336 //放置游戏道具
 337 
 338 void* playmusic(void *a);
 339 //播放音乐
 340 
 341 void* playgame(void *b);
 342 
 343 
 344 
 345 /*================================主函数=======================================*/
 346 int main()
 347 
 348 
 349     readdata();//加载排行榜
 350     init();//初始化素材
 351 
 352     //定义两个线程
 353     pthread_t thread1;//播放背景音乐
 354     pthread_t thread2;//游戏线程
 355 
 356     //背景音乐线程
 357     if(pthread_create(&thread1, NULL, playmusic, NULL) == -1)
 358     
 359         puts("fail to create pthread thread1");
 360         exit(1);
 361     
 362 
 363     //游戏线程
 364     if(pthread_create(&thread2, NULL, playgame, NULL) == -1)
 365     
 366         puts("fail to create pthread thread2");
 367         exit(1);
 368     
 369     // 等待线程结束
 370     void * result;
 371     if(pthread_join(thread1, &result) == -1)
 372     
 373         puts("fail to recollect thread1");
 374         exit(1);
 375     
 376 
 377     if(pthread_join(thread2, &result) == -1)
 378     
 379         puts("fail to recollect thread2");
 380         exit(1);
 381     
 382     getch();
 383 
 384 
 385 /*=============================其他函数=============================*/
 386 //背景音乐线程
 387 void* playmusic(void *a)
 388 
 389 
 390     mciSendString("open D:\\CC++code\\Starts\\Music\\background02.mp3",NULL,0,NULL);
 391 
 392     mciSendString("play D:\\CC++code\\Starts\\Music\\background02.mp3",NULL,0,NULL);
 393 
 394     while(1);
 395 
 396 
 397 //游戏线程
 398 void* playgame(void *b)
 399 
 400     //显示菜单
 401     menu();
 402 
 403 
 404 //游戏菜单
 405 void menu()
 406 
 407 
 408     int wantto=0;
 409     putimage(0,0,menubackground);
 410     mouse_msg mmsg;
 411     while(true)
 412     
 413         mmsg = getmouse();
 414         if (mmsg.is_left() && mmsg.is_down())//单击左键
 415         
 416             int Tx = mmsg.y;
 417             int Ty = mmsg.x;
 418             if(Tx>=255&&Tx<=307&&Ty>=102&&Ty<=366)
 419             
 420                 //开始游戏
 421                 wantto = 1;
 422                 break;
 423             
 424             else if(Tx>=325&&Tx<=377&&Ty>=102&&Ty<=366)
 425             
 426                 //游戏帮助
 427                 wantto = 2;
 428                 break;
 429             
 430             else if(Tx>=392&&Tx<=445&&Ty>=102&&Ty<=366)
 431             
 432                 //排行榜
 433                 wantto = 3;
 434                 break;
 435             
 436             else if(Tx>=489&&Tx<=523&&Ty>=355&&Ty<=443)
 437             
 438                 //退出游戏
 439                 wantto = 4;
 440                 break;
 441             
 442         
 443 
 444     
 445     if(wantto==1)
 446     
 447         // 设置字体样式
 448         setfont(-24, 0,"宋体");
 449         // 设置文字颜色
 450         setcolor(EGERGB(0xFF, 0x00, 0x00));
 451         //设置文字背景透明
 452         setbkmode(TRANSPARENT);
 453         //显示文字
 454         outtextxy(100,500,"正在进入游戏>>>");
 455         //绘制无颜色填充矩形
 456         rectangle(98,530,366,545);
 457         //设置矩形填充颜色
 458         setfillcolor(RED);
 459         //进度条进度
 460         int progress = 100;
 461         while(progress<366)
 462         
 463             //绘制有颜色填充矩形
 464             bar(100,532,progress,543);
 465             //进度增加
 466             progress+=1;
 467             Sleep(10);
 468         
 469         Sleep(500);
 470         //要求用户输入用户名
 471         inputbox_getline("请输入", "请输入您的用户名(回车确认)", name, 100);
 472         score = 0;
 473         customs = 0;
 474         next();
 475     
 476     else if(wantto==2)
 477     
 478         // 设置字体样式
 479         setfont(-24, 0,"宋体");
 480         // 设置文字颜色
 481         setcolor(EGERGB(0xFF, 0x00, 0x00));
 482         //设置文字背景透明
 483         setbkmode(TRANSPARENT);
 484         //显示文字
 485         outtextxy(100,500,"正在查询游戏帮助>>>");
 486         //绘制无颜色填充矩形
 487         rectangle(98,530,366,545);
 488         //设置矩形填充颜色
 489         setfillcolor(RED);
 490         //进度条进度
 491         int progress = 100;
 492         while(progress<366)
 493         
 494             //绘制有颜色填充矩形
 495             bar(100,532,progress,543);
 496             //进度增加
 497             progress+=1;
 498             Sleep(10);
 499         
 500         Sleep(500);
 501         help();
 502     
 503     else if(wantto==3)
 504     
 505         // 设置字体样式
 506         setfont(-24, 0,"宋体");
 507         // 设置文字颜色
 508         setcolor(EGERGB(0xFF, 0x00, 0x00));
 509         //设置文字背景透明
 510         setbkmode(TRANSPARENT);
 511         //显示文字
 512         outtextxy(100,500,"正在加载排行榜>>>");
 513         //绘制无颜色填充矩形
 514         rectangle(98,530,366,545);
 515         //设置矩形填充颜色
 516         setfillcolor(RED);
 517         //进度条进度
 518         int progress = 100;
 519         while(progress<366)
 520         
 521             //绘制有颜色填充矩形
 522             bar(100,532,progress,543);
 523             //进度增加
 524             progress+=1;
 525             Sleep(10);
 526         
 527         Sleep(500);
 528         showranks();
 529 
 530     
 531     else if(wantto==4)
 532     
 533         // 设置字体样式
 534         setfont(-24, 0,"宋体");
 535         // 设置文字颜色
 536         setcolor(EGERGB(0xFF, 0x00, 0x00));
 537         //设置文字背景透明
 538         setbkmode(TRANSPARENT);
 539         //显示文字
 540         outtextxy(100,500,"正在退出游戏>>>");
 541         //绘制无颜色填充矩形
 542         rectangle(98,530,366,545);
 543         //设置矩形填充颜色
 544         setfillcolor(RED);
 545         //进度条进度
 546         int progress = 100;
 547         while(progress<366)
 548         
 549             //绘制有颜色填充矩形
 550             bar(100,532,progress,543);
 551             //进度增加
 552             progress+=1;
 553             Sleep(10);
 554         
 555         Sleep(500);
 556         closegraph();
 557     
 558 
 559 
 560 
 561 //游戏逻辑
 562 void start()
 563 
 564     mouse_msg mmsg;
 565     //鼠标事件
 566     while (true)
 567     
 568         mmsg = getmouse();
 569         if (mmsg.is_left() && mmsg.is_down())//单击左键
 570         
 571 
 572             int tx = (mmsg.y-70)/40;
 573             int ty = (mmsg.x-30)/40;
 574             if(mmsg.x>=190&&mmsg.x<=265&&mmsg.y>=532&&mmsg.y<=558)//结束本局游戏按钮位置
 575             
 576                 putimage(100,300,label);
 577                 // 设置字体样式
 578                 setfont(-18, 0,"宋体");
 579                 // 设置文字颜色
 580                 setcolor(EGERGB(0x24, 0x24, 0x24));
 581                 //设置文字背景透明
 582                 setbkmode(TRANSPARENT);
 583                 //显示文字
 584                 outtextxy(105,330,"正在为您保存游戏数据,请稍候...");
 585                 //绘制无颜色填充矩形
 586                 rectangle(105,360,366,367);
 587                 //设置矩形填充颜色
 588                 setfillcolor(RED);
 589                 //进度条进度
 590                 int progress = 105;
 591                 while(progress<366)
 592                 
 593                     //绘制有颜色填充矩形
 594                     bar(107,362,progress,365);
 595                     //进度增加
 596                     progress+=1;
 597                     Sleep(10);
 598                 
 599 
 600                 //新建一个链表节点保存得分
 601                 Ranks *p = (Ranks *)malloc(sizeof(Ranks));//为节点分配内存空间
 602                 strcpy(p->name,name);//用户的名字
 603                 p->score = score;   //得分
 604                 p->nxt = head;
 605                 head = p;
 606                 putimage(150,295,gameover);
 607 
 608                 savadata();   //保存至文件
 609                 menu();      //返回菜单
 610 
 611             
 612 
 613             else if(mmsg.y>=72 && mmsg.y<=512 && mmsg.x>=32 && mmsg.x<=432)
 614             
 615                 //当前鼠标坐标对应矩阵的位置
 616                 int x = tx+1;
 617                 int y = ty+1;
 618                 tmp = mp[x][y];//记录点击的星星的颜色
 619 
 620                 //当当前位置没有星星时不执行深度优先搜素,否则容易导致栈溢出
 621                 if(tmp!=0)
 622                     findstart(x,y);
 623 
 624                 //如果点击的星星有相邻的相同颜色的星星
 625                 if(star_number>1)
 626                 
 627                     score+=scores[star_number];
 628                     breakstar();
 629                     update();//更新界面
 630 
 631                     if(isend()==true)     //没有可以消灭的星星
 632                     
 633                         int num = surplus();
 634                         score+=addscore[num];
 635 
 636                         //得分达到目标分,进入下一关
 637                         if(score>=goal[customs])
 638                         
 639                             next();
 640                         
 641 
 642                         //得分没有达到目标分
 643                         else
 644                         
 645                             putimage(100,300,label);
 646                             // 设置字体样式
 647                             setfont(-18, 0,"宋体");
 648                             // 设置文字颜色
 649                             setcolor(EGERGB(0x24, 0x24, 0x24));
 650                             //设置文字背景透明
 651                             setbkmode(TRANSPARENT);
 652                             //显示文字
 653                             outtextxy(105,330,"游戏结束,正在返回...");
 654                             //绘制无颜色填充矩形
 655                             rectangle(105,360,366,367);
 656                             //设置矩形填充颜色
 657                             setfillcolor(RED);
 658                             //进度条进度
 659                             int progress = 105;
 660                             while(progress<366)
 661                             
 662                                 //绘制有颜色填充矩形
 663                                 bar(107,362,progress,365);
 664                                 //进度增加
 665                                 progress+=1;
 666                                 Sleep(10);
 667                             
 668 
 669                             //保存得分
 670                             Ranks *p = (Ranks *)malloc(sizeof(Ranks));
 671                             p->nxt = head;
 672                             strcpy(p->name,name);
 673                             p->score = score;
 674                             head = p;
 675                             putimage(150,295,gameover);
 676 
 677                             savadata();
 678                             menu();
 679                         
 680                     
 681                 
 682                 else//只有一颗星星
 683                 
 684                     //将6还原成原来的状态
 685                     restore();
 686                 
 687 
 688                 //本次消灭的星星大于10个,激活道具
 689                 if(star_number>10+customs)
 690                 
 691                     //放置道具
 692                     drawprop();
 693                 
 694 
 695                 star_number=0;//星星数量归0
 696             
 697 
 698             //点击道具位置
 699             else if(mmsg.y>=25 && mmsg.y<=65 && mmsg.x>=200 && mmsg.x<=240 &&live!=0)
 700             
 701                 for(int i=1; i<=11; i++)
 702                 
 703                     for(int j=1; j<=10; j++)
 704                     
 705                         if(mp[i][j]==live)
 706                         
 707                             mp[i][j]=0;
 708                             star_number++;
 709                         
 710                     
 711                 
 712                 score+=scores[star_number];
 713                 star_number = 0;
 714                 live = 0;
 715                 putimage(200,25,40,40,background01,200,25);
 716                 update();//更新界面
 717 
 718                 if(isend()==true)     //没有可以消灭的星星
 719                 
 720                     int num = surplus();
 721                     score+=addscore[num];
 722 
 723                     //得分达到目标分,进入下一关
 724                     if(score>=goal[customs])
 725                     
 726                         next();
 727                     
 728 
 729                     //得分没有达到目标分
 730                     else
 731                     
 732                         putimage(100,300,label);
 733                         // 设置字体样式
 734                         setfont(-18, 0,"宋体");
 735                         // 设置文字颜色
 736                         setcolor(EGERGB(0x24, 0x24, 0x24));
 737                         //设置文字背景透明
 738                         setbkmode(TRANSPARENT);
 739                         //显示文字
 740                         outtextxy(105,330,"游戏结束,正在返回...");
 741                         //绘制无颜色填充矩形
 742                         rectangle(105,360,366,367);
 743                         //设置矩形填充颜色
 744                         setfillcolor(RED);
 745                         //进度条进度
 746                         int progress = 105;
 747                         while(progress<366)
 748                         
 749                             //绘制有颜色填充矩形
 750                             bar(107,362,progress,365);
 751                             //进度增加
 752                             progress+=1;
 753                             Sleep(10);
 754                         
 755                         //保存得分
 756                         Ranks *p = (Ranks *)malloc(sizeof(Ranks));
 757                         p->nxt = head;
 758                         strcpy(p->name,name);
 759                         p->score = score;
 760                         head = p;
 761                         putimage(150,295,gameover);
 762 
 763                         savadata();
 764                         menu();
 765                     
 766                 
 767             
 768         
 769     
 770 
 771 
 772 //素材初始化
 773 void init()
 774 
 775     //初始化窗口大小
 776     initgraph(465,590);
 777 
 778     menubackground = newimage();
 779     background01 = newimage();
 780     ranks = newimage();
 781     star1 = newimage();
 782     star2 = newimage();
 783     star3 = newimage();
 784     star4 = newimage();
 785     star5 = newimage();
 786     bomb1 = newimage();
 787     bomb2 = newimage();
 788     bomb3 = newimage();
 789     bomb4 = newimage();
 790     bomb5 = newimage();
 791     blank0 = newimage();
 792     number1 = newimage();
 793     number2 = newimage();
 794     number3 = newimage();
 795     gameover = newimage();
 796     button01 = newimage();
 797     label = newimage();
 798 
 799     getimage(menubackground,"Gameimage/menu01.png");
 800     getimage(background01,"Gameimage/background01.png");
 801     getimage(ranks,"Gameimage/ranks.png");
 802     getimage(star1,"Gameimage/star1.png");
 803     getimage(star2,"Gameimage/star2.png");
 804     getimage(star3,"Gameimage/star3.png");
 805     getimage(star4,"Gameimage/star4.png");
 806     getimage(star5,"Gameimage/star5.png");
 807     getimage(bomb1,"Gameimage/bomb1.png");
 808     getimage(bomb2,"Gameimage/bomb2.png");
 809     getimage(bomb3,"Gameimage/bomb3.png");
 810     getimage(bomb4,"Gameimage/bomb4.png");
 811     getimage(bomb5,"Gameimage/bomb5.png");
 812     getimage(blank0,"Gameimage/blank0.png");
 813     getimage(number1,"Gameimage/number1.png");
 814     getimage(number2,"Gameimage/number2.png");
 815     getimage(number3,"Gameimage/number3.png");
 816     getimage(gameover,"Gameimage/gameover.png");
 817     getimage(button01,"Gameimage/button01.png");
 818     getimage(label,"Gameimage/label02.png");
 819 
 820 
 821 
 822 //游戏帮助
 823 void help()
 824 
 825     putimage(0,0,background01);
 826     // 设置字体样式
 827     setfont(-20, 0,"宋体");
 828     // 设置文字颜色
 829     setcolor(EGERGB(0x4F, 0x4F, 0x4F));
 830     //设置文字背景透明
 831     setbkmode(TRANSPARENT);
 832     //显示文字
 833 
 834     outtextxy(100,100,"游戏开始会比较简单,相连的");
 835     Sleep(500);
 836     outtextxy(100,130,"比较近。开局会有很多相同颜");
 837     Sleep(500);
 838     outtextxy(100,160,"色的星星出现,这时候就需要");
 839     Sleep(500);
 840     outtextxy(100,190,"观察哪种颜色比较多,尽量把");
 841     Sleep(500);
 842     outtextxy(100,220,"最多颜色的所有星星都连在一");
 843     Sleep(500);
 844     outtextxy(100,250,"起!再进行消除,这样会得到");
 845     Sleep(500);
 846     outtextxy(100,280,"更多分数哦!!越玩到后面需");
 847     Sleep(500);
 848     outtextxy(100,310,"    要的分数会更高哦!");
 849     Sleep(500);
 850 
 851     setcolor(EGERGB(0xFF, 0x00, 0x00));
 852     //设置字体颜色
 853     outtextxy(100,360,"      祝您游戏愉快!");
 854     Sleep(500);
 855     outtextxy(100,390,"  单击任意位置返回主菜单");
 856 
 857     mouse_msg mmsg;
 858     while(true)
 859     
 860         mmsg = getmouse();
 861         if (mmsg.is_left() && mmsg.is_down())//单击左键
 862         
 863             //单击任意位置返回主菜单
 864             menu();
 865             break;
 866         
 867     
 868 
 869 void drawstars()
 870 
 871     for(int i=1; i<=11; i++)
 872     
 873         for(int j=1; j<=10; j++)
 874         
 875             if(mp[i][j]==1)
 876                 putimage((j-1)*40+32,(i-1)*40+72,star1);
 877             else if(mp[i][j]==2)
 878                 putimage((j-1)*40+32,(i-1)*40+72,star2);
 879             else if(mp[i][j]==3)
 880                 putimage((j-1)*40+32,(i-1)*40+72,star3);
 881             else if(mp[i][j]==4)
 882                 putimage((j-1)*40+32,(i-1)*40+72,star4);
 883             else if(mp[i][j]==5)
 884                 putimage((j-1)*40+32,(i-1)*40+72,star5);
 885             else if(mp[i][j]==0)
 886                 putimage((j-1)*40+32,(i-1)*40+72,40,40,background01,(j-1)*40+32,(i-1)*40+72);
 887 
 888         
 889     
 890     sprintf(str,"%d",score);
 891     //将得分转化为字符串
 892     putimage(390,32,100,32,background01,390,32);
 893     setcolor(EGERGB(0xEE, 0x0, 0x0));
 894     setfont(-20, 0,"宋体");
 895     outtextxy(390,45,str);
 896     //绘制出当前得分
 897 
 898 
 899 //更新界面
 900 void update()
 901 
 902     drawstars();
 903 
 904     //当还有星星可以下降时
 905     while(checkslowdown())
 906     
 907         //星星下落
 908         slowdown();
 909 
 910         //更新界面
 911         drawstars();
 912         Sleep(80);
 913     
 914 
 915     //当星星还能往中间靠时
 916     while(checktomiddle())
 917     
 918         //星星往中间靠
 919         tomiddle();
 920 
 921         //更新界面
 922         drawstars();
 923         Sleep(80);
 924     
 925 
 926 
 927 //查找相同的星星
 928 void findstart(int x,int y)
 929 
 930     star_number++;
 931     mp[x][y] = 6;
 932     for(int i = 0 ; i < 4; i++)
 933     
 934         int dx = x + fx[i][0];
 935         int dy = y + fx[i][1];
 936         if(mp[dx][dy]!=tmp)
 937         
 938             continue;
 939         
 940 
 941         findstart(dx,dy);
 942     
 943     return;
 944 
 945 
 946 //消灭星星
 947 void breakstar()
 948 
 949     for(int i = 1; i<=11; i++)
 950     
 951         for(int j = 1; j<=10; j++)
 952         
 953             if(mp[i][j]==6)
 954             
 955                 mp[i][j]=0;
 956             
 957         
 958     
 959 
 960 
 961 //矩阵恢复
 962 void restore()
 963 
 964     for(int i=1; i<=11; i++)
 965     
 966         for(int j=1; j<=10; j++)
 967         
 968             if(mp[i][j]==6)
 969                 mp[i][j]=tmp;
 970         
 971     
 972 
 973 
 974 //星星下落一格
 975 void slowdown()
 976 
 977     for(int i=10; i>=0; i--)
 978     
 979         for(int j = 1; j<=11; j++)
 980         
 981             if(mp[i+1][j]==0)
 982             
 983                 mp[i+1][j]=mp[i][j];
 984                 mp[i][j]=0;
 985             
 986         
 987     
 988 
 989 
 990 //判断是否还可以下落
 991 bool checkslowdown()
 992 
 993     for(int i = 1; i<=11; i++)
 994     
 995         for(int j=1; j<=10; j++)
 996         
 997             if(mp[i][j]==0&&mp[i-1][j]!=0)//空白的上面还有星星
 998                 return true;
 999         
1000     
1001     return false;
1002 
1003 
1004 //当某一列没有星星时,星星需要往中间靠
1005 void tomiddle()
1006 
1007     for(int j = 5; j>1; j--)
1008     
1009         if(mp[11][j]==0)//第j列没有星星
1010         
1011             //第j列左边的星星全都往右移动一列
1012             for(int i=1; i<=11; i++)
1013             
1014                 mp[i][j] = mp[i][j-1];
1015                 mp[i][j-1] = 0;
1016             
1017         
1018     
1019     for(int j = 6; j<10; j++)
1020     
1021         if(mp[11][j]==0)
1022         
1023             for(int i =1 ; i<= 11; i++)
1024             
1025                 mp[i][j] = mp[i][j+1];
1026                 mp[i][j+1] = 0;
1027             
1028         
1029     
1030 
1031 
1032 //判断是否还能往中间靠
1033 bool checktomiddle()
1034 
1035     for(int i = 5; i>1; i--)
1036     
1037         if(mp[11][i] == 0 && mp[11][i-1] != 0)
1038             return true;
1039     
1040     for(int i =6; i<10; i++)
1041     
1042         if(mp[11][i]==0 && mp[11][i+1] != 0)
1043             return true;
1044     
1045 
1046     return false;
1047 
1048 
1049 
1050 //判断游戏是否结束
1051 bool isend()
1052 
1053     for(int i = 1; i<=11; i++)
1054     
1055         for(int j=1; j<=10; j++)
1056         
1057             //有相邻的且颜色相同的星星
1058             if(mp[i][j]!=0&&(mp[i][j]==mp[i-1][j]||mp[i][j]==mp[i+1][j]||mp[i][j]==mp[i][j-1]||mp[i][j]==mp[i][j+1]))
1059                 return false;
1060         
1061     
1062     return true;
1063 
1064 
1065 
1066 //下一关
1067 void next()
1068 
1069     //关数加一
1070     customs++;
1071     if(customs>1)
1072     
1073         // 设置字体样式
1074         setfont(-18, 0,"宋体");
1075         // 设置文字颜色
1076         setcolor(EGERGB(0xFF, 0x00, 0xFF));
1077         //设置文字背景透明
1078         setbkmode(TRANSPARENT);
1079         //显示文字
1080         outtextxy(100,100,"恭喜通过本关,正在进入下一关...");
1081         //绘制无颜色填充矩形
1082         rectangle(98,130,366,145);
1083         //设置矩形填充颜色
1084         setfillcolor(EGERGB(0xFF, 0xFF, 0x00));
1085         //进度条进度
1086         int progress = 100;
1087         while(progress<366)
1088         
1089             //绘制有颜色填充矩形
1090             bar(100,132,progress,143);
1091             //进度增加
1092             progress+=1;
1093             Sleep(5);
1094         
1095     
1096     //放置背景图
1097     putimage(0,0,background01);
1098 
1099     //放置返回按钮
1100     putimage(190,530,button01);
1101 
1102     //复制地图
1103     copymp();
1104 
1105     //放置星星
1106     drawstars();
1107 
1108     //将关数转化为字符串
1109     sprintf(str,"%d",customs);
1110 
1111     // 设置字体样式
1112     setfont(-24, 0,"宋体");
1113 
1114     // 设置文字颜色
1115     setcolor(EGERGB(0x0, 0x0, 0xCD));
1116 
1117     //绘制文字
1118     outtextxy(50,30,"");
1119     outtextxy(85,30,str);
1120     outtextxy(110,30,"");
1121 
1122     sprintf(str,"%d",goal[customs]);
1123     //目标得分转化为字符串
1124 
1125     setfont(-20, 0,"宋体");
1126     outtextxy(300,15,"目标得分:");
1127     outtextxy(390,15,str);
1128     outtextxy(300,45,"当前得分:");
1129     start();
1130 
1131 
1132 
1133 //显示排行榜
1134 void showranks()
1135 
1136     putimage(0,0,ranks);
1137     putimage(40,75,number1,SRCAND);
1138     putimage(40,117,number2,SRCAND);
1139     putimage(40,159,number3,SRCAND);
1140     putimage(190,486,button01);
1141 
1142     //为排行榜排序
1143     sortrank();
1144 
1145     Ranks *p = head;
1146     int index = 1;
1147     while(p!=NULL&&index<=10)
1148     
1149         sprintf(str,"%d",index);                //排名转化为字符串
1150         outtextxy(75,40+index*40,str);          //排名
1151 
1152         outtextxy(130,40+index*40,p->name);    //名字
1153 
1154         sprintf(str,"%d",p->score);            //得分转化为字符串
1155         outtextxy(250,40+index*40,str);        //得分
1156 
1157         p = p->nxt;
1158         index++;
1159     
1160     mouse_msg mmsg;
1161     while(true)
1162     
1163         mmsg = getmouse();
1164         if(mmsg.is_left() && mmsg.is_down())
1165         
1166             if(mmsg.y>=486&&mmsg.y<=518&&mmsg.x>=190&&mmsg.x<=265)
1167             
1168                 // 设置字体样式
1169                 setfont(-24, 0,"宋体");
1170                 // 设置文字颜色
1171                 setcolor(EGERGB(0x24, 0x24, 0x24));
1172                 //设置文字背景透明
1173                 setbkmode(TRANSPARENT);
1174                 //显示文字
1175                 outtextxy(100,400,"正在返回菜单>>>");
1176                 //绘制无颜色填充矩形
1177                 rectangle(98,430,366,445);
1178                 //设置矩形填充颜色
1179                 setfillcolor(RED);
1180                 //进度条进度
1181                 int progress = 100;
1182                 while(progress<366)
1183                 
1184                     //绘制有颜色填充矩形
1185                     bar(100,432,progress,443);
1186                     //进度增加
1187                     progress+=1;
1188                     Sleep(10);
1189                 
1190                 menu();
1191             
1192         
1193     
1194 
1195 
1196 //排行榜按从高分到低分排序
1197 void sortrank()
1198 
1199     Ranks *p,*q;
1200     for(p = head; p != NULL; p = p->nxt)
1201     
1202         for(q = p; q != NULL; q = q->nxt)
1203         
1204             if(q->score>p->score)
1205             
1206                 int t = p->score;
1207                 p->score = q->score;
1208                 q->score = t;
1209 
1210                 char nametmp[100];
1211                 strcpy(nametmp,p->name);
1212                 strcpy(p->name,q->name);
1213                 strcpy(q->name,nametmp);
1214             
1215         
1216     
1217 
1218 
1219 //读取排行榜
1220 void readdata()
1221 
1222     FILE *fp;
1223     fp = fopen("datas.txt","r");
1224     if(fp == NULL)
1225     
1226         printf("系统异常,请重新进入游戏!\n");
1227         exit(1);
1228     
1229     char tmp_name[100];
1230     int tmp_score;
1231     Ranks *p;
1232     head = NULL;
1233     while(fscanf(fp,"%s %d",tmp_name,&tmp_score)!=EOF)
1234     
1235         p = (Ranks *)malloc(sizeof(Ranks));
1236         strcpy(p->name,tmp_name);
1237         p->score = tmp_score;
1238         p->nxt = head;
1239         head = p;
1240     
1241     fclose(fp);
1242 
1243 
1244 //保存游戏得分至文件
1245 void savadata()
1246 
1247     //文件指针
1248     FILE *fp;
1249     fp = fopen("datas.txt","w");//打开文件
1250     if(fp == NULL)
1251     
1252         printf("系统异常,数据保存失败!\n");
1253         exit(1);
1254     
1255 
1256     Ranks *p = head;
1257     while(p != NULL)
1258     
1259         fprintf(fp,"%s %d\n",p->name,p->score);
1260         p = p->nxt;
1261     
1262     fclose(fp);//关闭文件
1263 
1264 
1265 //复制地图
1266 void copymp()
1267 
1268     switch(customs)
1269     
1270     case 1:
1271         for(int i = 0; i<13; i++)
1272         
1273             for(int j=0; j<12; j++)
1274             
1275                 mp[i][j]=mp1[i][j];
1276             
1277         
1278         break;
1279     case 2:
1280         for(int i = 0; i<13; i++)
1281         
1282             for(int j=0; j<12; j++)
1283             
1284                 mp[i][j]=mp2[i][j];
1285             
1286         
1287         break;
1288     case 3:
1289         for(int i = 0; i<13; i++)
1290         
1291             for(int j=0; j<12; j++)
1292             
1293                 mp[i][j]=mp3[i][j];
1294             
1295         
1296         break;
1297     case 4:
1298         for(int i = 0; i<13; i++)
1299         
1300             for(int j=0; j<12; j++)
1301             
1302                 mp[i][j]=mp4[i][j];
1303             
1304         
1305         break;
1306     case 5:
1307         for(int i = 0; i<13; i++)
1308         
1309             for(int j=0; j<12; j++)
1310             
1311                 mp[i][j]=mp5[i][j];
1312             
1313         
1314         break;
1315     case 6:
1316         for(int i = 0; i<13; i++)
1317         
1318             for(int j=0; j<12; j++)
1319             
1320                 mp[i][j]=mp6[i][j];
1321             
1322         
1323         break;
1324     case 7:
1325         for(int i = 0; i<13; i++)
1326         
1327             for(int j=0; j<12; j++)
1328             
1329                 mp[i][j]=mp7[i][j];
1330             
1331         
1332         break;
1333     case 8:
1334         for(int i = 0; i<13; i++)
1335         
1336             for(int j=0; j<12; j++)
1337             
1338                 mp[i][j]=mp8[i][j];
1339             
1340         
1341         break;
1342     case 9:
1343         for(int i = 0; i<13; i++)
1344         
1345             for(int j=0; j<12; j++)
1346             
1347                 mp[i][j]=mp9[i][j];
1348             
1349         
1350         break;
1351     case 10:
1352         for(int i = 0; i<13; i++)
1353         
1354             for(int j=0; j<12; j++)
1355             
1356                 mp[i][j]=mp10[i][j];
1357             
1358         
1359         break;
1360     
1361 
1362 
1363 //剩余星星数
1364 int surplus()
1365 
1366     int num = 0;
1367     for(int i = 0; i<13; i++)
1368     
1369         for(int j=0; j<12; j++)
1370         
1371             if(mp[i][j]!=0)
1372                 num++;
1373         
1374     
1375     return num;
1376 
1377 
1378 //放置道具
1379 void drawprop()
1380 
1381     srand((unsigned)time(NULL));
1382     live = rand()%5+1;//产生1到5的随机数
1383     switch(live)
1384     
1385     case 1:
1386         putimage(200,25,bomb1);
1387         break;
1388     case 2:
1389         putimage(200,25,bomb2);
1390         break;
1391     case 3:
1392         putimage(200,25,bomb3);
1393         break;
1394     case 4:
1395         putimage(200,25,bomb4);
1396         break;
1397     case 5:
1398         putimage(200,25,bomb5);
1399         break;
1400     default:
1401         break;
1402     
1403 
View Code

以上是关于大一课设 消灭星星的主要内容,如果未能解决你的问题,请参考以下文章

Cocos Creator开发游戏消灭星星——星星生成

PopStar(消灭星星)游戏源代码下载分析及跨平台移植—第一篇(界面)

cocos2d-x实现一个PopStar(消灭星星)游戏的逻辑分析及源码

8月25号课设个人小结(组名:double-H)

Cocos2d-x 3.4 之 消灭星星 &gt; 第三篇(终) &lt;

使用Canvas绘制星星闪烁的效果