Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
17 kB
0
Indexable
Never
- Car.h

#pragma once

#include "Map.h"


class Car {
public:
	Car();

	void MoveLeft(Map* pmap);
	void MoveRight(Map* pmap);
	void DisplayCar(Map* pmap);
	bool Collision(Map* pmap);

	int _carPosition; /*= SCREEN_WIDTH / 2;*/
	char _car[4][4];
};

- Common.h

#pragma once

#include <iostream>
#include <conio.h>
#include <dos.h> 
#include <Windows.h>
#include <time.h>



void GoToXY(SHORT posX, SHORT posY);

void SetWindowSize(SHORT width, SHORT height); // Thay đổi màn hình cửa sổ window
void SetScreenBufferSize(SHORT width, SHORT height); // Thay đổi kích thước màn hình buffer
void DisableResizeWindow(); // Vô hiệu hóa nút thay đổi kích thước màn hình
void DisableCtrButton(bool Close, bool Min, bool Max); // Vô hiệu hóa các nút Minimize, Maximize và Close
void ShowScrollbar(BOOL Show); // Ẩn hiện thanh cuộn
void VietNamText(); // Hiện tiếng việt
void SetColor(int backgound_color, int text_color); // Chỉnh màu chữ + nền nơi có chữ (= highlight)  trên console
void SetTitle(); // Cài Title của console = "Racing Game 2D"
void ShowCur(bool CursorVisibility); // Ẩn hiện con trỏ trên console
COORD GetLargestConsoleWindowSize(); // Lấy kích thước tối đa của màn hình console
void MaximizeConsoleWindow(); // Tự động phóng to màn hình console

void DefaultMap(); // Giao diện bắt đầu game

void Mode1();
void Mode2();
void Guide1();
void Guide2();

-Enemy.h

#pragma once

#include "Map.h"




class Enemy {
public:
	Enemy();

	void DisplayEnemy(Map* pmap, int index);
	void EnemyGoDown(Map* pmap, int index);

	void EnemyGeneretion(int index);
	void EraseEnemy(Map* pmap, int index);

	int _enemyAlive[3];
	int _enemyPosition[3][2]; // (x, y)
	char _enemy[4][4];
};

- Game.h

#pragma once

#include "Map.h"
#include "Car.h"
#include "Enemy.h"
#include "Common.h"



class Game {
public:

	Game();

	void GameOver(Map* pmap);
	void Mode1();
	void Mode2();

	int _gameOver;
};

- Map.h

#pragma once


//#include "Enemy.h"
#include "Common.h"



using namespace std;


//COORD maxSize = GetLargestConsoleWindowSize();
#define SCREEN_WIDTH 174
#define SCREEN_HEIGHT 43


class Map {
public:
	Map();
	~Map();

	void DisplayMap();
	void DisplayScore();



	int _speed;
	int _score;
	char** _map;
};


- Car.cpp

#pragma once

#include "Car.h"



Car::Car() {
    for (int i = 0; i < 4; i++){
        for (int j = 0; j < 4; j++){
            if ((i == 0 || i == 2) && (j == 0 || j == 3)){
                _car[i][j] = ' ';
            }
            else {
                _car[i][j] = '�';
            }
        }
    }
    _carPosition = SCREEN_WIDTH / 2;
}


void Car::DisplayCar(Map* pmap) {
    SetColor(0, 4);
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            GoToXY(j + _carPosition, i + SCREEN_HEIGHT - 4);
            cout << _car[i][j];
            pmap->_map[i + SCREEN_HEIGHT - 4][j + _carPosition] = _car[i][j];
        }
    }
    SetColor(0, 7);
}


void Car::MoveLeft(Map* pmap) {
    if (_carPosition >= 59) {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                GoToXY(j + _carPosition, i + SCREEN_HEIGHT - 4);
                pmap->_map[i + SCREEN_HEIGHT - 4][j + _carPosition] = ' ';
                cout << ' ';
            }
        }
        _carPosition = _carPosition - 4;
        DisplayCar(pmap);
    }
}

