大家都在发圣诞树,我偏偏要发一个圣诞小游戏给大家玩【内附源码】

else if(e.key == “Left” || e.key == “ArrowLeft”) {

leftPressed = false;

} else if(e.code == “Space”) {

spacePressed = false;

}

}

function draw() {

ctx.clearRect(0, 0, canvas.width, canvas.height);

drawSnow();

timer++;

switch (gameMode) {

case gameModes.TITLE:

titleScreen();

break;

case gameModes.GAMEOVER:

gameOver();

break;

case gameModes.PLAYING:

gameLoop();

break;

}

}

function titleScreen() {

if (timer > titleColours.length) timer=0;

ctx.font = “50px Arial”;

ctx.fillStyle = titleColours[timer];

ctx.fillText(圣诞抓礼物!, 0, 50);

ctx.fillStyle = “yellow”;

ctx.font = “30px Arial”;

ctx.fillText(请按空格键开始!, 65, 140);

var highScore = getHighScore();

if (highScore != -1) ctx.fillText(High Score: ${highScore}, 90, 220);

drawRotatedImage(elfImage, canvas.width/2 - elfWidth/2, 330, elfRotation, 200);

elfRotation+=2;

if (elfRotation > 359) elfRotation = 0;

if (spacePressed && timer > 5) {

setGameMode(gameModes.PLAYING);

}

}

function gameLoop() {

drawSnowPerson();

spawnGifts();

processGifts();

drawFloor();

drawHUD();

drawElf();

drawBang();

if(rightPressed) {

elfX += elfSpeed;

if (elfX + elfWidth > canvas.width){

elfX = canvas.width - (elfWidth + 5);

}

}

else if(leftPressed) {

elfX -= elfSpeed;

if (elfX < -15){

elfX = -15;

}

}

}

function gameOver() {

ctx.font = “50px Arial”;

ctx.fillStyle = “yellow”;

ctx.fillText(GAME OVER!, 80, 200);

ctx.font = “30px Arial”;

ctx.fillText(Final score: ${score},130, 240);

ctx.fillText(‘Press space to continue’,80, 280);

if (spacePressed && timer > 5) {

initialiseGame();

setGameMode(gameModes.TITLE);

}

}

function processGifts() {

gifts.forEach((g) => {

if (g && g.alive) {

// draw gift

drawGift(g);

if (g.y > canvas.height) {

g.alive = false;

if (!g.bomb) score–;

}

// move gift

g.y+=g.speed;

// rotate gift

g.rotation+=5;

if ( g.rotation > 359) g.rotation = 0;

// check for collision

if ((g.y + (giftHeight/2)) >= ((canvas.height - elfHeight - snowHeight) + 20)

&& (g.y<canvas.height-snowHeight+20)) {

if ((elfX + 25) <= (g.x + (giftWidth/2)) && ((elfX+20) + (elfWidth)) >= g.x )

{

g.alive = false;

if (!g.bomb) {

score+=5;

} else {

doBombCollision();

}

}

}

}

});

}

function drawGift(g) {

switch (g.colour) {

case 1:

drawColouredGift(greenGiftImage, g);

break;

case 2:

drawColouredGift(redGiftImage, g);

break;

case 3:

drawColouredGift(blueGiftImage, g);

break;

case 4:

drawRotatedImage(bombImage, g.x, g.y, 180, 45);

break;

}

}

function drawColouredGift(colourImage, g) {

drawRotatedImage(colourImage, g.x, g.y, g.rotation, 35);

}

function doBombCollision() {

health–;

bangX=elfX;

bangTime = 5;

if (health == 0) {

setHighScore();

setGameMode(gameModes.GAMEOVER);

}

}

function drawBang() {

if (bangTime > 0) {

bangTime–;

ctx.drawImage(bangImage, bangX, (canvas.height-75)-snowHeight, 75,75);

}

}

function drawElf() {

ctx.drawImage(elfImage, elfX,(canvas.height - elfHeight) - (snowHeight - 2),80,80);

}

function spawn() {

var newX = 5 + (Math.random() * (canvas.width - 5));

var colour;

var bomb = false;

if (randomNumber(1,bombChance) == bombChance) {

colour = 4;

bomb = true;

} else {

colour = randomNumber(1,3);

}

var newGift = {

x: newX,

y: 0,

speed: randomNumber(2,6),

alive: true,

rotation: 0,

colour: colour,

bomb: bomb,

};

gifts[maxGift] = newGift;

maxGift++;

if (maxGift > 75) {

maxGift = 0;

}

}

function spawnGifts() {

if (timer > spawnTimer) {

spawn();

timer = 0;

}

}

function drawRotatedImage(image, x, y, angle, scale)

{

ctx.save();

ctx.translate(x, y);

ctx.rotate(angle * TO_RADIANS);

ctx.drawImage(image, -(image.width/2), -(image.height/2), scale, scale);

ctx.restore();

}

