搜索
查看: 960|回复: 6
打印 上一主题 下一主题

贪吃贪吃弹出

[复制链接]
跳转到指定楼层
楼主
发表于 2017-11-8 21:56:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
5啊哈币
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <windows.h>
//To-do list:
// 1. Develop the function that player key pressed respond result:done
// 2. Develop the core code of the game result:done
//  2.1 Solve the growth of the snake problem; result:done
//  2.2 Solve the the snake hit the wall or eat itself,quit the game problme  result:done
// 3. Develop the user interface for the game
//     3.1 Entrance interface result:done
//  3.2 Introduction interface result:done
//    3.3 Exit interface result:done
//     3.4 Pause interface
// 4. Free the map variable when the game is over. result:done;
// 5. Study the Sleep function to pause the runing thread.
struct {
    int Pause;
    int GameStarted;
    int GameOver;
    int Difficulty;
    int ScreenWidth, ScreenHeight;
    int **map;
}game;

struct{
    int sLong;
    char head[4];
    int *CoordX;
    int *CoordY;
    int moveX;
    int moveY;
    int direction;
}snake;

struct{
    int fCoordX;
    int fCoordY;
    int reput;
}food;

struct{
    int keypressed;
    int choice;
    int quit;
    int menuBack;
    int leave;
}player;

void GameEnvironmentSetting();
void SnakeInfoSetting();
void FoodInfoSetting();
void PlayerInfoSetting();
void LoadMainEntrance();
void MStartGame();
void SetDifficulty();
void MExitGame();
void GReStartGame();
void LoadPauseStatusEntrance();

