javascript - Snake Game with Mobile Button Controls -


in snake game, need buttons control snake arrow keys do. allow game played on mobile device. can see have code in there buttons, not working , not controlling movement of snake.

any advice make buttons work appreciated! thanks!

html

<section class="game" id="share">  <div class="container">      <div class="columns twelve borders center">          <div class="game-container">              <div class="container">                  <div class="splashscreen">                     <h1>                         snake                     </h1>                     <h2>                         click start.                     </h2>                     <input class="startbutton" type="button" value="start" />                 </div>                  <div class="finishscreen" style="display:none">                     <h1>                         game on                     </h1>                     <p>                         score was: <span id="score"></span>                     </p>                     <input class="startbutton" type="button" value="restart" />                 </div>                  <canvas id="canvasarea" height="300" width="300" style="display:none;"></canvas>              </div>              <div class="button-pad">                 <div class="btn-up">                     <input type="image" src="http://aaronblomberg.com/sites/ez/images/btn-up.png" alt="up" class="button up-btn" />                 </div>                  <div class="btn-right">                     <input type="image" src="http://aaronblomberg.com/sites/ez/images/btn-right.png" alt="right" class="button right-btn" />                 </div>                  <div class="btn-down">                     <input type="image" src="http://aaronblomberg.com/sites/ez/images/btn-down.png" alt="down" class="button down-btn" />                 </div>                  <div class="btn-left">                     <input type="image" src="http://aaronblomberg.com/sites/ez/images/btn-left.png" alt="left" class="button left-btn" />                 </div>             </div>          </div>      </div>  </div>  </section> 

javascript

