C語言實現打飛機簡易遊戲(半完成版)
阿新 • • 發佈:2018-11-13
打飛機簡易遊戲(半完成版),如圖。
感謝 童晶 老師的教程,連結地址:http://study.163.com/forum/detail/1003961010.htm
未完成內容:①邊框繪製 ②多個敵機 ③敵機子彈 ④戰機生命 等等。
以下是程式碼。(在帶c99標準的codeblocks下通過,在vc++6.0下未通過!)
*******************************************************************************************
// 打飛機簡易遊戲(未完成)。
#include<stdio.h> #include<stdlib.h> #include<windows.h> #include<conio.h> int bullet_x,bullet_y; int enemy_x,enemy_y; int high_max,width_max; int score; int x,y; void gotoxy(int,int); void picture(); void changed(); void start(); void show(); void other(); void Hide(); int main(){ SetConsoleTitleA("hit planes"); //設定視窗名稱。 system("mode con cols=30 lines=22"); //設定視窗大小。 start(); while(1){ //迴圈。 Hide(); show(); other(); changed(); } return 0; } void gotoxy(int x, int y){ //座標函式。 COORD coord; coord.X = x;coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } void start(){ //引數設定。 high_max = 20; //長度。即X軸。 width_max = 30; //寬度。即Y軸。 x = high_max / 2; //戰機座標。 y = width_max / 2; bullet_x = -2; //子彈座標。 bullet_y = y; enemy_x = 0; //敵機座標。 enemy_y = y; score = 0; //得分 } void show(){ //圖形介面繪製。 gotoxy(0,0); int i,j,k; for(i = 0;i < high_max;i++){ for(j = 0;j < width_max;j++){ if((i == x) && (j == y)) picture(); else if((i == enemy_x) && (j == enemy_y)) printf("O"); else if((i == bullet_x + 1) && (j == bullet_y)) printf("|"); else if((i == 0) || (i == high_max - 1)) printf("-"); else if((j == 0) || (j == width_max - 1)) printf("|"); else printf(" "); } printf("\n"); } for(k = 0;k < (width_max / 2) - 5;k++) printf(" "); printf("score:%d\n",score); //得分。 } void picture(){ //戰機圖形。 printf("*"); } void changed(){ //操作控制。不區分大小寫。WSAD 上下左右,J發射子彈 ,P 暫停,ESC 退出。 char sizein; if(kbhit()){ sizein = getch(); if(((sizein == 'a') || (sizein == 'A')) && (y > 1)) y--; if(((sizein == 'd') || (sizein == 'D')) && (y < width_max - 2)) y++; if(((sizein == 's') || (sizein == 'S')) && (x < high_max - 2)) x++; if(((sizein == 'w') || (sizein == 'W')) && (x > 1)) x--; if(sizein == 27) exit(0); if((sizein == 'p') || (sizein == 'P')) system("pause > nul"); if((sizein == 'j') || (sizein == 'J')){ bullet_x = x - 1; bullet_y = y; } } } void other(){ //敵機迴圈生成。 if(bullet_x > -1); bullet_x--; if((bullet_x == enemy_x) && (bullet_y == enemy_y)){ score++; enemy_x = -1; enemy_y = (rand() % (width_max - 1) + 1); bullet_x = -2; } if((enemy_x > high_max) || (enemy_y < 1)) { enemy_x = -1; enemy_y = (rand() % (width_max - 1) + 1); } static int speed = 0; if(speed < 10) //調整敵機移動速度。 speed++; if(speed == 10){ enemy_x++; speed = 0; } } void Hide(){ //隱藏游標函式呼叫。 CONSOLE_CURSOR_INFO CURSORINFO = {1,0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &CURSORINFO); }
******************************************************************************************