如何用C++编写一个小游戏 用C++编写的小游戏源代码

\u5982\u4f55\u7528C++\u7f16\u5199\u4e00\u4e2a\u5c0f\u6e38\u620f\uff1f

\u4f7f\u7528\u8bed\u8a00\uff1aC++\u4f7f\u7528\u5de5\u5177\uff1avs2019

\u4f7f\u7528\u8bed\u8a00\uff1aC++\u4f7f\u7528\u5de5\u5177\uff1avs2019

一个用C++编程的小游戏,可以实现的功能如下:

1、随机生成数字;

2、数字消除合并;

3、判定游戏结束;

一、游戏主体: 

因为用C++写的,所以用了类,棋盘用了一个二维数组,m是棋盘规格,取了4。

class game

{

public:

    int i, j;

    game() {

        count1 = 0;

        for (i = 0; i < m; i++)

            for (j = 0; j < m; j++)

                chessboard[i][j] = 0;

        srand((unsigned)time(NULL));

        x = rand() % m;

        y = rand() % m;

        if (count1 == 1 || count1 == 0)

            chessboard[x][y] = 2;

        else

            chessboard[x][y] = 4;

        showchessboard();

    }//构造初始棋盘

    void add(int count1);//新增数字

    void showchessboard();//显示棋盘

    void up();

    void down();

    void left();

    void right();

    bool gameover();//游戏失败

private:

    int chessboard[m][m];

    int x, y, count1, count2, temp1, temp2, k;//c1-连消,c2-空位标记,t1-判连消,t2,k-临时变量

    bool flag;//判消

};

二、随机生成数字

void game::add(int count1)

{

    for (i = 0; i < m; i++)

        for (j = 0; j < m; j++)

        {

            if (chessboard[i][j] == 0)

                goto loop;

        }

    showchessboard();

    return;

loop:srand((unsigned)time(NULL));

    do {

        x = rand() % m;

        y = rand() % m;

    } while (chessboard[x][y] != 0);

    if (count1 < 2)

        chessboard[x][y] = 2;

    else

        chessboard[x][y] = 4;

    showchessboard();

}

三、数字消除合并

void game::up()

{

    temp1 = count1;

    flag = false;

    for (j = 0; j < m; j++)

        for (i = 0; i < m;)

        {

            for (; i < 4 && chessboard[i][j] == 0; i++); // 找非零值

            if (i == 4)

                break;

            else

            {

                for (k = i + 1; k < 4 && chessboard[k][j] == 0; k++);//找下一个非零值

                if (k == 4)

                    break;

                else if (chessboard[i][j] == chessboard[k][j])//匹配

                {

                    chessboard[i][j] *= 2;

                    chessboard[k][j] = 0;

                    i = k + 1;

                    flag = true;

                }

                else if (chessboard[i][j] != chessboard[k][j] && k < 4)//不匹配

                {

                    i = k;

                }

            }

        }

    for (j = 0; j < m; j++)//排列棋盘

        for (i = 0, count2 = 0; i < m; i++)

        {

            if (chessboard[i][j] != 0)

            {

                temp2 = chessboard[i][j];

                chessboard[i][j] = 0;

                chessboard[count2][j] = temp2;

                count2++;

            }

        }

}

四、判断游戏结束

bool game::gameover()

{

    if (flag)

        count1++;//判连消

    if (temp1 == count1)

        count1 = 0;//未消除,连消归零

    add(count1);

    for (i = m - 1, j = 0; j < m; j++)//最后一行

    {

        if (j == m - 1)//右下角

        {

            if (chessboard[i][j] == 0)

                return false;

            else if (chessboard[i][j] == 2048)

            {

                cout << "You Win~
";

                return true;

            }

        }

        else

        {

            if (chessboard[i][j] == 0 || chessboard[i][j] == chessboard[i][j + 1])

                return false;

            else if (chessboard[i][j] == 2048)

            {

                cout << "You Win~
";

                return true;

            }

        }

    }

    for (i = 0, j = m - 1; i < m; i++)//最后一列

    {

        if (i == m - 1)//右下角

        {

            if (chessboard[i][j] == 0)

                return false;

            else if (chessboard[i][j] == 2048)

            {

                cout << "You Win~
";

                return true;

            }

        }

        else

        {

            if (chessboard[i][j] == 0 || chessboard[i][j] == chessboard[i + 1][j])

                return false;

            else if (chessboard[i][j] == 2048)

            {

                cout << "You Win~
";

                return true;

            }

        }

    }

    for (i = 0; i < m - 1; i++)

        for (j = 0; j < m - 1; j++)

        {

            if (chessboard[i][j] == 2048)

            {

                cout << "You Win!
";

                return true;

            }

            else if (chessboard[i][j] == chessboard[i][j + 1] || chessboard[i][j] == chessboard[i + 1][j] || chessboard[i][j] == 0)

                return false;

        }

    cout << "Game over.
";

    return true;

}

