C语言自制测色弱找方块游戏的示例代码

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C语言自制测色弱找方块游戏的示例代码

编程小鱼六六六   2022-12-01 我要评论

程序介绍

该程序是仿照最近网上流行的找方块游戏编写的,虽然没有仿照的一模一样,但是也实现了他的一些功能。个人觉得可玩性还是挺高的,是一个不错的娱乐放松的游戏。

编写简介

该游戏的编写还是挺容易的,可以大致分为三步。第一步,生成一个界面。第二步,使用鼠标获取不同颜色的方块。第三步,刷新屏幕。需要注意的地方就是其中的细节与动态效果,都是些高中物理知识。

效果图

找方块运行效果:

源码分享

///
// 程序名称:找方块
// 编译环境:Mictosoft Visual Studio 2013, EasyX_20200315(beta)
//更多好玩源码加Q群:724050348
//
 
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<time.h>
 
struct POS                // 一个点的结构体
{
  int x;
  int y;
  COLORREF color;
};
 
void Interface(int Level, int Mark);  // 绘制界面 Level 为关卡数,Mark 为分数
POS InitColor(int Grade);        // 初始化颜色和方块的位置 Grade 难度等级 返回坐标
int GetNum(int Mark);          // 判断一个数字是几位数
void GameOver();            // 游戏结束
void AddMark(POS pos, int addMark);    // 加分显示以及方块与分数的平移
void ShowIips();            // 显示提示
 
int main()
{
  initgraph(800, 600);
  int Level = 1;
  int Mark = 0;
  while (true)
  {
    int BeginTime = clock();
    int GameState = 0;  // 游戏状态
 
    Interface(Level, Mark);
    POS pos = InitColor(Level / 10);
    MOUSEMSG m;  
    while (true)
    {
      m = GetMouseMsg();
      switch (m.uMsg)
      {
      case WM_LBUTTONDOWN:
        if (pos.x - 35 < m.x && pos.x + 35 > m.x && pos.y - 35 < m.y && pos.y + 35 > m.y)
        {
          GameState = 1;
        }
        else
        {
          setlinecolor(WHITE);
          setlinestyle(PS_SOLID | PS_ENDCAP_SQUARE, 4);
          line(m.x - 10, m.y - 10, m.x + 10, m.y + 10);
          line(m.x + 10, m.y - 10, m.x - 10, m.y + 10);
          if (GameState == 2)
          {
            GameState = 3;
          }
          else
          {
            GameState = 2;
          }
        }
        break;
      }
      if (GameState == 1)
      {
        int EndTime = clock();
        int Times = EndTime - BeginTime;
        int addMark = (int)(80000.0 / Times);
        AddMark(pos, addMark);
        Mark += addMark;
        Level++;
        break;
      }
      if (GameState == 2)
      {
        continue;
      }
      if (GameState == 3)
      {
        GameOver();
        Level = 1;
        Mark = 0;
        break;
      }
    }
  }
  _getch();
  return 0;
}
 
void Interface(int Level, int Mark)
{
  setfillcolor(BLACK);
  bar(0, 0, 800, 50);
  bar(0, 550, 800, 750);
  settextcolor(WHITE);
  settextstyle(30, 0, L"微软雅黑");
  TCHAR str[25];
  _stprintf_s(str, _T("第 %d 关"), Level);
  outtextxy(50, 10, str);
  _stprintf_s(str, _T("%d"), Mark);
  outtextxy(750 - GetNum(Mark) * 15, 10, str);
}
 
