【基于Html+CSS+JS的canvas赛车小游戏(效果+源码)】

angle = 90;

ctx.beginPath();

ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);

ctx.lineWidth = 7;

ctx.fillStyle = ‘rgba(0, 0, 0, 0.4)’;

ctx.fill();

ctx.strokeStyle = color;

ctx.stroke();

for (var i = 0; i < tigs.length; i++) {

drawTig(ctx, centerX, centerY, radius, tigs[i], 7);

}

// draw pointer

angle = map($.state.speed, 0, $.state.car.maxSpeed, 90, 360);

drawPointer(ctx, color, 50, centerX, centerY, angle);

}

function drawPointer(ctx, color, radius, centerX, centerY, angle) {

var point = getCirclePoint(centerX, centerY, radius - 20, angle),

point2 = getCirclePoint(centerX, centerY, 2, angle + 90),

point3 = getCirclePoint(centerX, centerY, 2, angle - 90);

ctx.beginPath();

ctx.strokeStyle = “#FF9166”;

ctx.lineCap = ‘round’;

ctx.lineWidth = 4;

ctx.moveTo(point2.x, point2.y);

ctx.lineTo(point.x, point.y);

ctx.lineTo(point3.x, point3.y);

ctx.stroke();

ctx.beginPath();

ctx.arc(centerX, centerY, 9, 0, 2 * Math.PI, false);

ctx.fillStyle = color;

ctx.fill();

}

function drawTig(ctx, x, y, radius, angle, size) {

var startPoint = getCirclePoint(x, y, radius - 4, angle),

endPoint = getCirclePoint(x, y, radius - size, angle)

ctx.beginPath();

ctx.lineCap = ‘round’;

ctx.moveTo(startPoint.x, startPoint.y);

ctx.lineTo(endPoint.x, endPoint.y);

ctx.stroke();

}

function getCirclePoint(x, y, radius, angle) {

var radian = (angle / 180) * Math.PI;

return {

x: x + radius * Math.cos(radian),

y: y + radius * Math.sin(radian)

}

}

