Untitled

 avatar
unknown
plain_text
2 months ago
3.9 kB
6
Indexable
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

// Window size
int width = 800, height = 600;

// Player car
float playerX = 400;
float playerY = 100;

// Enemy car
float enemyX = 400;
float enemyY = 600;

// Game variables
int score = 0;
bool gameOver = false;
float speed = 0.3f;

// Road animation
float roadOffset = 0;

// Draw text
void drawText(float x, float y, const char* text) {
    glRasterPos2f(x, y);
    for (int i = 0; i < strlen(text); i++) {
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, text[i]);
    }
}

// Draw player car
void drawPlayer() {
    glColor3f(0, 0, 1);
    glBegin(GL_QUADS);
    glVertex2f(playerX - 20, playerY);
    glVertex2f(playerX + 20, playerY);
    glVertex2f(playerX + 20, playerY + 60);
    glVertex2f(playerX - 20, playerY + 60);
    glEnd();
}

// Draw enemy car
void drawEnemy() {
    glColor3f(1, 0, 0);
    glBegin(GL_QUADS);
    glVertex2f(enemyX - 20, enemyY);
    glVertex2f(enemyX + 20, enemyY);
    glVertex2f(enemyX + 20, enemyY + 60);
    glVertex2f(enemyX - 20, enemyY + 60);
    glEnd();
}

// Draw road
void drawRoad() {
    glColor3f(0.2, 0.2, 0.2);
    glBegin(GL_QUADS);
    glVertex2f(200, 0);
    glVertex2f(600, 0);
    glVertex2f(600, 600);
    glVertex2f(200, 600);
    glEnd();

    // Lane lines
    glColor3f(1, 1, 1);
    for (int i = 0; i < 10; i++) {
        float y = (i * 80 + (int)roadOffset) % 600;
        glBegin(GL_QUADS);
        glVertex2f(395, y);
        glVertex2f(405, y);
        glVertex2f(405, y + 40);
        glVertex2f(395, y + 40);
        glEnd();
    }
}

// Collision detection
bool checkCollision() {
    if (abs(playerX - enemyX) < 40 &&
        abs(playerY - enemyY) < 60) {
        return true;
    }
    return false;
}

// Display function
void display() {
    glClear(GL_COLOR_BUFFER_BIT);

    drawRoad();
    drawPlayer();
    drawEnemy();

    char scoreText[50];
    sprintf(scoreText, "Score: %d", score);
    glColor3f(1, 1, 1);
    drawText(10, 570, scoreText);

    if (gameOver) {
        drawText(350, 300, "GAME OVER");
        drawText(320, 260, "Press R to Restart");
    }

    glutSwapBuffers();
}

// Update game
void update(int value) {
    if (!gameOver) {
        enemyY -= speed * 10;
        roadOffset -= speed * 10;
        score++;

        if (enemyY < 0) {
            enemyY = 600;
            enemyX = 250 + rand() % 300;
        }

        if (checkCollision()) {
            gameOver = true;
        }

        // Increase difficulty
        if (score % 500 == 0) {
            speed += 0.05f;
        }
    }

    glutPostRedisplay();
    glutTimerFunc(16, update, 0); // ~60 FPS
}

// Keyboard input
void keyboard(unsigned char key, int x, int y) {
    if (key == 'r' || key == 'R') {
        // Restart game
        gameOver = false;
        score = 0;
        speed = 0.3f;
        enemyY = 600;
    }
}

// Special keys (arrow keys)
void specialKeys(int key, int x, int y) {
    if (!gameOver) {
        if (key == GLUT_KEY_LEFT && playerX > 220)
            playerX -= 20;

        if (key == GLUT_KEY_RIGHT && playerX < 580)
            playerX += 20;
    }
}

// Initialize OpenGL
void init() {
    glClearColor(0, 0.6, 0, 1); // green background
    gluOrtho2D(0, width, 0, height);
}

// Main function
int main(int argc, char** argv) {
    srand(time(0));

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(width, height);
    glutCreateWindow("2D Car Racing Game");

    init();

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutSpecialFunc(specialKeys);
    glutTimerFunc(0, update, 0);

    glutMainLoop();
    return 0;
}
Editor is loading...
Leave a Comment