int main(){
    GameEnvironmentSetting();
    SnakeInfoSetting();
    FoodInfoSetting();
    MStartGame();
    // while(!player.leave){
    //     LoadMainEntrance();
    //     GainPlayerInput();
    // };
    system("pause");
    return 0;
}
void GainPlayerInput(){
    void (*menuArray[])() ={MStartGame, SetDifficulty, MExitGame};
    char menu[3][20] = {"StartGame","SetDifficulty","ExitGame"};
    PlayerMakesChoice(menu, menuArray);
}
void PlayerMakesChoice(char menu[][20],void(*menuArray[3])()){
    int i,count, menuCount;
    char point[4] = "--->";
    menuCount = sizeof(menu);
    count = 0;
    while(!player.menuBack){
        for(i=0;i<menuCount;i++){
            if (i==count) printf("%s", point);
            else printf("    ");
            printf("%-5s\n", menu[i]);
        }
        player.keypressed = getch();
        if(player.keypressed == 0xe0) player.keypressed = getch();   
        switch(player.keypressed){
            case 72: if(count>0) count-=1; break;
            case 80: if(count<menuCount-2) count+=1; break;
            case 13: player.choice = 1;
        }
        if(player.choice){
            Sleep(100);
            menuArray[count]();
            player.choice = 0;
        }
    }
}
void LoadMainEntrance(){
    printf("\n");
    printf("             @@                                @@           \n");
    printf("           @@  @@                              @@           \n");
    printf("         @@@@    @@                         @@              \n");
    printf("       @@    @@    @@@@         @@@@@@@@    @@@@@@@@@@@@@@@ \n");
    printf("     @@  @@@@@@@@@@    @@@@@@   @@    @@  @@                \n");
    printf(" @@@@            @@     @@      @@    @@@@                  \n");
    printf("               @@               @@    @@  @@@@@@@@@@@@@@    \n");
    printf("     @@@@@@@@@@@@@@@@@@         @@    @@            @@      \n");
    printf("     @@              @@         @@    @@          @@        \n");
    printf("     @@      @@      @@         @@    @@        @@          \n");
    printf("     @@      @@      @@         @@@@@@@@      @@            \n");
    printf("     @@    @@        @@         @@    @@    @@              \n");
    printf("         @@    @@@@                       @@              @@\n");
    printf("      @@           @@@@                   @@              @@\n");
    printf("  @@@@                 @@                   @@@@@@@@@@@@@@  \n\n\n\n");
    printf("Use the Arrow key to Control The Direction\n");
}
void SetDifficulty(){
    printf("Press Up or Down Arrow to choose Difficulty\n");
    printf("Press Esc to go back\n");
    int back = 0;
    int LastDifficulty = game.Difficulty;
    int adjust = 0.5;

    while(!back){
        player.keypressed = getch();
        if(player.keypressed == 0xe0) player.keypressed = getch();
        switch(player.keypressed){
            case 72: game.Difficulty=5  ; if(LastDifficulty+adjust <5) game.Difficulty = LastDifficulty+adjust; break;
            case 80: game.Difficulty=0.5; if(LastDifficulty-adjust >0.5) game.Difficulty=LastDifficulty-adjust; break;
            case 27: back = 1; break;
        }
    }
}
void LoadPauseStatusEntrance(){
    void (*menuArray[3]) ={GReStartGame, SetDifficulty, MExitGame};
    char menu[3][20] = {"StartGame","SetDifficulty","ExitGame"};
    PlayerMakesChoice(menu, menuArray);

}
void GReStartGame(){
    snake.sLong = 3;
    food.reput = 1;
    GNewSnakeBirth(snake.sLong, 2 + rand() % (game.ScreenWidth-4), 2 + rand() % (game.ScreenHeight-4));
    GPutFood();
    MStartGame();
}
void GameEnvironmentSetting(){
    int row,col;
    game.GameStarted = 1;
    game.GameOver = 0;
    game.ScreenWidth = 20;
    game.ScreenHeight = 20;
    game.Difficulty = 1.5;
    system("mode con cols=61 lines=35");
    system("color f0");
    game.map = (int **)malloc(sizeof(int)*game.ScreenWidth);
    for(row=0;row<game.ScreenWidth;row++){
        game.map[row]=(int*)malloc(sizeof(int)*game.ScreenHeight);
    };
    GInitiateMap();
}
void GInitiateMap(){
    int row, col;
    for(row=0;row<game.ScreenHeight;row++){
        for(col=0;col<game.ScreenWidth;col++){
            game.map[row][col]=0;
            if(row==0 || row==game.ScreenHeight-1 || col==0 || col==game.ScreenWidth-1){
                game.map[row][col]=1;
            }
        }
    }
}
void SnakeInfoSetting(){
    int i;
    snake.sLong = 3;
    char temp[4] = {'^','V','<','>'};
    memcpy(snake.head,temp, 4*sizeof(char));
    srand((unsigned)time(NULL));
    snake.CoordX = (int*)malloc(sizeof(int)*game.ScreenWidth);
    snake.CoordY = (int*)malloc(sizeof(int)*game.ScreenHeight);
    snake.direction = rand() % 3;
    switch(snake.direction){
        case 0: snake.moveX = 1; snake.moveY = 0; break; //create snake right
        case 1: snake.moveX =-1; snake.moveY = 0; break; //create snake up
        case 2: snake.moveX = 0; snake.moveY = 1; break; //create snake left
        case 3: snake.moveX = 0; snake.moveY =-1; break; //create snake down
    }
    GNewSnakeBirth(snake.sLong, 2 + rand() % (game.ScreenWidth-4), 2 + rand() % (game.ScreenHeight-4));
}
void GNewSnakeBirth(int sLong, int headx,int heady){
    int i;
    for(i=0;i<snake.sLong;i++){
        if(i==0){
            snake.CoordX[i] = headx;
            snake.CoordY[i] = heady;
            game.map[snake.CoordX[i]][snake.CoordY[i]] = 2;
            continue;
        }
            snake.CoordX[i] = snake.CoordX[i-1] + snake.moveX;
            snake.CoordY[i] = snake.CoordY[i-1] + snake.moveY;
            game.map[snake.CoordX[i]][snake.CoordY[i]] = 3;
    }
}
void FoodInfoSetting(){
    food.reput = 1;
    GPutFood();
}
void PlayerInfoSetting(){
    player.choice = 0;
    player.quit = 0;
    player.leave = 0;
    player.menuBack = 0;
    player.keypressed = NULL;
}

void MStartGame(){
    game.GameStarted = 1;
    while(game.GameStarted){
        GDraw();
        player.keypressed = getch();
        if(player.keypressed == 0xe0) player.keypressed = getch();
        GMoveSnake();
        GPutFood();
        Sleep(50);
        fflush(stdin);
        if(player.keypressed == 27) LoadPauseStatusEntrance();
    }
}

