欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言對對碰游戲源碼分享

 更新時間:2018年02月26日 11:13:00   投稿:lijiao  
這篇文章主要為大家分享了C語言對對碰游戲源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C語言對對碰游戲的具體代碼,供大家參考,具體內(nèi)容如下

////////////////////////////////////////
//畫素材的x和y都是反的,因為x表示行,但是畫出來x表示列,y同
////////////////////////////////////////

#include <graphics.h>
#include <fstream>
#include <strstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#pragma comment(lib, "Winmm.lib")
using namespace std;


/***************定義枚舉類型*****************/
enum color{blank, red, yellow, blue, green, white, orange, purple, shizijia, zhadan, qicai, alarm};


/***************定義全局變量***************/
const int MaxT = 12; // 時間上限
const int T = 10 * MaxT;// 時間速度
const int V = 300; // 停留時間
clock_t start, now; // 控制時間
color gem[9][8]; // 地圖
  // IMAGE對象
IMAGE img[12], music_img[2], exit_img, jindutiao;
int Score, Time; // 成績 時間
bool Flag, Music = true;// 是否加載 音樂


/*******************函數(shù)聲明******************/
void load(void); // 加載IMAGE對象
void Blank(void); // 清空
bool soso(void); // 搜索空格
bool baidu(void); // 搜索解法
void New(void); // 更新
void print(void); // 輸出
bool judge(void); // 判斷是否可以消除
void fall(void); // 下落
void draw(void); // 消除
bool quit(void); // 是否重新開始
void play(void); // 游戲過程


/********************定義函數(shù)****************/
void load(void)
{
 char c[20];
 int i;

 // 加載IMAGE對象
 for (i = 0; i < 12; i++)
 {
 ostrstream strout(c, 50);
 strout <<"圖片\\" <<i <<".jpg" <<ends;
 loadimage(&img[i], c);
 }
 loadimage(&music_img[0], "圖片\\音樂關(guān).jpg");
 loadimage(&music_img[1], "圖片\\音樂開.jpg");
 loadimage(&exit_img, "圖片\\退出.jpg");
 loadimage(&jindutiao, "圖片\\進度條.jpg");

 // 加載音樂
 mciSendString("open 背景音樂.mp3 alias mymusic", NULL, 0, NULL);

 // 隨機種子
 srand(unsigned(time(NULL)));

 // 打開文件
 ifstream fin("存檔.dat");
 if (!fin)
 throw -1; // 如果打開失敗則拋出異常

 // 讀存檔
 fin >>Flag >>Music;
 if (Flag)
 {
 HWND wnd = GetHWnd();
 SetWindowText(wnd, "對對碰");
 if (MessageBox(wnd, "是否繼續(xù)上次游戲?", "游戲開始", MB_YESNO | MB_ICONQUESTION) == IDYES)
 {
 fin >>Score >>Time;
 for (i = 0; i < 9; i++)
 for (int j = 0; j < 8; j++)
 {
  int t;
  fin >>t;
  gem[i][j] = color(t);
 }
 }
 else
 Flag = false;
 }
 fin.close();
}

void Blank(void)
{
 for (int i = 1; i < 9; i++)
 for (int j = 0; j < 8; j++)
 gem[i][j] = blank;
 print();
 fall();
}

bool soso(void)
{
 for (int i = 1; i < 9; i++)
 for (int j = 0; j < 8; j++)
 if (gem[i][j] == blank)
 return true;
 return false;
}

bool baidu(void)
{
 int i, j;
 color t;
 bool flag = false;

 // 如果有一個道具則返回真
 for (i = 1; i < 9; i++)
 for (j = 0; j < 8; j++)
 if (gem[i][j] >= shizijia)
 return true;

 // 搜索解法
 for (i = 1; i < 9; i++)
 for (j = 0; j < 7; j++)
 {
 t = gem[i][j];
 gem[i][j] = gem[i][j + 1];
 gem[i][j + 1] = t;
 if (judge())
 flag = true;
 t = gem[i][j];
 gem[i][j] = gem[i][j + 1];
 gem[i][j + 1] = t;
 if (flag)
 return true;
 }
 for (i = 1; i < 7; i++)
 for (j = 0; j < 8; j++)
 {
 t = gem[i][j];
 gem[i][j] = gem[i + 1][j];
 gem[i + 1][j] = t;
 if (judge())
 flag = true;
 t = gem[i][j];
 gem[i][j] = gem[i + 1][j];
 gem[i + 1][j] = t;
 if (flag)
 return true;
 }
 return false;
}