void Car::MoveRight(Map* pmap) {
    if (_carPosition < SCREEN_WIDTH - 61) {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                GoToXY(j + _carPosition, i + SCREEN_HEIGHT - 4);
                pmap->_map[i + SCREEN_HEIGHT - 4][j + _carPosition] = ' ';
                cout << ' ';
            }
        }
        _carPosition = _carPosition + 4;
        DisplayCar(pmap);
    }
}

bool Car::Collision(Map* pmap) {
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            if (pmap->_map[SCREEN_HEIGHT - 4 + j][_carPosition + i] == '*') {
                return 1;
            }
        }
    }

    return 0;
}

- Common.cpp

#include "Common.h"

using namespace std;



void SetWindowSize(SHORT width, SHORT height){
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

    SMALL_RECT WindowSize;
    WindowSize.Top = 0;
    WindowSize.Left = 0;
    WindowSize.Right = width;
    WindowSize.Bottom = height;

    SetConsoleWindowInfo(hStdout, 1, &WindowSize);
}

void SetScreenBufferSize(SHORT width, SHORT height){
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

    COORD NewSize;
    NewSize.X = width;
    NewSize.Y = height;

    SetConsoleScreenBufferSize(hStdout, NewSize);
}

void DisableResizeWindow(){
    HWND hWnd = GetConsoleWindow();
    SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) & ~WS_SIZEBOX);
}

void DisableCtrButton(bool Close, bool Min, bool Max){
    HWND hWnd = GetConsoleWindow();
    HMENU hMenu = GetSystemMenu(hWnd, false);

    if (Close == 1)
    {
        DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
    }
    if (Min == 1)
    {
        DeleteMenu(hMenu, SC_MINIMIZE, MF_BYCOMMAND);
    }
    if (Max == 1)
    {
        DeleteMenu(hMenu, SC_MAXIMIZE, MF_BYCOMMAND);
    }
}

void ShowScrollbar(BOOL Show){
    HWND hWnd = GetConsoleWindow();
    ShowScrollBar(hWnd, SB_BOTH, Show);
}

COORD GetLargestConsoleWindowSize() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD maxSize = GetLargestConsoleWindowSize(hConsole);
    return maxSize;
}

void MaximizeConsoleWindow() {
    HWND console = GetConsoleWindow();
    ShowWindow(console, SW_MAXIMIZE);
}

void ShowCur(bool CursorVisibility){
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO ConCurInf;
    ConCurInf.dwSize = 1;
    ConCurInf.bVisible = CursorVisibility;
    SetConsoleCursorInfo(handle, &ConCurInf);
}


void VietNamText(){
	SetConsoleOutputCP(65001);
}

void SetColor(int backgound_color, int text_color) {
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    int color_code = backgound_color * 16 + text_color;
    SetConsoleTextAttribute(hStdout, color_code);
}


void SetTitle() {
    SetConsoleTitle("Racing Game 2D");
}

void GoToXY(SHORT posX, SHORT posY) {
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD Position;
    Position.X = posX;
    Position.Y = posY;
    SetConsoleCursorPosition(hStdout, Position);
}

void DefaultMap() {
    system("cls");
    GoToXY(70, 15 + 1);
    std::cout << "Racing Game 2D";
    GoToXY(70, 15 + 2);
    std::cout << "----------------";
    GoToXY(70, 15 + 3);
    std::cout << "Chose the mode game";
    GoToXY(70, 15 + 4);
    std::cout << "1. 1 Player";
    GoToXY(70, 15 + 5);
    std::cout << "2. 2 Player";
    GoToXY(70, 15 + 6);
    std::cout << "----------------";
    GoToXY(70, 15 + 7);
    std::cout << "Press 1-key or 2-key to chose the mode game: ";
}

