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://www.lively-kernel.org/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); }, 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') } });