void New(void)
{
 static int old_score = -1, old_time = T;
 bool flag = false;
 if (Score != old_score) // 更新分數(shù)
 {
 if (Score > 999999999)
 {
 if (MessageBox(GetHWnd(), "恭喜你達到了最高分!\n是否重新開始?", "游戲結(jié)束", MB_YESNO | MB_ICONQUESTION) == IDYES)
 Score = 0;
 else
 exit(0);
 }
 char s[15];
 ostrstream strout(s, 15);
 strout <<'$' <<setiosflags(ios::right) <<setw(9) <<Score <<ends;
 setfont(30, 0, "微軟雅黑");
 setcolor(RGB(255, 128, 0));
 outtextxy(480, 60, s);
 old_score = Score;
 flag = true;
 }
 if (Time != old_time) // 更新時間
 {
 if (Time > old_time)
 putimage(540, 140, &jindutiao);
 setcolor(BLACK);
 for (int i = 0; i < T - Time; i++)
 {
 line(540, 140 + 2 * i, 580, 140 + 2 * i);
 line(540, 140 + 2 * i + 1, 580, 140 + 2 * i + 1);
 }
 old_time = Time;
 flag = true;
 }
 if (flag) // 寫存檔
 {
 ofstream fout("存檔.dat");
 fout <<Flag <<'\t'
 <<Music <<'\t'
 <<Score <<'\t'
 <<Time <<endl;
 for (int i = 0; i < 9; i++)
 {
 for (int j = 0; j < 8; j++)
 fout <<int(gem[i][j]) <<'\t';
 fout <<endl;
 }
 fout.close();
 }
}

void print(void)
{
 int i, j;
 for (i = 1; i < 9; i++)
 for (j = 0; j < 8; j++)
 putimage(60 * j, 60 * (i - 1), &img[int(gem[i][j])]);
 New();
}

bool judge(void)
{
 int i, j;
 for (i = 1; i < 9; i++)
 for (j = 0; j < 6; j++)
 if (gem[i][j] == gem[i][j + 1] && gem[i][j] == gem[i][j + 2])
 return true;
 for (i = 1; i < 7; i++)
 for (j = 0; j < 8; j++)
 if (gem[i][j] == gem[i + 1][j] && gem[i][j] == gem[i + 2][j])
 return true;
 return false;
}

void fall(void)
{
 {
 int i, j, a[8] = {0};
 bool sign = false;
 for (j = 0; j < 8; j++)
 for (i = 8; i >= 1; i--)
 if (gem[i][j] == blank)
 {
  a[j] = i;
  sign = true;
  break;
 }
 if (sign == false)
 return;
 IMAGE im[8];
 for (j = 0; j < 8; j++)
 if (a[j] > 1)
 getimage(&im[j], 60 * j, 0, 60, 60 * (a[j] - 1));
 for (i = 0; i < 60 ; i += 2)
 for (j = 0; j < 8; j++)
 if (a[j] > 0)
 {
  if (a[j] > 1)
  putimage(60 * j, i + 2, &im[j]);
  putimage(60 * j, i + 2 - 60, &img[int(gem[0][j])]);
  Sleep(1);
 }
 for (j = 0; j < 8; j++)
 if (a[j] > 0)
 {
 for (i = a[j]; i > 0; i--)
  gem[i][j] = gem[i - 1][j];
 if (rand() % 50 == 0)
  gem[0][j] = color(rand() % 4 + 8);
 else
  gem[0][j] = color(rand() % 7 + 1);
 }
 } // 加對大括號使遞歸時撤銷內(nèi)存空間
 if (soso())
 fall();
 if (judge())
 draw();
}

