HTML5版的String Avoider小遊戲

來源:互聯網
上載者:User

標籤:

HTML5版的String Avoider小遊戲 http://www.newgrounds.com/portal/view/300760 蠻簡單也蠻考驗耐心,從遊戲起始點移動滑鼠到終點位置,滑鼠移動過程繪製出移動軌跡的String平滑曲線,整個過程不能碰撞到邊界,從技術角度來說其核心就是根據滑鼠移動位置產生String線的演算法,該遊戲是ActionScript寫的Flash版,這裡將其改造成HTML5版的JavaScript實現,並增加可自訂關卡功能的一種設計思路。

String連線我是緩衝了300個點資訊的數組,滑鼠移動時不斷調整300個點的新位置資訊,每次更新時先將新滑鼠點設定給第一個元素,後續更新x點時,計算其與x-1點的角度,在此方向長度為1的位置更新座標,這樣就達到了平滑曲線的效果。

除了繪製String線外還有個技術點就是監測碰撞,該Flash遊戲的邊界都是線段,因此第一想到的監測方式就是線線相交的思路,演算法可參考 http://en.wikipedia.org/wiki/Line%E2%80%93line_intersection ,如果以LineLine的相交思路只需要遍曆所有point間的線段,判斷是否與遊戲關卡定義的邊界線相交,但這種方式對不規則邊界就比較麻煩,監測效能也不高。

考慮到我們還需要提供使用者可DIY自訂遊戲關卡的功能,我們將採用監測顏色透明度資訊的方式,由於正常遊戲時情境無需使用者動態修改,因此邊界的資訊可提前緩衝到ImageData記憶體中,並且我們300個點的距離都是1,監測只需根據點進行就可以。

整個程式採用HT for Web的GraphView拓撲圖組件,再其上通過addTopPainter添加頂層畫筆繪製曲線,當曲線碰到Node圖元時繪製成紅色,否則繪製成黃色,監聽GraphView拓撲圖的interaction事件,在該事件中設定dirty的髒標誌,在繪製時根據dirty的標誌進行更新,採用這樣的方式可將多次變換最終彙總成一次更新,這也是圖形重新整理繪製常用的基本技巧。同時通過GraphView.setEditable(true)開啟了拓撲圖的可視化編輯功能,使用者可隨時改變介面圖元位置和旋轉等圖形資訊,相當於自訂關卡的效果。

所有代碼和運行效果如下:http://v.youku.com/v_show/id_XODU4NzY5MzQ0.html

function init(){                    dataModel = new ht.DataModel();                   graphView = new ht.graph.GraphView(dataModel); graphView.handleScroll = function(){};graphView.setEditable(true);graphView.setPannable(false)view = graphView.getView();view.style.left = ‘10px‘;view.style.top = ‘10px‘;view.style.width = ‘600px‘;view.style.height = ‘400px‘;view.style.background = ‘black‘;document.body.appendChild(view);createNode(20, 20, 80, 40, ‘rect‘);                createNode(200, 300, 80, 40, ‘star‘);createNode(400, 100, 80, 40, ‘oval‘);createShape();length = 1;count = 300;points = [];for(var i=0; i<count; i++){points.push({x: 0, y: 0});}view.addEventListener(‘mousemove‘, function(e){var point = graphView.lp(e);points[0].x = point.x;points[0].y = point.y;for (var i = 1; i < count - 1; i++) {var angle = Math.atan2(points[i].y - points[i - 1].y, points[i].x - points[i - 1].x);points[i].x = points[i - 1].x + length * Math.cos(angle);points[i].y = points[i - 1].y + length * Math.sin(angle);}if(imageData){hit = false;for (var i = 0; i < count; i++) {var x = Math.floor(points[i].x);var y = Math.floor(points[i].y);var index = (y * graphView.getWidth() + x) * 4;if(imageData.data[index+3] !== 0){hit = true;break;}}                        }                    graphView.redraw();});dirty = true;                imageData = null;graphView.mi(function(e){dirty = true;});                graphView.addTopPainter(function(g){if(dirty){dirty = false;    hit = false;imageData = g.getImageData(0, 0, graphView.getWidth(), graphView.getHeight());                                                                                                                                              ht.Default.callLater(graphView.redraw, graphView);  }else{g.beginPath();g.lineWidth = 3;g.strokeStyle = hit ? ‘red‘ : ‘yellow‘;                   g.moveTo(points[0].x, points[0].y);for (var i = 1; i < count - 1; i++) {g.lineTo(points[i].x, points[i].y);}g.stroke();                        }});}    function createNode(x, y, w, h, shape){var node = new ht.Node();node.setRect(x, y, w, h);node.s({‘shape‘: shape,‘select.width‘: 0});dataModel.add(node);return node;}


HTML5版的String Avoider小遊戲

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.