gpt-4.1-2025-04-14· OpenAI
LMArena Human Preference 140K
сделай 2d авиасимулятор на html с библиотекой p5js
- User
сделай 2d авиасимулятор на html с библиотекой p5js
- Assistant
Конечно! Вот пример простого 2D авиасимулятора на p5.js.
В этом примере реализовано:- Самолет управляется стрелками (влево/вправо — поворот, вверх — тяга).
- Реалистичная инерция, скорость, угол атаки.
- Можно стрелять пробелом (пули летят вперед).
- Примитивный враг (красный самолет), который летит по прямой.
- Столкновение пули с врагом — враг исчезает.
Скопируйте этот код в HTML-файл:
<!DOCTYPE html> <html lang="ru"> <head> <meta charset="UTF-8"> <title>2D Авиасимулятор на p5.js</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script> <style>body{margin:0;overflow:hidden;background:#222;}</style> </head> <body> <script> let plane; let bullets = []; let enemy; let enemyAlive = true; function setup() { createCanvas(900, 600); plane = new Plane(width/4, height/2); enemy = new Enemy(width*0.8, height/2); } function draw() { background(100, 180, 255); // небо // Земля fill(60, 180, 60); rect(0, height-40, width, 40); // Самолет plane.update(); plane.display(); // Пули for (let i = bullets.length-1; i >= 0; i--) { bullets[i].update(); bullets[i].display(); if (bullets[i].offscreen()) { bullets.splice(i, 1); } } // Враг if (enemyAlive) { enemy.update(); enemy.display(); // Проверка попадания for (let i = bullets.length-1; i >= 0; i--) { if (enemy.hit(bullets[i])) { enemyAlive = false; bullets.splice(i, 1); break; } } } else { fill(255,0,0); textSize(32); textAlign(CENTER); text("Враг сбит!", width/2, 60); } // HUD fill(0,150); rect(10,10,180,60,10); fill(255); textSize(16); textAlign(LEFT,TOP); text("Скорость: " + nf(plane.speed,1,1), 20, 20); text("Угол: " + nf(degrees(plane.angle)%360,1,0) + "°", 20, 40); } function keyPressed() { if (key === ' ') { bullets.push(new Bullet(plane.x, plane.y, plane.angle)); } } class Plane { constructor(x, y) { this.x = x; this.y = y; this.angle = 0; // радианы this.speed = 0; this.maxSpeed = 8; this.acc = 0.15; this.turnSpeed = 0.045; this.size = 40; this.vx = 0; this.vy = 0; } update() { // Управление if (keyIsDown(LEFT_ARROW)) this.angle -= this.turnSpeed; if (keyIsDown(RIGHT_ARROW)) this.angle += this.turnSpeed; if (keyIsDown(UP_ARROW)) this.speed = min(this.speed + this.acc, this.maxSpeed); else this.speed = max(this.speed - this.acc*0.5, 2); // Физика this.vx = cos(this.angle) * this.speed; this.vy = sin(this.angle) * this.speed; this.x += this.vx; this.y += this.vy; // Границы экрана (wrap) if (this.x < 0) this.x = width; if (this.x > width) this.x = 0; if (this.y < 0) this.y = 0; if (this.y > height-40) this.y = height-40; } display() { push(); translate(this.x, this.y); rotate(this.angle); // Тело fill(200); stroke(0); strokeWeight(1.5); rect(-this.size/2, -8, this.size, 16, 8); // Крылья fill(180); rect(-8, -22, 16, 44, 8); // Нос fill(100,100,255); triangle(this.size/2, 0, this.size/2-10, -8, this.size/2-10, 8); // Кабина fill(0,180,255,180); ellipse(0,0,18,14); pop(); } } class Bullet { constructor(x, y, angle) { this.x = x + cos(angle)*30; this.y = y + sin(angle)*30; this.vx = cos(angle)*14; this.vy = sin(angle)*14; this.r = 4; } update() { this.x += this.vx; this.y += this.vy; } display() { fill(255,220,0); noStroke(); ellipse(this.x, this.y, this.r*2, this.r*2); } offscreen() { return (this.x < 0 || this.x > width || this.y < 0 || this.y > height); } } class Enemy { constructor(x, y) { this.x = x; this.y = y; this.angle = PI; // летит влево this.speed = 3; this.size = 38; } update() { this.x += cos(this.angle) * this.speed; this.y += sin(this.angle) * this.speed; // Плавное движение вверх-вниз this.y += sin(frameCount*0.03)*1.2; if (this.x < -50) this.x = width+50; } display() { push(); translate(this.x, this.y); rotate(this.angle); // Тело fill(220,60,60); stroke(0); rect(-this.size/2, -7, this.size, 14, 7); // Крылья fill(180,0,0); rect(-7, -18, 14, 36, 7); // Нос fill(255,80,80); triangle(this.size/2, 0, this.size/2-8, -7, this.size/2-8, 7); // Кабина fill(0,0,0,120); ellipse(0,0,14,10); pop(); } hit(bullet) { let d = dist(this.x, this.y, bullet.x, bullet.y); return d < this.size/2 + bullet.r; } } </script> </body> </html>Управление:
- Влево/вправо — поворот самолета
- Вверх — ускорение
- Пробел — выстрел
Можно доработать:
- Добавить несколько врагов, облака, ландшафт, топливо, очки и т.д.
Если нужно что-то сложнее — напишите!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.