void draw(void)
{
 {
 int i, j;
 bool a[9][8] = {false};
 for (i = 1; i < 9; i++)
 for (j = 0; j < 6; j++)
 if (gem[i][j] == gem[i][j + 1] && gem[i][j] == gem[i][j + 2])
  a[i][j] = a[i][j + 1] = a[i][j + 2] = true;
 for (i = 1; i < 7; i++)
 for (j = 0; j < 8; j++)
 if (gem[i][j] == gem[i + 1][j] && gem[i][j] == gem[i + 2][j])
  a[i][j] = a[i + 1][j] = a[i + 2][j] = true;
 for (i = 1; i < 9; i++)
 for (j = 0; j < 8; j++)
 if (a[i][j])
 {
  gem[i][j] = blank;
  Score += 10;
 }
 Sleep(V);
 Time += T / MaxT;
 if (Time > T)
 Time = T;
 print();
 } // 加對大括號使遞歸時撤銷內(nèi)存空間
 fall();
}

bool quit(void)
{
 char str[50];
 ostrstream strout(str, 50);
 strout <<"得分:" <<Score <<"\n重新開始嗎?" <<ends;
 if (MessageBox(GetHWnd(), str, "游戲結(jié)束", MB_YESNO | MB_ICONQUESTION) == IDYES)
 return true;
 return false;
}

void play(void)
{
 MOUSEMSG m;
 int i, j, x, y, x1, y1;
 char fx;
 color t;
 bool sign;
 if (Flag == false)
 {
 do
 {
 for (i = 0; i < 9; i++)
 for (j = 0; j < 8; j++)
  gem[i][j] = color(rand() % 7 + 1);
 }while (judge() || !baidu());
 Score = 0;
 Time = T;
 }
 setbkcolor(BLACK);
 cleardevice();
 setfont(30, 0, "微軟雅黑");
 setcolor(RGB(255, 128, 0));
 outtextxy(480, 60, "$  0");
 putimage(480, 400, &music_img[int(Music)]);
 putimage(560, 400, &exit_img);
 putimage(540, 140, &jindutiao);
 print();
 now = start = clock();
 Flag = true;
 do
 {
 if (soso())
 fall();
 fx = 0;
 sign = true;
 while (true)
 {
 if (MouseHit())
 {
 m = GetMouseMsg();
 x1 = m.x;
 y1 = m.y;
 if (m.uMsg == WM_LBUTTONDOWN)
 {
  if (x1 < 480)
  {
  x = y1 / 60 + 1;
  y = x1 / 60;
  switch (gem[x][y])
  {
  case shizijia:
  for (i = 1; i < 9; i++)
  {
  gem[i][y] = blank;
  Score += 10;
  }
  for (j = 0; j < 8; j++)
  {
  gem[x][j] = blank;
  Score += 10;
  }
  Score -= 20;
  Time += T / MaxT;
  if (Time > T)
  Time = T;
  print();
  Sleep(V);
  fall();
  if (!baidu())
  Blank();
  break;

  case zhadan:
  if (x > 1)
  {
  gem[x - 1][y] = blank;
  Score += 10;
  if (y > 0)
  {
   gem[x - 1][y - 1] =blank;
   Score += 10;
  }
  }
  if (y > 0)
  {
  gem[x][y - 1] = blank;
  if (x < 8)
  {
   gem[x + 1][y - 1] =blank;
   Score += 10;
  }
  }
  if (x < 8)
  {
  gem[x + 1][y] = blank;
  if (y < 7)
  {
   gem[x + 1][y + 1] =blank;
   Score += 10;
  }
  }
  if (y < 7)
  {
  gem[x][y + 1] = blank;
  if (x > 1)
  {
   gem[x - 1][y + 1] =blank;
   Score += 10;
  }  
  }
  gem[x][y] = blank;
  Time += T / MaxT;
  if (Time > T)
  Time = T;
  print();
  Sleep(V);
  fall();
  if (!baidu())
  Blank();
  break;

  case qicai:
  t = color(rand() % 7 + 1);
  putimage(60 * y, 60 * (x - 1), &img[int(t)]);
  Sleep(V);
  for (i = 1; i < 9; i++)
  for (j = 0; j < 8; j++)
   if (gem[i][j] == t)
   {
   gem[i][j] = blank;
   putimage(60 * j, 60 * (i - 1), &img[0]);
   Score += 10;
   }
  gem[x][y] = blank;
  Time += T / MaxT;
  if (Time > T)
  Time = T;
  print();
  fall();
  if (!baidu())
  Blank();
  break;

  case alarm:
  Time = T;
  gem[x][y] = blank;
  print();
  fall();
  if (!baidu())
  Blank();
  break;
  }
  break;
  }
  else if (x1 > 480 && x1 < 560 && y1 > 400 && y1 < 480)
  {
  if (Music)
  mciSendString("stop mymusic", NULL, 0, NULL);
  else
  mciSendString("play mymusic from 0 repeat", NULL, 0, NULL);
  Music = !Music;
  putimage(480, 400, &music_img[int(Music)]);
  }
  else if (x1 > 560 && x1 < 640 && y1 > 400 && y1 < 480)
  exit(0);
 }
 }
 now = clock();
 if (now - start >= CLOCKS_PER_SEC * MaxT / T)
 {
 start = now;
 Time--;
 New();
 if (Time <= 0)
  return;
 }
 }
 while (m.mkLButton && m.y < 480)
 {
 m = GetMouseMsg();
 x = m.x;
 y = m.y;
 if (x - x1 > 30 || x1 - x > 30 || y - y1 > 30 || y1 - y > 30)
 {
 sign = false;
 break;
 }
 now = clock();
 if (now - start >= CLOCKS_PER_SEC * MaxT / T)
 {
 start = now;
 Time--;
 New();
 if (Time <= 0)
  return;
 }
 }
 if(sign)
 {
 putimage(x1 - x1 % 60, y1 - y1 % 60, &img[int(gem[y1 / 60 + 1][x1 / 60])], SRCPAINT);
 continue;
 }
 if (y1 - y > 30 && y1 / 60 > 0)
 fx = 'u';
 if (y - y1 > 30 && y1 / 60 < 7)
 fx = 'd';
 if (x1 - x > 30 && x1 / 60 > 0)
 fx = 'l';
 if (x - x1 > 30 && x1 / 60 < 7)
 fx = 'r';
 x = y1 / 60 + 1;
 y = x1 / 60;
 x1 = y1 = 0;
 switch (fx)
 {
 case 'u':x1 = -1; break;
 case 'd':x1 = 1; break;
 case 'l':y1 = -1; break;
 case 'r':y1 = 1; break;
 case '\0':continue;
 }
 for (i = 0; i < 60;)
 {
 putimage(60 * y, 60 * (x - 1), &img[0]);
 putimage(60 * y, 60 * (x - 1), &img[0]);
 i += 1;
 putimage(60 * (y + y1) - i * y1, 60 * (x - 1 + x1) - i * x1, &img[int(gem[x + x1][y + y1])]);
 putimage(60 * y + i * y1, 60 * (x - 1) + i * x1, &img[int(gem[x][y])]);
 Sleep(2);
 }
 t = gem[x][y];
 gem[x][y] = gem[x + x1][y + y1];
 gem[x + x1][y + y1] = t;
 if (judge())
 {
 draw();
 if (!baidu())
 Blank();
 }
 else
 {
 for (i = 0; i < 60;)
 {
 putimage(60 * y, 60 * (x - 1), &img[0]);
 putimage(60 * y, 60 * (x - 1), &img[0]);
 i += 1;
 putimage(60 * (y + y1) - i * y1, 60 * (x - 1 + x1) - i * x1, &img[int(gem[x + x1][y + y1])]);
 putimage(60 * y + i * y1, 60 * (x - 1) + i * x1, &img[int(gem[x][y])]);
 Sleep(2);
 }
 t = gem[x][y];
 gem[x][y] = gem[x + x1][y + y1];
 gem[x + x1][y + y1] = t;
 }
 now = clock();
 if (now - start >= CLOCKS_PER_SEC * MaxT / T)
 {
 start = now;
 Time--;
 New();
 if (Time <= 0)
 return;
 }
 }while (true);
}


