与C的蛇游戏

时间:2015-12-29 19:36:45

标签: c

以下用C语言编写的蛇游戏代码。 在这种情况下,我们希望从最初的情况下由4颗星组成的蛇开始 (****)并且当比赛开始时蛇会自动向右移动。我在这段代码中进行了试验,但是我看到蛇以3颗星的形式开始,当我按下任何一个方向按钮时,它变成了一颗4星级的蛇。如何解决这个问题?

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
bool gameOver;
bool hit = false;
const width = 50;
const height = 20;
int x,y,fruitX,fruitY,score;
int tailX[200],tailY[200];
int ntail=3;
char snake[200];
   typedef enum  {STOP = 0,LEFT,RIGHT,UP,DOWN} Direction;
Direction Dir;
void menu(){
    int a;
    printf("\n\n\n\n\n\n");
    printf("                              THE SNAKE GAME                      \n\n\n");
    printf("                              1)   NEW GAME                       \n");
    printf("                              2GAME WITH BORDERS                 \n");
    printf("                              3.QUIT THE GAME                 \n\n");
    printf("        Please enter your choice: ");
    scanf("%d", &a);
}
// This function is used to initialize(reset the game when it begins
void setup(){
// This function will be used to print the snake in each of its different cases inside its fame
    gameOver = false;
    Dir = STOP;
    x= width/2;
    y= height/2;
    fruitX = rand()%width;
    fruitY = rand()%height;
    score = 0;
}
void draw(){
// This is used to take inputs when we press on any key on the keyboard and send it to the logic function to be interpreted
// This is used to
    system("cls");
    printf("Score:%d",score);
    printf("\n");
    int i;
    for(i= 0 ;i<width+1;i++){ // first row
        printf("=");
    }
    printf("\n");
    int p;
    for(p= 0 ;p<height;p++){
        int q;
        for(q= 0 ;q<width;q++){
            if(q==0 || q==width-1){ // first last elt
                printf("!");
            }
            if(p == y && q == x){//head coordinates
                printf("*");

            }else if(p == fruitY && q == fruitX){
                printf("#");
            }else{
                    int k=0;
                    bool print = false;
                    for(k=0;k<ntail;k++){
                        if(tailX[k]==q && tailY[k]==p){
                            printf("*");
                            print = true;
                        }
                    }
                    if(!print){printf(" ");}
            }
        }
        printf("\n");
    }

    int j;
    for(j= 0 ;j<width+1;j++){
        printf("=");
    }

}
void input(){
// The logic function
    if(_kbhit()){
        switch(_getch()){
            case '4':
                Dir = LEFT;
                hit= true;
                break;
            case '8':
                Dir = UP;
                hit= true;
                break;
            case '6':
                Dir = RIGHT;
                hit= true;
                break;
            case '2':
                Dir = DOWN;
                hit= true;
                break;
            case 'x':
                gameOver = true;
                break;
        }
    }else if(!hit){
        x++;
    }
}
void logic(){
// main function

    int lastX = tailX[0]; // The one before 0
    int lastY = tailY[0];
    int last2X, last2Y;
    tailX[0]=x;
    tailY[0]=y;
    int i=0;
    for(i=1; i<ntail;i++){
        last2X = tailX[i];// 2 takes place of 1
        last2Y = tailY[i];
        tailX[i]=lastX; // 1 takes place of 0
        tailY[i]=lastY;
        lastX = last2X; // 1 takes place of 1
        lastY = last2Y;
    }
    switch(Dir){
            case UP:
                y--;
                break;
            case DOWN:
                y++;
                break;
            case LEFT:
                x--;
                break;
            case RIGHT:
                x++;
                break;

        }
    if(x<0 || width<x || y<0 || height<y){
        gameOver = true;
        system("cls");
        printf("#******#######################****# GAME OVER #***#######################******#");
    }
    if(x==fruitX && y==fruitY){
        ntail++;
        score+=10;
        fruitX = rand()%width;
        fruitY = rand()%height;
    }
}
int main(){
    menu();

    setup();
    draw();
    while(!gameOver){// each time we repeat a new picture will new drawn and we will see the snake in its new state
        draw();
        input();
        logic();
    }

    return 0;
}

1 个答案:

答案 0 :(得分:3)

  

我在这段代码中进行了试验,但我看到蛇开始了   形式的3星

根据tailX[0]=x; tailY[0]=y;,您使用tailX[0]tailY[0]作为头部位置。你初始化int ntail=3;并从1迭代到3以下(for(i=1; i<ntail;i++),所以你的尾巴的长度为2.总之,你的蛇的开头是3个元素。

  

当我按任意一个方向按钮时,它变为4星   蛇

第一次按下按钮DIR != STOP后,在设置xy后增加或减少tailXtailY 。现在x != tail[0]y != tail[0]和你的蛇的总长度为4。

我的解决方案是移动你从函数x增加input的其他情况并将其放入函数logic

void input(){
    if(_kbhit()){
        switch(_getch()){
            case '4': Dir = LEFT;  break;
            case '8': Dir = UP;    break;
            case '6': Dir = RIGHT; break;
            case '2': Dir = DOWN;  break;
            case 'x': gameOver = true; break;
        }
    }
    // from here ---->
}

void logic(){
    for(int i=ntail-1; i>0;i--){
        tailX[i]=tailX[i-1];
        tailY[i]=lastY[i-1];
    }
    tailX[0]=x;
    tailY[0]=y;
    switch(Dir){
            case UP:    y--; break;
            case DOWN:  y++; break;
            case LEFT:  x--; break;
            case RIGHT: x++; break;

            case STOP:  x++; break; // <----- to here    
        }
   ...
}