鱼C论坛

 找回密码
 立即注册
查看: 9806|回复: 278

[技术交流] 垃圾版飞机大战

  [复制链接]
发表于 2019-9-21 14:36:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 一个账号 于 2020-3-28 15:48 编辑

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<conio.h>
  4. #include<windows.h>
  5. #include<stdlib.h>
  6. #define MAX 100
  7. long long int speed = 0;//控制敌机的速度
  8. int position_x, position_y;//飞机的所在位置
  9. int high, width;//地图的大小
  10. int bullet_x, bullet_y;//子弹的位置
  11. int enemy_x, enemy_y;//敌人的位置
  12. int map[MAX][MAX];
  13. /*0表示空白,1表示战机*的区域,2表示敌人战机的位置。
  14. 3表示上下围墙,4表示左右围墙,5表示子弹的位置*/
  15. int score;
  16. void starup()//初始化所有的信息
  17. {
  18.     high = 20;
  19.     width = 30;
  20.     position_x = high / 2;
  21.     position_y = width / 2;
  22.     bullet_x = 0;
  23.     bullet_y = position_y;
  24.     enemy_x = 2;
  25.     enemy_y = position_y - 1;
  26.     score = 0;

  27. }
  28. void startMap()
  29. {
  30.     int i, j;
  31.     for (i = 1; i <= high - 1; i++)
  32.     {
  33.         map[i][1] = 4;
  34.         for (j = 2; j <= width - 1; j++)
  35.             map[i][j] = 0;
  36.         map[i][width] = 4;
  37.     }
  38.     //下方围墙的初始化
  39.     i = high;
  40.     for (j = 1; j <= width; j++)
  41.         map[i][j] = 3;

  42.     map[bullet_x][bullet_y] = 5;
  43.     /*这里是战机大小的初始化开始*/
  44.     map[position_x - 1][position_y] = 1;
  45.     i = position_x;
  46.     for (j = position_y - 2; j <= position_y + 2; j++)
  47.         map[i][j] = 1;
  48.     map[position_x + 1][position_y - 1] = 1;
  49.     map[position_x + 1][position_y + 1] = 1;
  50.     /***      初始化结束         **/

  51.     /* 敌人战机的初始化 */
  52.     map[enemy_x][enemy_y] = 2;
  53.     map[enemy_x - 1][enemy_y - 1] = 2;
  54.     map[enemy_x - 1][enemy_y + 1] = 2;
  55.     /* 敌人战机初始化结束*/
  56. }
  57. void HideCursor()//隐藏光标
  58. {
  59.     CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
  60.     SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  61. }
  62. void gotoxy(int x, int y)//清理一部分屏幕
  63. {
  64.     HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
  65.     COORD pos;
  66.     pos.X = x;
  67.     pos.Y = y;
  68.     SetConsoleCursorPosition(handle, pos);
  69. }
  70. void updateWithoutInput()//于输入无关的跟新
  71. {
  72.         int i, j;
  73.        
  74.     if (bullet_x > 0)
  75.         bullet_x--;
  76.     if ((bullet_x == enemy_x) && (bullet_y == enemy_y))//当敌人的飞机被击中时
  77.     {
  78.         score++;
  79.         enemy_x = 0;
  80.         enemy_y = rand() % width;
  81.         bullet_x = 0;
  82.     }
  83.     if (enemy_x > high)//当飞机超出区域
  84.     {
  85.         enemy_x = 0;
  86.         enemy_y = rand() % width;
  87.     }
  88.     if (speed == 1)
  89.         for (i = 1; i <= 10000; i++)//用来控制敌机的速度
  90.         {
  91.             for (j = 1; j <= 1000; j++)
  92.             {
  93.                 speed = 1;
  94.             }
  95.         }
  96.     speed = 0;
  97.     if (speed == 0)
  98.     {
  99.         enemy_x++;
  100.         speed = 1;
  101.     }
  102. }
  103. void updateWithInput()//与输入有关的更新
  104. {
  105.     char input;
  106.     if (kbhit())//在VC6.0++下,为_kbhit()
  107.     {
  108.         input = getch();//在VC6.0++下为_getch();
  109.         if (input == 'a')
  110.             position_y--;
  111.         if (input == 's')
  112.             position_x++;
  113.         if (input == 'd')
  114.             position_y++;
  115.         if (input == 'w')
  116.             position_x--;
  117.         if (input == ' ')
  118.         {
  119.             bullet_x = position_x - 1;
  120.             bullet_y = position_y;
  121.         }
  122.     }
  123. }
  124. void show()//展示的内容
  125. {
  126.     gotoxy(0, 0);
  127.     int i, j;
  128.     for (i = 1; i <= high; i++)
  129.     {
  130.         for (j = 1; j <= width; j++)
  131.         {
  132.             if (map[i][j] == 0)
  133.                 printf(" ");
  134.             if (map[i][j] == 1)
  135.                 printf("*");
  136.             if (map[i][j] == 2)
  137.                 printf("#");
  138.             if (map[i][j] == 3)
  139.                 printf("~");
  140.             if (map[i][j] == 4)
  141.                 printf("|");
  142.             if (map[i][j] == 5)
  143.                 printf("|");
  144.         }
  145.         printf("\n");
  146.     }
  147.     printf("\n你的得分:%d\n\n", score);
  148.     printf("操作说明: WSAD移动飞机,空格是发出子弹\n");
  149. }
  150. int main()
  151. {
  152.     starup();
  153.     while (1)
  154.     {
  155.         HideCursor();
  156.         startMap();
  157.         show();
  158.         updateWithoutInput();
  159.         updateWithInput();
  160.     }
  161.     return 0;
  162. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-9-21 14:36:44 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-21 14:37:34 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

 楼主| 发表于 2019-9-21 14:40:02 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-21 18:13:20 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-22 17:15:37 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-22 18:56:39 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-23 18:36:22 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-24 16:55:42 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-25 00:01:09 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-25 15:26:58 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-25 21:03:51 From FishC Mobile | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-25 22:09:21 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-26 08:39:04 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-26 08:59:55 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-26 09:34:10 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-26 09:47:43 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-26 12:14:47 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-26 14:18:30 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2019-9-26 14:26:58 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-16 18:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表