Lively Kernel canvas
getRandomNumber = function(max) { return Math.floor(Math.random()*max+1)-1};
ImageMorph.subclass('Mouse', {
howFarCanIGoWithOneStep: 5,
maxTurn: Math.PI/2,
initialize: function($super) {
	// call the initialize of ImageMorph with the mouse picture
	$super(new Rectangle(45, 45, 49, 49), 'http://livelykernel.sunlabs.com/repository/lively-wiki/mouse.png');
	// move position to the center of the morph instead of top left corner, nicer rotating behavior
	this.moveOriginBy(this.getExtent().scaleBy(0.5));
	// remove the blue background
	this.setFill(null);
},
calculateNewPosition: function() {
	var p = this.getPosition();
	var angle = this.getRotation()+Math.PI*(1/2);
	var r = this.howFarCanIGoWithOneStep;
	return pt(p.x+r*Math.cos(angle), p.y+r*Math.sin(angle));
},
moveOneStep: function() {
	this.mayBeChangeDirection();
	var newPos = this.calculateNewPosition();
	while (!WorldMorph.current().bounds().containsPoint(newPos)) {
		this.newDirection(); 
		newPos = this.calculateNewPosition();
	}
	this.setPosition(newPos);
	this.eatCheese();
},
mayBeChangeDirection: function() { if (getRandomNumber(10) == 5) this.newDirection() },
newDirection: function() {
	this.rotateBy(getRandomNumber(this.maxTurn*100)/100 - this.maxTurn/2);
},
startSteppingScripts: function() { this.startStepping(20, 'moveOneStep') },
eatCheese: function() {
	this.owner.submorphs
		.select(function(ea) { return ea.isCheese && this.bounds().intersect(ea.bounds()) }, this)
		.forEach(function(ea) { ea.remove(); console.log('Hmmm, delicious!') });
}
});
Example2/3301
ChangingthedirectionNowthemouseshouldchangeitsdirectionbyitselfeverysooften.ForthatahelpermethodishandywhichwrapsMath.random()andgeneratearandomnumberbetween0andaparameter:getRandomNumber=function(max){returnMath.floor(Math.random()*max+1)-1};Withthattwonewmethodsfordirectionchangingcanbeimplemented:Mouse.addMethods({maxTurn:Math.PI/2,//defineshowabruptlythedirectioncanbechangedmayBeChangeDirection:function(){if(getRandomNumber(10)==5)this.newDirection()},newDirection:function(){this.rotateBy(getRandomNumber(this.maxTurn*100)/100-this.maxTurn/2);}});Additionallythemouseshouldnotdisappear.ForthatyoucandefinethatthemouseturnswhenithaslefttheboundsoftheWordMorph:Mouse.addMethods({moveOneStep:function(){this.mayBeChangeDirection();//changethedirectionsometimesvarp=this.getPosition();varangle=this.getRotation()+Math.PI*(1/2);//inwhichdirectiondoIlook?varr=10;//the'radius'orhowfarIcangowithonestepvarnewPos=pt(p.x+r*Math.cos(angle),p.y+r*Math.sin(angle));//stayonscreenwhile(!WorldMorph.current().bounds().containsPoint(newPos)){this.newDirection();varangle=this.getRotation()+Math.PI*(1/2);newPos=pt(p.x+r*Math.cos(angle),p.y+r*Math.sin(angle));}this.setPosition(newPos);}});Tryitoutagain.CreateanewmouseandcallmoveOneStepmultipletimes,likethis:mouse=newMouse();mouse.openInWorld();mouse.startSteppingScripts();//removewhendonemouse.remove();38
20464.29524178564631.083640266615153 
Next22
Previous22
10006.974955070893536Wikicontroltrue 
null22