Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

write me code racing game in c++ programming

  1. User

    write me code racing game in c++ programming

  2. Assistant

    Creating a full racing game requires a lot of code and is beyond the scope of this platform. However, I can provide you with a simple text-based racing game using the C++ programming language as a starting point for you to expand upon. You may want to research game development frameworks like Unity or Unreal Engine if you want to make an advanced racing game with graphics.

    Here's a simple text-based racing game in C++:

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <unistd.h>
    
    const int trackWidth = 20;
    const int trackLength = 40;
    
    void drawTrack(const char track[][trackLength], int playerPosition) {
        for (int i = 0; i < trackWidth; ++i) {
            for (int j = 0; j < trackLength; ++j) {
                if (i == playerPosition && j == trackLength / 2) {
                    std::cout << 'P';
                } else {
                    std::cout << track[i][j];
                }
            }
            std::cout << "\n";
        }
    }
    
    void movePlayer(int &playerPosition, char direction) {
        if (direction == 'a' && playerPosition > 0) {
            --playerPosition;
        } else if (direction == 'd' && playerPosition < trackWidth - 1) {
            ++playerPosition;
        }
    }
    
    int main() {
        srand(time(0));
    
        char track[trackWidth][trackLength] = {};
    
        for (int i = 0; i < trackWidth; ++i) {
            for (int j = 0; j < trackLength; ++j) {
                if (j == 0 || j == trackLength - 1) {
                    track[i][j] = '|';
                } else {
                    track[i][j] = ' ';
                }
            }
        }
    
        int playerPosition = trackWidth / 2;
        int laps = 3;
        int lapDistance = 0;
    
        while (laps > 0) {
            system("clear"); // For Windows use: system("cls");
    
            drawTrack(track, playerPosition);
    
            char input;
            std::cin >> input;
            movePlayer(playerPosition, input);
    
            int move = rand() % 3 - 1;
            if (playerPosition + move >= 0 && playerPosition + move < trackWidth) {
                playerPosition += move;
            }
    
            if (++lapDistance >= trackLength) {
                --laps;
                lapDistance = 0;
            }
    
            usleep(50000); // Sleep for 50 milliseconds
        }
    
        std::cout << "Congratulations! You finished the race!\n";
    
        return 0;
    }
    

    This code is a basic representation of a racing game where you control a car (P) between the tracks, and you can move left (a) and right (d) on the track. The car moves forward automatically, and you have to complete laps number of laps (default: 3) to finish the game.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.