Web前端--HTML+CSS+JS实现圣诞抓礼物小游戏

var snowflakes = [];

const maxSnowflakes = 80;

const snowflakeSize = 3;

const snowflakeMinSpeed = 1;

const snowflakeMaxSpeed = 4;

const snowflakeColours = [“rgba(255,255,255,0.95)”, “rgba(255,255,255,0.65)”, “rgba(255,255,255,0.4)”];

const gameModes = {

TITLE: ‘title’,

PLAYING: ‘playing’,

GAMEOVER: ‘gameover’

};

var gameMode = gameModes.TITLE;

document.addEventListener(“keydown”, keyDownHandler, false);

document.addEventListener(“keyup”, keyUpHandler, false);

function keyDownHandler(e) {

if (e.key == “Right” || e.key == “ArrowRight”) {

rightPressed = true;

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

leftPressed = true;

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

spacePressed = true;

}

}

function keyUpHandler(e) {

if (e.key == “Right” || e.key == “ArrowRight”) {

rightPressed = false;

} 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);

}

}

CSS样式:

.cube {

width: 100%;

算法

  1. 冒泡排序

  2. 选择排序

  3. 快速排序

  4. 二叉树查找: 最大值、最小值、固定值

  5. 二叉树遍历

  6. 二叉树的最大深度

  7. 给予链表中的任一节点,把它删除掉

  8. 链表倒叙

  9. 如何判断一个单链表有环

由于篇幅限制小编,pdf文档的详解资料太全面,细节内容实在太多啦,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!

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

}

}

CSS样式:

.cube {

width: 100%;

算法

  1. 冒泡排序

  2. 选择排序

  3. 快速排序

  4. 二叉树查找: 最大值、最小值、固定值

  5. 二叉树遍历

  6. 二叉树的最大深度

  7. 给予链表中的任一节点,把它删除掉

  8. 链表倒叙

  9. 如何判断一个单链表有环

    [外链图片转存中…(img-bdajCa5o-1718042422172)]

由于篇幅限制小编,pdf文档的详解资料太全面,细节内容实在太多啦,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值