File size: 5,624 Bytes
6c9e867
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
document.addEventListener('DOMContentLoaded', () => {
    // Game elements
    const playerCar = document.getElementById('player-car');
    const gameContainer = document.getElementById('game-container');
    const road = document.getElementById('road');
    const scoreElement = document.getElementById('score');
    const gameOverScreen = document.getElementById('game-over');
    const finalScoreElement = document.getElementById('final-score');
    const restartBtn = document.getElementById('restart-btn');
    const startScreen = document.getElementById('start-screen');
    const startBtn = document.getElementById('start-btn');

    // Game state
    let gameRunning = false;
    let score = 0;
    let obstacles = [];
    let animationId;
    let speed = 3;
    let gameSpeed = 2;
    let playerPosition = {
        x: gameContainer.offsetWidth / 2 - playerCar.offsetWidth / 2,
        y: gameContainer.offsetHeight - playerCar.offsetHeight - 20
    };

    // Initialize player car position
    playerCar.style.left = `${playerPosition.x}px`;
    playerCar.style.top = `${playerPosition.y}px`;

    // Event listeners
    startBtn.addEventListener('click', startGame);
    restartBtn.addEventListener('click', startGame);

    // Keyboard controls
    const keys = {
        ArrowLeft: false,
        ArrowRight: false,
        ArrowUp: false,
        ArrowDown: false
    };

    document.addEventListener('keydown', (e) => {
        if (keys.hasOwnProperty(e.key)) {
            keys[e.key] = true;
        }
    });

    document.addEventListener('keyup', (e) => {
        if (keys.hasOwnProperty(e.key)) {
            keys[e.key] = false;
        }
    });

    // Game functions
    function startGame() {
        // Reset game state
        score = 0;
        speed = 3;
        gameSpeed = 2;
        obstacles.forEach(obs => obs.element.remove());
        obstacles = [];
        scoreElement.textContent = `Score: ${score}`;

        // Reset player position
        playerPosition = {
            x: gameContainer.offsetWidth / 2 - playerCar.offsetWidth / 2,
            y: gameContainer.offsetHeight - playerCar.offsetHeight - 20
        };
        playerCar.style.left = `${playerPosition.x}px`;
        playerCar.style.top = `${playerPosition.y}px`;

        // Hide screens
        gameOverScreen.classList.add('hidden');
        startScreen.classList.add('hidden');

        // Start game loop
        gameRunning = true;
        gameLoop();
    }

    function gameLoop() {
        if (!gameRunning) return;

        // Move player
        if (keys.ArrowLeft && playerPosition.x > 20) {
            playerPosition.x -= speed;
        }
        if (keys.ArrowRight && playerPosition.x < gameContainer.offsetWidth - playerCar.offsetWidth - 20) {
            playerPosition.x += speed;
        }
        if (keys.ArrowUp && playerPosition.y > 0) {
            playerPosition.y -= speed;
        }
        if (keys.ArrowDown && playerPosition.y < gameContainer.offsetHeight - playerCar.offsetHeight) {
            playerPosition.y += speed;
        }

        playerCar.style.left = `${playerPosition.x}px`;
        playerCar.style.top = `${playerPosition.y}px`;

        // Generate obstacles
        if (Math.random() < 0.02) {
            createObstacle();
        }

        // Move obstacles
        moveObstacles();

        // Check collisions
        if (checkCollisions()) {
            gameOver();
            return;
        }

        // Update score
        score++;
        scoreElement.textContent = `Score: ${score}`;

        // Increase difficulty
        if (score % 500 === 0) {
            gameSpeed += 0.2;
        }

        animationId = requestAnimationFrame(gameLoop);
    }

    function createObstacle() {
        const width = Math.random() * 100 + 50;
        const height = Math.random() * 70 + 30;
        const x = Math.random() * (gameContainer.offsetWidth - width);
        
        const obstacle = document.createElement('div');
        obstacle.className = 'obstacle';
        obstacle.style.width = `${width}px`;
        obstacle.style.height = `${height}px`;
        obstacle.style.left = `${x}px`;
        obstacle.style.top = `-${height}px`;
        gameContainer.appendChild(obstacle);

        obstacles.push({
            element: obstacle,
            x,
            y: -height,
            width,
            height
        });
    }

    function moveObstacles() {
        obstacles.forEach(obstacle => {
            obstacle.y += gameSpeed;
            obstacle.element.style.top = `${obstacle.y}px`;

            // Remove obstacles that are off screen
            if (obstacle.y > gameContainer.offsetHeight) {
                obstacle.element.remove();
                obstacles = obstacles.filter(obs => obs !== obstacle);
            }
        });
    }

    function checkCollisions() {
        const playerRect = {
            x: playerPosition.x,
            y: playerPosition.y,
            width: playerCar.offsetWidth,
            height: playerCar.offsetHeight
        };

        return obstacles.some(obstacle => {
            return !(
                playerRect.x + playerRect.width < obstacle.x ||
                playerRect.x > obstacle.x + obstacle.width ||
                playerRect.y + playerRect.height < obstacle.y ||
                playerRect.y > obstacle.y + obstacle.height
            );
        });
    }

    function gameOver() {
        gameRunning = false;
        cancelAnimationFrame(animationId);
        finalScoreElement.textContent = `Score: ${score}`;
        gameOverScreen.classList.remove('hidden');
    }
});