void Guide1() {
    system("cls");
    GoToXY(70, 15);
    cout << "Guide for 1 Player";
    GoToXY(70, 15 + 1);
    cout << "----------------";
    GoToXY(70, 15 + 2);
    cout << "Avoid Enemies by moving left or right. ";
    GoToXY(70, 15 + 3);
    cout << "Press 'a' or 'A' to move left";
    GoToXY(70, 15 + 4);
    cout << "Press 'd' or 'D' to move right";
    GoToXY(70, 15 + 5);
    cout << "Press 'w' or 'W' to speed up";
    GoToXY(70, 15 + 6);
    cout << "Press 's' or 'S' to speed down";
    GoToXY(70, 15 + 7);
    cout << "Press 'escape' to exit";
    GoToXY(70, 15 + 8);
    cout << "Press any key to play game";
    _getch();
    system("cls");
}

void Guide2() {
    system("cls");
    GoToXY(70, 15);
    cout << "Guide for 2 Players";
    GoToXY(30, 15 + 1);
    cout << "----------------";
    GoToXY(30, 15 + 2);
    cout << "Avoid Enemies by moving left or right. ";
    GoToXY(30, 15 + 3);
    cout << "----------------";
    GoToXY(30, 15 + 4);
    cout << "Player 1 \t\t\t\t\t\t\t Player 2";
    GoToXY(30, 15 + 5);
    cout << "Press 'a' or 'A' to move left \t Press Left arrow key to move left";
    GoToXY(30, 15 + 6);
    cout << "Press 'd' or 'D' to move right \t Press Right arrow key to move right";
    GoToXY(30, 15 + 7);
    cout << "Press 'w' or 'W' to speed up \t Press Up arrow key to speed up";
    GoToXY(30, 15 + 8);
    cout << "Press 's' or 'S' to speed down \t Press Down arrow key to speed down";
    GoToXY(30, 15 + 9);
    cout << "Press 'escape' to exit";
    GoToXY(30, 15 + 10);
    cout << "Press any key to go back to menu";
    _getch();
    DefaultMap();
}

- Enemy.cpp

#pragma once

#include <ctime>
#include <cstdlib>
#include "Enemy.h"

Enemy::Enemy(){
    //_enemyAlive = new int[3];
    for (int i = 0; i < 3; i++){
        _enemyAlive[i] = 0;
    }
    for (int i = 0; i < 3; i++){
        //_enemyPosition = new int*[2];
        for (int j = 0; j < 2; j++){
            //_enemyPosition[j] = new int;
            _enemyPosition[i][j] = 0;
        }
    }
    //_enemyPosition[3][2] = { {0, 0}, {0, 0}, {0, 0} }; // (x, y)
    for (int i = 0; i < 4; i++){
        //_enemyPosition = new int*[4];
        for (int j = 0; j < 4; j++){
            //_enemyPosition[j] = new int;
            if ((i == 0 || i == 2) && (j == 0 || j == 3)){
                _enemy[i][j] = ' ';
            }
            else {
                _enemy[i][j] = '*';
            }
        }
    }
    //_enemy[4][4] = { ' ','*','*',' ',
				//	 '*','*','*','*',
			 //		 ' ','*','*',' ',
				//	 '*','*','*','*' };
}

void Enemy::EnemyGeneretion(int index) {
    if (_enemyAlive[index] == 0) {
        //srand((int)time(nullptr));
        _enemyPosition[index][0] = 55 + rand() % (SCREEN_WIDTH - 1 - 55 - 59);
        _enemyPosition[index][1] = 0;
        _enemyAlive[index] = 1;
    }
}

void Enemy::EraseEnemy(Map* pmap, int index) {
    if (_enemyAlive[index] == 1) {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                GoToXY(j + _enemyPosition[index][0], i + _enemyPosition[index][1]);
                cout << ' ';
                pmap->_map[i + _enemyPosition[index][1]][j + _enemyPosition[index][0]] = ' ';
            }
        }
    }
}

void Enemy::DisplayEnemy(Map* pmap, int index) {
    SetColor(0, 13);
    if (_enemyAlive[index] == 1) {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                GoToXY(j + _enemyPosition[index][0], i + _enemyPosition[index][1]);
                cout << _enemy[i][j];
                pmap->_map[i + _enemyPosition[index][1]][j + _enemyPosition[index][0]] = _enemy[i][j];
            }
        }
    }
    SetColor(0, 7);
}


