Untitled
unknown
plain_text
9 months ago
4.5 kB
3
Indexable
import pygame
import random
import sys
from flask import Flask, render_template_string
# Initialize Flask app
app = Flask(__name__)
# HTML template for the game
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Car Game</title>
<style>
canvas { display: block; margin: 0 auto; background: #808080; }
</style>
</head>
<body>
<h1 style="text-align: center;">Car Game</h1>
<p style="text-align: center;">Use arrow keys to move the car and avoid obstacles!</p>
<div id="game-container">
<script>
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
const ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 600;
const carWidth = 50;
const carHeight = 50;
const carSpeed = 8;
let carX = canvas.width / 2 - carWidth / 2;
const carY = canvas.height - 100;
const rocks = [];
const rockWidth = 50;
const rockHeight = 50;
const maxRocks = 5;
let score = 0;
let gameRunning = true;
function createRock() {
return {
x: Math.random() * (canvas.width - rockWidth),
y: -rockHeight,
speed: Math.random() * 4 + 4,
};
}
for (let i = 0; i < maxRocks; i++) {
rocks.push(createRock());
}
const keys = {
ArrowLeft: false,
ArrowRight: false,
};
window.addEventListener('keydown', (e) => {
if (e.key in keys) keys[e.key] = true;
});
window.addEventListener('keyup', (e) => {
if (e.key in keys) keys[e.key] = false;
});
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw car
ctx.fillStyle = 'red';
ctx.fillRect(carX, carY, carWidth, carHeight);
// Move car
if (keys.ArrowLeft && carX > 0) {
carX -= carSpeed;
}
if (keys.ArrowRight && carX < canvas.width - carWidth) {
carX += carSpeed;
}
// Update and draw rocks
for (const rock of rocks) {
rock.y += rock.speed;
ctx.fillStyle = 'black';
ctx.fillRect(rock.x, rock.y, rockWidth, rockHeight);
// Check for collision
if (
carX < rock.x + rockWidth &&
carX + carWidth > rock.x &&
carY < rock.y + rockHeight &&
carY + carHeight > rock.y
) {
gameRunning = false;
}
// Reset rock if it moves off screen
if (rock.y > canvas.height) {
rock.y = -rockHeight;
rock.x = Math.random() * (canvas.width - rockWidth);
rock.speed = Math.random() * 4 + 4;
score++;
}
}
// Draw score
ctx.fillStyle = 'white';
ctx.font = '20px Arial';
ctx.fillText(`Score: ${score}`, 10, 30);
if (gameRunning) {
requestAnimationFrame(gameLoop);
} else {
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'red';
ctx.font = '36px Arial';
ctx.fillText('Game Over', canvas.width / 2 - 100, canvas.height / 2);
ctx.fillStyle = 'white';
ctx.fillText(`Final Score: ${score}`, canvas.width / 2 - 110, canvas.height / 2 + 50);
}
}
gameLoop();
</script>
</div>
</body>
</html>
"""
@app.route('/')
def index():
return render_template_string(HTML_TEMPLATE)
if __name__ == "__main__":
app.run(debug=True)
Editor is loading...
Leave a Comment