GameBuilder develops the 100-line code of the game application series to achieve covet and gamebuilder100 lines

Source: Internet
Author: User

GameBuilder develops the 100-line code of the game application series to achieve covet and gamebuilder100 lines

Online Preview: http://osgames.duapp.com/apprun.html? Appid = osgames1-801422234293697
Online Editing: http://osgames.duapp.com/gamebuilder.php? Appid = osgames1-801422234293697
SCAN:
Run:

In addition to gravity sensing games, GameBuilder is not inferior to traditional games. As a nostalgic person, it is always a special liking for such games.

The greedy snake mainly relies on a UICanvas. The previous blog GameBuilder developed the 100-line code implementation of the game application series. Don't step on the white block. We have introduced it, and used four UIButton to control the direction.

Food generation
win.makeFoodBlock = function(num) {    var bl = win.blockList;    var fl = win.foodList;    Math.min(48 * 72 - bl.length - fl.length, num);    var x, y;    for(var i = 0; i < num; i++) {        var block = {x:0, y:0};        do {            x = Math.floor(Math.random() * 48);            y = Math.floor(Math.random() * 72);            block.x = x; block.y = y;        } while(win.blockInList(block, win.blockList) || win.blockInList(block, win.foodList));        win.foodList.push(block);    }};
Direction Control

Implement the onClick event of UIButton.

Var win = this. getWindow (); win. changeDirection (win. DIR_UP); // play the sound effect this. playSoundEffect ("disappear.pdf ");
win.changeDirection = function(dir) {    switch(win.direction) {        case win.DIR_RIGHT:            if(dir == win.DIR_LEFT) {                return;            }            break;        case win.DIR_UP:            if(dir == win.DIR_DOWN) {                return;            }            break;        case win.DIR_LEFT:            if(dir == win.DIR_RIGHT) {                return;            }            break;        case win.DIR_DOWN:            if(dir == win.DIR_UP) {                return;            }            break;    }    win.direction = dir;};
Calculate the next position of the snake Header
win.nextFirst = function (first) {    var nextFirst = {x:0, y:0};    switch(win.direction) {        case win.DIR_RIGHT:            nextFirst.x = (first.x + 1) % 48;            nextFirst.y = first.y;            break;        case win.DIR_UP:            nextFirst.y = (first.y === 0) ? 71 : first.y - 1;            nextFirst.x = first.x;            break;        case win.DIR_LEFT:            nextFirst.x = (first.x === 0) ? 47 : first.x - 1;            nextFirst.y = first.y;            break;        case win.DIR_DOWN:            nextFirst.y = (first.y + 1) % 72;            nextFirst.x = first.x;    }    return nextFirst;};
Main logic of greedy snakes
Win. updateList = function () {var last = win. blockList [win. blockList. length-1]; var first = win. blockList [0]; var nf = win. nextFirst (first); // if (win. blockInList (nf, win. foodList) {win. removeBlockFromFL (nf); win. blockList. unshift (nf); win. find ("ui-sound-effects "). play ("disappear.pdf"); win. makeFoodBlock (1); if (48*72 = win. blockList. length) {win. openWindow ("win-result", function (ret ){ }, False, win. blockList. length); win. initGame () ;}} else if (win. blockInList (nf, win. blockList )&&! Win. checkEqual (nf, last) {// when the snake head hits the snake body, the game is over! Win. openWindow ("win-result", function (ret) {}, false, win. blockList. length); win. initGame (); console. log ("Game Over");} else {// traveling normally win. blockList. pop (); win. blockList. unshift (nf);} setTimeout (win. updateList, 150 );};
Draw a snake

Implements the onPaint interface of UICanvas.

Var win = this. getWindow (); var bl = win. blockList; var fl = win. foodList; var ctx = canvas2dCtx; for (var I = 0; bl! = Null & I <bl. length; I ++) {ctx. strokeStyle = "white"; ctx. fillStyle = "block"; ctx. strokeRect (bl [I]. x * 10, bl [I]. y * 10, 10, 10); ctx. fillRect (bl [I]. x * 10, bl [I]. y * 10, 10, 10) ;}for (var I = 0; fl! = Null & I <fl. length; I ++) {ctx. strokeRect (fl [I]. x * 10, fl [I]. y * 10, 10, 10); ctx. fillRect (fl [I]. x * 10, fl [I]. y * 10, 10, 10);} 100 lines of code are used because food is a list rather than a traditional food. This requires more code to maintain the food list.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.