Emmagodden/Formula
0
1import webbrowser2from http.server import BaseHTTPRequestHandler, HTTPServer3 4PORT = 80005 6html = """7<!DOCTYPE html>8<html lang="de">9<head>10 <meta charset="UTF-8">11 <title>F1 WASD Racer</title>12 <style>13 html, body {14 margin: 0;15 overflow: hidden;16 background: #111;17 }18 canvas {19 display: block;20 margin: auto;21 background: #2e2e2e;22 border: 4px solid #f1c40f;23 border-radius: 10px;24 }25 </style>26</head>27<body>28 <canvas id="gameCanvas" width="900" height="600"></canvas>29 <audio autoplay loop>30 <source src="https://www.bensound.com/bensound-music/bensound-dubstep.mp3" type="audio/mpeg">31 </audio>32 <script>33 const canvas = document.getElementById("gameCanvas");34 const ctx = canvas.getContext("2d");35 const carImg = new Image();36 carImg.src = "https://i.imgur.com/O7b6gWg.png";37 const car = {38 x: canvas.width / 2,39 y: canvas.height / 2,40 width: 40,41 height: 80,42 angle: 0,43 speed: 0,44 maxSpeed: 6,45 acceleration: 0.2,46 friction: 0.05,47 rotationSpeed: 0.0448 };49 const keys = {};50 document.addEventListener("keydown", e => keys[e.key.toLowerCase()] = true);51 document.addEventListener("keyup", e => keys[e.key.toLowerCase()] = false);52 function updateCar() {53 if (keys["w"]) car.speed = Math.min(car.speed + car.acceleration, car.maxSpeed);54 if (keys["s"]) car.speed = Math.max(car.speed - car.acceleration, -2);55 if (!keys["w"] && !keys["s"]) {56 if (car.speed > 0) car.speed -= car.friction;57 if (car.speed < 0) car.speed += car.friction;58 if (Math.abs(car.speed) < 0.05) car.speed = 0;59 }60 if (keys["a"]) car.angle -= car.rotationSpeed;61 if (keys["d"]) car.angle += car.rotationSpeed;62 car.x += Math.sin(car.angle) * car.speed;63 car.y -= Math.cos(car.angle) * car.speed;64 car.x = Math.max(0, Math.min(canvas.width, car.x));65 car.y = Math.max(0, Math.min(canvas.height, car.y));66 }67 function drawCar() {68 ctx.save();69 ctx.translate(car.x, car.y);70 ctx.rotate(car.angle);71 ctx.drawImage(carImg, -car.width / 2, -car.height / 2, car.width, car.height);72 ctx.restore();73 }74 function gameLoop() {75 ctx.clearRect(0, 0, canvas.width, canvas.height);76 updateCar();77 drawCar();78 requestAnimationFrame(gameLoop);79 }80 carImg.onload = gameLoop;81 </script>82</body>83</html>84"""85 86class GameHandler(BaseHTTPRequestHandler):87 def do_GET(self):88 self.send_response(200)89 self.send_header('Content-type', 'text/html')90 self.end_headers()91 self.wfile.write(html.encode('utf-8'))92 93def run():94 print(f"Spiel läuft auf http://localhost:{PORT}")95 webbrowser.open(f"http://localhost:{PORT}")96 server = HTTPServer(('', PORT), GameHandler)97 server.serve_forever()98 99if __name__ == "__main__":100 run()101 