Home > Article > Web Front-end > Chuanzhi Podcast JavaScript object-oriented completion of the Snake game video tutorial materials (courseware, source code) sharing
"Chuanzhi Podcast JavaScript object-oriented video tutorial to complete the Snake game" introduces the knowledge about JavaScript object-oriented, and uses object-oriented programming ideas to complete the Snake game. The writing is rather confusing, and there is a logical error: after the snake eats the fruit, it should add a section to the snake's tail, but it is written to add a section to the snake's head - -. You can use the up, down, left and right keys on the keyboard;
## Course playback address: http://www.php.cn/course/591.html
The teacher’s teaching style:
The more difficult point in this video is Snake:
ThoughtsThe main points of Snake The problem needs to be solvedTurn, each point of the snake body must turn when passing the turning pointTo eat, every time one is eaten, the snake body will increase by one pointFailure, hitting the wall or hitting the snake is considered a failureBasically, "Snake" is difficult in these three places. In this order, the difficulty is from high to low. The easiest thing is not to hit the wall. Judgment failed. The hardest part is steering, and then eating. Let’s solve these problems step by step from the very beginning. Some variablesvar mapItemX=60; //游戏地图横向点数 var mapItemY=31; //游戏地图纵向点数 var snakeLen=5; //蛇的初始长度 var snakeMoveDirection='E'; //蛇的移动方向 var snakeStartPoints={'x':5,'y':15}; //蛇的起始位置 var snake=new Array(); //用于存放蛇身点的坐标 var corner=new Array(); //用于存放转角点坐标 var cornerNum=0; //转角数 var timer,speed=100; //移动计时器和初始移动速度 var timeiner,timeSecond=0,timeMinute=0,timestr=0; //时间计时器,分,秒,总秒数 var mouseX,mouseY; //老鼠位置(吃的) var start=false; //是否开始Initializing the map
function init(){ var maps=''; for(var i=0;i<mapItemY;i++){ for(var j=0;j<mapItemX;j++){ maps=maps+'<p id="mapItem_'+j+'_'+i+'" class="mapItem"></p>'; } } $("#game_map").html(maps); //放地图的容器 }The map is very simple, but please note that it must start with 0,0 in the first line and 0,1 in the second line. , and so on, it is a two-dimensional array, which is directly related to positioning, so such a structure must be guaranteed. Each point generated has an ID based on vertical and horizontal coordinates, which will be the necessary thing to control these points
Here we also recommend downloading source code resources: http://www.php.cn/xiazai/learn/2117
The above is the detailed content of Chuanzhi Podcast JavaScript object-oriented completion of the Snake game video tutorial materials (courseware, source code) sharing. For more information, please follow other related articles on the PHP Chinese website!