p5+matter实现愤怒的小鸟小游戏代码
代码语言:html
所属分类:游戏
代码描述:p5+matter实现愤怒的小鸟小游戏代码,鼠标点击小鸟不放拖动往后拉瞄准目标松开左键,按 "空格键" 重来。按 "回车键" 清除场景,按住 'q', 'w', 'e', 'r', 或 't'(游戏对象)或 'm'(怪物),然后点击以添加对象到世界并创建你自己的结构。
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="es"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="background: #000; margin:0; background-size: 1500px 800px; background-repeat: no-repeat;" onclick="showText()"> <p id="infoText" style="display:none; font-family: system-ui; color: #a2a2a2; line-height: 1.5em; margin-left: 20px; margin-top: 750px; position:absolute;"> 👉 鼠标点击小鸟不放拖动往后拉瞄准目标松开左键。<br> 👉 按 "空格键" 重来。<br> 👉 按 "回车键" 清除场景。<br> 👉 编辑模式:按住 'q', 'w', 'e', 'r', 或 't'(游戏对象)或 'm'(怪物),然后点击以添加对象到世界并创建你自己的结构。 </p> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/p5.1.8.0.js"></script> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/matter.0.19.0.js"></script> <script> function showText() { document.getElementById('infoText').style.display = 'block'; } </script> <script > let Engine = Matter.Engine, World = Matter.World, Bodies = Matter.Bodies, Body = Matter.Body, Events = Matter.Events; let maxStretch = 100; // Maximum stretch distance for the slingshot let strength = 0.00161; // Strength of the slingshot force let simulationSpeed = 0.8; // Simulation speed (1 is normal) let interactRadius = 50; // Radius within which mouse interaction is allowed let boxes = []; let pumpkinReleased = false; // Flag to track if the pumpkin has been released let pumpkinHasCollided = false; // Flag to track if the pumpkin has collided with any body let pumpkinBeingDragged = false; // Flag to track if the pumpkin is being dragged let gameStarted = false; // Flag to track if the game has started function preload() { titleScreen = loadImage('//repo.bfw.wiki/bfwrepo/images/game/skech/angry_pumpkins.jpg'); imgSkull = loadImage('//repo.bfw.wiki/bfwrepo/images/game/skech/skull.png'); pumpkinImg = loadImage('//repo.bfw.wiki/bfwrepo/images/game/skech/angry2.png'); imgBoxSkull = loadImage('//repo.bfw.wiki/bfwrepo/images/game/skech/box2.png'); imgStone1 = loadImage('//repo.bfw.wiki/bfwrepo/images/game/skech/stone2.png'); imgBone1 = loadImage('//repo.bfw.wiki/bfwrepo/images/game/skech/bone1.png'); imgPlank1 = loadImage('//repo.bfw.wiki/bfwrepo/images/game/skech/plank1.png'); monsterImg = loadImage('//repo.bfw.wiki/bfwrepo/images/game/skech/monster.png'); } function setup() { let canvas = createCanvas(1500, 800); engine = Engine.create(); engine.timing.timeScale = simulationSpeed; ground = Bodies.rectangle(width / 2, height - 100, width, 20, { isStatic: true }); World.add(engine.world, ground); pumpkin = Bodies.circle(150, height - 200, 20, { isStatic: true }); World.add(engine.world, pumpkin); slingshot = new SlingShot(150, height - 200, pumpkin); Events.on(engine, 'collisionStart', collision); torch1 = new Torch(330, 620); torch2 = new Torch(1250, 455); explosionManager = new ExplosionManager(); // Init objects boxes.push(new GameObject(600, 650, 30, 100, imgBone1, 1.05)); boxes.push(new GameObject(600, 550, 30, 100, imgBone1, 1.05)); boxes.push(new GameObject(650, 600, 50, 50, monsterImg, 1.1, true, true)); boxes.push(new GameObject(700, 650, 30, 100, imgBone1, 1.05)); boxes.push(new GameObject(700, 550, 30, 100, imgBone1, 1.05)); boxes.push(new GameObject(650, 480, 150, 25, imgPlank1, 1.05)); boxes.push(new GameObject(650, 450, 70, 70, imgBoxSkull, 1.05)); boxes.push(new GameObject(650, 380, 50, 50, monsterImg, 1.1, true, true)); boxes.push(new GameObject(1100, 650, 70, 70, imgBoxSkull, 1.05)); boxes.push(new GameObject(1170, 650, 70, 70, imgBoxSkull, 1.05)); boxes.push(new GameObject(1135, 580, 70, 70, imgBoxSkull, 1.05)); boxes.push(new GameObject(1135, 510, 50, 50, monsterImg, 1.1, true, true)); boxes.push(new GameObject(450, 650, 150, 25, imgPlank1, 1.05)); boxes.push(new GameObject(450, 620, 70, 70, imgBoxSkull, 1.05)); boxes.push(new GameObject(450, 550, 20, 70, imgStone1, 1.05)); boxes.push(new GameObject(450, 490, 50, 50, monsterImg, 1.1, true, true)); boxes.push(new GameObject(800, 650, 70, 70, imgBoxSkull, 1.05)); boxes.push(new GameObject(800, 550, 30, 100, imgBone1, 1.05)); boxes.push(new GameObject(900, 650, 70, 70, imgBoxSkull, 1.05)); boxes.push(new GameObject(900, 550, 30, 100, imgBone1, 1.05)); boxes.push(new GameObject(850, 650, 30, 100, imgBone1, 1.05)); boxes.push(new GameObject(850, 600, 50, 50, monsterImg, 1.1, true, true)); boxes.push(new GameObject(850, 480, 150, 25, imgPlank1, 1.05)); boxes.push(new GameObject(850, 450, 70, 70, imgBoxSkull, 1.05)); boxes.push(new OvalObject(850, 410, 48, 56, imgSkull, 1.2)); boxes.push(new OvalObject(850, 410, 48, 56, imgSkull, 1.2)); boxes.push(new GameObject(850, 360, 70, 70, imgBoxSkull, 1.05)); boxes.push(new GameObject(850, 220, 150, 25, imgPlank1, 1.05)); boxes.push(new GameObject(850, 190, 70, 70, imgBoxSkull, 1.05)); boxes.push(new GameObject(850, 120, 50, 50, monsterImg, 1.1, true, true)); } class ExplosionManager { constructor() { this.explosions = []; } createExplosion(x, y) { let explosion = new Explosion(x, y); this.explosions.push(explosion); } updateAndDisplay() { for (let i = this.explosions.length - 1; i >= 0; i--) { this.explosions[i].update(); this.explosions[i].display(); if (this.explosions[i].isDead()) { this.explosions.splice(i, 1); } } } } class Explosion { constructor(x, y) { this.pos = createVector(x, y); this.particles = []; for (let i = 0; i < 50; i++) { this.particles.push(new ExplosionParticle(this.pos.x, this.pos.y)); } } update() { for (let particle of this.particles) { particle.update(); } } display() { for (let particle of this.particles) { particle.display(); } } isDead() { return this.particles.every(particle => particle.isDead()); } } class ExplosionParticle { constructor(x, y) { this.pos = createVector(x, y); this.vel = p5.Vector.random2D().mult(random(1, 3)); // Random speed and direction this.lifespan = 255; this.size = random(3, 10); } update() { this.vel.mult(0.95); // Decelerate this.pos.add(this.vel); this.lifespan -= 5; } display() { noStroke(); fill(255, this.lifespan); ellipse(this.pos.x, this.pos.y, this.size); } isDead() { return this.lifespan < 0; } } function resetpumpkin() { // Portal effect let disappearPortal = new PortalEffect(pumpkin.position.x, pumpkin.position.y); explosionManager.explosions.push(disappearPortal); // Delete pumpkin World.remove(engine.world, pumpkin); // Create new pumpkin pumpkin = Bodies.circle(150, height - 200, 20, { isStatic: true }); World.add(engine.world, pumpkin); // Portal effect let appearPortal = new PortalEffect(pumpkin.position.x, pumpkin.position.y); explosionManager.explosions.push(appearPortal); // Reset Decelerate slingshot = new SlingShot(150, height - 200, pumpkin); // Resetear states pumpkinReleased = false; pumpkinHasCollided = false; pumpkinBeingDragged = false; } function mouseDragged() { let d = dist(mouseX, mouseY, pumpkin.position.x, pumpkin.position.y); if (!pumpkinReleased && d < interactRadius) { pumpkinBeingDragged = true; let stretchDistance = dist(mouseX, mouseY, slingshot.origin.x, slingshot.origin.y); if (stretchDistance > maxStretch) { // Calculate angle between origin and mouse let angle = atan2(mouseY - slingshot.origin.y, mouseX - slingshot.origin.x); // Calculate pumpkin position in the limit of the max distance let newPosX = slingshot.origin.x + maxStretch * cos(angle); let newPosY = slingshot.origin.y + maxStretch * sin(angle); Body.setPosition(pumpkin, { x: newPosX, y: newPosY }); } else { Body.setPosition(pumpkin, { x: mouseX, y: mouseY }); } } } function mousePressed() { if (!gameStarted) { gameStarted = true; } else { if (keyIsPressed && (key === 'q' || key === 'Q')) { let box = new GameObject(mouseX, mouseY, 70, 70, imgBoxSkull, 1.05); boxes.push(box); } if (keyIsPressed && (key === 'w' || key === 'W')) { let box = new GameObject(mouseX, mouseY, 20, 70, imgStone1, 1.05); boxes.push(box); } if (keyIsPressed && (key === 'e' || key === 'E')) { let box = new GameObject(mouseX, mouseY, 30, 100, imgBone1, 1.05); boxes.push(box); } if (keyIsPressed && (key === 'r' || key === 'R')) { let box = new GameObject(mouseX, mouseY, 150, 25, imgPlank1, 1.05); boxes.push(box); } if (keyIsPressed && (key === 't' || key === 'T')) { let oval = new OvalObject(mouseX, mouseY, 48.........完整代码请登录后点击上方下载按钮下载查看
网友评论0