programming languageTutorials
Trending

How to create a simple snake game with C++ Programming Language

To create a simple snake game using C++ programming language, you can follow these steps:

Step 1: Set up the game environment

  • Include the necessary libraries such as <iostream>, <conio.h>, and <windows.h> to allow for user input and to manipulate console screen.
  • Create the necessary variables to store the game’s data such as the snake’s position and length, the food’s position, the score, and the game’s speed.

Step 2: Create the game loop

  • Use a while loop to keep the game running until the player wins or loses.
  • Inside the loop, update the snake’s position based on user input and move the snake accordingly.
  • Check for collisions between the snake’s head and the food or the walls.
  • If the snake eats the food, add points to the score and generate a new food.
  • If the snake hits the wall or its own body, end the game.

Step 3: Draw the game screen

  • Use the console window to draw the game screen, including the snake, the food, and the score.
  • Use the Sleep() function from <windows.h> to control the game’s speed.

Here is an example code for a simple snake game:

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

// Game settings
const int width = 20;
const int height = 20;
int score = 0;
bool gameOver = false;

// Snake settings
int snakeX[100], snakeY[100];
int snakeLength = 1;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection direction;

// Food settings
int foodX, foodY;

// Console screen functions
void Setup()
{
    direction = STOP;
    snakeX[0] = width / 2;
    snakeY[0] = height / 2;
    foodX = rand() % width;
    foodY = rand() % height;
}

void Draw()
{
    system("cls");
    // Draw top wall
    for (int i = 0; i < width + 2; i++)
        cout << "#";
    cout << endl;

    // Draw side walls and snake
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (j == 0)
                cout << "#";
            if (i == snakeY[0] && j == snakeX[0])
                cout << "O";
            else if (i == foodY && j == foodX)
                cout << "F";
            else
            {
                bool print = false;
                for (int k = 1; k < snakeLength; k++)
                {
                    if (snakeX[k] == j && snakeY[k] == i)
                    {
                        cout << "o";
                        print = true;
                    }
                }
                if (!print)
                    cout << " ";
            }

            if (j == width - 1)
                cout << "#";
        }
        cout << endl;
    }

    // Draw bottom wall
    for (int i = 0; i < width + 2; i++)
        cout << "#";
    cout << endl;

    // Draw score
    cout << "Score: " << score << endl;
}

void Input()
{
    if (_kbhit())
    {
        switch (_getch())
        {
        case 'a':
            direction = LEFT;
            break;
        case 'd':
            direction = RIGHT;
            break;
        case 'w':
            direction = UP;
            break;
        case 's

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button