A. 如何用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~ ";
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;
}
(1)怎么看小游戏的编程扩展阅读:
C++语言的程序因为要体现高性能,所以都是编译型的。但其开发环境,为了方便测试,将调试环境做成解释型的。
生成程序是指将源码(C++语句)转换成一个可以运行的应用程序的过程。如果程序的编写是正确的,那么通常只需按一个功能键,即可搞定这个过程。但是该过程实际上分成两个步骤。
第一步是对程序进行编译,这需要用到编译器(compiler)。编译器将C++语句转换成机器码(也称为目标码);
第二步就是对程序进行链接,这需要用到链接器(linker)。链接器将编译获得机器码与C++库中的代码进行合并。C++库包含了执行某些常见任务的函数(“函数”是子程序的另一种称呼)。
参考资料来源:
网络-C++
B. c++编程小游戏代码
以下是贪吃蛇源代码:
#include<iostream.h>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#defineN21
voidgotoxy(intx,inty)//位置函数{
COORDpos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
voidcolor(inta)//颜色函数{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
voidinit(intapple[2])//初始化函数(初始化围墙、显示信息、苹果)
{
inti,j;//初始化围墙
intwall[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<<"■";
elsecout<<"□";
}
cout<<endl;
}
gotoxy(N+3,1);//显示信息
color(20);
cout<<"按WSAD移动方向"<<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;
}
intmain()
{
inti,j;
int**snake=NULL;
intapple[2];
intscore=0;
inttail[2];
intlen=3;
charch='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<<"失败!!!"<<endl;
for(i=0;i<len;i++)
free(snake[i]);
Sleep(INFINITE);
exit(0);
}
}
return0;
}
C. 少儿编程就是玩游戏吗
少儿编程可以用来制作小游戏小程序,很多家长对图形编程都有误区,认为编程就是玩游戏,其实不是的,编程的确可以用来制作游戏,但是更多的是让孩子在制作游戏的过程中,能学习到知识,可以去诺可编程官网看看,知名度高。
D. 用C语言编写的小游戏代码是什么
/*也不知道你是什么级别的,我是一个新手,刚接触编程语言,以下是我自己变得一个小程序,在所有c语言的编译器(vc++6.0、turbo…………)上都能运行,你还可以进一步改进。这是一个类似贪吃蛇的小游戏。祝你好运*/
/*贪吃蛇*/
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
int head=3 ,tail=0;
int main()
{
int i,j,k=0;
int zuobiao[2][80];
long start;
int direction=77;
int gamespeed;
int timeover;
int change(char qipan[20][80],int zuobiao[2][80],char direction);
zuobiao[0][tail]=1;zuobiao[1][tail]=1;zuobiao[0][1]=1;zuobiao[1][1]=2;zuobiao[0][2]=1;zuobiao[1][2]=3;zuobiao[0][head]=1;zuobiao[1][head]=4;
/*处理棋盘*/
char qipan[20][80];//定义棋盘
for(i=0;i<20;i++)
for(j=0;j<80;j++)
qipan[i][j]=' ';//初始化棋盘
for(i=0;i<80;i++)
qipan[0][i]='_';
for(i=0;i<20;i++)
qipan[i][0]='|';
for(i=0;i<20;i++)
qipan[i][79]='|';
for(i=0;i<80;i++)
qipan[19][i]='_';
qipan[1][1]=qipan[1][2]=qipan[1][3]='*';//初始化蛇的位置
qipan[1][4]='#';
printf("This is a game of a SNAKE.\nGOOD LUCK TO YOU !\n");
printf("Input your game speed,please.(e.g.300)\n");
scanf("%d",&gamespeed);
while(direction!='q')
{
system("cls");
for(i=0;i<20;i++)//打印出棋盘
for(j=0;j<80;j++)
printf("%c",qipan[i][j]);
timeover=1;
start=clock();
while(!kbhit()&&(timeover=clock()-start<=gamespeed));
if(timeover)
{
getch();
direction=getch();
}
else
direction=direction;
if(!(direction==72||direction==80||direction==75||direction==77))
{
return 0;
system("cls");
printf("GAME OVER!\n");
}
if(!change(qipan,zuobiao,direction))
{
direction='q';
system("cls");
printf("GAME OVER!\n");
}
}
return 0;
}
int change(char qipan[20][80],int zuobiao[2][80],char direction)
{
int x,y;
if(direction==72)
x=zuobiao[0][head]-1;y=zuobiao[1][head];
if(direction==80)
x=zuobiao[0][head]+1;y=zuobiao[1][head];
if(direction==75)
x=zuobiao[0][head];y=zuobiao[0][head]-1;
if(direction==77)
x=zuobiao[0][head];y=zuobiao[1][head]+1;
if(x==0||x==18||y==78||y==0)
return 0;
if(qipan[x][y]!=' ')
return 0;
qipan[zuobiao[0][tail]][zuobiao[1][tail]]=' ';
tail=(tail+1)%80;
qipan[zuobiao[0][head]][zuobiao[1][head]]='*';
head=(head+1)%80;
zuobiao[0][head]=x;
zuobiao[1][head]=y;
qipan[zuobiao[0][head]][zuobiao[1][head]]='#';
return 1;
}