如何用C++编写一个小游戏
一个用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~\n";
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~\n";
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~\n";
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~\n";
return true;
}
}
}
for (i = 0; i < m - 1; i++)
for (j = 0; j < m - 1; j++)
{
if (chessboard[i][j] == 2048)
{
cout << "You Win!\n";
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.\n";
return true;
}
扩展资料:
C++语言的程序因为要体现高性能,所以都是编译型的。但其开发环境,为了方便测试,将调试环境做成解释型的。
生成程序是指将源码(C++语句)转换成一个可以运行的应用程序的过程。如果程序的编写是正确的,那么通常只需按一个功能键,即可搞定这个过程。但是该过程实际上分成两个步骤。
第一步是对程序进行编译,这需要用到编译器(compiler)。编译器将C++语句转换成机器码(也称为目标码);
第二步就是对程序进行链接,这需要用到链接器(linker)。链接器将编译获得机器码与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 ( '\n' == 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 ( '\n' == 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;
}