亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

html5游戲制作入門系列教程(三)

系統(tǒng) 1561 0

今天,我們繼續(xù)一系列文章,使用HTML5的canvas組件進(jìn)行游戲開發(fā)。接下來,我們將開始學(xué)習(xí)如何添加動(dòng)畫以及一些更有趣的功能。我 們的演示將包括一艘太空船飛越時(shí)空,并使用一個(gè)新的游戲元素 – 對(duì)話框。對(duì)話框?qū)瑑身摚覀兊陌粹o用來切換對(duì)話框的頁面,并隱藏對(duì)話框。

?

你可以點(diǎn)擊這里閱讀這一系列教程的前一篇文章: html5游戲制作入門系列教程(二) 。我們的將基于之前的程序和代碼進(jìn)行開發(fā)。

這里有我們的演示和下載包:

在線演示 源碼下載

?

好吧,下載所需文件,讓我們開始編碼!

?

步驟1: HTML
這里是我演示的HTML

        <!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>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" >返回原文 <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 {
    position:relative;
}
      

?

步驟3: JS

        // inner variables
var canvas, ctx;
var button;
var backgroundImage;
var spaceShip;
var iBgShiftX = 1024;
var bDrawDialog = true;
var iDialogPage = 1;
// -------------------------------------------------------------

// objects :
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;
}
function SpaceShip(x, y, w, h, image) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.image = image;
    this.bDrag = false;
}
// -------------------------------------------------------------

// draw functions :

function clear() { // clear canvas function
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}

function drawDialog() { // draw dialog function
    if (bDrawDialog) {
        var bg_gradient = ctx.createLinearGradient(0, 200, 0, 400);
        bg_gradient.addColorStop(0.0, 'rgba(160, 160, 160, 0.8)');
        bg_gradient.addColorStop(1.0, 'rgba(250, 250, 250, 0.8)');

        ctx.beginPath(); // custom shape begin
        ctx.fillStyle = bg_gradient;
        ctx.moveTo(100, 100);
        ctx.lineTo(700, 100);
        ctx.lineTo(700, 500);
        ctx.lineTo(100, 500);
        ctx.lineTo(100, 100);
        ctx.closePath(); // custom shape end
        ctx.fill(); // fill custom shape

        ctx.lineWidth = 2;
        ctx.strokeStyle = 'rgba(128, 128, 128, 0.5)';
        ctx.stroke(); // draw border

        // draw the title text
        ctx.font = '42px DS-Digital';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'top';
        ctx.shadowColor = '#000';
        ctx.shadowOffsetX = 2;
        ctx.shadowOffsetY = 2;
        ctx.shadowBlur = 2;
        ctx.fillStyle = '#fff';
        if (iDialogPage == 1) {
            ctx.fillText('Welcome to lesson #3', ctx.canvas.width/2, 150);
            ctx.font = '24px DS-Digital';
            ctx.fillText('After closing dialog you will able', ctx.canvas.width/2, 250);
            ctx.fillText('to handle with spaceship with your mouse', ctx.canvas.width/2, 280);
        } else if (iDialogPage == 2) {
            ctx.fillText('Second page of dialog', ctx.canvas.width/2, 150);
            ctx.font = '24px DS-Digital';
            ctx.fillText('Any another text', ctx.canvas.width/2, 250);
        }
    }
}