function drawHUD() {

ctx.font = “20px Arial”;

ctx.fillStyle = “yellow”;

ctx.fillText(Score: ${score}, 0, 25);

var heart = ‘❤’;

var hearts = health > 0 ? heart.repeat(health) : " ";

ctx.fillText(“Helf:”, canvas.width - 120, 25);

ctx.fillStyle = “red”;

ctx.fillText(${hearts}, canvas.width - 60, 26);

}

function initialiseGame() {

health = 3;

elfX = (canvas.width-elfWidth)/2;

bangTime = 0;

score = 0;

snowHeight = 6;

timer = 0;

spawnTimer = 50;

gifts = [];

}

function initialiseSnow() {

for (i=0; i<maxSnowflakes; i++) {

var startY = -randomNumber(0, canvas.height);

snowflakes[i] = {

x: randomNumber(0, canvas.width-snowflakeSize),

y: startY,

startY: startY,

colour: snowflakeColours[randomNumber(0,3)],

radius: (Math.random() * 3 + 1),

speed: randomNumber(snowflakeMinSpeed, snowflakeMaxSpeed)

};

}

}

function drawSnow() {

for (i=0; i<maxSnowflakes; i++) {

snowflakes[i].y+=snowflakes[i].speed;

if (snowflakes[i].y>canvas.height) snowflakes[i].y = snowflakes[i].startY;

ctx.beginPath();

ctx.arc(snowflakes[i].x, snowflakes[i].y, snowflakes[i].radius, 0, 2 * Math.PI, false);

ctx.fillStyle = snowflakes[i].colour;

ctx.fill();

}

}

function drawFloor() {

var snowTopY = canvas.height - snowHeight;

ctx.fillStyle = ‘#fff’;

ctx.beginPath();

ctx.moveTo(0, snowTopY);

ctx.lineTo(canvas.width, snowTopY);

ctx.lineTo(canvas.width, canvas.height);

ctx.lineTo(0, canvas.height);

ctx.closePath();

ctx.fill();

}

function drawSnowPerson() {

var snowTopY = canvas.height - snowHeight;

drawCircle(“#fff”, 100, snowTopY-20, 40);

drawCircle(“#fff”, 100, snowTopY-70, 20);

drawRectangle(“#835C3B”, 85, snowTopY-105, 30, 20);

drawRectangle(“#835C3B”, 75, snowTopY-90, 50, 6);

drawTriangle(“#ffa500”, 100, snowTopY-64, 7);

drawCircle(“#000”, 93, snowTopY-76, 3);

drawCircle(“#000”, 108, snowTopY-76, 3);

drawCircle(“#000”, 100, snowTopY-40, 2);

drawCircle(“#000”, 100, snowTopY-30, 2);

drawCircle(“#000”, 100, snowTopY-20, 2);

}

function drawTriangle(color, x, y, height) {

ctx.strokeStyle = ctx.fillStyle = color;

ctx.beginPath();

ctx.moveTo(x, y);

ctx.lineTo(x - height, y - height);

ctx.lineTo(x + height, y - height);

ctx.fill();

}

function drawCircle(color, x, y, radius) {

ctx.strokeStyle = ctx.fillStyle = color;

ctx.beginPath();

ctx.arc(x, y, radius, 0, Math.PI * 2, true);

ctx.closePath();

ctx.stroke();

ctx.fill();

}

function drawRectangle(color, x, y, width, height) {

ctx.strokeStyle = ctx.fillStyle = color;

ctx.fillRect(x, y, width, height);

}

function randomNumber(low, high) {

return Math.floor(Math.random() * high) + low;

}

function makeColorGradient(frequency1, frequency2, frequency3,

phase1, phase2, phase3,

center, width, len) {

var colours = [];

for (var i = 0; i < len; ++i)

{

var r = Math.sin(frequency1*i + phase1) * width + center;

var g = Math.sin(frequency2*i + phase2) * width + center;

var b = Math.sin(frequency3*i + phase3) * width + center;

colours.push(RGB2Color(r,g,b));

}

最后

笔者已经把面试题和答案整理成了面试专题文档

image

image

image

image

image

image

frequency3,

phase1, phase2, phase3,

center, width, len) {

var colours = [];

for (var i = 0; i < len; ++i)

{

var r = Math.sin(frequency1*i + phase1) * width + center;

var g = Math.sin(frequency2*i + phase2) * width + center;

var b = Math.sin(frequency3*i + phase3) * width + center;

colours.push(RGB2Color(r,g,b));

}

最后

笔者已经把面试题和答案整理成了面试专题文档

[外链图片转存中…(img-UqlOPP99-1719195037735)]

[外链图片转存中…(img-gYi4Av2k-1719195037736)]

[外链图片转存中…(img-VWwpDrB5-1719195037736)]

[外链图片转存中…(img-ONtp4x1k-1719195037737)]

[外链图片转存中…(img-tV8FKpiR-1719195037737)]

[外链图片转存中…(img-ofAQdszg-1719195037738)]

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值