簡體   English   中英

如何在終端中自動執行代碼

[英]How to automatically execute code in terminal

因此,我用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;
}

最簡單的方法是使用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++;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM