首页 > 其他分享 >html5游戏制作入门系列教程(二)

html5游戏制作入门系列教程(二)

时间:2023-06-08 17:34:06浏览次数:48  
标签:circles 教程 入门 gradient button ctx width html5 var


今天,我们继续html5游戏制作入门系列的系列文章。今天,我们将继续基础知识(也许甚至是高级技巧的基础)。我要告诉你如何具有渐变颜色填充对象,绘制文本,使用自定义的字体绘制文本,基本的动画,以及最重要的UI元素 – 按钮。

 

我们以前的文章中,你可以在这里阅读:html5游戏制作入门系列教程(一)。我们会用到以前的脚本,并将其功能加强。我要绘制文本,使用自定义字体,动画对象(方形)与渐变色填充,将利用“播放/暂停”按钮暂停动画。

 

这里有我们的演示和下载包:

在线演示 源码下载

 

好吧,下载所需文件,让我们开始编码!

 

步骤1: HTML

这里是我演示的HTML

 

index.html

<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>html5game-html5游戏制作入门系列教程(二)</title>

<link href="main.css" rel="stylesheet" type="text/css" />

<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div class="container">
<canvas id="scene" width="800" height="600"></canvas>
</div>

<footer>
<h2>html5游戏制作入门系列教程(二)</h2>
<a class="stuts" href="http://html5gamedev.org/?p=304">返回原文 <span>html5游戏制作入门系列教程(二)</span></a>
</footer>
</body>
</html>

 

步骤2:CSS

下面是CSS样式。

/* general styles */
*{
    margin:0;
    padding:0;
}
@font-face {
    font-family: "DS-Digital";
    src: url("Ds-digib.ttf");
}
body {
    background-color:#bababa;
    background-image: -webkit-radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
    background-image: -moz-radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
    background-image: -o-radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
    background-image: radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
    color:#fff;
    font:14px/1.3 Arial,sans-serif;
    min-height:1000px;
}
.container {
    width:100%;
}
.container > * {
    display:block;
    margin:50px auto;
}
footer {
    background-color:#212121;
    bottom:0;
    box-shadow: 0 -1px 2px #111111;
    display:block;
    height:70px;
    left:0;
    position:fixed;
    width:100%;
    z-index:100;
}
footer h2{
    font-size:22px;
    font-weight:normal;
    left:50%;
    margin-left:-400px;
    padding:22px 0;
    position:absolute;
    width:540px;
}
footer a.stuts,a.stuts:visited{
    border:none;
    text-decoration:none;
    color:#fcfcfc;
    font-size:14px;
    left:50%;
    line-height:31px;
    margin:23px 0 0 110px;
    position:absolute;
    top:0;
}
footer .stuts span {
    font-size:22px;
    font-weight:bold;
    margin-left:5px;
}
h3 {
    text-align:center;
}
#scene {
    background-image:url(01.jpg);
    position:relative;
}

 

注意@ font-face的这种使用方式,自定义字体文件(TTF)连接到我们的教程中(在画布上绘制)。

 

步骤3: 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

    // draw the title text
    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;
    }

    // central object behavior
    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));

    // load the guide sprite image
    buttonImage = new Image();
    buttonImage.src = '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
});



 

下面是关于一些新功能代码的解释:
1)我们可以绘制文本(自定义字体)使用下面的代码,如下:


ctx.font = '42px DS-Digital';
ctx.textAlign = 'center';
ctx.fillStyle = '#ffffff';
ctx.fillText('Welcome to lesson #2', ctx.canvas.width/2, 50);


 

2)运用渐变色:



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.fillStyle = bg_gradient;



 

3)按钮 – 我用一个精灵图像,绘制按钮的三个状态,并添加悬停/事件的事件处理程序。下面是加载和绘制图像画布的方式:



buttonImage = new Image();
buttonImage.src = 'button.png';
.......
ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, width, height);



 

结论

超级酷,不是吗?我会很高兴看到您的评论和意见。祝你好运!

在线演示 源码下载

 

 


