H5圣诞老人悬崖小游戏,先来看看效果:
部分核心的代码如下:
let status = "waiting";
let lastTimestamp;
let santaX;
let santaY;
let sceneOffset;
let score = 0;
let platforms = [];
let sticks = [];
let trees = [];
let clouds = [];
const config = {
canvasWidth: 375,
canvasHeight: 375,
platformHeight: 100,
santaDistanceFromEdge: 10,
paddingX: 100,
perfectAreaSize: 10,
backgroundSpeedMultiplier: 0.2,
speed: 4,
santaWidth: 17,
santaHeight: 30
};
const colours = {
lightBg: "#62AFB9",
medBg: "#182757",
darkBg: "#0D5B66",
lightHill: "#E9E9E9",
medHill: "#34A65F",
darkHill: "#07133A",
platform: "#9B4546",
platformTop: "#620E0E",
em: "#CC231E",
skin: "#CF6D60"
};
const hills = [
{
baseHeight: 120,
amplitude: 20,
stretch: 0.5,
colour: colours.lightHill
},
{
baseHeight: 100,
amplitude: 10,
stretch: 1,
colour: colours.medHill
},
{
baseHeight: 70,
amplitude: 20,
stretch: 0.5,
colour: colours.darkHill
}
];
const scoreElement = createElementStyle(
"div",
`position:absolute;top:1.5em;font-size:5em;font-weight:900;text-shadow:${addShadow(
colours.darkHill,
7
)}`
);
const canvas = createElementStyle("canvas");
const introductionElement = createElementStyle(
"div",
`font-size:1.2em;position:absolute;text-align:center;transition:opacity 2s;width:250px`,
"Press and hold anywhere to stretch out a sugar cane, it has to be the exact length or Santa will fall down"
);
const perfectElement = createElementStyle(
"div",
"position:absolute;opacity:0;transition:opacity 2s",
"Double Score"
);
const restartButton = createElementStyle(
"button",
`width:120px;height:120px;position:absolute;border-radius:50%;color:white;background-color:${colours.em};border:none;font-weight:700;font-size:1.2em;display:none;cursor:pointer`,
"RESTART"
);
for (let i = 0; i <= 30; i++) {
createElementStyle(
"i",
`font-size: ${3 * Math.random()}em;left: ${
100 * Math.random()
}%; animation-delay: ${10 * Math.random()}s, ${2 * Math.random()}s`,
"."
);
}
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const ctx = canvas.getContext("2d");
Array.prototype.last = function () {
return this[this.length - 1];
};
Math.sinus = function (degree) {
return Math.sin((degree / 180) * Math.PI);
};
window.addEventListener("keydown", function (event) {
if (event.key == " ") {
event.preventDefault();
resetGame();
return;
}
});
["mousedown", "touchstart"].forEach(function (evt) {
window.addEventListener(evt, function (event) {
if (status == "waiting") {
lastTimestamp = undefined;
introductionElement.style.opacity = 0;
status = "stretching";
window.requestAnimationFrame(animate);
}
});
});
["mouseup", "touchend"].forEach(function (evt) {
window.addEventListener(evt, function (event) {
if (status == "stretching") {
status = "turning";
}
});
});
window.addEventListener("resi