void Enemy::EnemyGoDown(Map* pmap, int index) {
    if (_enemyAlive[index] == 1) {
        EraseEnemy(pmap, index);
        _enemyPosition[index][1]++;
        DisplayEnemy(pmap, index);

    }
}



- Game.cpp

#include "Game.h"


Game::Game(){
    _gameOver = 0;
}

void Game::GameOver(Map* pmap) {
    GoToXY(SCREEN_WIDTH / 2 - 16, SCREEN_HEIGHT / 2 - 1);
    cout << "            Game Over            ";
    GoToXY(SCREEN_WIDTH / 2 - 16, SCREEN_HEIGHT / 2 - 0);
    cout << "         Your score is " << pmap->_score << "          ";
    GoToXY(SCREEN_WIDTH / 2 - 16, SCREEN_HEIGHT / 2 + 2);
    cout << "Press any key to go back to menu";
    for (int i = SCREEN_WIDTH / 2 - 16; i <= SCREEN_WIDTH / 2 + 16; i++) {
        for (int j = SCREEN_HEIGHT / 2 - 2; j <= SCREEN_HEIGHT / 2 + 1; j++) {
            if (i == SCREEN_WIDTH / 2 - 16 || i == SCREEN_WIDTH / 2 + 16) {
                GoToXY(i, j);
                cout << '|';
            }
            if (j == SCREEN_HEIGHT / 2 - 2 || j == SCREEN_HEIGHT / 2 + 1) {
                GoToXY(i, j);
                cout << '-';
            }
        }
    }
    while (!_kbhit()) {
        SetColor(0, 7);
        GoToXY(SCREEN_WIDTH / 2 - 16, SCREEN_HEIGHT / 2 + 2);
        cout << "Press any key to go back to menu";
        Sleep(500);
        SetColor(0, 6);
        GoToXY(SCREEN_WIDTH / 2 - 16, SCREEN_HEIGHT / 2 + 2);
        cout << "Press any key to go back to menu";
        Sleep(500);
        SetColor(0, 7);
    }
    
}

void Game::Mode1() {
    Guide1();

	Map* pmap = new Map();
    pmap->DisplayMap();
    pmap->DisplayScore();

    Car* car = new Car;
    car->DisplayCar(pmap);


    Enemy* enemy = new Enemy();
    enemy->EnemyGeneretion(0);
    enemy->DisplayEnemy(pmap, 0);

    clock_t  startTime = clock();
    clock_t  currentTime;
    double duration;

    while (1) {
        // Mỗi 5 giây thì tăng tốc thêm 10 đơn vị vận tốc 
        currentTime = clock();
        duration = (currentTime - startTime) / CLOCKS_PER_SEC;
        if (duration >= 5 && duration < 6) {
            if (pmap->_speed > 10) {
                pmap->_speed = pmap->_speed - 10;
            }
            startTime = currentTime;
            duration = 0;
        }

        // Di chuyển car và kiểm tra xem có va chạm không 
        if (_kbhit()) {
            char ch = _getch();
            if (ch == 'a' || ch == 'A') {
                car->MoveLeft(pmap);
            }
            if (ch == 'd' || ch == 'D') {
                car->MoveRight(pmap);
            }
            if (ch == 'w' || ch == 'W') {
                if (pmap->_speed > 100) {
                    pmap->_speed = pmap->_speed - 100;
                }
            }
            if (ch == 's' || ch == 'S') {
                pmap->_speed = pmap->_speed + 100;
            }
            if (ch == 27) {
                _gameOver = 1;
            }
        }
        for (int i = 0; i < 3; i++) {
            if (car->Collision(pmap) == 1) {
                _gameOver = 1;
                //return;
            }
        }

        // Sinh thêm kẻ địch mới
        if (enemy->_enemyPosition[2][1] == 15 && enemy->_enemyAlive[0] == 0) {
            enemy->EnemyGeneretion(0);
            enemy->DisplayEnemy(pmap, 0);
        }
        if (enemy->_enemyPosition[0][1] == 15 && enemy->_enemyAlive[1] == 0) {
            enemy->EnemyGeneretion(1);
            enemy->DisplayEnemy(pmap, 1);
        }
        if (enemy->_enemyPosition[1][1] == 15 && enemy->_enemyAlive[2] == 0) {
            enemy->EnemyGeneretion(2);
            enemy->DisplayEnemy(pmap, 2);
        }

        Sleep(pmap->_speed);

        // Xử lý vấn đề tăng điểm 
        for (int i = 0; i < 3; i++) {
            if (enemy->_enemyPosition[i][1] == SCREEN_HEIGHT - 4) {
                enemy->EraseEnemy(pmap, i);
                if (enemy->_enemyAlive[i] == 1) {
                    pmap->_score++;
                }
                pmap->DisplayScore();
                enemy->_enemyAlive[i] = 0;
            }
            enemy->EnemyGoDown(pmap, i);
        }

        if (_gameOver == 1) {
            GameOver(pmap);
            return;
        }
    }

}