function drawScene() { // main drawScene function
    clear(); // clear canvas

    // draw background
    iBgShiftX -= 10;
    if (iBgShiftX <= 0) {         
       iBgShiftX = 1024;     
    }     
    ctx.drawImage(backgroundImage, 0 + iBgShiftX, 0, 1024, 768, 0, 0, 800, 600);     
    // draw space ship
    ctx.drawImage(spaceShip.image, 0, 0, spaceShip.w, spaceShip.h, spaceShip.x-128, spaceShip.y-128, spaceShip.w,spaceShip.h);     
    // draw dialog     
    drawDialog();     
   // draw button     
   ctx.drawImage(button.image, 0, button.imageShift, button.w, button.h, button.x, button.y, button.w, button.h);       
   // draw button's text     
   ctx.font = '22px DS-Digital';     
   ctx.fillStyle = '#ffffff';     
   ctx.fillText('next/hide/show', 400, 465);     
   ctx.fillText('dialog', 400, 500); } 

   // ------------------------------------------------------------- 
   // initialization 
   $(function(){     
      canvas = document.getElementById('scene');     
      ctx = canvas.getContext('2d');     
      var width = canvas.width;     
      var height = canvas.height;     
      // load background image     
      backgroundImage = new Image();     
      backgroundImage.src = 'stars.jpg';     
      backgroundImage.onload = function() {}     
      backgroundImage.onerror = function() {
         console.log('Error loading the background image.');}     
      // initialization of space ship     
      var oSpShipImage = new Image();     
      oSpShipImage.src = 'space_ship.png';     
      oSpShipImage.onload = function() {}     
      spaceShip = new SpaceShip(400, 300, 256, 256, oSpShipImage);     
      // load the button sprite image     
      var buttonImage = new Image();     
      buttonImage.src = 'button.png';     
      buttonImage.onload = function() {}
      button = new Button(310, 450, 180, 120, 'normal', buttonImage);
      $('#scene').mousedown(function(e) { 
          // binding mousedown event (for dragging)         
          var mouseX = e.layerX || 0; 
          var mouseY = e.layerY || 0;      
          if (!bDrawDialog && mouseX > spaceShip.x-128 && mouseX < spaceShip.x-128+spaceShip.w &&mouseY > spaceShip.y-128 && mouseY < spaceShip.y-128+spaceShip.h) {  
               spaceShip.bDrag = true;             
               spaceShip.x = mouseX;             
               spaceShip.y = mouseY;         
          }
          // 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
         var mouseX = e.layerX || 0;
         var mouseY = e.layerY || 0;
         if (!bDrawDialog && spaceShip.bDrag) {
            spaceShip.x = mouseX; 
            spaceShip.y = mouseY;
         }
         // 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) { // binding mouseup event
        spaceShip.bDrag = false;

        // button behavior
        if (button.state == 'pressed') {
            if (iDialogPage == 0) {
                iDialogPage++;
                bDrawDialog = !bDrawDialog;
            } else if (iDialogPage == 2) {
                iDialogPage = 0;
                bDrawDialog = !bDrawDialog;
            } else {
                iDialogPage++;
            }
        }
        button.state = 'normal';
        button.imageShift = 0;
    });

    setInterval(drawScene, 30); // loop drawScene
});
      

?

下面是關(guān)于一些新功能代碼的解釋:
1)我們繪制星空背景的移動(dòng),使用下面的代碼:

        iBgShiftX -= 10;
if (iBgShiftX <= 0) {
    iBgShiftX = 1024;
}
ctx.drawImage(backgroundImage, 0 + iBgShiftX, 0, 1024, 768, 0, 0, 800, 600);
      

?

結(jié)論
超級(jí)酷,不是嗎?我會(huì)很高興看到您的評(píng)論和意見。祝你好運(yùn)!

在線演示 源碼下載

?

轉(zhuǎn)載請(qǐng)注明: HTML5游戲開發(fā)者社區(qū) ? html5游戲制作入門系列教程(三)

html5游戲制作入門系列教程(三)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 成人小视频免费在线观看 | 亚洲午夜久久影院 | 亚洲色啦啦狠狠网站 | 狠狠躁天天躁夜夜躁婷婷 | 免费播放欧美毛片欧美a | 日日摸夜夜添夜夜添欧美毛片 | 国产在线精品观看 | chinese在线播放91国内 | 久久日精品 | 99精品一区二区三区 | 欧美视频在线视频 | 四虎影院永久网站 | 国产高清美女一级a毛片久久w | 久久机热一这里只精品 | 天天躁夜夜躁狂狂躁综合 | 亚洲国产精品成人午夜在线观看 | 精品一区二区三区在线成人 | 人人澡人人澡人人看欧美 | 国产伦久视频免费观看 视频 | 四虎www成人影院免费观看 | 国产臀控福利视频在线 | 中文字幕 日韩在线 | 毛片大全免费 | 久久97精品久久久久久久看片 | 91热爆在线 | 激情午夜网 | 中文字幕亚洲在线 | 色黄网站成年女人色毛片 | 四虎新网站 | 毛片女人十八以上观看 | 欧美成人老熟妇暴潮毛片 | 国产精品一区二区在线播放 | 免费一级欧美大片久久网 | 国产亚洲第一伦理第一区 | 高清色视频 | 手机看片福利盒子久久青 | 色中色污 | 国产乱子伦手机在线 | 99热99re8国产在线播放 | 亚洲第一区二区快射影院 | 精品免费国产一区二区三区 |