( function( $ ) {      $( function() {          $(document).ready(function () {              $(".startbutton").click(function () {                 $(".splashscreen").hide();                 $(".finishscreen").hide();                 $("#canvasarea").show();                 init();             });              //canvas stuff             var canvas = $("#canvasarea")[0];             var ctx = canvas.getcontext("2d");             var w = $("#canvasarea").width();             var h = $("#canvasarea").height();              //lets save cell width in variable easy control             var sw = 10;             var direction;             var nd;             var food;             var score;              //lets create snake             var snake_array; //an array of cells make snake              function endgame() {                 $("#canvasarea").hide();                 $("#score").text(score);                 $(".finishscreen").show();             }              function init() {                 direction = "right"; //default direction                 nd = [];                 create_snake();                 create_food(); //now can see food particle                 //finally lets display score                 score = 0;                  //lets move snake using timer trigger paint function                 //every 60ms                 if (typeof game_loop != "undefined") clearinterval(game_loop);                 game_loop = setinterval(paint, 60);             }              function create_snake() {                 var length = 5; //length of snake                 snake_array = []; //empty array start                 (var = length - 1; >= 0; i--) {                     //this create horizontal snake starting top left                     snake_array.push({                         x: i,                         y: 0                     });                 }             }              //lets create food             function create_food() {                 food = {                     x: math.round(math.random() * (w - sw) / sw),                     y: math.round(math.random() * (h - sw) / sw),                   };                 //this create cell x/y between 0-44                 //because there 45(450/10) positions accross rows , columns              }              //lets paint snake             function paint() {                 if (nd.length) {                     direction = nd.shift();                 }                  //to avoid snake trail need paint bg on every frame                 //lets paint canvas                 ctx.fillstyle = "#0056a0";                 ctx.fillrect(0, 0, w, h);                 ctx.strokestyle = "#ffffff";                 ctx.strokerect(0, 0, w, h);                  //the movement code snake come here.                 //the logic simple                 //pop out tail cell , place infront of head cell                 var nx = snake_array[0].x;                 var ny = snake_array[0].y;                  //these position of head cell.                 //we increment new head position                 //lets add proper direction based movement                 if (direction == "right") nx++;                 else if (direction == "left") nx--;                 else if (direction == "up") ny--;                 else if (direction == "down") ny++;                  //lets add game on clauses                 //this restart game if snake hits wall                 //lets add code body collision                 //now if head of snake bumps body, game restart                 if (nx == -1 || nx == w / sw || ny == -1 || ny == h / sw || check_collision(nx, ny, snake_array)) {                     //end game                     return endgame();                 }                  //lets write code make snake eat food                 //the logic simple                 //if new head position matches of food,                 //create new head instead of moving tail                 if (nx == food.x && ny == food.y) {                     var tail = {                         x: nx,                         y: ny                     };                     score++;                      //create new food                     create_food();                 } else                  {                     var tail = snake_array.pop(); //pops out last cell                     tail.x = nx;                     tail.y = ny;                 }                  //the snake can eat food.                 snake_array.unshift(tail); //puts tail first cell                  (var = 0; < snake_array.length; i++) {                     var c = snake_array[i];                      //lets paint 10px wide cells                     paint_cell(c.x, c.y);                 }                  //lets paint food                 paint_cell(food.x, food.y);                  //lets paint score                 var score_text = "score: " + score;                 ctx.fillstyle = "#ffffff";                 ctx.filltext(score_text, 5, h - 5);                  //set font , font size                 ctx.font = '12px arial';                  //position of fill text counter                 ctx.filltext(itemcounter, 10, 10);              }              //lets first create generic function paint cells             function paint_cell(x, y) {                 ctx.fillstyle = "#d8d8d8";                 ctx.fillrect(x * sw, y * sw, sw, sw);             }              function check_collision(x, y, array) {                 //this function check if provided x/y coordinates exist                 //in array of cells or not                 (var = 0; < array.length; i++) {                     if (array[i].x == x && array[i].y == y) return true;                 }                  return false;             }              // lets prevent default browser action arrow key usage             var keys = {};             window.addeventlistener("keydown",                 function(e){                     keys[e.keycode] = true;                     switch(e.keycode){                         case 37: case 39: case 38:  case 40: // arrow keys                         case 32: e.preventdefault(); break; // space                         default: break; // not block other keys                     }                 },             false);             window.addeventlistener('keyup',                 function(e){                     keys[e.keycode] = false;                 },             false);              //lets add keyboard controls             $(document).keydown(function (e) {                 var key = e.which;                 var td;                 if (nd.length) {                     var td = nd[nd.length - 1];                  } else {                     td = direction;                 }                  //we add clause prevent reverse gear                 if (key == "37" && td != "right") nd.push("left");                 else if (key == "38" && td != "down") nd.push("up");                 else if (key == "39" && td != "left") nd.push("right");                 else if (key == "40" && td != "up") nd.push("down");                  //the snake keyboard controllable              });          });           $(document).on('click', '.button-pad > button', function(e) {             if ($(this).hasclass('left-btn')) {                 e = 37;             }             else if ($(this).hasclass('up-btn')) {                 e = 38;             }             else if ($(this).hasclass('right-btn')) {                 e = 39;             }             else if ($(this).hasclass('down-btn')) {                 e = 40;             }             $.event("keydown", {keycode: e});         });       });  })( jquery ); 

fiddle

and time

so have errors going on. first 1 styling. need make styles more flexible, solve right button problem having.

.button-pad > div {     z-index: 9999;     width: 50px; } 

http://jsfiddle.net/34oeacnm/8/

 $(document).on('click', '.button-pad .button', function(e) {      var e = jquery.event("keydown");      if ($(this).hasclass('left-btn')) {         e.which = 37;      }      else if ($(this).hasclass('up-btn')) {         e.which = 38;      }      else if ($(this).hasclass('right-btn')) {          e.which = 39;      }      else if ($(this).hasclass('down-btn')) {         e.which = 40;      }      $(document).trigger(e); });  

you had of selectors off. , stole information on how trigger definitive way trigger keypress events jquery.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -