仅用于记录。
代码
#include <graphics.h> #include <time.h> #include <stdio.h> #include <conio.h> #define ROW 9 #define COL 9 #define SIZE 25 #define EASY 10 #define SPEED 500 int main() { //重新开始游戏 again: int x,y,r = 0,g = 0,b = 0,count = 0,row,col,s = 0; int speed = SPEED; //建立一个用于存放小球的二维数组 int arr[ROW][COL] = { 0 }; //随机数 srand((unsigned int)time(NULL)); //建立窗口 initgraph(ROW * (2 * SIZE) + 200, COL * (2 * SIZE)); //设定背景颜色 setbkcolor(WHITE); cleardevice(); //初始化小球数组 for (x = 0; x < ROW; x++) { for (y = 0; y < COL; y++) { arr[x][y] = 1; } } //建立字符数组,用于后面显示分数 char ch[5]; //显示游戏基本情况 settextstyle(16, 0, "新宋体"); settextcolor(RGB(248, 99, 82)); outtextxy(ROW * (2 * SIZE) + 10, 50, "通过鼠标点击来消除小球"); outtextxy(ROW * (2 * SIZE) + 10, 70, "当屏幕中小球达到10个时"); outtextxy(ROW * (2 * SIZE) + 10, 90, "则游戏结束"); outtextxy(ROW * (2 * SIZE) + 10, 110, "难度:普通"); outtextxy(ROW * (2 * SIZE) + 10, 130, "按下空格键开始游戏"); setlinecolor(BROWN); rectangle(1, 1, ROW * (2 * SIZE)-1, COL * (2 * SIZE)- 1); //键盘操作开始游戏 char key = 0; key = _getch(); while (1) { switch (key) { case 32: while (count < EASY) { for (row = 0; row < ROW; row++) { for (col = 0; col < COL; col++) { //使小球获得随机颜色 r = rand() % 255 + 20; g = rand() % 255 + 20; b = rand() % 255 + 20; x = rand() % ROW; setfillcolor(RGB(r, g, b)); //获取鼠标点击的信息 if (MouseHit()) { MOUSEMSG msg = GetMouseMsg(); switch (msg.uMsg) { case WM_LBUTTONDOWN: //判断鼠标点击的坐标处是否有小球 if (arr[msg.y / (SIZE*2)][msg.x / (SIZE*2)] == 2) { clearcircle(SIZE + ((msg.x / (SIZE*2)) * 2 * SIZE), SIZE + ((msg.y / (SIZE*2)) * 2 * SIZE), SIZE); arr[msg.y / (SIZE*2)][msg.x / (SIZE*2)] = 1; count--; s += 1; printf("sore = %d\n", s); sprintf_s(ch, "%d", s); outtextxy(ROW * (2 * SIZE) + 10, 150, "score:"); outtextxy(ROW * (2 * SIZE) + 80, 150, ch); } } } //鼠标无操作,则立即开始随机生成小球 //避免重复生成在同一个位置,加多判断条件 else if (x == 5 && arr[row][col] != 2) { arr[row][col] = 2; solidcircle(SIZE + ((col) * 2 * SIZE), SIZE + ((row) * 2 * SIZE), SIZE); count++; Sleep(speed); } else continue; } } //使得生成小球的速度随着鼠标点击次数越多而越快 if (speed >= 350) speed -= 10; } //显示游戏结束情况 outtextxy(ROW * (2 * SIZE) + 10, 200, "游戏结束"); outtextxy(ROW * (2 * SIZE) + 10, 220, "您最终的游戏分数为"); settextstyle(18, 0, "宋体"); settextcolor(RGB(51, 89, 24)); sprintf_s(ch, "%d", s); outtextxy(ROW * (2 * SIZE) + 40, 250, ch); //重新开始游戏,跳到again outtextxy(ROW * (2 * SIZE) + 10, 300, "按下空格键重新开始"); key = _getch(); while (1) { switch (key) { case 32: goto again; break; default: break; } } break; default: break; } } getchar(); closegraph(); }
运行结果