snake

 avatar
unknown
c_cpp
5 months ago
4.3 kB
5
Indexable
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;

void Setup()
{
    gameOver = false; // Tro choi bat dau, chua ket thuc
    dir = STOP; // Ban dau ran dung yen
    x = width / 2; // Vi tri xuat phat cua dau ran o giua ban do
    y = height / 2;
    fruitX = rand() % width; // Vi tri ngau nhien cua trai cay
    fruitY = rand() % height;
    score = 0; // Diem so bat dau tu 0
    nTail = 0; // Do dai ban dau cua duoi la 0
}

void Draw()
{
    // Su dung ham nay de khong can xoa man hinh
    COORD cursorPosition;
    cursorPosition.X = 0;
    cursorPosition.Y = 0;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursorPosition);
    
    // Ve vien tren
    for (int i = 0; i < width + 2; i++)
        cout << "#";
    cout << endl;

    // Ve than tro choi
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (j == 0)
                cout << "#"; // Vien ben trai
            if (i == y && j == x)
                cout << "O"; // Ve dau ran
            else if (i == fruitY && j == fruitX)
                cout << "F"; // Ve trai cay
            else
            {
                bool print = false;
                for (int k = 0; k < nTail; k++)
                {
                    if (tailX[k] == j && tailY[k] == i)
                    {
                        cout << "o"; // Ve duoi ran
                        print = true;
                    }
                }
                if (!print)
                    cout << " "; // O trong
            }
            if (j == width - 1)
                cout << "#"; // Vien ben phai
        }
        cout << endl;
    }

    // Ve vien duoi
    for (int i = 0; i < width + 2; i++)
        cout << "#";
    cout << endl;
    
    cout << "Score: " << score << endl; // Hien thi diem so
}

void Input()
{
    if (_kbhit())
    {
        switch (_getch())
        {
        case 'a':
            if (dir != RIGHT) dir = LEFT;  // Di chuyen trai, khong cho phep quay nguoc
            break;
        case 'd':
            if (dir != LEFT) dir = RIGHT;  // Di chuyen phai
            break;
        case 'w':
            if (dir != DOWN) dir = UP;  // Di chuyen len
            break;
        case 's':
            if (dir != UP) dir = DOWN;  // Di chuyen xuong
            break;
        case 'x':
            gameOver = true;  // Nhan 'x' de thoat tro choi
            break;
        }
    }
}

void Logic()
{
    // Cap nhat vi tri duoi
    int prevX = tailX[0];
    int prevY = tailY[0];
    int prev2X, prev2Y;
    tailX[0] = x;
    tailY[0] = y;
    for (int i = 1; i < nTail; i++)
    {
        prev2X = tailX[i];
        prev2Y = tailY[i];
        tailX[i] = prevX;
        tailY[i] = prevY;
        prevX = prev2X;
        prevY = prev2Y;
    }
    
    // Cap nhat vi tri dau
    switch (dir)
    {
    case LEFT:
        x--;
        break;
    case RIGHT:
        x++;
        break;
    case UP:
        y--;
        break;
    case DOWN:
        y++;
        break;
    default:
        break;
    }

    // Xu ly ran di xuyen tuong
    if (x >= width) x = 0; else if (x < 0) x = width - 1;
    if (y >= height) y = 0; else if (y < 0) y = height - 1;

    // Kiem tra va cham voi duoi
    for (int i = 0; i < nTail; i++)
        if (tailX[i] == x && tailY[i] == y)
            gameOver = true;

    // Kiem tra an trai cay
    if (x == fruitX && y == fruitY)
    {
        score += 10; // Tang diem
        fruitX = rand() % width; // Tao trai cay moi o vi tri ngau nhien
        fruitY = rand() % height;
        nTail++; // Tang do dai duoi
    }
}

int main()
{
    Setup(); // Cai dat ban dau
    while (!gameOver)
    {
        Draw(); // Ve man hinh
        Input(); // Nhan du lieu tu nguoi choi
        Logic(); // Xu ly logic cua tro choi
        Sleep(100);  // Dieu chinh toc do tro choi (0.1 giay moi khung hinh)
    }
    return 0;
}
Editor is loading...
Leave a Comment