void GDraw(){
    int row, col;
    for(row=0; row<game.ScreenHeight;row++){
        for(col=0; col<game.ScreenWidth;col++){
            switch(game.map[row][col]){
                case 1: printf("#"); break;
                case 2: printf("%c",snake.head[snake.direction]); break;
                case 3: printf("*"); break;
                case 4: printf("f"); break;
                default: printf(" "); break;
            }
            if(col==game.ScreenWidth-1) printf("\n");
        }
    }
}

void GMoveSnake(){
    int i, changeDirect, sLong, headx, heady;
    int move[4][2] = {
        {-1, 0}, //MOVE UP
        { 1, 0}, //MOVE DOWN
        { 0,-1}, //MOVE LEFT
        { 0, 1}  //MOVE RIGHT
    };
    switch(player.keypressed){
        //ESC ascii is 27
        case 72: changeDirect = 0; break;        //Up arrow ascii is 72
        case 80: changeDirect = 1; break;        //Down arrow ascii is 80
        case 75: changeDirect = 2; break;        //Left arrow ascii is 75
        case 77: changeDirect = 3; break;        //Right arrow ascii is 77
    }
    sLong = snake.sLong;
    headx = snake.CoordX[0];
    heady = snake.CoordY[0];
    if (game.map[headx+move[changeDirect][0]][heady+move[changeDirect][1]]!=3)
    {
        snake.direction = changeDirect;
    }
    if(headx+move[snake.direction][0] == food.fCoordX && heady+move[snake.direction][1] == food.fCoordY){
        snake.sLong++;
        game.map[food.fCoordX][food.fCoordY] = 0;
        food.reput = 1;
    }
    if(game.map[headx+move[snake.direction][0]][heady+move[snake.direction][1]]==1){
        game.GameOver = 1;
        return;
    }
    headx = headx+move[snake.direction][0];
    heady = heady+move[snake.direction][1];
    for(i=snake.sLong-1;i>=0;i--){
        if(i==snake.sLong-1){
            game.map[snake.CoordX[i]][snake.CoordY[i]] = 0;
        }
        if(i==0){
            snake.CoordX[i] = headx;
            snake.CoordY[i] = heady;
            game.map[snake.CoordX[i]][snake.CoordY[i]] = 2;
            break;
        }
        snake.CoordX[i] = snake.CoordX[i-1];
        snake.CoordY[i] = snake.CoordY[i-1];
        game.map[snake.CoordX[i]][snake.CoordY[i]] = 3;
    }
}

void GPutFood(){
    srand((unsigned)time(NULL));
    while(food.reput){
        food.fCoordX= 2 + rand() % (game.ScreenWidth-1);
        food.fCoordY= 2 + rand() % (game.ScreenHeight-1);
        if (game.map[food.fCoordX][food.fCoordY] == 0){
            game.map[food.fCoordX][food.fCoordY] = 4;
            food.reput = 0;
        }
    }
}

void MExitGame(){
    GClearGameVariable();
    player.leave = 1;
    player.menuBack = 1;
    return;
}
void GClearGameVariable(){
    int i,row;
    if(snake.CoordX == NULL && snake.CoordY == NULL &  game.map == NULL) return;
    for(i=0;i<snake.sLong;i++){
        if(snake.CoordX!=NULL) free(snake.CoordX);
        if(snake.CoordX!=NULL) free(snake.CoordY);
    }
    for(row=0;row<game.ScreenWidth;row++){
        if(game.map[row]!=NULL) free(game.map[row]);
    }
    if(game.map!=NULL) free(game.map);
    game.map = snake.CoordX = snake.CoordY = NULL;
}这是别人的,我不知道哪里错了??

最佳答案

查看完整内容

在啊哈C上试了一下貌似没问题??????????(编辑时间长达一小时的评论。。。)
沙发
发表于 2017-11-8 21:56:53 | 只看该作者
在啊哈C上试了一下貌似没问题??????????(编辑时间长达一小时的评论。。。)
板凳
发表于 2017-11-9 12:40:09 | 只看该作者
你编写的??????????????????????????????????
地板
 楼主| 发表于 2017-11-9 20:42:12 | 只看该作者
不是,是别人编写的.
5#
发表于 2017-11-30 13:02:10 | 只看该作者
完全看不懂
6#
发表于 2017-12-9 13:03:34 | 只看该作者
。。。。。。。。。。。。。。。。。。。。。。。。。。
7#
发表于 2017-12-9 14:03:15 | 只看该作者
就是才一个画面。。。。。。。。。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表