Snake Game

Computer Science | Artificial Intelligence | Computational Biology | Chess | Blender

Snake Game

This is a simple console-based Snake game developed in C++. The game is automated moving the letter 'o' around the screen to eat food. The game ends when the snake runs into the letter 'T'. It's not a typical snake game though, but this is how far I can go.

The additional libraries except 'iostream' are just my revision that they exist so you cn do without them.

Snake Game Code

#include <iostream>
#include <ctime>
#include <cmath>
#include <termios.h>
#include <unistd.h>
#include <thread>
#include <chrono>
#include <vector>
bool gameOver = false;

class Game{
    char fruit = '*';
    char head = 'O';
    int width = 60;
    int height = 40;
    int headX = width / 2;
    int headY = height / 2;
    int fruitX = rand() % width;
    int fruitY = rand() % height;
    int nTail = 0;
    enum direction {stop = 0,up,down,left,right};
    direction dir;
public:
    void setUp(){
        srand(time(NULL));  
        system("clear");
        dir = stop;
        for(int i = 0; i < width;i++){
            std::cout << "#";
        }
        std::cout << std::endl;
        for(int i = 0;i < height;i++){
            for(int j = 0;j < width;j++){
                if(j == 0){
                    std::cout << '!';
                }
                else if(j == width-1){
                    std::cout << '!';
                }
                else{
                    if(headY == i && headX == j){
                        std::cout << head;
                    }
                    else if(fruitY == i && fruitX == j){
                        std::cout << fruit;
                    }
                    else{
                        if(j == 5 && i == 5){
                            std::cout << 'T';
                        }
                        else if(j == 55 && i == 35){
                            std::cout << 'T';
                        }
                        else if(j == 55 && i == 5){
                            std::cout << 'T';
                        }
                        else if(j == 5 && i == 35){
                            std::cout << 'T';
                        }
                        else{
                            std::cout << ' ';
                        }
                    }
                }
            }
            std::cout << std::endl;
        }
        for(int i = 0;i < width;i++){
            std::cout << "#";
        }
        std::cout << std::endl;
        if(headX == width){
            headX = 0;
        }
        else if(headX == 5 && headY == 35){
            gameOver = true;
        }
        else if(headX == 55 && headY == 5){
            gameOver = true;
        }
        else if(headX == 55 && headY == 35){
            gameOver = true;
        }
        else if(headX == 5 && headY == 5){
            gameOver = true;
        }
        else if(headY == height){
            headY = 0;
        }
        else if(headX < 0){
            headX = width;
        }
        else if(headY < 0){
            headY = height;
        }
        else if(headX == fruitX && headY == fruitY){
            fruitX = rand() % width;
            fruitY = rand() % height;
            nTail ++;
        }
    }
    void input(){
        int yDir = (fruitY - headY);
        int xDir = (headX - fruitX);
        if(fruitX < headX){
            dir = right;
        }
        else if(fruitX > headX){
            dir = left;
        }
        else if(fruitY < headY){
            dir = up;
        }
        else if(fruitY > headY){
            dir = down;
        }
        else if((fruitX == headX) && (fruitY > headY)){
            dir = up;
        }
        else if((fruitX == headX) && (fruitY < headY)){
            dir = down;
        }
        else if((fruitY == headY) && (fruitX > headX)){
            dir = left;
        }
        else if((fruitY == headY) && (fruitX < headX)){
            dir = right;
        }
        else{
            dir = stop;
        }
    }
    void logic(){
        switch(dir){
            case up: headY--;
                break;
            case down: headY++;
                break;
            case left: headX++;
                break;
            case right: headX--;
                break;
            default:
                break;
        }
    }
};
int main() {
    Game snakeGame;
    while(!gameOver){
        std::this_thread::sleep_for(std::chrono::milliseconds(15));
        snakeGame.setUp();
        snakeGame.input();
        snakeGame.logic();
    }
    return 0;
}