1. 程式人生 > >一個好玩的小遊戲(純C語言編寫)

一個好玩的小遊戲(純C語言編寫)

最近在看知乎是發現了一個這一個專欄
https://zhuanlan.zhihu.com/c2game
從中獲取的許多知識,本文中的遊戲也是從裡面學到的,不過本人又自己加了一些功能。
這是一個類似於飛機大戰的遊戲,不過目前程式碼量比較小,所以看起來非常簡陋遊戲介面如下
更新日誌,本人將原來的原來的程式碼有進一步的優化了一下,之前是隻有一個非常小的戰機現在更新後可以產生一個非常大的戰機(看起來也更有氣勢了~~)和敵人的戰機,不過死亡的判定條件和邊境的判斷條件還沒有做好,等下次再繼續加油。2017.3.12更新
就是這樣一個簡陋的遊戲(實在慚愧,本人目前能力有限)
如下圖:
這裡寫圖片描述
完整的程式碼如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<windows.h>
#include<stdlib.h>
#define MAX 100
long long int speed = 0;//控制敵機的速度 
int position_x, position_y;//飛機的所在位置 
int high, width;//地圖的大小 
int bullet_x, bullet_y;//子彈的位置 
int enemy_x, enemy_y;//敵人的位置 
int map[MAX][MAX];
/*0表示空白,1表示戰機*的區域,2表示敵人戰機的位置。
3表示上下圍牆,4表示左右圍牆,5表示子彈的位置*/
int score; void starup()//初始化所有的資訊 { high = 20; width = 30; position_x = high / 2; position_y = width / 2; bullet_x = 0; bullet_y = position_y; enemy_x = 2; enemy_y = position_y - 1; score = 0; } void startMap() { int i, j; for (i = 1; i <= high - 1; i++) { map
[i][1] = 4; for (j = 2; j <= width - 1; j++) map[i][j] = 0; map[i][width] = 4; } //下方圍牆的初始化 i = high; for (j = 1; j <= width; j++) map[i][j] = 3; map[bullet_x][bullet_y] = 5; /*這裡是戰機大小的初始化開始*/ map[position_x - 1][position_y] = 1; i = position_x; for (j = position_y - 2; j <= position_y + 2; j++) map[i][j] = 1; map[position_x + 1][position_y - 1] = 1; map[position_x + 1][position_y + 1] = 1; /*** 初始化結束 **/ /* 敵人戰機的初始化 */ map[enemy_x][enemy_y] = 2; map[enemy_x - 1][enemy_y - 1] = 2; map[enemy_x - 1][enemy_y + 1] = 2; /* 敵人戰機初始化結束*/ } void HideCursor()//隱藏游標 { CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void gotoxy(int x, int y)//清理一部分螢幕 { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle, pos); } void updateWithoutInput()//於輸入無關的跟新 { if (bullet_x > 0) bullet_x--; if ((bullet_x == enemy_x) && (bullet_y == enemy_y))//當敵人的飛機被擊中時 { score++; enemy_x = 0; enemy_y = rand() % width; bullet_x = 0; } if (enemy_x > high)//當飛機超出區域 { enemy_x = 0; enemy_y = rand() % width; } if (speed == 1) for (int i = 1; i <= 10000; i++)//用來控制敵機的速度 { for (int j = 1; j <= 1000; j++) { speed = 1; } } speed = 0; if (speed == 0) { enemy_x++; speed = 1; } } void updateWithInput()//與輸入有關的更新 { char input; if (kbhit())//在VC6.0++下,為_kbhit() { input = getch();//在VC6.0++下為_getch(); if (input == 'a') position_y--; if (input == 's') position_x++; if (input == 'd') position_y++; if (input == 'w') position_x--; if (input == ' ') { bullet_x = position_x - 1; bullet_y = position_y; } } } void show()//展示的內容 { gotoxy(0, 0); int i, j; for (i = 1; i <= high; i++) { for (j = 1; j <= width; j++) { if (map[i][j] == 0) printf(" "); if (map[i][j] == 1) printf("*"); if (map[i][j] == 2) printf("#"); if (map[i][j] == 3) printf("~"); if (map[i][j] == 4) printf("|"); if (map[i][j] == 5) printf("|"); } printf("\n"); } printf("\n你的得分:%d\n\n", score); printf("操作說明: ASDW分別操作 左下右上四個的移動\n"); printf("**空格是發出子彈**\n"); } int main() { starup(); while (1) { HideCursor(); startMap(); show(); updateWithoutInput(); updateWithInput(); } return 0; }

注意107行和109行的kbhit()和getch()
如果你看不明白,我建議你先去上面的那個連線中看看,他會教你如何一步步的進行最後做成一個完整的遊戲。