Code viewer for World: Alex Murphy P5 chase World
AB.maxSteps        = 100;    

AB.screenshotStep  = 50;    

	// AB has its own timer separate to the draw timer. 

AB.drawRunControls = false;
		
const OBJECTSIZE 			= 150;							// Size of objects 
const CMOVESTEP   			= 10 ;			            	// How much to move each step 
const RMOVESTEP   			= 10 ;			            	// How much to move each step 
const CLOSE_DISTANCE		= OBJECTSIZE / 2 ;				// Get a point if this close  

const ACTION_LEFT 			= 0;		   
const ACTION_RIGHT 			= 1;
const ACTION_UP 			= 2;		 
const ACTION_DOWN 			= 3;
const ACTION_STAYSTILL 		= 4;

var tmp = getRandomInt(14);
let array = ['red', 'yellow', 'green', 'blue', 'brown', 'purple', 'pink', 'orange', 'lime', 'navy', 'grey', 'silver', 'olive', 'aqua'];

var fullwidth  = ABWorld.fullwidth();
var fullheight = ABWorld.fullheight();

// global variables:

var chase, run;     // direction for chaser and chased to move
var x, y;			// position of moving object
var tx, ty;			// position of target object

function preload() {
  images = loadImage('/uploads/alexmurphy1996/JohnTravolta.png');
  chaser = loadImage('/uploads/alexmurphy1996/Sam.L.Jackson.png');
  song = loadSound('uploads/alexmurphy1996/SayWhatAgain.mp3');
}


function setup() 		 
{
    createCanvas ( fullwidth, fullheight );		 
	 

  	background(array[tmp]);

// set objects start position  
	reset();	
}



function reset()	
// set the two objects to random positions within canvas 
// i.e. center should not be in 0 to max 
// but rather in OBJECTSIZE to max-OBJECTSIZE
{	
	x  = AB.randomIntAtoB ( OBJECTSIZE, fullwidth  - OBJECTSIZE );
	y  = AB.randomIntAtoB ( OBJECTSIZE, fullheight - OBJECTSIZE );
	
	tx = AB.randomIntAtoB ( OBJECTSIZE, fullwidth  - OBJECTSIZE );
	ty = AB.randomIntAtoB ( OBJECTSIZE, fullheight - OBJECTSIZE );
}
	
	

function draw()                
{		
	chasing();
}

function chasing() {
    var old = [ x, y, tx, ty ];
    
    if ( AB.randomBoolean() )				// randomly choose to track target x or y each step 
		{
			if ( x < tx ) {
			    chase = ACTION_RIGHT;
			    if((tx+CMOVESTEP)<(fullwidth-OBJECTSIZE)) {
			        run = ACTION_RIGHT;
			    }
			    else {
			        if((ty-CMOVESTEP)>0){
			            run = ACTION_UP;
			        }
			        else{
			            run = ACTION_LEFT
			        }
			    }
			}            
			else {
			    chase = ACTION_LEFT;
			    if(tx-CMOVESTEP>0) {
			        run = ACTION_LEFT;
			    }
			    else {
			        if((ty+CMOVESTEP)<(fullheight-OBJECTSIZE)){
			            run = ACTION_DOWN;
			        }
			        else{
			            run = ACTION_RIGHT
			        }
			    }
			}
		}
	else 
		{
			if ( y < ty ){
			    chase = ACTION_DOWN;
			    if((ty+CMOVESTEP)<(fullheight-OBJECTSIZE)){
			        run = ACTION_DOWN;
			    }
			    else{
    			    if(tx-CMOVESTEP>0) {
    			        run = ACTION_LEFT;
    			    }
    			    else{
			            run = ACTION_UP
			        }
			    }    			
			}
			else {
			    chase = ACTION_UP;
			    if((ty-CMOVESTEP)>0){
			        run = ACTION_UP;
			    }
			    else {
			        if((tx+CMOVESTEP)<(fullwidth-OBJECTSIZE)) {
			            run = ACTION_RIGHT;
			        }
			        else{
			            run = ACTION_DOWN
			        }
			    }
			}
		}
	// restore global variables from array, in case Mind changed them:
	x 		= old[0]; 
	y 		= old[1];
	tx 		= old[2]; 
	ty 		= old[3];

	// Mind can maybe cheat another way. No guarantees. See discussion:  
	// https://ancientbrain.com/fake.scores.php
	// --- end of call Mind --------------------------------------
	
	
	     if ( chase == ACTION_LEFT  	) x = x - CMOVESTEP ;
	else if ( chase == ACTION_RIGHT 	) x = x + CMOVESTEP ;
	else if ( chase == ACTION_UP		) y = y - CMOVESTEP ;
	else if ( chase == ACTION_DOWN 		) y = y + CMOVESTEP ;

	     if ( run == ACTION_LEFT  	) tx = tx - RMOVESTEP ;
	else if ( run == ACTION_RIGHT 	) tx = tx + RMOVESTEP ;
	else if ( run == ACTION_UP		) ty = ty - RMOVESTEP ;
	else if ( run == ACTION_DOWN 		) ty = ty + RMOVESTEP ;	
	
	// clear background:
  	background(array[tmp]); 
	
	// draw target object:
	image ( images, tx, ty, OBJECTSIZE, OBJECTSIZE ); 
	
	// draw moving object:
	image ( chaser, x, y, OBJECTSIZE, OBJECTSIZE ); 
	
	if ( AB.distance2D ( x, y, tx, ty ) < CLOSE_DISTANCE ) 
	{
	    background(0);
	    image ( chaser, 0, 0, fullwidth, fullheight );
	    song.play();
	    if (song.isPlaying()) {
	        noLoop();
	    }
	    else {
	        loop();
	        reset();
	    }
	}
}

function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}

// --- when re-size page, re-size canvas ----------------------------------------

function reSize()		 
{
	resizeCanvas ( fullwidth, fullheight ); 
	
 	background (  SKYCOLOR  ); 
}

if ( AB.onDesktop() )	window.addEventListener ( "resize", 			function() { 	reSize();  	 	});
else					window.addEventListener ( "orientationchange", 	function() { 	reSize();  	 	});