domready
" event provided by domready
.window . addEvent ( 'domready' , function () {
//
var elem = $$( 'canvas' )[ 0 ];
// LibCanvas
var libcanvas = new LibCanvas . Canvas2D ( elem );
// ,
libcanvas . autoUpdate = true ;
// 60 fps
libcanvas . fps = 60 ;
//
libcanvas . start ();
});
libcanvas.start();
:libcanvas . listenMouse ();
var player = new Player (). setZIndex ( 30 );
libcanvas . addElement ( player );
libcanvas . addProcessor ( 'pre' ,
new LibCanvas . Processors . Clearer ( '#000' )
);
// ,
var bait = new Bait (). setZIndex ( 20 );
libcanvas . addElement ( bait );
var Player = new Class({
// ...
initialize : function () {
// ..
this . shape = new LibCanvas . Shapes . Circle ({
center : this . createPosition ()
// ...
},
createPosition : function () {
return this . libcanvas . mouse . point ;
},
var Bait = new Class({
Extends : GameObject ,
radius : 15 ,
color : '#f0f'
});
var Player = new Class({
Extends : GameObject ,
radius : 15 ,
color : '#080' ,
createPosition : function () {
return this . libcanvas . mouse . point ;
},
draw : function () {
if ( this . libcanvas . mouse . inCanvas ) {
this . parent ();
}
}
});
(function(){
bait . isCatched ( player );
}. periodical ( 30 ));
isCatched : function ( player ) {
if ( player . shape . intersect ( this . shape )) {
this . move ();
return true ;
}
return false ;
},
move : function () {
//
this . shape . center = this . createPosition ();
}
var Bait = new Class({
Extends : GameObject ,
Implements : [ LibCanvas . Behaviors . Moveable ],
// ...
move : function () {
// (800),
this . moveTo ( this . createPosition (), 800 );
}
});
var Barrier = new Class({
Extends : GameObject ,
full : null ,
speed : null ,
radius : 8 ,
color : '#0ff' ,
initialize : function () {
this . parent ();
this . speed = new LibCanvas . Point (
$random ( 2 , 5 ), $random ( 2 , 5 )
);
// ,
$random ( 0 , 1 ) && ( this . speed . x *= - 1 );
// ,
$random ( 0 , 1 ) && ( this . speed . y *= - 1 );
},
move : function () {
this . shape . center . move ( this . speed );
return this ;
},
intersect : function ( player ) {
return ( player . shape . intersect ( this . shape ));
}
});
bait . isCatched ( player );
//
if ( bait . isCatched ( player )) {
player . createBarrier ();
}
player . checkBarriers ();
barriers : [],
createBarrier : function () {
var barrier = new Barrier (). setZIndex ( 10 );
this . barriers . push ( barrier );
// libcanvas,
this . libcanvas . addElement ( barrier );
return barrier ;
},
checkBarriers : function () {
for (var i = this . barriers . length ; i --;) {
if ( this . barriers [ i ]. move (). intersect ( this )) {
this .die();
return true ;
}
}
return false ;
},
die : function () { },;
draw : function () {
// ...
this . libcanvas . ctx . text ({
text : 'Score : ' + this . barriers . length ,
to : [ 20 , 10 , 200 , 40 ],
color : this . color
});
},
// .. - - -
die : function () {
for (var i = this . barriers . length ; i --;) {
this . libcanvas . rmElement ( this . barriers [ i ]);
}
this . barriers = [];
}
libcanvas.listenMouse()
to libcanvas.listenKeyboard()
player.checkMovement();
to the timeout player.checkMovement();
.draw
method, we remove the mouse check and implement the movement using the arrows:speed : 8 ,
checkMovement : function () {
var pos = this . shape . center ;
if ( this . libcanvas . getKey ( 'left' )) pos . x -= this . speed ;
if ( this . libcanvas . getKey ( 'right' )) pos . x += this . speed ;
if ( this . libcanvas . getKey ( 'up' )) pos . y -= this . speed ;
if ( this . libcanvas . getKey ( 'down' )) pos . y += this . speed ;
},
isMoveTo : function ( dir ) {
return this . libcanvas . getKey ( dir );
},
checkMovement : function () {
var pos = this . shape . center ;
var full = this . getFull ();
if ( this . isMoveTo ( 'left' ) && pos . x > 0 ) pos . x -= this . speed ;
if ( this . isMoveTo ( 'right' ) && pos . x < full . width ) pos . x += this . speed ;
if ( this . isMoveTo ( 'up' ) && pos . y > 0 ) pos . y -= this . speed ;
if ( this . isMoveTo ( 'down' ) && pos . y < full . height ) pos . y += this . speed ;
},
control : {
up : 'up' ,
down : 'down' ,
left : 'left' ,
right : 'right'
},
isMoveTo : function ( dir ) {
return this . libcanvas . getKey ( this . control [ dir ]);
},
var player = new Player (). setZIndex ( 30 );
libcanvas . addElement ( player );
// =>
var players = [];
( 2 ). times (function ( i ) {
var player = new Player (). setZIndex ( 30 + i );
libcanvas . addElement ( player );
players . push ( player );
});
//
players [ 1 ]. color = '#ff0' ;
players [ 1 ]. control = {
up : 'w' ,
down : 's' ,
left : 'a' ,
right : 'd'
};
players . each (function ( player ) { /* * */ });
players . each (function ( player ) { /* * */ });
var Player = new Class({
// ...
// :
createBarrier : function () {
// ...
barrier . color = this . barrierColor || this . color ;
// ...
},
//
maxScore : 0 ,
die : function () {
this . maxScore = Math . max ( this . maxScore , this . barriers . length );
// ...
},
index : 0 ,
draw : function () {
this . parent ();
this . libcanvas . ctx . text ({
// :
text : 'Score : ' + this . barriers . length + ' (' + this . maxScore + ')' ,
// 20 :
to : [ 20 , 10 + 20 * this . index , 200 , 40 ],
color : this . color
});
}
});
( 2 ). times (function ( i ) {
var player = new Player (). setZIndex ( 30 + i );
player . index = i ;
// ...
});
players [ 0 ]. color = '#09f' ;
players [ 0 ]. barrierColor = '#069' ;
//
players [ 1 ]. color = '#ff0' ;
players [ 1 ]. barrierColor = '#960' ;
players [ 1 ]. control = {
up : 'w' ,
down : 's' ,
left : 'a' ,
right : 'd'
};
players [ 2 ]. color = '#f30' ;
players [ 2 ]. barrierColor = '#900' ;
players [ 2 ]. control = {
up : 'i' ,
down : 'k' ,
left : 'j' ,
right : 'l'
};
// players[0] uses numpad
// players[3] uses home end delete & pagedown
players [ 3 ]. color = '#3f0' ;
players [ 3 ]. barrierColor = '#090' ;
players [ 3 ]. control = {
up : '$' ,
down : '#' ,
left : 'delete' ,
right : '"'
};
Source: https://habr.com/ru/post/102010/
All Articles