【问题标题】:How to automatically execute code in terminal如何在终端中自动执行代码
【发布时间】:2016-03-02 20:48:59
【问题描述】:

所以我用 c 语言构建了这个非常简单的游戏,你是一艘船,需要避免子弹从屏幕上升起。一旦我在终端运行它,每次我想移动船时,我都会输入(左)s(直)或 d(右)。我想知道是否有一种方法可以在每次移动后不强制使用回车键,而是让子弹像每秒一个空格一样逐渐上升或自动升起。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#define height 20
#define width 15

int main (int argc, char *argv[]) {
    printf("Beginning Game\n");
    printf("Press 's' to start\n");
    char move = 0;
    int count = 6;
    int turn = 0;
    int row = 0;
    int col = 0;
    int left = 0;
    int right = 14;
    srand(time(NULL));
    int shootcol =  rand() % (width - 3) + 2 ;
    int shootrow = height + 1;
    int shoot2col = rand() % (width - 3) + 2 ;
    int shoot2row = height + 15; 



while (move != 'e') {
    printf("\n");
    scanf(" %c", &move);
    if (move == 's') {
        turn++;
    }
    else if (move == 'a') {
        count--;
        turn++;

    } else if (move == 'd') {
        count++;
        turn++;

    } 
    row = 0;
    col = 0;
    printf("TURN: %d\n", turn);
    while (row < height) {
        while (col < width) {
            //printf("%d", col );
            if (((row == shootrow) && (col == shootcol)) || ((row == shoot2row) && (col == shoot2col))) {
                printf(":");


            } else if (col == left) {

                printf("|");


            } else if (col == right) {
                printf("|");

            } else if (row < 5) {
                printf(" ");

            } else if (row == 5) {

                if (col < count) {
                    printf(" ");
                } else if (col == count) {
                    printf("|");

                } else if (col == count + 1) {
                    printf("-");

                } else if (col == count + 2) {
                    printf("|");

                } else if (col >= count + 3)    {
                    printf(" ");
                }
            } else if (row == 6) {

                if (col < count) {
                    printf(" ");
                } else if (col == count) {
                    printf("\\");

                } else if (col == count + 1) {
                    printf(" ");

                } else if (col == count + 2) {
                    printf("/");

                } else if (col >= count + 3)    {
                    printf(" ");
                }

            } else {
                printf(" ");

            }
            col++;

        }
        col = 0;
        printf("\n");
        row++;

    }
    shootrow--;
    shoot2row--;
    if ((count == left) || (count == right - 2)) {
        printf("YOU LOSE!!\n");
        move = 'e';
    } else if ((shootrow == 5) || (shootrow == 4)) {
        if ((count == shootcol) || (count == shootcol - 2 ) || (count == shootcol - 1)) {
            printf("YOU LOSE!!\n");
            move = 'e';
        } if ((count == shoot2col) || (count == shoot2col - 2 ) || (count == shoot2col - 1)) {
            printf("YOU LOSE!!\n");
            move = 'e';
        }

    } if (shootrow <= 0) {
        shootrow = height - 1;
        shootcol =  rand() % (width - 3) + 2 ;
    } if (shoot2row <= 0) {
        shoot2row = height - 1;
        shoot2col = rand() % (width - 3) + 2;
    }

}

return 0;
}

【问题讨论】:

  • 检测到按键(非阻塞):LinuxWindows。您可以将sleep (windows, linux) 添加到您的while
  • 在标准 C 中没有办法。您将不得不使用非标准方法 - 这将特定于您的目标环境、编译器等。例如 Kenney 提到的那些。
  • 我使用的是 mac,所以有什么方法可以在 mac 上做到这一点,而且我之前没有见过 sleep 功能,我将在哪里以及在我的代码中添加什么。谢谢

标签: c time time.h


【解决方案1】:

最简单的方法是使用 conio 库

#include <conio.h>
#define DELAY 300    // define here the amount of milliseconds to sleep

如果你使用的是 linux 或 mac

#include <unistd.h>

如果您使用的是 Windows

#include <windows.h>

这就是你的循环开始的样子

while (move != 's')
    scanf(" %c", &move);

move = ' ';

while (move != 'e') {
    printf("\n");

    //Sleep(DELAY);    // windows only sleep 's' lowercase for linux
    usleep(DELAY);     // for mac

    if (_kbhit())
    {
        move = _getch();
    }

    if (move == 's') {
        turn++;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 2015-01-19
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 2012-09-02
    • 2012-10-30
    相关资源
    最近更新 更多