void Game::Mode2() {

}

- Main.cpp

#pragma once


#include "Game.h"
//#include "Map.h"
//#include "Car.h"
//#include "Enemy.h"
//#include "Common.h"
//#include <iostream>
//#include <conio.h>
//#include <dos.h> 
//#include <Windows.h>
//#include <ctime>


using namespace std;

//COORD maxSize = GetLargestConsoleWindowSize();

int main() {
	MaximizeConsoleWindow(); //Tự động phóng to màn hình console
	//SetWindowSize(maxSize.X, maxSize.Y); // Thay đổi màn hình cửa sổ window
	//SetScreenBufferSize(maxSize.X, maxSize.Y); // Thay đổi kích thước màn hình buffer
	DisableResizeWindow(); // Vô hiệu hóa nút thay đổi kích thước màn hình
	DisableCtrButton(0, 1, 1); // Vô hiệu hóa các nút Minimize, Maximize và Close
	ShowScrollbar(0); // Ẩn hiện thanh cuộn
	//VietNamText(); // Hiện tiếng việt
	//SetColor(int backgound_color, int text_color); // Chỉnh màu chữ + nền nơi có chữ (= highlight)  trên console
	SetTitle(); // Cài Title của console = "Racing Game 2D"
	ShowCur(0); // Ẩn hiện con trỏ trên console
    
	char ch;
	do {
		DefaultMap();
		ch = _getch();
		if (ch == '1') {
			cout << 1;
			Sleep(100);
			system("cls");
			Game* game = new Game;
			game->Mode1();
		}
		else if (ch == '2') {
			cout << 2;
			Sleep(100);
			system("cls");
			Game* game = new Game;
			game->Mode2();
		}
	} while (ch != '1' || ch != '2');
        
    
	
	return 0;
}

- Map.cpp

#pragma once


#include "Map.h"





Map::Map() {
    _speed = 50;
    _score = 0;
    _map = new char* [SCREEN_HEIGHT];
    for (int i = 0; i < SCREEN_HEIGHT; i++) {
        _map[i] = new char[SCREEN_WIDTH];
    }
}

//Map::~Map() {
//    for (int i = 0; i < SCREEN_HEIGHT; i++) {
//        delete[] _map[i];
//    }
//    delete[] _map;
//}

void Map::DisplayMap() {
    SetColor(0, 10);
    for (int x = 50; x < SCREEN_WIDTH - 50; x++) {
        for (int y = 0; y < SCREEN_HEIGHT ; y++) {
            if (x < 55 || x >= SCREEN_WIDTH - 55) {
                _map[y][x] = '$';
                GoToXY(x, y);
                cout << '$';
            }
        }
    }
    SetColor(0, 7);
}

void Map::DisplayScore() {
    GoToXY(SCREEN_WIDTH - 30, 15);
    cout << "Score: " << _score;
}