
开发第一个Html5游戏(二)
今天我们继续学习Html5系列游戏开发,我们会在第一章的基础上做一些扩展,加入颜色渐变、
绘制文本、自定义文本样式、以及动画,我们还将学习UI中一个非常重要的元素,按钮。
我们的上一篇文章: (译)开发第一个Html5游戏(一)
.
JS代码:(完整代码请看文章结尾链接)
var canvas, ctx; var circles = []; var selectedCircle; var hoveredCircle; var button; var moving = false; var speed = 2.0; // ------------------------------------------------------------- // objects : function Circle(x, y, radius){ this.x = x; this.y = y; this.radius = radius; } function Button(x, y, w, h, state, image) { this.x = x; this.y = y; this.w = w; this.h = h; this.state = state; this.imageShift = 0; this.image = image; } // ------------------------------------------------------------- // draw functions : function clear() { // clear canvas function ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); } function drawCircle(ctx, x, y, radius) { // draw circle function ctx.fillStyle = 'rgba(255, 35, 55, 1.0)'; ctx.beginPath(); ctx.arc(x, y, radius, 0, Math.PI*2, true); ctx.closePath(); ctx.fill(); ctx.lineWidth = 1; ctx.strokeStyle = 'rgba(0, 0, 0, 1.0)'; ctx.stroke(); // draw border } function drawScene() { // main drawScene function clear(); // clear canvas // 绘制文本 ctx.font = '42px DS-Digital'; ctx.textAlign = 'center'; ctx.fillStyle = '#ffffff'; ctx.fillText('Welcome to lesson #2', ctx.canvas.width/2, 50); //这里实现颜色渐变 var bg_gradient = ctx.createLinearGradient(0, 200, 0, 400); bg_gradient.addColorStop(0.0, 'rgba(255, 0, 0, 0.8)'); bg_gradient.addColorStop(0.5, 'rgba(0, 255, 0, 0.8)'); bg_gradient.addColorStop(1.0, 'rgba(0, 0, 255, 0.8)'); ctx.beginPath(); // custom shape begin ctx.fillStyle = bg_gradient; ctx.moveTo(circles[0].x, circles[0].y); for (var i=0; i<circles.length; i++) { ctx.lineTo(circles[i].x, circles[i].y); } ctx.closePath(); // custom shape end ctx.fill(); // fill custom shape ctx.lineWidth = 2; ctx.strokeStyle = 'rgba(0, 0, 255, 0.5)'; ctx.stroke(); // draw border // reverting direction if (circles[0].x <= 300 || circles[0].x >= 385) { speed = -speed; } // 实现图像抖动 if (moving) { circles[0].x -= speed; circles[0].y -= speed; circles[1].x += speed; circles[1].y -= speed; circles[2].x += speed; circles[2].y += speed; circles[3].x -= speed; circles[3].y += speed; } drawCircle(ctx, circles[0].x, circles[0].y, (hoveredCircle == 0) ? 25 : 15); drawCircle(ctx, circles[1].x, circles[1].y, (hoveredCircle == 1) ? 25 : 15); drawCircle(ctx, circles[2].x, circles[2].y, (hoveredCircle == 2) ? 25 : 15); drawCircle(ctx, circles[3].x, circles[3].y, (hoveredCircle == 3) ? 25 : 15); // draw button ctx.drawImage(button.image, 0, button.imageShift, button.w, button.h, button.x, button.y, button.w, button.h); // draw text ctx.font = '30px DS-Digital'; ctx.fillStyle = '#ffffff'; ctx.fillText('Play/Pause', 135, 480); ctx.fillText(button.state, 135, 515); } // ------------------------------------------------------------- // initialization $(function(){ canvas = document.getElementById('scene'); ctx = canvas.getContext('2d'); var circleRadius = 15; var width = canvas.width; var height = canvas.height; // lets add 4 circles manually circles.push(new Circle(width / 2 - 20, height / 2 - 20, circleRadius)); circles.push(new Circle(width / 2 + 20, height / 2 - 20, circleRadius)); circles.push(new Circle(width / 2 + 20, height / 2 + 20, circleRadius)); circles.push(new Circle(width / 2 - 20, height / 2 + 20, circleRadius)); // 图片按钮 buttonImage = new Image(); buttonImage.src = 'images/button.png'; buttonImage.onload = function() { } button = new Button(50, 450, 180, 120, 'normal', buttonImage); // binding mousedown event (for dragging) $('#scene').mousedown(function(e) { var mouseX = e.layerX || 0; var mouseY = e.layerY || 0; for (var i=0; i<circles.length; i++) { // checking through all circles - are mouse down inside circle or not var circleX = circles[i].x; var circleY = circles[i].y; var radius = circles[i].radius; if (Math.pow(mouseX-circleX,2) + Math.pow(mouseY-circleY,2) < Math.pow(radius,2)) { selectedCircle = i; break; } } // button behavior if (mouseX > button.x && mouseX < button.x+button.w && mouseY > button.y && mouseY < button.y+button.h) { button.state = 'pressed'; button.imageShift = 262; } }); $('#scene').mousemove(function(e) { // binding mousemove event for dragging selected circle var mouseX = e.layerX || 0; var mouseY = e.layerY || 0; if (selectedCircle != undefined) { // var canvasPosition = $(this).offset(); var radius = circles[selectedCircle].radius; circles[selectedCircle] = new Circle(mouseX, mouseY,radius); // changing position of selected circle } hoveredCircle = undefined; for (var i=0; i<circles.length; i++) { // checking through all circles - are mouse down inside circle or not var circleX = circles[i].x; var circleY = circles[i].y; var radius = circles[i].radius; if (Math.pow(mouseX-circleX,2) + Math.pow(mouseY-circleY,2) < Math.pow(radius,2)) { hoveredCircle = i; circles[hoveredCircle] = new Circle(circleX, circleY, 25); break; } } // button behavior if (button.state != 'pressed') { button.state = 'normal'; button.imageShift = 0; if (mouseX > button.x && mouseX < button.x+button.w && mouseY > button.y && mouseY < button.y+button.h) { button.state = 'hover'; button.imageShift = 131; } } }); $('#scene').mouseup(function(e) { // on mouseup - cleaning selectedCircle selectedCircle = undefined; // button behavior if (button.state == 'pressed') { moving = !moving; } button.state = 'normal'; button.imageShift = 0; }); setInterval(drawScene, 30); // loop drawScene });
相关推荐
这个"html5开发的塔防游戏.zip"资源提供了一个基于HTML5构建的塔防游戏的完整解决方案,名为"HTML5 塔防 游戏_HTML5游戏_solution4ht"。塔防游戏是一种策略类游戏,玩家通过在地图上建立防御塔来抵御一波波敌人的...
《HTML5 Canvas游戏开发实战》在介绍每个游戏开发的过程时,都会包括游戏分析、开发过程、代码解析和小结等相关内容,以帮助读者了解每种类型游戏开发的详细步骤,让读者彻底掌握各种类型游戏的开发思想。...
本资源“新手入门级html5游戏开发源码(蘑菇熊)”是一个很好的起点,它提供了一系列逐步教程,帮助初学者理解游戏开发的核心概念。 首先,我们来看标题中的"蘑菇熊",这很可能是游戏的主题或主角,展示了如何设计...
HTML5游戏开发素材 HTML5游戏开发素材 HTML5游戏开发素材 HTML5游戏开发素材
《Unity3D游戏开发 第二版》是一本深入讲解Unity3D引擎的权威教程,针对游戏开发者和爱好者提供了全面而详尽的知识体系。随着游戏行业的快速发展,Unity3D因其强大的功能、易用性和跨平台特性,已成为全球最受欢迎的...
《HTML5 Canvas 开发详解》第二版pdf 版
《HTML5 Canvas游戏开发实战》.(张路斌).pdf电子高清扫描版
Phaser是一个流行的游戏开发框架,它基于HTML5 Canvas构建,提供了丰富的游戏开发功能,包括精灵、动画、物理引擎、碰撞检测等。开发者可以利用Phaser快速搭建游戏结构,减少底层实现的复杂性,专注于游戏逻辑和用户...
HTML5 Canvas的核心是一个二维绘图上下文,通过JavaScript调用其提供的方法进行图形绘制。这些方法包括但不限于`fillRect()`(填充矩形)、`strokeRect()`(描边矩形)、`beginPath()`(开始路径)、`moveTo()`和`...
HTML5 移动游戏开发高级编程
HTML5 Canvas是Web开发中的一个关键技术,它提供了一个在网页上绘制图形的API,使得开发者可以直接在浏览器中创建丰富的交互式2D图形、动画以及游戏。"HTML5 Canvas核心技术—图形、动画与游戏开发"这本书深入浅出地...
标题中的“最新H5猜骰子游戏二次开发版源码.zip”表明这是一份基于HTML5(H5)技术的猜骰子游戏源代码,经过了二次开发,适合程序员或游戏开发者进行进一步定制和改进。这类游戏通常适用于移动设备和网页,具有跨...
1. **Canvas API**:这是一个二维绘图表面,开发者可以通过JavaScript来绘制图形,实现动态游戏画面。在这个街霸游戏中,战斗场景、角色动作和特效可能都是通过Canvas进行渲染的。 2. **Web Audio API**:提供了...
这个名为“PPT_Unity 3D游戏开发(第2版).zip”的压缩包包含了一系列关于Unity 3D游戏开发的PPT教程,可能是某个课程或工作坊的教学材料。以下是基于提供的文件名推测的各个章节内容: 1. **第1章:Unity入门** ...
总的来说,财神HTML5游戏源码是一个可以让你深入了解HTML5游戏开发的实践案例,通过对源码的学习和实践,你可以提升自己在Web游戏开发领域的技能,同时也能了解到如何将HTML5的新特性应用到实际项目中。
项目:HTML5, JavaScript中的Canvas Pokémon...在这个游戏项目的开发中,使用了多种图片、声音和脚本。它使用JavaScript来实现最终产出。所有的游戏功能都是用JavaScript设置的,而HTML和CSS用于布局和其他次要功能。
Canvas是HTML5中的一个核心元素,它是一个二维绘图上下文,允许开发者在网页上进行动态图形绘制。通过JavaScript,我们可以操纵Canvas的每一个像素,实现各种复杂的动画效果和游戏逻辑。Canvas的API提供了画线、填充...
html5游戏开发
为了深入研究HTML5游戏开发,本文以喜羊羊与灰太狼这一广为流传的故事为题材,创建了一个基于HTML5的游戏案例。通过该案例,我们能够具体探讨和分析HTML5游戏开发的实现过程。在实现过程中,JavaScript作为Web前端...
第1篇 益智游戏 第1章 连连看游戏 第2章 黑白棋游戏 第3章 汉诺塔游戏 第4章 推箱子游戏 第5章 扫雷游戏 第6章 七巧板游戏 第7章 21点扑克牌游戏 第8章 人物拼图游戏(一) 第9章 人物拼图游戏(二) ...