js遊戲引擎探索指南之Quintus

Quintus是一款易於上手、輕量級、開源的HTML5 JavaScript遊戲引擎,包含一個模塊化的引擎可輕鬆開發遊戲,並在同一個頁面上運行多個實例,支持桌面及移動平臺瀏覽器。Quintus引用面向對象的思想來進行HTML5遊戲開發,同時依賴於jQuery來提供事件處理機制和元素選取操作。


官網



一打開官網就有一個demoGame,左邊是代碼,修改後立即能看效果。下面還有針對這個代碼的簡單介紹,下面來粗略地說一下

先總體說一下,quintus提供的遊戲元素主要有Quintus(引擎),Stage(舞臺),Scene(場景),Sprite(精靈)。代碼的結構可以這樣:加載引擎,創建精靈和場景這些遊戲內容,加載資源(資源加載完畢就進入遊戲的第一個場景)。


開頭的工作:

把庫文件導入,本地或cdn獲取都行,2000多行的代碼。你還可以根據需要導入庫模塊。

<!-- Pull the engine from the Quintus CDN or load it locally -->
    <!-- (use quintus-all.min.js for production) -->
    <script src='http://cdn.html5quintus.com/v0.2.0/quintus-all.js'></script>

開始遊戲的代碼了,這裏是引擎的加載。創建引擎的實例,然後加載需要的模塊,往html頁面中添加canvas元素,添加默認的控制(鍵盤和按鈕),添加觸屏。這裏還提到一個頁面可以創建多個引擎實例,也就是說你一個頁面可以運行多個遊戲。很酷。

// Now set up your game (most games will load a separate .js file)
      var Q = Quintus()                          // Create a new engine instance
              .include("Sprites, Scenes, Input, 2D, Touch, UI") // Load any needed modules
              .setup()                           // Add a canvas element onto the page
              .controls()                        // Add in default controls (keyboard, buttons)
              .touch();                          // Add in touch support (for the UI)

創建精靈:

quintus混合支持了面向對象,基於事件以及基於組件機制。允許創建一個典型的繼承而來的模型同時支持添加可重用的組件。(這是編程模式,面向對象的繼承,基於組件各有千秋,quintus讓開發者兼得魚與熊掌)

// You can create a sub-class by extending the Q.Sprite class to create Q.Player(繼承引擎的Sprite來創建自定義的Player)
Q.Sprite.extend("Player",{

  // the init constructor is called on creation
  init: function(p) {
  
    // You can call the parent's constructor with this._super(..)
    this._super(p, {
      sheet: "player",  // Setting a sprite sheet sets sprite width and height
      x: 410,           // You can also set additional properties that can(在這裏設置繼承自父類的屬性吧)
      y: 90            // be overridden on object creation
    });
    
    // Add in pre-made components to get up and running quickly(基於組件的方式)
    this.add('2d, platformerControls');
    
    // Write event handlers to respond hook into behaviors.(事件處理)
    // hit.sprite is called everytime the player collides with a sprite
    this.on("hit.sprite",function(collision) {
      // Check the collision, if it's the Tower, you win!
      if(collision.obj.isA("Tower")) {
        // Stage the endGame scene above the current stage
        Q.stageScene("endGame",1, { label: "You Won!" }); 
        // Remove the player to prevent them from moving
        this.destroy();
      }
    });
  }
});

// Sprites can be simple, the Tower sprite just sets a custom sprite sheet
Q.Sprite.extend("Tower", {
  init: function(p) {
    this._super(p, { sheet: 'tower' });
  }
});

