mirror of
https://github.com/flynx/Course-JavaScript.git
synced 2025-10-29 02:50:09 +00:00
more tweaking...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
d42e1531bd
commit
68f77acd88
@ -306,14 +306,15 @@ var KEY_CONFIG = {
|
|||||||
Left: ['left'],
|
Left: ['left'],
|
||||||
Right: ['right'],
|
Right: ['right'],
|
||||||
}
|
}
|
||||||
function kbHandler(event){
|
function kbHandler(snake){
|
||||||
|
return function(event){
|
||||||
//console.log('KEY:', event.key)
|
//console.log('KEY:', event.key)
|
||||||
var action = KEY_CONFIG[event.key]
|
var action = KEY_CONFIG[event.key]
|
||||||
action
|
action
|
||||||
&& action[0] in Snake
|
&& action[0] in snake
|
||||||
&& Snake[action[0]].apply(Snake, action.slice(1))
|
&& snake[action[0]].apply(snake, action.slice(1)) }}
|
||||||
}
|
function tapHandler(snake){
|
||||||
function tapHandler(event){
|
return function(event){
|
||||||
// top of screen...
|
// top of screen...
|
||||||
(event.clientY || event.changedTouches[0].pageY) <= (document.body.clientHeight / 8) ?
|
(event.clientY || event.changedTouches[0].pageY) <= (document.body.clientHeight / 8) ?
|
||||||
setup()
|
setup()
|
||||||
@ -323,43 +324,43 @@ function tapHandler(event){
|
|||||||
// left/right of screen...
|
// left/right of screen...
|
||||||
: (event.clientX || event.changedTouches[0].pageX) <= (document.body.clientWidth / 2) ?
|
: (event.clientX || event.changedTouches[0].pageX) <= (document.body.clientWidth / 2) ?
|
||||||
Snake.left()
|
Snake.left()
|
||||||
: Snake.right() }
|
: Snake.right() }}
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
// XXX need to place the snake with some headroom in the
|
||||||
|
// direction of travel...
|
||||||
function setup(snake){
|
function setup(snake){
|
||||||
snake = (snake || Snake)
|
snake = snake || Snake
|
||||||
.setup('.simplesnake')
|
|
||||||
|
|
||||||
|
// setup kb handler (only once)...
|
||||||
|
if(!HANDLER_SET){
|
||||||
|
document.addEventListener('keydown', kbHandler(snake))
|
||||||
|
//document.addEventListener('click', tapHandler)
|
||||||
|
document.addEventListener('touchstart', tapHandler(snake))
|
||||||
|
HANDLER_SET = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return snake
|
||||||
|
.setup('.simplesnake')
|
||||||
//.basicLevel()
|
//.basicLevel()
|
||||||
.randomLevel()
|
.randomLevel()
|
||||||
|
.start(150)
|
||||||
|
.pause()
|
||||||
|
|
||||||
|
// stuff...
|
||||||
.appleEaten(function(){ this.apple() })
|
.appleEaten(function(){ this.apple() })
|
||||||
.apple()
|
.apple()
|
||||||
.apple()
|
.apple()
|
||||||
|
|
||||||
// XXX need to place the snake with some headroom in the
|
// players...
|
||||||
// direction of travel...
|
|
||||||
.snakeKilled(function(color){
|
.snakeKilled(function(color){
|
||||||
this
|
this
|
||||||
.pause()
|
.pause()
|
||||||
.snake(color, null, null, 3)
|
.snake(color, null, null, 3)
|
||||||
})
|
})
|
||||||
.snake('blue', null, null, 3)
|
.snake('blue', null, null, 3)
|
||||||
|
|
||||||
.start(150)
|
|
||||||
.pause()
|
|
||||||
|
|
||||||
// setup kb handler (only once)...
|
|
||||||
if(!HANDLER_SET){
|
|
||||||
document.addEventListener('keydown', kbHandler)
|
|
||||||
//document.addEventListener('click', tapHandler)
|
|
||||||
document.addEventListener('touchstart', tapHandler)
|
|
||||||
HANDLER_SET = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return snake
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user