随便用 raylib.h 做了个扫雷
软件是小熊猫c++1.4
编译器是 MinGW GCC 11.2.0 64-bit Release
下载小熊猫c++
代码:
#include<raylib.h>
#include<thread>
#include<cmath>
#include<iostream>
#define width 480
#define height 580
#define size_block width/(level*4)
#define key(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
int level=1,stop,gameover=0,gamewon=0,open=0,boom=0,time_open=100;//变量定义
char getkey;
//其实也可以用struct的
Vector2 pop_xy;
int game_map[100][100]={},game_map_open[100][100]={},game_map_boom[100][100]={};//数组定义
void draw_block(int a,bool b,int x,int y,bool c,int x_,int y_){//绘制方块
if(a>=10&&c){
DrawRectangle(x,y,size_block,size_block,DARKPURPLE);
DrawRectangleLines(x,y,size_block,size_block,BLACK);
}
else if(b&&game_map[x_][y_]!=0){
DrawRectangle(x,y,size_block,size_block,DARKGRAY);
DrawRectangleLines(x,y,size_block,size_block,BLACK);
if(a==1)
DrawText(TextFormat("%i",a),x+size_block/2,y,size_block,SKYBLUE);
else if(a==2)
DrawText(TextFormat("%i",a),x+size_block/2-15,y,size_block,BLUE);
else if(a==3)
DrawText(TextFormat("%i",a),x+size_block/2-15,y,size_block,ORANGE);
else if(a==4)
DrawText(TextFormat("%i",a),x+size_block/2-15,y,size_block,RED);
else
DrawText(TextFormat("%i",a),x+size_block/2-15,y,size_block,LIGHTGRAY);
}
else if(game_map[x_][y_]==0&&b){
DrawRectangle(x,y,size_block,size_block,DARKGRAY);
DrawRectangleLines(x,y,size_block,size_block,BLACK);
}
else if(!b){
DrawRectangle(x,y,size_block,size_block,GRAY);
DrawRectangleLines(x,y,size_block,size_block,BLACK);
}
//else
}
void open_block(int x_x,int y_y){
time_open++;
if(game_map_boom[x_x][y_y]==1){
gameover=1;
}
else{
//cout<<x_x<<" "<<y_y<<endl;
//game_map_open[x_x][y_y]=1;
if(game_map[x_x][y_y]==0){
for(int i=-1;i<2;i++){
for(int j=-1;j<2;j++){
if(game_map_boom[x_x+i][y_y+j]==0&&game_map_open[x_x+i][y_y+j]==0){
if(game_map[x_x+i][y_y+j]==0&&x_x+i>=0&&y_y+j>=0&&x_x+i<level*4&&y_y+j<level*4){
game_map_open[x_x+i][y_y+j]=1;
open_block(x_x+i,y_y+j);
//cout<<x_x+i<<" "<<y_y+j<<endl;
}
game_map_open[x_x+i][y_y+j]=1;
}
}
}
}
else
game_map_open[x_x][y_y]=1;
}
}
void add_round(int x,int y){
game_map[x+1][y]++;
game_map[x-1][y]++;
game_map[x+1][y+1]++;
game_map[x+1][y-1]++;
game_map[x-1][y+1]++;
game_map[x-1][y-1]++;
game_map[x][y+1]++;
game_map[x][y-1]++;
game_map[x][y]=10;
}
void rand_map_boom(int a){//随机布雷
int rand_x,rand_y;
for(int i=0;i<a;){
rand_x=rand()%(level*4);
rand_y=rand()%(level*4);
if(game_map_boom[rand_x][rand_y]==0){
game_map_boom[rand_x][rand_y]=1;
add_round(rand_x,rand_y);
i++;
//测试用的绘制
//BeginDrawing();
//DrawFPS(10,550);
//ClearBackground(DARKGRAY);
//for(int i=0;i<4*level;i++){
// for(int j=0;j<4*level;j++)
// draw_block(game_map[i][j],true,i*size_block,j*size_block,false,i,j);
//}
//EndDrawing();
}
}
}
void reset_game(){//重新设置游戏
gameover=0,gamewon=0,level=2,open=0,time_open=100;
Vector2 pop_xy;
for(int i=0;i<100;i++){
for(int j=0;j<100;j++)
game_map[i][j]=0,game_map_open[i][j]=0,game_map_boom[i][j]=0;
}
//int game_map[100][100]={},game_map_open[100][100]={},game_map_boom[100][100]={};//colormap[9]={BLACK,RED,BLUE,ORANGE,GREEN,DARKGRAY,DARKGRAY,DARKGRAY,DARKGRAY}
}
void gamewon_(){//游戏胜利
for(int i=0;i<4*level;i++){//绘制含地雷全图
for(int j=0;j<4*level;j++)
draw_block(game_map[i][j],true,i*size_block,j*size_block,true,i,j);
}
DrawText("Game Won", 120-2, 170, 50, BLACK);
DrawText("Game Won", 117-2, 170, 50, GREEN);
DrawText("right click to restart", 118-2, 220, 25, WHITE);
DrawText("right click to restart", 117-2, 220, 25, BLACK);
}
void gameover_(){//游戏失败
for(int i=0;i<4*level;i++){//绘制含地雷全图
for(int j=0;j<4*level;j++)
draw_block(game_map[i][j],true,i*size_block,j*size_block,true,i,j);
}
DrawText("Game Over", 120-2, 170, 50, BLACK);//width-50*(9/2)
DrawText("Game Over", 117-2, 170, 50, RED);
DrawText("right click to restart", 118-2, 220, 25, WHITE);
DrawText("right click to restart", 117-2, 220, 25, BLACK);
}
void test_won(){//判断游戏胜利
open=0;
for(int i=0;i<4*level;i++){//绘制含地雷全图
for(int j=0;j<4*level;j++){
if(game_map_open[i][j])
open++;
}
}
if(open==level*level*16-boom)
gamewon=1;
}
void open_type(int a){//扫雷的经典小黄人头提示
if(a<=5)
DrawRectangle(10,510,30,30,RED);
else if(a<=10)
DrawRectangle(10,510,30,30,ORANGE);
else if(a<=15)
DrawRectangle(10,510,30,30,GREEN);
else if(a>15)
DrawRectangle(10,510,30,30,DARKGREEN);
}
void start_game(){//开始游戏
//使用thread的detach把按键获取分离
//thread move_1(move_);
//move_1.detach();
level=3,boom=10;
rand_map_boom(boom);
while(!WindowShouldClose()){
BeginDrawing();
DrawFPS(10,550);
ClearBackground(DARKGRAY);
test_won();
if(gameover!=1&&gamewon!=1){
for(int i=0;i<4*level;i++){
for(int j=0;j<4*level;j++)
draw_block(game_map[i][j],game_map_open[i][j]==1,i*size_block,j*size_block,false,i,j);
}
pop_xy=GetMousePosition();
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){//开地图
int x=floor(pop_xy.x/(480.0/(level*4))),y=floor(pop_xy.y/(480.0/(level*4)));
time_open=0;
open_block(x,y);
}
open_type(time_open);
}
else if(gameover==1){
time_open=1;
gameover_();
break;
}
else if(gamewon==1){
time_open=100;
gamewon_();
break;
}
EndDrawing();
}
while(1){
BeginDrawing();
if(gameover==1)
gameover_();
else if(gamewon==1)
gamewon_();
EndDrawing();
if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)){
break;
}
}
reset_game();
start_game();
}
int main(){//没啥用的主函数
reset_game();
srand(time(0));
InitWindow(width,height,"raylib扫雷");
SetTargetFPS(500);
start_game();
return 0;
}