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.dis.........完整代码请登录后点击上方下载按钮下载查看
网友评论0