// Create the Enemy class to add in some baddies
Q.Sprite.extend("Enemy",{
  init: function(p) {
    this._super(p, { sheet: 'enemy', vx: 100 });
    
    // Enemies use the Bounce AI to change direction 
    // whenver they run into something.
    this.add('2d, aiBounce');
    
    // Listen for a sprite collision, if it's the player,
    // end the game unless the enemy is hit on top
    this.on("bump.left,bump.right,bump.bottom",function(collision) {
      if(collision.obj.isA("Player")) { 
        Q.stageScene("endGame",1, { label: "You Died" }); 
        collision.obj.destroy();
      }
    });
    
    // If the enemy gets hit on the top, destroy it
    // and give the user a "hop"
    this.on("bump.top",function(collision) {
      if(collision.obj.isA("Player")) { 
        this.destroy();
        collision.obj.p.vy = -300;//這裏是讓玩家向上自動彈跳,馬里奧踩烏龜的效果知道吧
      }
    });
  }
});

設置場景

quintus可以讓你輕易創建可重用的場景。通過加載它們到舞臺中可以切換場景。在棧結構上來看,舞臺在每個場景的頂層。(姑且理解爲舞臺管理各個場景吧,quintus一個引擎實例默認提供一個舞臺實例。有經驗的遊戲開發者對這方面不難理解,很簡單的,在這裏還沒有說生命週期這些較複雜的東西)

<span style="font-family:Helvetica, Tahoma, Arial, sans-serif;">// Create a new scene called level 1
Q.scene("level1",function(stage) {

  // Add in a tile layer, and make it the collision layer
  stage.collisionLayer(new Q.TileLayer({
                             dataAsset: 'level.json',
                             sheet:     'tiles' }));
                             
  // Create the player and add him to the stage
  var player = stage.insert(new Q.Player());
  
  // Give the stage a moveable viewport and tell it
  // to follow the player.
  stage.add("viewport").follow(player);
  
  // Add in a couple of enemies
  stage.insert(new Q.Enemy({ x: 700, y: 0 }));
  stage.insert(new Q.Enemy({ x: 800, y: 0 }));
  
  // Finally add in the tower goal
  stage.insert(new Q.Tower({ x: 180, y: 50 }));
});

// To display a game over / game won popup box, 
// create a endGame scene that takes in a `label` option
// to control the displayed message.
Q.scene('endGame',function(stage) {
  var container = stage.insert(new Q.UI.Container({
    x: Q.width/2, y: Q.height/2, fill: "rgba(0,0,0,0.5)"
  }));
  
  var button = container.insert(new Q.UI.Button({ x: 0, y: 0, fill: "#CCCCCC",
                                                  label: "Play Again" }))         
  var label = container.insert(new Q.UI.Text({x:10, y: -10 - button.p.h, 
                                                   label: stage.options.label }));//stage.options.label是切換該場景時傳入的
  // When the button is clicked, clear all the stages
  // and restart the game.
  button.on("click",function() {
    Q.clearStages();
    Q.stageScene('level1');
  });
  
  // Expand the container to visibily fit it's contents(佈局的設置,影響面板外圍的大小)
  container.fit(20);
});</span>

加載和運行:

quintus提供了加載資源的簡單方式。舞臺調用場景以開始遊戲的運行。

// Q.load can be called at any time to load additional assets
// assets that are already loaded will be skipped(除了在遊戲開始,可以隨時調用的Q.load也能加載後期需要的資源,對於加載過的資源會忽///略)
Q.load("sprites.png, sprites.json, level.json, tiles.png",
  // The callback will be triggered when everything is loaded(下面的函數是在資源加載完畢後調用的)
  function() {
    // Sprites sheets can be created manually
    Q.sheet("tiles","tiles.png", { tilew: 32, tileh: 32 });
    
    // Or from a .json asset that defines sprite locations(compoleSheets是集合了很多圖片的,用過texturePacker的自然知道)
    Q.compileSheets("sprites.png","sprites.json");
    
    // Finally, call stageScene to run the game
    Q.stageScene("level1");
  });
以上就是看了quintus主頁後寫的,很喜歡主頁的設計,提供一個隨改隨測的demo讓開發者先體驗體驗,真正的Let's start。你們可以去試試,說不定就選中quintus了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章