诗岑 2020-09-26 15:14 采纳率: 93%
浏览 331
已采纳

【C语言】飞机大战怎样控制速度

如题,光标下落过快,修改speed值后仍是这样

#include<stdio.h>
#include<Windows.h>
#include<conio.h>
int height, width;
int plane_x, plane_y;//飞机位置
int bullet_x, bullet_y;//子弹位置
int score;
int flag;
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 start() {
    height = 18, width = 26;
    plane_x = height - 3;
    plane_y = width - 2;
    bullet_x = 0;
    bullet_y = plane_y;
    score = 0;
    flag = 0;
}
void show() {
    int i, j;
    for (i = 0;i < height;i++) {
        for (j = 0;j < width;j++) {
            if (flag) {
                break;
            }
            else if (i == plane_x && j == plane_y) {
                printf("*");
            }
            else if (i == bullet_x && j == bullet_y) {
                printf("&");
            }
            else {
                printf(" ");
            }

        }
        printf("\n");
    }
    printf("\n");
    if (plane_x == bullet_x && plane_y == bullet_y) {
        flag = 1;
        printf("GAME OVER");
    }
    else {
        printf("YOU WIN!");
    }
}
void bulletMove() {
    //控制速度
    static int speed;
    if (speed < 3) {
        speed++;
    }
    if (speed == 3) {
        if (bullet_x < height) {
            bullet_x++;
        }
        else {
            bullet_x = 0;
            bullet_y = 2 + rand() % width - 2;
        }

    }
}speed = 0;
void planeMove() {
    char input;
    if (_kbhit()) {
        input = _getch();
        if (input == 'w' && plane_x >= 1) {
            plane_x--;
        }
        if (input == 's' && plane_x< height - 2) {
            plane_x++;
        }
        if (input == 'a' && plane_y > 1) {
            plane_y--;
        }
        if (input == 'd' && plane_y < width - 2) {
            plane_y++;
        }
    }
}
int main() {
    system("color 2f");
    start();
    while (1) {
        gotoxy(0, 0);
        show();
        bulletMove();
        planeMove();
    }
}
  • 写回答

1条回答 默认 最新

  • dabocaiqq 2020-09-26 15:55
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 9月27日

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看