I made a snake video game in c++

in #programming7 years ago

snake video game.PNG

I started programming year ago.

sn ake video game 2.PNG

I didn't know what to program during the holidays so I tried to make this video game.

I did it!

snake video game 3.PNG

If You have some ideas for practicing tell me :)

There are some bugs.
I will fix it as soon as possible.

Here is the code:

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <concrt.h>
using namespace std;
bool gameover;
const int height = 20;
const int width = 20;
int x, y, fx, fy, score;
int tailx[100], taily[100];
int tail;
enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirecton dir;
void setup()
{
    gameover = false;
    x = width / 2;
    y = height / 2;
    fx = rand() % width;
    fy = rand() % height;
    score = 0;

}
void draw()
{
    system("cls");
    for (int i = 0; i < width+2; i++)
        cout << "-";
    cout << endl;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (j == 0)
                cout << "|";
            if (i==y&&j == x)
                cout << "O";
            else if (i == fy&&j == fx)
                cout << "%";
            else
            {
                bool print = false;
                for (int k = 0; k < tail; k++)
                {
                    if (tailx[k] == j&&taily[k] == i)
                    {
                        cout << "o";
                        print = true;
                    }
                }
                if(!print)
                    cout << " ";
            }
            if (j == width - 1)
                cout << "|";
        }
        cout << endl;
    }
    for (int i = 0; i < width+2; i++)
        cout << "-";
    cout << endl;
    cout << "Score:" << score << endl;
}
void input()
{
    if (_kbhit())
    {
        switch(_getch())
        {
        case 'a':
            dir = LEFT;
            break;
        case 'd':
            dir = RIGHT;
            break;
        case 'w':
            dir = UP;
            break;
        case 's':
            dir = DOWN;
            break;
        case 'x':
            gameover = true;
            break;
        }
    }
}
void logic()
{
    int prevx = tailx[0];
    int prevy = taily[0];
    int prev2x, prev2y;
    tailx[0] = x;
    taily[0] = y;
    for (int i = 1; i < tail; i++)
    {
        prev2x = tailx[i];
        prev2y = taily[i];
        tailx[i] = prevx;
        taily[i] = prevy;
        prevx = prev2x;
        prevy = prev2y;
    }
    switch (dir)
    {
    case LEFT:
        x--;
        break;
    case RIGHT:
        x++;
        break;
    case UP:
        y--;
        break;
    case DOWN:
        y++;
        break;
    default:
        break;
    }
    if (x == width) 
        x = 0;
    else if (x < 0) 
        x = width - 1;
    if (y == height) 
        y = 0; 
    else if (y < 0) 
        y = height - 1;
    for (int i = 1; i <= tail; i++)
    {
        if (x == tailx[i] && y == taily[i])
            gameover = true;
    }
    
    if (x == fx && y == fy)
    {
        score += 10;
        fx = rand() % width;
        fy = rand() % height;
        tail++;
    }
}
int main()
{
    setup();
    while (!gameover)
    {
        draw();
        input();
        logic();
        /*sleep(10);*/
    }
    cout << "GAMEOVER!"<<endl;
    system("pause");
    return 0;
}

Coin Marketplace

STEEM 0.15
TRX 0.16
JST 0.028
BTC 67814.21
ETH 2401.94
USDT 1.00
SBD 2.34