📜  C蛇游戏

📅  最后修改于: 2021-05-30 17:13:11             🧑  作者: Mango

在本文中,任务是实现基本的Snake Game 下面给出了该游戏的一些功能:

  • 蛇用0 (零)符号表示。
  • 水果用* (星号)符号表示。
  • 借助键盘( WASD键),蛇可以根据用户向任何方向移动。
  • 当蛇吃水果时,分数将增加10分。
  • 水果将在边界内自动生成。
  • 每当蛇接触边界时,游戏就结束了。

创建此游戏的步骤

  • 将有四个用户定义的功能。
  • 建立一个可以玩游戏的边界。
  • 果实是随机产生的。
  • 然后,每当蛇吃水果时就增加分数。

用户自定义 该程序中创建的功能如下:

  • Draw():此函数创建将在其中玩游戏的边界。
  • Setup():此函数将设置水果在边界内的位置。
  • Input():此函数将从键盘上获取输入。
  • Logic():此函数将设置蛇的运动。

使用的内置功能

  • kbhit(): C中的此函数用于确定是否已按下某个键。要在程序中使用此函数,请包含头文件conio.h 。如果已按下某个键,则它返回一个非零值,否则返回零。
  • rand(): rand()函数在stdlib.h中声明。每次调用它都会返回一个随机整数值。

头文件和变量

  • 该程序中使用的头文件和变量是:

  • 这里包括sleep()头文件 函数。

Draw():此函数负责构建将要玩游戏的边界。

以下是使用draw()构建轮廓边界的C程序:

C
// C program to build the outline
// boundary using draw()
#include 
#include 
int i, j, height = 30;
int width = 30, gameover, score;
  
// Function to draw a boundary
void draw()
{
    // system("cls");
    for (i = 0; i < height; i++) {
        for (j = 0; j < width; j++) {
            if (i == 0 || i == width - 1 || j == 0
                || j == height - 1) {
                printf("#");
            }
            else {
                printf(" ");
            }
        }
        printf("\n");
    }
}
  
// Driver Code
int main()
{
    // Function Call
    draw();
  
    return 0;
}


C
// C program to build the complete
// snake game
#include 
#include 
#include 
#include 
  
int i, j, height = 20, width = 20;
int gameover, score;
int x, y, fruitx, fruity, flag;
  
// Function to generate the fruit
// within the boundary
void setup()
{
    gameover = 0;
  
    // Stores height and width
    x = height / 2;
    y = width / 2;
label1:
    fruitx = rand() % 20;
    if (fruitx == 0)
        goto label1;
label2:
    fruity = rand() % 20;
    if (fruity == 0)
        goto label2;
    score = 0;
}
  
// Function to draw the boundaries
void draw()
{
    system("cls");
    for (i = 0; i < height; i++) {
        for (j = 0; j < width; j++) {
            if (i == 0 || i == width - 1
                || j == 0
                || j == height - 1) {
                printf("#");
            }
            else {
                if (i == x && j == y)
                    printf("0");
                else if (i == fruitx
                         && j == fruity)
                    printf("*");
                else
                    printf(" ");
            }
        }
        printf("\n");
    }
  
    // Print the score after the
    // game ends
    printf("score = %d", score);
    printf("\n");
    printf("press X to quit the game");
}
  
// Function to take the input
void input()
{
    if (kbhit()) {
        switch (getch()) {
        case 'a':
            flag = 1;
            break;
        case 's':
            flag = 2;
            break;
        case 'd':
            flag = 3;
            break;
        case 'w':
            flag = 4;
            break;
        case 'x':
            gameover = 1;
            break;
        }
    }
}
  