POS InitColor(int Grade)
{
  srand((unsigned int)time(NULL));
  float H = (float)(rand() % 360);      // 色相
  float S = (rand() % 100) / 100.f;      // 饱和度
  float L = (rand() % 70 + 30) / 100.f;    // 亮度
  double h = 0;                // 高度
  double v = 0;                // 速度(方向向下)
  double dv = 9.8 / 50;            // 加速度(每 1/50 秒)
  BeginBatchDraw();
  int x1, y1, x2, y2;
  x1 = 800;
  y1 = 50;
  x2 = 1600;
  y2 = 550;
  while (true)
  {
    v += dv;                // 根据加速度计算速度
    h -= (v - dv / 2);            // 计算高度
    x1 = x1 + (int)h;
    x2 = x2 + (int)h;
    setfillcolor(HSVtoRGB(H, S, L));
    bar(x1, y1, x2, y2);
    ShowIips();
    FlushBatchDraw();
    Sleep(20);
    if (x1 <= 0)
    {
      break;
    }
  }
  EndBatchDraw();
 
  POS pos;
  pos.x = rand() % 730 + 35;
  pos.y = rand() % 430 + 85;
  pos.color = HSVtoRGB(H, S, L);
 
  if (Grade == 0)
  {
    setfillcolor(HSVtoRGB(H, S, (L - 0.1f) < 0 ? L = L + 0.1f : L = L - 0.1f));
  }
  if (Grade == 1)
  {
    setfillcolor(HSVtoRGB(H, (S - 0.1f) < 0 ? S = S + 0.1f : S = S - 0.1f, L));
  }
  if (Grade >= 2)
  {
    setfillcolor(HSVtoRGB((H - 20) < 0 ? H = H + 20 : H = H - 20, S, L));
  }
 
  bar(pos.x - 35, pos.y - 35, pos.x + 35, pos.y + 35);
  ShowIips();
  return pos;
}
 
int GetNum(int Mark)
{
  int sum = 0;
  while (Mark)
  {
    sum++;
    Mark /= 10;
  }
  return sum == 0 ? sum = 1 : sum;
}
 
void GameOver()
{
  settextstyle(60, 0, L"微软雅黑");
  RECT r = { 0, 0, 800, 600 };
  settextcolor(WHITE);
  drawtext(_T("游戏结束"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  Sleep(1000);
}
 
void AddMark(POS pos, int addMark)
{
  setfillcolor(WHITE);
  bar(pos.x - 35, pos.y - 35, pos.x + 35, pos.y + 35);
  setbkmode(TRANSPARENT);
  settextstyle(30, 0, L"微软雅黑");
  settextcolor(WHITE);
  TCHAR str[25];
  _stprintf_s(str, _T("+%d"), addMark);
  POS textpos;
  textpos.x = pos.x - 20;
  textpos.y = pos.y - 35 - 30;
  textpos.color = WHITE;
  outtextxy(textpos.x, textpos.y, str);
 
  double k = textpos.y / (textpos.x - 700.0);
  double b = (700 * textpos.y) / (textpos.x - 700.0);
 
  double h = 0;              // 高度
  double v = 0;              // 速度(方向向下)
  double dv = 9.8 / 50;          // 加速度(每 1/50 秒)
  BeginBatchDraw();
  while (true)
  {
    v += dv;              // 根据加速度计算速度
    h -= (v - dv / 2);          // 计算高度
    pos.y = pos.y - (int)h;
    setfillcolor(WHITE);
    settextcolor(WHITE);
    bar(pos.x - 35, pos.y - 35, pos.x + 35, pos.y + 35);
    textpos.x = textpos.x - (int)h;
    textpos.y = (int)(k*textpos.x - b);
    outtextxy(textpos.x, textpos.y, str);
    FlushBatchDraw();
    Sleep(20);
    setfillcolor(pos.color);
    settextcolor(pos.color);
    outtextxy(textpos.x, textpos.y, str);
    bar(pos.x - 35, pos.y - 35, pos.x + 35, pos.y + 35);
    if (pos.y > 600 && textpos.x > 700)
    {
      break;
    }
  }
  EndBatchDraw();
}
 
void ShowIips()
{
  setbkmode(TRANSPARENT);
  settextstyle(30, 0, L"微软雅黑");
  RECT r = { 0, 50, 800, 100 };
  settextcolor(RGB(252, 203, 147));
  drawtext(_T("找出颜色不一样的小方块"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们