Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <stdio.h>
- #include <dos.h>
- #include <Windows.h>
- #include <string.h>
- #include <conio.h>
- #include <cstdlib>
- #include <ctime>
- #include <thread>
- #include "function.h"
- #define MAX_SIZE_SNAKE 100
- #define MAX_SIZE_FOOD 4
- #define MAX_SPEED 3
- #define POINT_EACH_FOOD 10
- #define TOCDO 100
- using namespace std;
- // global variables
- POINT snake[MAX_SIZE_SNAKE];
- POINT food[MAX_SIZE_FOOD];
- POINT porch[5] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
- int CHAR_LOCK;
- int MOVING;
- int SPEED;
- int HEIGH_CONSOLE, WIDTH_CONSOLE;
- int FOOD_INDEX;
- int SIZE_SNAKE;
- int STATE;
- int x_coordinate, y_coordinate;
- int LEVEL = 1, SCORE;
- char name[50], ID[50], filename[50];
- int len, apple, initlen;
- char temp;
- int idx;
- void DrawPorch(char c)
- {
- for (int i = 0; i < 5; i++)
- {
- gotoxy(porch[i].x, porch[i].y);
- printf("%c", c);
- }
- if (c == ' ')
- memset(porch, 0, sizeof(porch));
- }
- bool IsValid(int x, int y)
- {
- for (int i = 0; i < SIZE_SNAKE; i++)
- if (snake[i].x == x && snake[i].y == y)
- return false;
- return true;
- }
- bool isFoodPosition(int x, int y)
- {
- for (int i = 0; i < MAX_SIZE_FOOD; i++)
- {
- if (x == food[i].x && y == food[i].y)
- return true;
- }
- return false;
- }
- bool isVaLid_PorchPostiton(int x, int y)
- {
- if (!IsValid(x - 1, y - 1) || !IsValid(x, y - 1) || !IsValid(x + 1, y - 1)
- || !IsValid(x - 1, y) || !IsValid(x + 1, y))
- return false;
- return true;
- }
- void GenerateFood()
- {
- int x, y;
- for (int i = 0; i < MAX_SIZE_FOOD; i++)
- {
- do
- {
- x = x_coordinate + rand() % (WIDTH_CONSOLE - 4) + 2;
- y = y_coordinate + rand() % (HEIGH_CONSOLE - 4) + 2;
- } while (IsValid(x, y) == false);
- food[i] = { x, y };
- }
- }
- // tao cong
- void CreatePorch()
- {
- int x, y;
- for (int i = 0;; i++)
- {
- x = x_coordinate + rand() % (WIDTH_CONSOLE - 6) + 2;
- y = y_coordinate + rand() % (HEIGH_CONSOLE - 6) + 2;
- if (IsValid(x - 1, y - 1) && IsValid(x, y - 1) && IsValid(x + 1, y - 1) && IsValid(x - 1, y) && IsValid(x + 1, y))
- {
- if (!isFoodPosition(x - 1, y - 1) && !isFoodPosition(x, y - 1) && !isFoodPosition(x + 1, y - 1) && !isFoodPosition(x-1,y)
- && !isFoodPosition(x+1,y))
- break;
- }
- }
- porch[0] = { x - 1, y };
- porch[1] = { x - 1, y - 1 };
- porch[2] = { x, y - 1 };
- porch[3] = { x + 1, y - 1 };
- porch[4] = { x + 1, y };
- }
- void ResetData()
- {
- // khoi tao gia tri toan cuc
- CHAR_LOCK = 'A', MOVING = 'D', SPEED = 1; FOOD_INDEX = 0, WIDTH_CONSOLE = 70, HEIGH_CONSOLE = 20, SIZE_SNAKE = 6;
- apple = 0; SCORE = 0; LEVEL = 1; idx = 0, len = initlen;
- // khoi tao gia tri mac dinh cho snake
- snake[0] = { 10+x_coordinate, 5+y_coordinate }; snake[1] = { 11+x_coordinate, 5+y_coordinate };
- snake[2] = { 12+x_coordinate, 5+y_coordinate }; snake[3] = { 13+x_coordinate, 5+y_coordinate };
- snake[4] = { 14+x_coordinate, 5+y_coordinate }; snake[5] = { 15+x_coordinate, 5+y_coordinate };
- GenerateFood();
- // tao mang thuc an food
- }
- void StartGame()
- {
- system("cls"); // xoa man hinh
- TextColor(9);
- DrawBoard(x_coordinate, y_coordinate, WIDTH_CONSOLE, HEIGH_CONSOLE);
- TextColor(14);
- gotoxy(x_coordinate+25, 1);
- printf("***** SNAKE GAME *****");
- gotoxy(x_coordinate + 18, 2);
- printf("------------------------------------");
- gotoxy(x_coordinate + WIDTH_CONSOLE - 10, 3);
- printf("LEVEL: %d", LEVEL);
- gotoxy(x_coordinate + WIDTH_CONSOLE - 10, 4);
- printf("SCORE: %d", SCORE);
- gotoxy(x_coordinate, 4);
- printf("SNAKE: %s", ID);
- gotoxy(x_coordinate + WIDTH_CONSOLE + 3, 5);
- printf("\t\t\tLuat choi");
- gotoxy(x_coordinate + WIDTH_CONSOLE + 3, 6);
- printf("%c 'W'-Len 'S'-Xuong 'A'-Trai 'D'-Phai", 254);
- gotoxy(x_coordinate + WIDTH_CONSOLE + 3, 7);
- printf("%c 'P'-Dung game 'Esc'-Thoat game", 254);
- gotoxy(x_coordinate + WIDTH_CONSOLE + 3, 8);
- printf("%c 'L'-Luu Game 'T'-Tai Game", 254);
- gotoxy(x_coordinate + WIDTH_CONSOLE + 3, 9);
- printf("%c Khi an het 4 thuc an se xuat hien 1 cong", 254);
- gotoxy(x_coordinate + WIDTH_CONSOLE + 3, 10);
- printf("SNAKE quay ve va tang cap do va toc do");
- TextColor(230);
- if (porch[0].x != 0 && porch[0].y != 0)
- {
- char c = (char)176;
- DrawPorch(c);
- }
- TextColor(7);
- STATE = 1; // bat dau cho thread chay
- }
- bool checktouchPorch() // kiem tra xem co dung cong khong
- {
- for (int i = 0; i < SIZE_SNAKE; i++)
- {
- for (int j = 0; j < 5; j++)
- {
- if (porch[j].x == snake[i].x && porch[j].y == snake[i].y)
- return true;
- }
- }
- return false;
- }
- void ExitGame(HANDLE t)
- {
- system("cls");
- TerminateThread(t, 0);
- }
- void PauseGame(HANDLE t)
- {
- SuspendThread(t);
- }
- void Eat()
- {
- apple++;
- // xu ly chuoi snack la MSSV
- name[len] = name[idx++];
- len++;
- name[len] = '\0';
- snake[SIZE_SNAKE] = food[FOOD_INDEX];
- SIZE_SNAKE++;
- if (FOOD_INDEX == MAX_SIZE_FOOD - 1)
- {
- FOOD_INDEX = 0;
- GenerateFood();
- CreatePorch();
- TextColor(230);
- char c = (char)176;
- DrawPorch(c);
- TextColor(7);
- }
- else
- FOOD_INDEX++;
- // updates score and level
- SCORE += POINT_EACH_FOOD;
- gotoxy(x_coordinate + WIDTH_CONSOLE - 3, 4);
- printf("%d", SCORE);
- }
- void DrawSnakeAndFood(char* str)
- {
- TextColor(7);
- gotoxy(food[FOOD_INDEX].x, food[FOOD_INDEX].y);
- if (strcmp(str, " ") == 0 || (porch[0].x != 0 && porch[0].y != 0))
- printf(" ");
- else
- printf("@");
- for (int i = 0; i < SIZE_SNAKE; i++)
- {
- gotoxy(snake[i].x, snake[i].y);
- if (i == SIZE_SNAKE - 1)
- TextColor(12);
- printf("%c", str[i]);
- }
- }
- void ProcessDead() {
- idx = 0;
- len = initlen;
- STATE = 0;
- gotoxy(35, 15);
- printf("GAME OVER!");
- int i = 1;
- while (i <= 15)
- {
- TextColor(i++);
- for (int j = 0; j < SIZE_SNAKE; j++)
- {
- gotoxy(snake[j].x, snake[j].y);
- printf("?");
- }
- Sleep(50);
- i++;
- }
- TextColor(7);
- gotoxy(0, HEIGH_CONSOLE + y_coordinate + 1);
- printf(" Nhan phim 'Y' de tiep tuc game ... hoac nhan Esc de thoat game.");
- memset(porch, 0, sizeof(porch));
- }
- bool touchItsBody() // Kiem tra Ran co cham vao than hoac duoi
- {
- for (int i = 0; i < SIZE_SNAKE-1; i++)
- {
- if (snake[SIZE_SNAKE - 1].x == snake[i].x
- && snake[SIZE_SNAKE - 1].y == snake[i].y)
- return true;
- }
- return false;
- }
- void MoveRight() {
- if (checktouchPorch() ||touchItsBody() ||snake[SIZE_SNAKE - 1].x + 1 == WIDTH_CONSOLE+x_coordinate)
- {
- ProcessDead();
- }
- else {
- if (porch[0].x == 0 && porch[0].y == 0 && snake[SIZE_SNAKE - 1].x + 1 == food[FOOD_INDEX].x && snake[SIZE_SNAKE- 1].y == food[FOOD_INDEX].y) {
- Eat();
- }
- for (int i = 0; i < SIZE_SNAKE - 1; i++) {
- snake[i].x = snake[i + 1].x;
- snake[i].y = snake[i + 1].y;
- }
- snake[SIZE_SNAKE - 1].x++;
- }
- }
- void MoveLeft() {
- if (checktouchPorch() || touchItsBody() ||snake[SIZE_SNAKE - 1].x - 1 == x_coordinate) {
- ProcessDead();
- }
- else {
- if (porch[0].x == 0 && porch[0].y == 0 && snake[SIZE_SNAKE - 1].x - 1 == food[FOOD_INDEX].x && snake[SIZE_SNAKE - 1].y == food[FOOD_INDEX].y) {
- Eat();
- }
- for (int i = 0; i < SIZE_SNAKE - 1; i++) {
- snake[i].x = snake[i + 1].x;
- snake[i].y = snake[i + 1].y;
- }
- snake[SIZE_SNAKE - 1].x--;
- }
- }
- void MoveDown() {
- if (checktouchPorch() || touchItsBody() ||snake[SIZE_SNAKE - 1].y + 1 == HEIGH_CONSOLE+y_coordinate) {
- ProcessDead();
- }
- else {
- if (porch[0].x == 0 && porch[0].y == 0 && snake[SIZE_SNAKE - 1].x == food[FOOD_INDEX].x && snake[SIZE_SNAKE - 1].y + 1 == food[FOOD_INDEX].y) {
- Eat();
- }
- for (int i = 0; i < SIZE_SNAKE - 1; i++) {
- snake[i].x = snake[i + 1].x;
- snake[i].y = snake[i + 1].y;
- }
- snake[SIZE_SNAKE - 1].y++;
- }
- }
- void MoveUp() {
- if (checktouchPorch() ||touchItsBody() || snake[SIZE_SNAKE - 1].y - 1 == y_coordinate) {
- ProcessDead();
- }
- else {
- if (porch[0].x == 0 && porch[0].y == 0 && snake[SIZE_SNAKE - 1].x == food[FOOD_INDEX].x && snake[SIZE_SNAKE - 1].y - 1 == food[FOOD_INDEX].y) {
- Eat();
- }
- for (int i = 0; i < SIZE_SNAKE - 1; i++) {
- snake[i].x = snake[i + 1].x;
- snake[i].y = snake[i + 1].y;
- }
- snake[SIZE_SNAKE - 1].y--;
- }
- }
- // thu tuc cho thread
- void checkUpLevel()
- {
- if ((snake[SIZE_SNAKE - 1].x == (porch[0].x + 1)) && (snake[SIZE_SNAKE - 1].y == porch[0].y))
- {
- SIZE_SNAKE--;
- if (SIZE_SNAKE == 0)
- {
- DrawPorch(' ');
- if (apple == MAX_SIZE_FOOD*MAX_SPEED)
- apple = 0;
- SIZE_SNAKE = 6 + apple;
- temp = 'D';
- MOVING = 'D';
- CHAR_LOCK = 'A';
- for (int i = 0; i < SIZE_SNAKE; i++)
- {
- snake[i].x = 10 + i + x_coordinate;
- snake[i].y = 5 + y_coordinate;
- }
- // updates level
- LEVEL++;
- gotoxy(x_coordinate + WIDTH_CONSOLE - 3, 3);
- printf("%d", LEVEL);
- if (SPEED == MAX_SPEED)
- {
- len = initlen;
- idx = 0;
- SPEED = 1;
- POINT p[6];
- for (int i = SIZE_SNAKE - 6, k = 0; i < SIZE_SNAKE; i++, k++)
- p[k] = snake[i];
- SIZE_SNAKE = 6;
- for (int i = 0; i < SIZE_SNAKE; i++)
- snake[i] = p[i];
- }
- else
- SPEED++;
- }
- }
- }
- void ThreadFunc()
- {
- Nocursortype();
- while (1)
- {
- if (STATE == 1)
- {// neu snake van con song
- DrawSnakeAndFood(" ");
- switch (MOVING)
- {
- case 'A':
- {
- MoveLeft();
- break;
- }
- case 'D':
- {
- MoveRight();
- break;
- }
- case 'W':
- {
- MoveUp();
- break;
- }
- case 'S':
- {
- MoveDown();
- break;
- }
- }
- /// kiem tra len level khi qua cong
- checkUpLevel();
- DrawSnakeAndFood(name);
- Sleep(TOCDO / SPEED);//ham ngu theo toc do speed
- }
- }
- }
- //////////////////////////////////////
- void SaveGame(ofstream& infile)
- {
- TextColor(7);
- gotoxy(0, HEIGH_CONSOLE + y_coordinate + 1);
- printf(" SAVE GAME:\n Moi ban nhap ten file de luu game: ");
- fflush(stdin);
- gets_s(filename);
- fflush(stdin);
- infile.open(filename);
- if (!infile)
- return;
- infile << name << endl;
- infile << idx << endl;
- infile << initlen << endl;
- infile << len << endl;
- infile << LEVEL << endl;
- infile << SPEED << endl;
- infile << SCORE << endl;
- infile << SIZE_SNAKE << endl;
- for (int i = 0; i < SIZE_SNAKE; i++)
- infile << snake[i].x << " " << snake[i].y << endl;
- infile << FOOD_INDEX << endl;
- for (int i = FOOD_INDEX; i < MAX_SIZE_FOOD; i++)
- infile << food[i].x << " " << food[i].y << endl;
- infile << MOVING << endl;
- infile << CHAR_LOCK << endl;
- for (int i = 0; i < 5; i++)
- infile << porch[i].x << " " << porch[i].y << endl;
- infile << apple << endl;
- //
- infile.close();
- }
- void LoadGame(ifstream& infile)
- {
- infile.getline(name, 50);
- infile >> idx;
- infile >> initlen;
- infile >> len;
- infile >> LEVEL;
- infile >> SPEED;
- infile >> SCORE;
- infile >> SIZE_SNAKE;
- int x, y;
- for (int i = 0; i < SIZE_SNAKE; i++)
- {
- infile >> x >> y;
- snake[i].x = x;
- snake[i].y = y;
- }
- infile >> FOOD_INDEX;
- for (int i = FOOD_INDEX; i < MAX_SIZE_FOOD; i++)
- {
- infile >> x >> y;
- food[i].x = x;
- food[i].y = y;
- }
- infile >> MOVING;
- infile >> CHAR_LOCK;
- for (int i = 0; i < 5; i++)
- infile >> porch[i].x >> porch[i].y;
- infile >> apple;
- infile.close();
- }
- void read()
- {
- TextColor(14);
- ifstream fp("welcome.txt");
- char ch;
- while (!fp.eof())
- {
- ch = fp.get();
- cout << ch;
- Sleep(20);
- }
- Sleep(2000); // pause 2s
- TextColor(7);
- system("cls");
- }
- int main()
- {
- FixConsoleWindow();
- // gioi thieu game
- resizeConsole(1200, 600);
- read();
- // thiet lap man hinh console
- WIDTH_CONSOLE = 70;
- HEIGH_CONSOLE = 20;
- x_coordinate = 5; // toa do tam
- y_coordinate = 5;
- ofstream savefile;
- ifstream openfile;
- // man hinh console co 80 cot, 50 hang
- printf("\tMoi ban nhap phim:\n\tPhim 'T' de mo game da luu\n\tPhim bat ky de choi game moi:...? ");
- srand((unsigned int)time(0));
- temp = toupper(_getch());
- if (temp == 'T')
- {
- printf("\nNhap ten file de tai game da luu: ");
- do
- {
- fflush(stdin);
- gets_s(filename);
- fflush(stdin);
- openfile.open(filename);
- if (!openfile)
- printf("\n Khong ton tai tap tin!!!\nMoi ban nhap lai: ");
- } while (!openfile);
- LoadGame(openfile);
- MOVING = 0;
- }
- else
- {
- system("cls");
- printf("Moi nhap ten nguoi choi (6 ki tu): ");
- gets_s(name);
- fflush(stdin);
- //
- ResetData();
- }
- strcpy_s(ID, name);
- len = strlen(name);
- initlen = len;
- system("cls");
- StartGame();
- thread t1(ThreadFunc); //Tao thread cho snake
- HANDLE handle_t1 = t1.native_handle(); //Lay handle cua thread
- while (1)
- {
- temp = toupper(_getch());
- if (STATE == 1)
- {
- if (temp == 'P'){
- PauseGame(handle_t1);
- }
- else if (temp == 'L'){
- PauseGame(handle_t1);
- DrawSnakeAndFood(name);
- SaveGame(savefile);
- TextColor(7);
- gotoxy(0, x_coordinate + HEIGH_CONSOLE + 3);
- printf("Ban muon tiep tuc choi game hay thoat game (Yes/No): ");
- char t;
- cin >> t;
- if (t == 'n')
- {
- t1.detach();
- break;
- }
- else
- {
- system("cls");
- StartGame();
- DrawSnakeAndFood(name);
- }
- }
- else if (temp == 'T'){
- PauseGame(handle_t1);
- DrawSnakeAndFood(name);
- TextColor(7);
- gotoxy(x_coordinate+WIDTH_CONSOLE+3, 12);
- printf("%c LOAD GAME: Moi ban nhap ten file da luu game: ", 254);
- do
- {
- fflush(stdin);
- gets_s(filename);
- fflush(stdin);
- openfile.open(filename);
- if (!openfile)
- {
- gotoxy(x_coordinate + WIDTH_CONSOLE + 3, 13);
- printf("! Moi ban nhap lai: ");
- }
- } while (!openfile);
- LoadGame(openfile);
- system("cls");
- StartGame();
- DrawSnakeAndFood(name);
- }
- else if (temp == 27)
- {
- ExitGame(handle_t1);
- t1.detach();
- break;
- }
- else
- {
- ResumeThread(handle_t1);
- if ((temp != CHAR_LOCK) && (temp == 'D' || temp == 'A' || temp == 'W' || temp == 'S'))
- {
- if (temp == 'D')
- CHAR_LOCK = 'A';
- else if (temp == 'W')
- CHAR_LOCK = 'S';
- else if (temp == 'S')
- CHAR_LOCK = 'W';
- else
- CHAR_LOCK = 'D';
- MOVING = temp;
- }
- }
- }
- else
- {
- if (temp == 27) // nhan phim ESC de thoat game
- {
- ExitGame(handle_t1);
- t1.detach();
- break;
- }
- else if (temp == 'Y')
- {
- ResetData();
- StartGame();
- }
- }
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement