飛機大戰也是在練習各類語言開發中一個比較重要的案例,話不多說,直接開碼!
以前的飛機大戰教程:
- 飛機遊戲(1)
- 飛機遊戲(2)
文章目錄
-
- 數組實作飛機控制
- 發射子彈
- 擊中敵機
- 最後
數組實作飛機控制
仍然将畫布中所有元素儲存在二維數組 canvas[High][Width] 當中,用 0 表示空格,用 1 表示飛機。
飛機移動控制就和上一次小球反彈的實作類似了。
上代碼:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define High 25
#define Width 50
//全局變量
int pos_x, pos_y; //飛機位置
int canvas[High][Width] = [0]; //畫布元素數組
void gotoxy(int x, int y) //移動光标便于清屏重畫
{
HANDLE handle = GetStdHandle(STD_UOTPUT_HANDLE);
CROOD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void startup() //初始化
{
pos_x = High/2;
pos_y = Width/2;
canvas[pos_x][pos_y] = 1;
}
void show() //畫面顯示
{
gotoxy(0, 0);
int i,j;
for(i=0; i<High; i++)
{
for(j=0; j<Width; j++)
{
if(canvas[i][j] == 0)
printf(" ");
else if(canvas[i][j] == 1)
printf("*"); //輸出飛機
}
printf("\n"); //每完成一行後換行
}
sleep(20);
}
void updateWithoutInput() //與輸入無關的更新
{}
void updateWithInput()
{
char input;
if(kbhit()) //判斷是否輸入
{
input = getch();
if(input == 'a') //左移
{
canvas[pos_x][pos_y] = 0;
pos_y --;
canvas[pos_x][pos_y] = 1;
}
if(input == 'd') //右移
{
canvas[pos_x][pos_y] = 0;
pos_y ++;
canvas[pos_x][pos_y] = 1;
}
if(input == 'w') //上移
{
canvas[pos_x][pos_y] = 0;
pos_x --;
canvas[pos_x][pos_y] = 1;
}
if(input == 's') //下移
{
canvas[pos_x][pos_y] = 0;
pos_x ++;
canvas[pos_x][pos_y] = 1;
}
}
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
發射子彈
完成以上的部分後,畫布上就有了一個飛機(雖然就是個星号),那麼下一步就是給他加上子彈:
新定義數組内元素值為 2 時,函數 show() 在畫布上畫出子彈 ‘|’ 。
具體步驟如下:
// show()函數中
...
else if(canvas[i][j] == 2)
printf("|");
...
void updateWithoutInput()
{
int i,j;
for(i=0; i<High; i++)
{
for(j=0; j<Width; j++)
{
if(canvas[i][j] == 2) //子彈向上移動
{
canvas[i][j] = 0;
if(i>0)
canvas[i - 1][j] = 2;
}
}
}
}
// updateWithInput() 部分
...
else if(input == ' ') //發射子彈
canvas[pos_x - 1][pos_y] = 2;
...
擊中敵機
到這一步,我們的飛機大戰就基本搞定了。
最後加入敵機重新整理和敵機的相關判定,飛機大戰遊戲基本功能就告成了,當然大家也可以為它添加其他規則或元素。
定義數組中元素為 3 時輸出敵機 ’ @ '。
需要添加的代碼如下:
// 全局變量
int score; //得分
// 初始化
//敵人坐标
enemy_x = 0;
enemy_y = pos_y;
canvas[enemy_x][enemy_y] = 3;
//得分
score = 0;
// show()
...
else if(canvas[i][j] == 3)
printf("@");
...
printf("得分: %3d\n", score);
// 擊中判定
if(canvas[i][j] == 2)
{
if((i == enemy_x)&&(j == enemy_y))
{
score ++;
canvas[enemy_x][enemy_y] = 0;
// 重新整理敵機位置
enemy_x = 0;
enemy_y = rand()%Width;
canvas[enemy_x][enemy_y] = 3;
// 子彈消失
canvas[i][j] = 0;
}
//子彈向上移動
else{
canvas[i][j] = 0;
if(i>0)
canvas[i - 1][j] = 2;
}
}
// 相撞
if((pos_x == enemy_x)&&(pos_y == enemy_y))
{
printf("Game Over");
sleep(1000);
system("pause");
exit(0);
}
// 敵機越界
if(enemy_x > High)
{
canvas[enemy_x][enemy_y] = 0;
// 重新整理敵機位置
enemy_x = 0;
enemy_y = rand()%Width;
canvas[enemy_x][enemy_y] = 3;
// 減分
score --;
}
// 敵機下落
static int speed;
if(speed < 10)
speed ++;
if(speed == 10)
{
canvas[enemy_x][enemy_y] = 0;
enemy_x ++;
speed = 0;
canvas[enemy_x][enemy_y] = 3;
}
最後
到這裡基本功能就全部實作了,以下是拓展到更多功能的小提示:
多架敵機:
或許使用數組來儲存敵機位置(一個二維數組或2個一維數組)可以幫到你。
更多子彈:
可以定義其他值來輸出不同子彈,并且給這些子彈的判定分出不同的類。
敵機主動攻擊:
隻需要改一改 updateWithoutInput() 即可實作。
BOSS:
更簡單了,相當于又新加了一個 enemy 而已。
感謝閱讀