c++图形库编写点灯小游戏

文章目录

  • 前言
  • 使用步骤
    • 1.引入库
  • 总结


前言

这是用c++以及图形库来编写的点灯小游戏


工具:图形库和VS2019
图形库:easyx
图形库下载

使用步骤

1.引入库

图形库

#include

总结

代码如下:

#include
#include
#include
#include
#pragma comment(lib,"winmm.lib")
#define WIN_WIDTH 640
#define WIN_HEIGHT 480
#define GRID_NUM 4  //每一行每一列的格子数
#define GRID_WIDTH 30//格子宽度
struct Grid //格子
{
     
  int top; //上面一条线的x坐标
  int down;//下面一条线的x坐标
  int left;//左边一条线的y坐标
  int right;//右边边一条线的y坐标
  int foot; //步数
  int map[GRID_NUM][GRID_NUM];
  POINT mouse; //鼠标点击的格子的下标
}grid;
MOUSEMSG msg;
IMAGE img;
//开始界面
void Welcome()
{
     
//播放音乐
  mciSendString("open ./slhxq.mp3 alias a", 0, 0, 0);
  mciSendString("play a repeat", 0, 0, 0);
  //显示背景图片
  loadimage(&img, "./bk.jpg",640,480);
  putimage(0, 0, &img);
  setbkmode(TRANSPARENT);
  settextcolor(GREEN);
  settextstyle(64, 0, "楷体");
  outtextxy(70, 50, "涂格子小游戏");
  settextcolor(RED);
  settextstyle(16, 0, "宋体");
  outtextxy(100, 200, "每点一个格子,上下左右的格子也会做出于现状相反的动作");
  outtextxy(100, 240, "总共11关,左键填色,右键重来,中键退出");
  outtextxy(400, 320, "作者:孟瀚宇");
  settextstyle(16, 0, "楷体");
  outtextxy(400, 340, "^V^");
  
  while (!_kbhit())
  {
     
    settextcolor(RGB(0, rand()%256, 0));
    outtextxy(280, 400, "按任意键继续!");
    Sleep(200);
  }
  _getch();//按任意键继续
}
void GameInit()
{
     
  // 游戏区域大小,屏幕中心位置x,y,减去总格子宽度的一半
  grid.left = WIN_WIDTH/2 - GRID_WIDTH * GRID_NUM / 2;
  grid.right = WIN_WIDTH /2 + GRID_WIDTH * GRID_NUM / 2;
  grid.top = WIN_HEIGHT / 2 - GRID_WIDTH * GRID_NUM / 2;
  grid.down = WIN_HEIGHT / 2 + GRID_WIDTH * GRID_NUM / 2;
  grid.foot = 0;
  for (int i = 0; i < GRID_NUM; i++)
  {
     
    for (int k = 0; k < GRID_NUM; k++)
    {
     
      grid.map[i][k] = 1;
    }
  }
}
void GameDraw()
{
     
  cleardevice();
  putimage(0, 0, &img);
  // 绘制格子
  setlinecolor(RGB(0, 200, 0));
  //循环画格子
  for (int x = grid.left; x <= grid.right; x += GRID_WIDTH)
  {
     
    line(x, grid.top, x, grid.down);
  }
  for (int y = grid.top; y <= grid.down; y += GRID_WIDTH)
  {
     
    line(grid.left, y, grid.right, y);
  }
  // 外边框
  for (int x = 20; x > 10; x--)
  {
     
    line(grid.left - x, grid.top - x, grid.right + x, grid.top - x); //画上面的线
    line(grid.left - x, grid.down + x, grid.right + x, grid.down + x);//画下面的线
    line(grid.left - x, grid.top - x, grid.left - x, grid.down + x);//画左面的线
    line(grid.right + x, grid.top - x, grid.right + x, grid.down + x);//画右面的线
  }
  //绘制格子颜色
  int x, y;
  for (int i = 0; i < GRID_NUM; i++)
  {
     
    for (int k = 0; k < GRID_NUM; k++)
    {
     
      x = i * GRID_WIDTH + grid.left;
      y = k * GRID_WIDTH + grid.top;
      if (grid.map[i][k] == 1)
      {
     
        setfillcolor(BLACK);
        solidrectangle(x + 1, y + 1, x + GRID_WIDTH - 1, y + GRID_WIDTH - 1);//加1减一是为了让每个格子的边框显示出来
      }
      else if (grid.map[i][k] == -1)
      {
     
        setfillcolor(RGB(0, 150, 0));
        solidrectangle(x + 1, y + 1, x + GRID_WIDTH - 1, y + GRID_WIDTH - 1);//加1减一是为了让每个格子的边框显示出来
      }
    }
  }
  char foot[20] = "";
  sprintf(foot, "步数:%d", grid.foot);
  settextcolor(WHITE);
  outtextxy(10, 10, foot);
}
void GameControl()
{
     
  if (MouseHit())
  {
     
    msg = GetMouseMsg();
    if (msg.uMsg == WM_LBUTTONDOWN)
    {
       //鼠标点击位置在游戏区域时
      if (msg.x > grid.left && msg.x<grid.right && msg.y>grid.top && msg.y < grid.down)
      {
           
        grid.mouse.x = (msg.x - grid.left) / GRID_WIDTH;
        grid.mouse.y = (msg.y - grid.top) / GRID_WIDTH;
        grid.map[grid.mouse.x][grid.mouse.y] = -grid.map[grid.mouse.x][grid.mouse.y];//点击的变色
        if (grid.mouse.x >= 0 && grid.mouse.x < GRID_NUM - 1)grid.map[grid.mouse.x+1][grid.mouse.y] = -grid.map[grid.mouse.x+1][grid.mouse.y];//右面的变色
        if (grid.mouse.x > 0 && grid.mouse.x <= GRID_NUM - 1)grid.map[grid.mouse.x-1][grid.mouse.y] = -grid.map[grid.mouse.x-1][grid.mouse.y];//左面变色
        if (grid.mouse.y >= 0 && grid.mouse.y < GRID_NUM - 1)grid.map[grid.mouse.x][grid.mouse.y+1] = -grid.map[grid.mouse.x][grid.mouse.y+1];//下边变色
        if (grid.mouse.y > 0 && grid.mouse.y <= GRID_NUM - 1)grid.map[grid.mouse.x][grid.mouse.y-1] = -grid.map[grid.mouse.x][grid.mouse.y-1];//上边变色
        grid.foot++;
      }
    }
  }

}
int GameJudge()
{
     
  for (int i = 0; i < GRID_NUM; i++)
  {
     
    for (int k = 0; k < GRID_NUM; k++)
    {
     
      if (grid.map[i][k] == 1)
      {
     
        return 0;
      }
    }
  }
  return 1;
}
int main()
{
     
  initgraph(640, 480);
  Welcome();
  GameInit();
  BeginBatchDraw();
  while (1)
  {
           
    
    GameControl();
    GameDraw();
    FlushBatchDraw();
    if (GameJudge())
    {
     
      HWND hwd = GetHWnd();
      MessageBox(hwd, "你赢了~", "提示:", MB_OK);
      exit(666);
    }
  }
  return 0;
}

你可能感兴趣的:(搜狗,游戏开发,bmp,wxpython,游戏)