C语言如何实现飞机大战
短信预约 -IT技能 免费直播动态提醒
本文小编为大家详细介绍“C语言如何实现飞机大战”,内容详细,步骤清晰,细节处理妥当,希望这篇“C语言如何实现飞机大战”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
具体代码如下
#include<stdio.h>#include<stdlib.h>#include<conio.h>#include<Windows.h>int score = 0;int plane_col, plane_row;//·É»úλÖÃint bullet_col,bullet_row;//×Óµ¯µÄλÖÃint area_height, area_width;//ÓÎÏ·ÇøÓò 0-n-1int enemy_col, enemy_row;int enemy_vh, enemy_vv;int a[100][100] = { 0 };void gotoxy(int x, int y) {//ˢРHANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle, pos);}void HideCursor() { CONSOLE_CURSOR_INFO cursor_info = { 1,0 }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);}void startup()//³õʼ»¯ { area_height = 20; area_width = 30; plane_col = 14; plane_row = 10; bullet_col = 0; bullet_row = -1; enemy_col = rand() % area_width; enemy_row = 0; enemy_vh = 0; enemy_vv = 1;}//int[][] planeArray() {// // a[plane_col][plane_row] = 1;// for (int i = plane_col - 2; i < plane_col + 2; i++)// a[i][plane_row + 1] = 1;// a[plane_col - 1][plane_row + 2] = 1; a[plane_col + 1][plane_row + 2] = 1;//// return a;//}void show()//Öð¸ö×Ö·ûɨÃè²¢´òÓ¡ { gotoxy(0, 0); int i, j; //ÏÔʾ //system("cls"); for (i = 0; i < area_height; i++)//ÐбéÀú { for (j = 0; j < area_width; j++)//ÁбéÀú { if (i == plane_row && j == plane_col) { printf("*"); } else if (i == bullet_row && j == bullet_col) printf("|"); else if (i == enemy_row && j == enemy_col) printf("@"); else printf(" "); } printf("\n"); } printf("score:%d\n",score);}void updateWithInput()//½»»¥£¬¿ØÖÆ·É»úÒƶ¯£¬Éä»÷{ char input; //µÈ´ýÓû§µÄÊäÈ룬½»»¥ if (kbhit()) { input = getch(); switch (input) { case 'w': if(plane_row != 0) plane_row--; break; case 'a': if(plane_col != 0) plane_col--; break; case 'd': if(plane_col != area_width) plane_col++; break; case 's': if(plane_row != area_height) plane_row++; break; case ' ': if (bullet_row < 0)//ÆÁÄ»ÀïûÓÐ×Óµ¯ { bullet_row = plane_row - 1; bullet_col = plane_col; } break; default: break; } }}int IsCrash() { //ÅжÏÎÒ·Å·É»úÊÇ·ñ×¹»Ù if (enemy_col == plane_col && enemy_row == plane_row) { return 1; } return 0;}void updateWithourInput()//×Óµ¯Òƶ¯ÓëµÐÈËÒƶ¯ { //¸üРbullet_row--; static int count = 0; count ++; if (count == 40) { enemy_row += enemy_vv; enemy_col += enemy_vh; count = 0; } }void crack() {//»÷»ÙµÐÈË if(enemy_row > area_height){ bullet_row = -1; enemy_row = -1; enemy_col = rand() % area_height; } else if (bullet_col == enemy_col && bullet_row == enemy_row) { score += 10; bullet_row = -1; enemy_row = -1; enemy_col = rand() % area_height; }}int IsFinish() {//ÓÎÏ·ÊÇ·ñ½áÊø if (score == 100) { system("cls"); printf("congretulations!!!"); score = 0; _sleep(500);//ÏÈÔÝÍ£ÔÚÏÖʵ·ûºÏÈËÐÔ»¯ system("pause"); return 1; } else if (IsCrash() == 1) { system("cls"); printf("you have lost!!!"); score = 0; _sleep(500); system("pause"); return 1; } return 0;}int main(){ HideCursor(); startup(); while (1) { show(); updateWithInput(); updateWithourInput(); crack(); if(IsFinish()==1){ startup(); continue; } } return 0;}
读到这里,这篇“C语言如何实现飞机大战”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网行业资讯频道。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341