扩展资料:

C++语言的程序因为要体现高性能,所以都是编译型的。但其开发环境,为了方便测试,将调试环境做成解释型的。

生成程序是指将源码(C++语句)转换成一个可以运行的应用程序的过程。如果程序的编写是正确的,那么通常只需按一个功能键,即可搞定这个过程。但是该过程实际上分成两个步骤。

第一步是对程序进行编译,这需要用到编译器(compiler)。编译器将C++语句转换成机器码(也称为目标码);

第二步就是对程序进行链接,这需要用到链接器(linker)。链接器将编译获得机器码与C++库中的代码进行合并。C++库包含了执行某些常见任务的函数(“函数”是子程序的另一种称呼)。

参考资料来源:

百度百科-C++



分享一个大家小时候常玩的小游戏。希望能够喜欢。



在百度上看到的一个贪吃蛇游戏
#include<iostream.h>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#define N 21
void gotoxy(int x,int y)//位置函数
{
COORD pos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果)
{
int i,j;//初始化围墙
int wall[N+2][N+2]={{0}};
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
wall[i][j]=1;
}
color(11);
for(i=0;i<N+2;i++)
{
for(j=0;j<N+2;j++)
{
if(wall[i][j])
cout<<"■";
else cout<<"□" ;
}
cout<<endl;
}
gotoxy(N+3,1);//显示信息
color(20);
cout<<"按 W S A D 移动方向"<<endl;
gotoxy(N+3,2);
color(20);
cout<<"按任意键暂停"<<endl;
gotoxy(N+3,3);
color(20);
cout<<"得分:"<<endl;
apple[0]=rand()%N+1;//苹果
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout<<"●"<<endl;
}
int main()
{
int i,j;
int** snake=NULL;
int apple[2];
int score=0;
int tail[2];
int len=3;
char ch='p';
srand((unsigned)time(NULL));
init(apple);
snake=(int**)realloc(snake,sizeof(int*)*len);
for(i=0;i<len;i++)
snake[i]=(int*)malloc(sizeof(int)*2);
for(i=0;i<len;i++)
{
snake[i][0]=N/2;
snake[i][1]=N/2+i;
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<<endl;
}
while(1)//进入消息循环
{
tail[0]=snake[len-1][0];
tail[1]=snake[len-1][1];
gotoxy(tail[0],tail[1]);
color(11);
cout<<"■"<<endl;
for(i=len-1;i>0;i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<<endl;
}
if(kbhit())
{
gotoxy(0,N+2);
ch=getche();
}
switch(ch)
{
case 'w':snake[0][1]--;break;
case 's':snake[0][1]++;break;
case 'a':snake[0][0]--;break;
case 'd':snake[0][0]++;break;
default: break;
}
gotoxy(snake[0][0],snake[0][1]);
color(14);
cout<<"★"<<endl;
Sleep(abs(200-0.5*score));
if(snake[0][0]==apple[0]&&snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1
{
score++;
len++;
snake=(int**)realloc(snake,sizeof(int*)*len);
snake[len-1]=(int*)malloc(sizeof(int)*2);
apple[0]=rand()%N+1;
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout<<"●"<<endl;
gotoxy(N+5,3);
color(20);
cout<<score<<endl;
}
if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败
{
gotoxy(N/2,N/2);
color(30);
cout<<"Game over"<<endl;
for(i=0;i<len;i++)
free(snake[i]);
Sleep(INFINITE);
exit(0);
}
}
return 0;
}

使用语言:C++使用工具:vs2019



猜数字游戏


代码如下:


#include <iostream>
using namespace std;

int main(int argc, char** argv) {
int intMimiNumbers[4];
int intDangqianNumbers[4];
int num;
int intTryTimes = 0;
int intACount;
int intBCount;

cout << "猜数字游戏" << endl;
cout << "请输入4位秘密数字,以空格隔开,按ENTER键确认:";
for(int i = 0; i < 4; i++){
cin >> intMimiNumbers[i];
if ( '
' == cin.get())
{
break;
}
}

system("CLS");

cout << "猜数字游戏" << endl;
while(1){
cout << "请输入4位猜测的数字, 以空格隔开,按ENTER键确认:";

intDangqianNumbers[0] = 0;
intDangqianNumbers[1] = 0;
intDangqianNumbers[2] = 0;
intDangqianNumbers[3] = 0;
for(int i = 0; i < 4; i++){
cin >> intDangqianNumbers[i];
if ( '
' == cin.get())
{
break;
}
}

intACount = 0;
intBCount = 0;
for(int iDangqian = 0; iDangqian < 4; iDangqian++)
{
for(int iMimi = 0; iMimi < 4; iMimi++)
{
if(intDangqianNumbers[iDangqian] == intMimiNumbers[iMimi] && iDangqian == iMimi)
{
intACount++;

}

if(intDangqianNumbers[iDangqian] == intMimiNumbers[iMimi] && iDangqian != iMimi)
{
intBCount++;

}
}
}

intTryTimes ++;

cout <<"[" << intTryTimes << "] : "<< intACount << "A" << intBCount << "B" << endl;

if(intACount == 4)
{
cout << "好样的!你猜对了!" << endl;
break;
}
}

return 0;
}



  • c璇█鎵撴暟瀛娓告垙c璇█灏忔父鎴
    绛旓細棣栧厛锛屾垜浠渶瑕佺煡閬撴暟瀛娓告垙鐨勮鍒欍傛暟瀛楁父鎴忔槸涓涓1~100鐨勭寽鏁版父鎴忋傜▼搴忎細闅忔満鐢熸垚涓涓1~100鐨勬暟瀛楋紝鐒跺悗鐜╁闇瑕佹牴鎹彁绀烘潵鐚滄祴鏁板瓧锛岀洿鍒扮寽涓负姝傜▼搴忓皢浼氳緭鍑衡滄瘮瀹冨皬鈥濇垨鈥滄瘮瀹冨ぇ鈥濇彁绀猴紝鐩村埌鏁板瓧琚寽涓傛帴鐫锛屾垜浠渶瑕佷负绋嬪簭鐢熸垚闅忔満鏁般傚湪C璇█涓紝鎴戜滑鍙互浣跨敤rand()鍑芥暟鏉ョ敓鎴愰殢鏈烘暟銆備负浜...
  • 鑳藉湪dev-c++涓婅繍琛岄氳繃鐨勬壂闆,璐悆铔,绛灏忔父鎴鐨c璇█浠g爜
    绛旓細void CloseGame(void); /* 鍏抽棴娓告垙鍑芥暟 */ void DrawGame(void); /* 鐢诲浘鍑芥暟 */ void GameOver(void); /* 杈撳嚭澶辫触鍑芥暟 */ void PlayGame(); /* 娓告垙鎺у埗鍑芥暟 涓昏鎺у埗搴忓垪 */ void Delay(char ch); /* 璋冭妭娓告垙閫熷害 */ /* 涓诲嚱鏁 */ int main(void) { int choice;choice = M...
  • c璇█灏忔父鎴缂栫▼浠g爜,C璇█缂栫▼鏃
    绛旓細getchar鍑芥暟涓巔utchar鍑芥暟缁忓父闇瑕佸尮閰嶄娇鐢紝鍙互鐢ㄦ潵瀹炵幇寰堝鐨勫皬鍔熻兘锛屼緥濡傦紝鍙互鍐欎釜C绋嬪簭缁勫悎瀹冧滑瀹炵幇鍥炴樉鍔熻兘銆佺畝鍗曠殑鏂囦欢鎷疯礉鍔熻兘銆佺敋鑷充竴浜灏忔父鎴绛夈4銆乻canf鍑芥暟杩欎釜鍑芥暟浼拌鏄敤鐨勬渶澶氱殑杈撳叆鍑芥暟浜嗭紝瀹冨彲浠ヤ粠缂撳啿鍖轰腑璇诲叆鏁板瓧銆佸瓧绗︾瓑锛屽氨鍍忎笂杩癈绋嬪簭杩愯鐨勭粨鏋滀竴鏍凤紝涓嶈繃浣跨敤scanf鍑芥暟鏈変竴涓鐐硅娉...
  • 濡備綍鐢–璇█缂栧啓涓涓鍙互鑷姩杩愯鏌愪簺绋嬪簭鐨勫皬绋嬪簭?
    绛旓細void main(){ system("title 婧愪笘鐣屾暣鐞");int x,y;while(1){x=rand()%801;y=rand()%601;SetCursorPos(x,y);} return ;}
  • 鐢–璇█缂栧啓鐨灏忔父鎴浠g爜鏄粈涔?
    绛旓細/*涔熶笉鐭ラ亾浣犳槸浠涔堢骇鍒殑,鎴戞槸涓涓柊鎵,鍒氭帴瑙︾紪绋嬭瑷,浠ヤ笅鏄垜鑷繁鍙樺緱涓涓皬绋嬪簭,鍦ㄦ墍鏈c璇█鐨勭紪璇戝櫒(vc++6.0銆乼urbo???)涓婇兘鑳借繍琛,浣犺繕鍙互杩涗竴姝ユ敼杩涖傝繖鏄竴涓被浼艰椽鍚冭泧鐨灏忔父鎴銆傜浣犲ソ杩*/\x0d\x0a/*璐悆铔*/\x0d\x0a#include\x0d\x0a#include\x0d\x0a#include\x0d\x0a#includ...
  • 姹傚嚑C璇█涓皬娓告垙浠g爜,绠鍗曠殑,瑕佹敞閲娿併佽阿璋簡銆
    绛旓細// Calcu24.cpp : Defines the entry point for the console application.// / 6-6 24鐐娓告垙 / include "conio.h"include "stdlib.h"include "time.h"include "math.h"include "string.h"/ 浠庝竴鍓墤鍏嬬墝涓紝浠诲彇4寮犮2-10 鎸夊叾鐐规暟璁$畻(涓轰簡琛ㄧず鏂逛究10鐢═琛ㄧず)锛孞,Q,K,A 缁熶竴鎸 1 ...
  • c璇█缂栧啓涓涓寮瑰姏鐞灏忔父鎴鏍规嵁绯荤粺鏃堕棿鐢ㄦ墍鐜╂父鎴忕殑鏃堕棿鏉ョ粰鐜╁鎺掑悕鎬...
    绛旓細瑕佽幏鍙栫郴缁熸椂闂达紝鏈変袱涓唴閮ㄥ嚱鏁板彲浠ョ敤銆備竴銆乼ime鍑芥暟銆傝繑鍥炲兼槸time_t绫诲瀷鐨勭粨鏋勪綋锛屽兼槸浠1970骞1鏈1鏃ュ紑濮嬫墍缁忚繃鐨勭銆備簩銆乴ocaltime鍑芥暟銆傝繑鍥瀟m绫诲瀷鐨勭粨鏋勪綋锛屽寘鍚簡骞存湀鏃ユ椂鍒嗙绛変俊鎭備綘杩欑闇姹傜敤绗涓涓灏卞彲浠ヤ簡锛屽樊鍊煎氨鑳界敤鏉ユ帓鍚嶃傜浜岀涓昏鐢ㄦ潵杈撳嚭鎵撳嵃鏃堕棿鐨勩傚ご鏂囦欢鏄痶ime.h銆
  • 鐢–璇█缂栧啓鐨灏忔父鎴浠g爜鏄粈涔?
    绛旓細"鎵浄"灏忔父鎴廋浠g爜 include<stdio.h>#include<math.h>#include#include<stdlib.h>main( ){char a[102][102],b[102][102],c[102][102],w;int i,j; /*寰幆鍙橀噺*/int x,y,z[999]; /*闆风殑浣嶇疆*/int t,s; /*鏍囪*/int m,n,lei; /*璁℃暟*/int u,v; /*杈撳叆*/int...
  • C璇█缂栧啓鐚滄暟瀛娓告垙
    绛旓細void help(void){ char i =0,j = 0;for(i =0;i<4;i++){ for(j = 0;j<4;j++)if(Num[j] == getNum[i]){ if(i==j)printf("%dth is correct!\r\n",i);else { printf("%d is a correct data\r\n",getNum[i]);} } else { if(i == 3&&j==3){printf("/*...
  • 鐢–++缂栧啓鐨灏忔父鎴婧愪唬鐮
    绛旓細if (c == 'n')break;} } protected:int ChoiceMode() //閫夋嫨妯″紡 { int i = 0;system("cls"); //绯荤粺璋冪敤锛屾竻灞 InitChessBoard(); //閲嶆柊鍒濆鍖栨鐩 cout << "***0銆侀鍑 1銆佺數鑴憊s鐜╁ 2銆佺帺瀹秜s鐜╁***" << endl;while (1){ cout << "璇烽夋嫨锛";cin >> ...
  • 扩展阅读:少儿编程小游戏 ... 6-12岁儿童室内游戏 ... 免费小游戏 ... 自己编程做游戏的软件 ... 如何自己编程做游戏 ... 小学生最火的游戏 ... 如何自己编程一个软件 ... 100个简单小游戏 ... 免费游戏马上玩游戏 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网