C++實現(xiàn)猜牌小游戲
更新時間:2020年07月05日 13:01:22 作者:qq_1161294726
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)猜牌小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
這是大一的時用C寫的猜牌小游戲,里面用了easyx圖形庫,供大家參考,具體內(nèi)容如下
運行效果
代碼
#include<stdio.h> #include<time.h> #include<stdlib.h> #include<Windows.h> #include<graphics.h> #include<mmsystem.h> #pragma comment(lib,"winmm.lib") #define HANG 4 #define LIE 4 #define SIZE 200 int g_matt[HANG][LIE] = { 0 }; int g_matt_time[HANG][LIE] = { 0 }; int g_matt_mask[HANG*LIE] = { 0 }; IMAGE img[10]; void Put(){ char jpg[50]; for (int i = 0; i <10; i++){ sprintf(jpg, "images\\%d.jpg", i); loadimage(&img[i], jpg, SIZE, SIZE); //printf("%s\n", jpg); } } int getNextNumber() { char stat[8] = { 0 }; for (int i = 0; i < HANG; i++){ for (int j = 0; j < LIE; j++){ if (g_matt[i][j]>0 && g_matt[i][j]<9)//讓數(shù)組里面的數(shù)不能重復(fù)出現(xiàn)3次 stat[g_matt[i][j]-1]++;// //printf("%c", stat); } } char n = 0; while (1) { n = rand() % 8 + 1;//隨機1到8 if (stat[n-1] >= 2) {//判斷這個數(shù)組 里面不能出現(xiàn)2個以上重復(fù)的1到8 continue; } else { break; } } return n; } void Playone(){ for (int i = 0; i < HANG; i++){ for (int j = 0; j < LIE; j++){ g_matt[i][j] = getNextNumber(); printf("%d ", g_matt[i][j]); } printf("\n"); } for (int i = 0; i < HANG; i++){ for (int j = 0; j < LIE; j++){ if (g_matt[i][j] >= 1 && g_matt[i][j] <= 8) g_matt[i][j] += 20; printf("%d ", g_matt[i][j]); } printf("\n"); } } void Plus(){ for (int i = 0; i < HANG; i++){ for (int j = 0; j < LIE; j++){ if (g_matt[i][j] == 1){ putimage(i*SIZE, j*SIZE, &img[1]); } else if (g_matt[i][j] == 2){ putimage(i*SIZE, j*SIZE, &img[2]); } else if (g_matt[i][j] == 3){ putimage(i*SIZE, j*SIZE, &img[3]); } else if (g_matt[i][j] == 4){ putimage(i*SIZE, j*SIZE, &img[4]); } else if (g_matt[i][j] == 5){ putimage(i*SIZE, j*SIZE, &img[5]); } else if (g_matt[i][j] == 6){ putimage(i*SIZE, j*SIZE, &img[6]); } else if (g_matt[i][j] == 7){ putimage(i*SIZE, j*SIZE, &img[7]); } else if (g_matt[i][j] == 8){ putimage(i*SIZE, j*SIZE, &img[8]); } else if (g_matt[i][j] >= 20 && g_matt[i][j] <= 28){ putimage(i*SIZE, j*SIZE, &img[0]); } else if (g_matt[i][j] > 100){ putimage(i*SIZE, j*SIZE, &img[9]); } } } } void Play(){ MOUSEMSG msg = { 0 }; msg = GetMouseMsg(); //if (WM_LBUTTONDOWN == matt[msg.x][msg.y] >= 20 || matt[msg.x][msg.y]<=28){ switch (msg.uMsg) { case WM_LBUTTONDOWN: { int row = msg.y / SIZE; int col = msg.x / SIZE; if (g_matt[col][row] >= 20 && g_matt[col][row] <= 28){ g_matt[col][row] -= 20; //printf("%d ", g_matt[col][row]); } //mciSendString("open images\\click.wav", NULL, NULL, NULL); mciSendString("play images\\click.wav", NULL, NULL, NULL); Sleep(10); mciSendString("cloes images\\click.wav", NULL, NULL, NULL); } } //cleardevice(); } void Over(){ int *p = (int*)g_matt+1; for (int i = 0; i < HANG*LIE; i++) { for (int j = i + 1; j < HANG*LIE; j++) { if(p[i] <= 8 && p[i] >= 1 && p[i] == p[j]){ p[i] += 120; p[j] += 120; //mciSendString("open images\\search.wav", NULL, NULL, NULL); mciSendString("play images\\search.wav", NULL, NULL, NULL); Sleep(10); mciSendString("cloes images\\search.wav", NULL, NULL, NULL); } } } } void Keep(){ for (int i = 0; i < HANG; i++){ for (int j = 0; j < LIE; j++){ if (g_matt[i][j] >= 1 && g_matt[i][j] <= 8){ g_matt[i][j] += 20; //printf("%d ", g_matt[i][j]); } } printf("\n"); } } int main() { //int *p1; mciSendString("open images\\東京不太熱.mp3", NULL, NULL, NULL); mciSendString("play images\\東京不太熱.mp3", NULL, NULL, NULL); srand(time(NULL)); HWND hwnd =initgraph(800, 800); Put(); Playone(); int frames = 0; DWORD t1, t2,t3; t1 = GetTickCount();//程序運行時間 while (1) { t2 = GetTickCount(); Plus();//貼圖 Play();//鼠標(biāo)點擊 if (t2-t1>=1600){//等于當(dāng)前時間減去程序運行時間大于等于兩秒才運行 也就是卡兩秒 Keep();//加密 //continue; t1 = t2;//當(dāng)前時間賦給程序運行時間 } Over(); char ch[16] = { 0 }; char n = 0; for (int i = 0; i < HANG; i++){ for (int j = 0; j < LIE; j++){ if (g_matt[i][j] >= 120){ ch[g_matt[i][j] - 1]++; } } } while (n>6) { if (ch[n - 1] >= 6) {//判斷這個數(shù)組 里面不能出現(xiàn)2個以上重復(fù)的1到8 MessageBox(hwnd, "恭喜你贏了", "", MB_OK); //continue; n++; } } } return 0; }
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
- 這篇文章主要介紹了C++強制轉(zhuǎn)換與智能指針示例,智能指針(Smart Pointer)是一種抽象的數(shù)據(jù)類型。在程序設(shè)計中,它通常是經(jīng)由類模板來實現(xiàn),借由模板來達(dá)成泛型,借由類別的析構(gòu)函數(shù)來達(dá)成自動釋放指針?biāo)赶虻拇鎯ζ骰驅(qū)ο?/div> 2022-11-11
解析如何在C語言中調(diào)用shell命令的實現(xiàn)方法
本篇文章是對如何在C語言中調(diào)用shell命令的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C++實現(xiàn)LeetCode(107.二叉樹層序遍歷之二)
這篇文章主要介紹了C++實現(xiàn)LeetCode(107.二叉樹層序遍歷之二),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07詳解C++中二進(jìn)制求補運算符與下標(biāo)運算符的用法
這篇文章主要介紹了C++中二進(jìn)制求補運算符與下標(biāo)運算符的用法,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-01-01最新評論