// Function for the logic behind
// each movement
void logic()
{
    sleep(0.01);
    switch (flag) {
    case 1:
        y--;
        break;
    case 2:
        x++;
        break;
    case 3:
        y++;
        break;
    case 4:
        x--;
        break;
    default:
        break;
    }
  
    // If the game is over
    if (x < 0 || x > height
        || y < 0 || y > width)
        gameover = 1;
  
    // If snake reaches the fruit
    // then update the score
    if (x == fruitx && y == fruity) {
    label3:
        fruitx = rand() % 20;
        if (fruitx == 0)
            goto label3;
  
    // After eating the above fruit
    // generate new fruit
    label4:
        fruity = rand() % 20;
        if (fruity == 0)
            goto label4;
        score += 10;
    }
}
  
// Driver Code
void main()
{
    int m, n;
  
    // Generate boundary
    setup();
  
    // Until the game is over
    while (!gameover) {
  
        // Function Call
        draw();
        input();
        logic();
    }
}


输出:
##############################
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
##############################

setup(): nThisbfunction用于编写代码,以使用rand()函数在边界内生成水果。

  • 使用rand()%20是因为边界的大小为length = 20width = 20,因此水果将在边界内生成。

Input():在此函数,程序员编写代码以从键盘(W,A,S,D,X键)获取输入。

logic():在这里,编写该程序的所有逻辑,例如蛇的移动,增加分数,当蛇触及边界时游戏将结束,退出游戏并随机生成水果一旦蛇会吃掉水果。

sleep(): C语言中的此函数是将程序执行延迟给定秒数的函数。在此代码中,sleep()用于减慢蛇的运动,因此用户可以轻松玩耍。

main():从main()函数开始执行程序。它调用所有功能。

下面是构建完整的蛇游戏的C程序:

C

// C program to build the complete
// snake game
#include 
#include 
#include 
#include 
  
int i, j, height = 20, width = 20;
int gameover, score;
int x, y, fruitx, fruity, flag;
  
// Function to generate the fruit
// within the boundary
void setup()
{
    gameover = 0;
  
    // Stores height and width
    x = height / 2;
    y = width / 2;
label1:
    fruitx = rand() % 20;
    if (fruitx == 0)
        goto label1;
label2:
    fruity = rand() % 20;
    if (fruity == 0)
        goto label2;
    score = 0;
}
  
// Function to draw the boundaries
void draw()
{
    system("cls");
    for (i = 0; i < height; i++) {
        for (j = 0; j < width; j++) {
            if (i == 0 || i == width - 1
                || j == 0
                || j == height - 1) {
                printf("#");
            }
            else {
                if (i == x && j == y)
                    printf("0");
                else if (i == fruitx
                         && j == fruity)
                    printf("*");
                else
                    printf(" ");
            }
        }
        printf("\n");
    }
  
    // Print the score after the
    // game ends
    printf("score = %d", score);
    printf("\n");
    printf("press X to quit the game");
}
  
// Function to take the input
void input()
{
    if (kbhit()) {
        switch (getch()) {
        case 'a':
            flag = 1;
            break;
        case 's':
            flag = 2;
            break;
        case 'd':
            flag = 3;
            break;
        case 'w':
            flag = 4;
            break;
        case 'x':
            gameover = 1;
            break;
        }
    }
}
  
// Function for the logic behind
// each movement
void logic()
{
    sleep(0.01);
    switch (flag) {
    case 1:
        y--;
        break;
    case 2:
        x++;
        break;
    case 3:
        y++;
        break;
    case 4:
        x--;
        break;
    default:
        break;
    }
  
    // If the game is over
    if (x < 0 || x > height
        || y < 0 || y > width)
        gameover = 1;
  
    // If snake reaches the fruit
    // then update the score
    if (x == fruitx && y == fruity) {
    label3:
        fruitx = rand() % 20;
        if (fruitx == 0)
            goto label3;
  
    // After eating the above fruit
    // generate new fruit
    label4:
        fruity = rand() % 20;
        if (fruity == 0)
            goto label4;
        score += 10;
    }
}
  
// Driver Code
void main()
{
    int m, n;
  
    // Generate boundary
    setup();
  
    // Until the game is over
    while (!gameover) {
  
        // Function Call
        draw();
        input();
        logic();
    }
}

输出:

示范:

想要从精选的最佳视频中学习和练习问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”