请选择 进入手机版 | 继续访问电脑版
工作室 好人游戏乱跑,解决方案 投稿
鼠标 宏命令 手游 工作室文化
快打器 微信 常见问题 常用平板与手机
魔兽视频
查看: 1209|回复: 0

[C语言] 【实战教程】一个好玩的小游戏(纯C语言编写)

[复制链接]
  • TA的每日心情
    开心
    2019-9-24 12:14
  • 签到天数: 5 天

    连续签到: 1 天

    [LV.2]偶尔看看I

    405

    主题

    1454

    帖子

    6601

    积分

    实习版主

    Rank: 7Rank: 7Rank: 7

    UID
    19
    帖子
    1454
    精华
    2
    威望
    551
    贡献
    135
    虾币
    3902
    阅读权限
    100
    注册时间
    2013-9-14

    热心会员永不封号灌水之王勤奋版主游戏达人美女勋章

    发表于 2019-10-11 14:34:52 | 显示全部楼层 |阅读模式
    一个C语言程序写的小游戏实战代码。


    完整的代码如下:
    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.     if (bullet_x > 0)
    73.         bullet_x--;
    74.     if ((bullet_x == enemy_x) && (bullet_y == enemy_y))//当敌人的飞机被击中时
    75.     {
    76.         score++;
    77.         enemy_x = 0;
    78.         enemy_y = rand() % width;
    79.         bullet_x = 0;
    80.     }
    81.     if (enemy_x > high)//当飞机超出区域
    82.     {
    83.         enemy_x = 0;
    84.         enemy_y = rand() % width;
    85.     }
    86.     if (speed == 1)
    87.         for (int i = 1; i <= 10000; i++)//用来控制敌机的速度
    88.         {
    89.             for (int j = 1; j <= 1000; j++)
    90.             {
    91.                 speed = 1;
    92.             }
    93.         }
    94.     speed = 0;
    95.     if (speed == 0)
    96.     {
    97.         enemy_x++;
    98.         speed = 1;
    99.     }
    100. }
    101. void updateWithInput()//与输入有关的更新
    102. {
    103.     char input;
    104.     if (kbhit())//在VC6.0++下,为_kbhit()
    105.     {
    106.         input = getch();//在VC6.0++下为_getch();
    107.         if (input == 'a')
    108.             position_y--;
    109.         if (input == 's')
    110.             position_x++;
    111.         if (input == 'd')
    112.             position_y++;
    113.         if (input == 'w')
    114.             position_x--;
    115.         if (input == ' ')
    116.         {
    117.             bullet_x = position_x - 1;
    118.             bullet_y = position_y;
    119.         }
    120.     }
    121. }
    122. void show()//展示的内容
    123. {
    124.     gotoxy(0, 0);
    125.     int i, j;
    126.     for (i = 1; i <= high; i++)
    127.     {
    128.         for (j = 1; j <= width; j++)
    129.         {
    130.             if (map[i][j] == 0)
    131.                 printf(" ");
    132.             if (map[i][j] == 1)
    133.                 printf("*");
    134.             if (map[i][j] == 2)
    135.                 printf("#");
    136.             if (map[i][j] == 3)
    137.                 printf("~");
    138.             if (map[i][j] == 4)
    139.                 printf("|");
    140.             if (map[i][j] == 5)
    141.                 printf("|");
    142.         }
    143.         printf("\n");
    144.     }
    145.     printf("\n你的得分:%d\n\n", score);
    146.     printf("操作说明: ASDW分别操作 左下右上四个的移动\n");
    147.     printf("**空格是发出子弹**\n");
    148. }
    149. int main()
    150. {
    151.     starup();
    152.     while (1)
    153.     {
    154.         HideCursor();
    155.         startMap();
    156.         show();
    157.         updateWithoutInput();
    158.         updateWithInput();
    159.     }
    160.     return 0;
    161. }
    复制代码


    注意107行和109行的kbhit()和getch()

    本帖子中包含更多资源

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

    x
    该会员没有填写今日想说内容.
    高级模式
    B Color Image Link Quote Code Smilies

    本版积分规则