标签:circles,教程,入门,gradient,button,ctx,width,html5,var
From: https://blog.51cto.com/u_8895844/6441698

相关文章

  • html5游戏制作入门系列教程(一)
    从今天开始,我们将开始HTML5游戏开发一系列的文章。在我们的第一篇文章中,我们将讲解在画布canvas上的基础工作,创建简单的对象,填充和事件处理程序。另外,要注意在这个阶段中,我们不会立即学习WebGL相关的3D部分。但我们会尽快在未来的WebGL。 在每篇文章中,我们都将学习到一些新的东西......
  • Angular6 教程_编程入门自学教程_菜鸟教程-免费教程分享
    教程简介Angular6是一个JavaScript框架,用于构建JavaScript,html和TypeScript中的Web应用程序和应用程序,它是JavaScript的超集。它是Angular的一个更新版本,相当于angular的6.x版本。Angular6入门教程-从基本到高级概念的简单的步骤了解Angular6,其中包括概述,环境设置,项目设置,......
  • 正则表达式30分钟入门教程(第二版),正则讲解
    作者:mfkidt目录本文目标如何使用本教程什么是正则表达式?入门测试正则表达式元字符字符转义重复字符类反义替换分组后向引用位置指定负向位置指定注释贪婪与懒惰平衡组还有些什么东西没提到一些我认为你可能已经知道的术语的参考网上的资......
  • VSCode 插件开发系列教程
    VSCode插件架构,VSCode是通过Electron实现跨平台的,而Electron则是基于Chromium和Node.js,比如VSCode的界面,就是通过Chromium进行渲染的。同时,VSCode是多进程架构,当VSCode第一次被启动时会创建一个主进程(mainprocess),然后每个窗口,都会创建一个渲染进程(Renderer......
  • uniapp打包所需的ios证书和证书profile文件获取的图文教程
    使用uniapp进行云打包,可以打包android和ios两种app,但是uniapp官方并不能凭空产生这两种平台所需的打包证书。那么这两种打包证书又是如何获取呢?android相对简单,使用jdk的工具生成就可以了,也可以使用香蕉云编来一键生成。但是ios证书的生成就没有这么简单,因为ios证书的生成需要......
  • ESP32-C3入门教程——导读
    文章目录一、环境篇二、基础篇三、系统篇四、WiFi篇五、蓝牙篇六、网络篇七、IoT篇八、问题篇九、ESP-IDF5.x篇十、开源代码十一、视频演示关于更新进度有超链接的文章是已经完成的,可以点击跳转直接看没有超链接的文章是计划要写的,暂时还没写的,我会尽快完成一、环境篇ESP32-C3入......
  • 泰凌微8258入门教程——导读
    基于泰凌微TLSR8258Bluetooth®SIGMesh的入门系列教程本专栏第一阶段的博文撰写在2021年9月份全部完成,主要包括环境篇,开发套件购买+开发环境搭建+开发内容介绍Mesh基础篇,Mesh基础内容Mesh进阶篇,Mesh进阶内容,包含独门绝密内容本专栏第二阶段的博文撰写在2022年3月份再次开启......
  • PowerDesigner使用教程(转)
    PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一。Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesigner则与其正好相反,它是以数据库建模起家,后来才发展为一款综合全面的Case工具。PowerDesigner主要分为7种建模文......
  • Vue3从入门到精通(二)
    vue3侦听器在Vue3中,侦听器的使用方式与Vue2相同,可以使用watch选项或$watch方法来创建侦听器。不同之处在于,Vue3中取消了immediate选项,同时提供了新的选项和API。创建侦听器可以使用watch选项或$watch方法来创建侦听器,语法与Vue2相同。示例如下:<template><div>{{messa......
  • Makefile基础教程(自动生成依赖关系)
    @TOC前言在前面的文章中我们都只使用到了.c文件作为依赖但是在实际的工程中肯定是不可能只有.c文件的还存在.h文件,那么在包含了.h文件后又该如何来包含依赖关系呢?一、makefile不包含.h依赖的后果首先先在目录下新建四个文件夹,其中就包含了fun.h这个文件。makefile:OBJS:=fun.omai......