function calcMovement() {

var move = $.state.speed * 0.01,

newCurve = 0;

if($.state.keypress.up) {

$.state.speed += . s t a t e . c a r . a c c − ( .state.car.acc - ( .state.car.acc(.state.speed * 0.015);

} else if ($.state.speed > 0) {

$.state.speed -= $.state.car.friction;

}

if($.state.keypress.down && $.state.speed > 0) {

$.state.speed -= 1;

}

// Left and right

. s t a t e . x p o s − = ( .state.xpos -= ( .state.xpos=(.state.currentCurve * $.state.speed) * 0.005;

if($.state.speed) {

if($.state.keypress.left) {

. s t a t e . x p o s + = ( M a t h . a b s ( .state.xpos += (Math.abs( .state.xpos+=(Math.abs(.state.turn) + 7 + ($.state.speed > . s t a t e . c a r . m a x S p e e d / 4 ? ( .state.car.maxSpeed / 4 ? ( .state.car.maxSpeed/4?(.state.car.maxSpeed - ($.state.speed / 2)) : $.state.speed)) * 0.2;

$.state.turn -= 1;

}

if($.state.keypress.right) {

. s t a t e . x p o s − = ( M a t h . a b s ( .state.xpos -= (Math.abs( .state.xpos=(Math.abs(.state.turn) + 7 + ($.state.speed > . s t a t e . c a r . m a x S p e e d / 4 ? ( .state.car.maxSpeed / 4 ? ( .state.car.maxSpeed/4?(.state.car.maxSpeed - ($.state.speed / 2)) : $.state.speed)) * 0.2;

$.state.turn += 1;

}

if(KaTeX parse error: Expected 'EOF', got '&' at position 19: …ate.turn !== 0 &̲& !.state.keypress.left && !$.state.keypress.right) {

$.state.turn += $.state.turn > 0 ? -0.25 : 0.25;

}

}

. s t a t e . t u r n = c l a m p ( .state.turn = clamp( .state.turn=clamp(.state.turn, -5, 5);

. s t a t e . s p e e d = c l a m p ( .state.speed = clamp( .state.speed=clamp(.state.speed, 0, $.state.car.maxSpeed);

// section

$.state.section -= $.state.speed;

if($.state.section < 0) {

$.state.section = randomRange(1000, 9000);

newCurve = randomRange(-50, 50);

if(Math.abs($.state.curve - newCurve) < 20) {

newCurve = randomRange(-50, 50);

}

$.state.curve = newCurve;

}

if($.state.currentCurve < KaTeX parse error: Expected 'EOF', got '&' at position 14: .state.curve &̲& move < Math.a….state.currentCurve - $.state.curve)) {

$.state.currentCurve += move;

} else if($.state.currentCurve > KaTeX parse error: Expected 'EOF', got '&' at position 14: .state.curve &̲& move < Math.a….state.currentCurve - $.state.curve)) {

$.state.currentCurve -= move;

}

if(Math.abs($.state.xpos) > 550) {

$.state.speed *= 0.96;

}

. s t a t e . x p o s = c l a m p ( .state.xpos = clamp( .state.xpos=clamp(.state.xpos, -650, 650);

}

function keyUp(e) {

move(e, false);

}

function keyDown(e) {

move(e, true);

}

function move(e, isKeyDown) {

if(e.keyCode >= 37 && e.keyCode <= 40) {

e.preventDefault();

}

if(e.keyCode === 37) {

$.state.keypress.left = isKeyDown;

}

if(e.keyCode === 38) {

$.state.keypress.up = isKeyDown;

}

if(e.keyCode === 39) {

$.state.keypress.right = isKeyDown;

}

if(e.keyCode === 40) {

$.state.keypress.down = isKeyDown;

}

}

function randomRange(min, max) {

return min + Math.random() * (max - min);

}

function norm(value, min, max) {

return (value - min) / (max - min);

}

function lerp(norm, min, max) {

return (max - min) * norm + min;

}

function map(value, sourceMin, sourceMax, destMin, destMax) {

return lerp(norm(value, sourceMin, sourceMax), destMin, destMax);

}

function clamp(value, min, max) {

return Math.min(Math.max(value, min), max);

}

function drawBg() {

$.ctx.fillStyle = $.colors.sky;

$.ctx.fillRect(0, 0, $.canvas.width, $.settings.skySize);

drawMountain(0, 60, 200);

drawMountain(280, 40, 200);

drawMountain(400, 80, 200);

drawMountain(550, 60, 200);

$.storage.bg = $.ctx.getImageData(0, 0, $.canvas.width, $.canvas.height);

}

function drawMountain(pos, height, width) {

$.ctx.fillStyle = $.colors.mountains;

$.ctx.strokeStyle = $.colors.mountains;

$.ctx.lineJoin = “round”;

$.ctx.lineWidth = 20;

$.ctx.beginPath();

$.ctx.moveTo(pos, $.settings.skySize);

$.ctx.lineTo(pos + (width / 2), $.settings.skySize - height);

$.ctx.lineTo(pos + width, $.settings.skySize);

$.ctx.closePath();

$.ctx.stroke();

$.ctx.fill();

}

function drawSky() {

$.ctx.fillStyle = $.colors.sky;

$.ctx.fillRect(0, 0, $.canvas.width, $.settings.skySize);

}

function drawRoad(min, max, squishFactor, color) {

var basePos = $.canvas.width + $.state.xpos;

$.ctx.fillStyle = color;

$.ctx.beginPath();

. c t x . m o v e T o ( ( ( b a s e P o s + m i n ) / 2 ) − ( .ctx.moveTo(((basePos + min) / 2) - ( .ctx.moveTo(((basePos+min)/2)(.state.currentCurve * 3), $.settings.skySize);

. c t x . q u a d r a t i c C u r v e T o ( ( ( ( b a s e P o s / 2 ) + m i n ) ) + ( .ctx.quadraticCurveTo((((basePos / 2) + min)) + ( .ctx.quadraticCurveTo((((basePos/2)+min))+(.state.currentCurve / 3) + squishFactor, $.settings.skySize + 52, (basePos + max) / 2, $.canvas.height);

$.ctx.lineTo((basePos - max) / 2, $.canvas.height);

. c t x . q u a d r a t i c C u r v e T o ( ( ( ( b a s e P o s / 2 ) − m i n ) ) + ( .ctx.quadraticCurveTo((((basePos / 2) - min)) + ( .ctx.quadraticCurveTo((((basePos/2)min))+(.state.currentCurve / 3) - squishFactor, . s e t t i n g s . s k y S i z e + 52 , ( ( b a s e P o s − m i n ) / 2 ) − ( .settings.skySize + 52, ((basePos - min) / 2) - ( .settings.skySize+52,((basePosmin)/2)(.state.currentCurve * 3), $.settings.skySize);

$.ctx.closePath();

$.ctx.fill();

}

function drawCar() {

var carWidth = 160,

carHeight = 50,

carX = ($.canvas.width / 2) - (carWidth / 2),

carY = 320;

// shadow

roundedRect($.ctx, “rgba(0, 0, 0, 0.35)”, carX - 1 + $.state.turn, carY + (carHeight - 35), carWidth + 10, carHeight, 9);

// tires

roundedRect($.ctx, “#111”, carX, carY + (carHeight - 30), 30, 40, 6);

roundedRect($.ctx, “#111”, (carX - 22) + carWidth, carY + (carHeight - 30), 30, 40, 6);

drawCarBody($.ctx);

}

function drawCarBody(ctx) {

var startX = 299, startY = 311,

lights = [10, 26, 134, 152],

lightsY = 0;

/* Front */

roundedRect(KaTeX parse error: Expected 'EOF', got '#' at position 8: .ctx, "#̲C2C2C2", startX….state.turn * 1.1), startY - 18, 146, 40, 18);

ctx.beginPath();

ctx.lineWidth=“12”;

ctx.fillStyle=“#FFFFFF”;

ctx.strokeStyle=“#FFFFFF”;

ctx.moveTo(startX + 30, startY);

ctx.lineTo(startX + 46 + $.state.turn, startY - 25);

ctx.lineTo(startX + 114 + $.state.turn, startY - 25);

ctx.lineTo(startX + 130, startY);

ctx.fill();

ctx.stroke();

/* END: Front */

ctx.lineWidth=“12”;

ctx.lineCap = ‘round’;

ctx.beginPath();

ctx.fillStyle=“#DEE0E2”;

ctx.strokeStyle=“#DEE0E2”;

ctx.moveTo(startX + 2, startY + 12 + ($.state.turn * 0.2));

ctx.lineTo(startX + 159, startY + 12 + ($.state.turn * 0.2));

ctx.quadraticCurveTo(startX + 166, startY + 35, startX + 159, startY + 55 + ($.state.turn * 0.2));

ctx.lineTo(startX + 2, startY + 55 - ($.state.turn * 0.2));

ctx.quadraticCurveTo(startX - 5, startY + 32, startX + 2, startY + 12 - ($.state.turn * 0.2));

ctx.fill();

ctx.stroke();

ctx.beginPath();

ctx.lineWidth=“12”;

ctx.fillStyle=“#DEE0E2”;

ctx.strokeStyle=“#DEE0E2”;

ctx.moveTo(startX + 30, startY);

ctx.lineTo(startX + 40 + ($.state.turn * 0.7), startY - 15);

ctx.lineTo(startX + 120 + ($.state.turn * 0.7), startY - 15);

ctx.lineTo(startX + 130, startY);

ctx.fill();

ctx.stroke();

roundedRect(ctx, “#474747”, startX - 4, startY, 169, 10, 3, true, 0.2);

roundedRect(ctx, “#474747”, startX + 40, startY + 5, 80, 10, 5, true, 0.1);

ctx.fillStyle = “#FF9166”;

lights.forEach(function(xPos) {

ctx.beginPath();

ctx.arc(startX + xPos, startY + 20 + lightsY, 6, 0, Math.PI*2, true);

ctx.closePath();

ctx.fill();

lightsY += $.state.turn * 0.05;

});

ctx.lineWidth=“9”;

ctx.fillStyle=“#222222”;

ctx.strokeStyle=“#444”;

roundedRect($.ctx, “#FFF”, startX + 60, startY + 25, 40, 18, 3, true, 0.05);

}

function roundedRect(ctx, color, x, y, width, height, radius, turn, turneffect) {

var skew = turn === true ? $.state.turn * turneffect : 0;

ctx.fillStyle = color;

ctx.beginPath();

ctx.moveTo(x + radius, y - skew);

// top right

ctx.lineTo(x + width - radius, y + skew);

ctx.arcTo(x + width, y + skew, x + width, y + radius + skew, radius);

ctx.lineTo(x + width, y + radius + skew);

// down right

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

ctx.arcTo(x + width, y + height + skew, (x + width) - radius, y + height + skew, radius);

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

// down left

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

ctx.arcTo(x, y + height - skew, x, (y + height - skew) - radius, radius);

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

// top left

ctx.lineTo(x, y + radius - skew);

ctx.arcTo(x, y - skew, x + radius, y - skew, radius);

ctx.lineTo(x + radius, y - skew);

ctx.fill();

}

function drawGround(ctx, offset, lightColor, darkColor, width) {

var pos = ($.settings.skySize - $.settings.ground.min) + offset, stepSize = 1, drawDark = $.state.startDark, firstRow = true;

ctx.fillStyle = lightColor;

ctx.fillRect(0, $.settings.skySize, width, $.settings.ground.size);

ctx.fillStyle = darkColor;

while(pos <= $.canvas.height) {

stepSize = norm(pos, $.settings.skySize, $.canvas.height) * $.settings.ground.max;

if(stepSize < $.settings.ground.min) {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)

最后

你要问前端开发难不难,我就得说计算机领域里常说的一句话,这句话就是『难的不会,会的不难』,对于不熟悉某领域技术的人来说,因为不了解所以产生神秘感,神秘感就会让人感觉很难,也就是『难的不会』;当学会这项技术之后,知道什么什么技术能做到什么做不到,只是做起来花多少时间的问题而已,没啥难的,所以就是『会的不难』。

我特地针对初学者整理一套前端学习资料,免费分享给大家,戳这里即可免费领取

前端路线图

够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

[外链图片转存中…(img-03wtsxzH-1713631887668)]

[外链图片转存中…(img-5L2okJC1-1713631887668)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

[外链图片转存中…(img-pYnsGe7c-1713631887669)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)

[外链图片转存中…(img-cBnxZs89-1713631887669)]

最后

你要问前端开发难不难,我就得说计算机领域里常说的一句话,这句话就是『难的不会,会的不难』,对于不熟悉某领域技术的人来说,因为不了解所以产生神秘感,神秘感就会让人感觉很难,也就是『难的不会』;当学会这项技术之后,知道什么什么技术能做到什么做不到,只是做起来花多少时间的问题而已,没啥难的,所以就是『会的不难』。

我特地针对初学者整理一套前端学习资料,免费分享给大家,戳这里即可免费领取

[外链图片转存中…(img-uwWnRu0E-1713631887669)]

vue.js的36个技巧

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值