/****************主函數(shù)******************/
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
 try
 {
 // 加載素材
 load();
 }
 catch (int)
 {
 ofstream fout("存檔.dat");
 fout <<false <<'\t' <<true <<endl;
 }

 // 打開界面
 initgraph(640, 480);

 // 游戲過程
 do
 {
 if (Music)
 mciSendString("play mymusic from 0 repeat", NULL, 0, NULL);
 play();
 if (Music)
 mciSendString("stop mymusic", NULL, 0, NULL);
 Flag = false;
 }while (quit());

 // 關(guān)閉游戲
 ofstream fout("存檔.dat");
 fout <<false <<'\t' <<Music <<endl;
 fout.close();
 mciSendString("close mymusic", NULL, 0, NULL);
 closegraph();
 return 0;
}


/**************THE END****************/

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++讀取文本文件中的漢字亂碼情況原因及解決

    C++讀取文本文件中的漢字亂碼情況原因及解決

    本文介紹簡體中文Windows操作系統(tǒng)中,C++讀取文本文件中的漢字亂碼情況原因及解決,文中通過代碼和圖文給大家介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下
    2024-01-01
  • C++利用ImGUI繪制D3D外部菜單

    C++利用ImGUI繪制D3D外部菜單

    ImGUI 它是與平臺無關(guān)的C++輕量級跨平臺圖形界面庫,沒有任何第三方依賴,可以將ImGUI的源碼直接加到項目中使用。本文將利用ImGUI繪制D3D外部菜單,需要的可以參考一下
    2022-09-09
  • C語言求冪計算的高效解法

    C語言求冪計算的高效解法

    這篇文章主要介紹了C語言求冪計算的高效解法,分別演示了求冪運算與整數(shù)次方的解法,具有不錯的參考借鑒價值,需要的朋友可以參考下
    2014-09-09
  • C語言實現(xiàn)輸入ascii碼,輸出對應(yīng)的字符方式

    C語言實現(xiàn)輸入ascii碼,輸出對應(yīng)的字符方式

    這篇文章主要介紹了C語言實現(xiàn)輸入ascii碼,輸出對應(yīng)的字符方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 哈夫曼的c語言實現(xiàn)代碼

    哈夫曼的c語言實現(xiàn)代碼

    著先通過 HuffmanTree() 函數(shù)構(gòu)造哈夫曼樹,然后在主函數(shù) main()中自底向上開始(也就是從數(shù)組序號為零的結(jié)點開始)向上層層判斷,若在父結(jié)點左側(cè),則置碼為 0,若在右側(cè),則置碼為 1。最后輸出生成的編碼
    2013-07-07
  • 淺談C++中的string 類型占幾個字節(jié)

    淺談C++中的string 類型占幾個字節(jié)

    本篇文章小編并不是為大家講解string類型的用法,而是講解我個人比較好奇的問題,就是string 類型占幾個字節(jié)
    2013-08-08
  • C++中vector容器的注意事項總結(jié)

    C++中vector容器的注意事項總結(jié)

    在c++中,vector是一個十分有用的容器,下面這篇文章主要給大家介紹了關(guān)于C++中vector容器的注意事項,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-12-12
  • c++判斷是否為目錄的示例分享

    c++判斷是否為目錄的示例分享

    這篇文章主要介紹了c++判斷是否為目錄的示例,需要的朋友可以參考下
    2014-03-03
  • Ubuntu20.04安裝使用jsoncpp、json-c庫的方法實例

    Ubuntu20.04安裝使用jsoncpp、json-c庫的方法實例

    這篇文章主要給大家介紹了關(guān)于Ubuntu20.04安裝使用jsoncpp、json-c庫的相關(guān)資料,文中通過代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作就有一定的參考借鑒價值,需要的朋友可以參考下
    2024-04-04
  • Visual Studio 2019配置qt開發(fā)環(huán)境的搭建過程

    Visual Studio 2019配置qt開發(fā)環(huán)境的搭建過程

    這篇文章主要介紹了Visual Studio 2019配置qt開發(fā)環(huán)境的搭建過程,本文圖文并茂給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03

最新評論