導航:首頁 > 編程知識 > 怎麼看小游戲的編程

怎麼看小游戲的編程

發布時間:2024-08-24 14:07:26

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;
}

閱讀全文

與怎麼看小游戲的編程相關的資料

熱點內容
數據挖掘能實現什麼 瀏覽:686
e420硬體升級 瀏覽:225
QQ域名防紅代碼 瀏覽:756
viahdwin10 瀏覽:448
360軟體小助手怎麼添加文件夾 瀏覽:401
電視內置網路有什麼好處 瀏覽:81
微信10分鍾下款口子 瀏覽:62
中國買賣書畫的網站有哪個 瀏覽:648
vb程序最小公倍數 瀏覽:747
迅雷雲盤下載的文件怎麼找不到 瀏覽:49
樹莓派做win10電視盒子 瀏覽:690
模仁編程是什麼意思 瀏覽:267
分數app 瀏覽:135
硬碟格式化文件系統 瀏覽:325
如果禁用分頁文件 瀏覽:80
公司級的路由器密碼設置 瀏覽:581
京東app的好物榜怎麼進 瀏覽:803
word打開臨時文件夾 瀏覽:686
如何復制選中的數據 瀏覽:96

友情鏈接