Difference between revisions of "Today"

From CodeStuff
Jump to: navigation, search
 
(7 intermediate revisions by the same user not shown)
Line 6: Line 6:
 
*[[Keycodes]]
 
*[[Keycodes]]
 
*[[Functions]]
 
*[[Functions]]
 +
*[[Loops]]
 +
*[[Aiming]]
  
 
The [[API|API page]] has a list of some of the functions you can use such as  
 
The [[API|API page]] has a list of some of the functions you can use such as  
Line 15: Line 17:
  
  
<edcode>var cx=320;
+
<edcode>var c = {x:320, y:240, lastx:320, lasty:240, power:0.1, penSize:4};
var cy=240;
+
  
var maryx=200;
+
var gameOver = { image : loadImage("http://fingswotidun.com/images/GameOver.png"),
var maryy=240;
+
                x:0, y: -10000,
 +
                endx: 0, endy: 150};
 +
               
 +
var gameAge=0;
 +
var stillAlive=true;
  
var frankx=380;
+
var mary = {x:200, y:240, speed:0.35,colour:"blue"};
var franky=240;
+
var frank = {x:380, y:240, speed:0.2,colour:"cyan"};
 +
var bob = {x:580, y:100, speed: 0.25,colour:"lightgreen"};
  
var power=0.1;
+
var chasers = [mary,frank,bob];
var penSize=4;
+
  
var lastx=cx;
+
chasers.add({x:100,y:100});
var lasty=cy;
+
  
function move() {     
+
function addChaser(x,y,speed=0.3,colour="yellow") {
   var dx = cx-lastx;
+
  chasers.add({x,y,speed,colour});
   var dy = cy-lasty;
+
}
 +
 
 +
addChaser(200,100);
 +
addChaser(300,400,0.5);
 +
addChaser(400,400,0.45,"magenta");
 +
 
 +
function drawSpot(colour,x,y) {
 +
  setColour("red");
 +
  fillCircle(x,y,10);
 +
  setColour(colour);
 +
  fillCircle(x,y,8);
 +
}
 +
 
 +
function moveTowards(seeker,target) {
 +
  if (seeker.y < target.y) {
 +
    seeker.y+=seeker.speed;
 +
  }
 +
 
 +
  if (seeker.y > target.y) {
 +
    seeker.y-=seeker.speed;
 +
  }
 +
 
 +
  if (seeker.x <target.x) {
 +
    seeker.x+=seeker.speed;
 +
  }
 +
 
 +
  if (seeker.x > target.x) {
 +
    seeker.x-=seeker.speed;
 +
  }
 +
 
 +
}
 +
 
 +
function play() {     
 +
   var dx = c.x-c.lastx;
 +
   var dy = c.y-c.lasty;
  
   lastx=cx;
+
   c.lastx=c.x;
   lasty=cy;
+
   c.lasty=c.y;
 
    
 
    
   cx=cx+dx;
+
   c.x=c.x+dx;
   cy=cy+dy;
+
   c.y=c.y+dy;
 
    
 
    
 
     // the arrow keys have key codes 37,38,39 and 40
 
     // the arrow keys have key codes 37,38,39 and 40
 
    
 
    
 
   if (keyIsDown(38)) {
 
   if (keyIsDown(38)) {
     cy-=power;  
+
     c.y-=c.power;  
 
   }
 
   }
  
 
   if (keyIsDown(40)) {
 
   if (keyIsDown(40)) {
     cy+=power;   
+
     c.y+=c.power;   
 
   }
 
   }
  
 
   if (keyIsDown(37)) {
 
   if (keyIsDown(37)) {
     cx-=power;
+
     c.x-=c.power;
 
   }
 
   }
  
 
   if (keyIsDown(39)) {
 
   if (keyIsDown(39)) {
     cx+=power;
+
     c.x+=c.power;
 
   }   
 
   }   
 
    
 
    
 
   //keep cx onscreen
 
   //keep cx onscreen
   if (cx>640 ) {
+
   if (c.x>640 ) {
     cx=640;
+
     c.x=640;
 
   }
 
   }
 
    
 
    
   if (cx <0) {
+
   if (c.x <0) {
     cx=0;  
+
     c.x=0;  
 
   }
 
   }
 
    
 
    
 
   //keep cy onScreen
 
   //keep cy onScreen
   if (cy>480) {
+
   if (c.y>480) {
     cy=480;
+
     c.y=480;
 
   }
 
   }
 
      
 
      
   if (cy <0) {
+
   if (c.y <0) {
     cy=0;
+
     c.y=0;
 
   }
 
   }
 
    
 
    
   //move mary towards cx,cy
+
   //move chasers towards c
   if (maryy < cy) {
+
   for (var meep of chasers) {
    maryy+=0.3;
+
    moveTowards(meep,c);
 +
   
 +
    if ( distance(meep,c) < 8) {
 +
      stillAlive=false;
 +
    }
 
   }
 
   }
 
+
   if (maryy > cy) {
+
   if ( (gameAge %300) === 0) {
     maryy-=0.3;
+
     var newX = Math.random() * 640;
 +
    var newY = Math.random() * 480;
 +
   
 +
    addChaser(newX,newY,0.45+ gameAge/3000,"magenta");
 
   }
 
   }
 
+
}
   if (maryx <cx) {
+
 
    maryx+=0.3;
+
function draw() {
 +
 
 +
   clear();
 +
   
 +
  setColour("purple");
 +
  fillCircle(c.x,c.y,c.penSize);
 +
 
 +
  for (var q of chasers) {
 +
      drawSpot(q.colour,q.x,q.y);
 
   }
 
   }
 
    
 
    
  if (maryx > cx) {
+
  setColour("black");
    maryx-=0.3;
+
  print("the game age is "+gameAge);
  }
+
  print(gameAge % 300);
 
+
 
    
 
    
   if (keyIsDown(32)) {
+
   drawImage(gameOver.image,gameOver.x,gameOver.y);
    clear();
+
}
  }
+
 
+
   
+
  setColour("purple");
+
  fillCircle(cx,cy,penSize);
+
  
  setColour("red");
+
function move() {
  fillCircle(maryx,maryy,10);
+
  gameAge+=1;
  setColour("yellow");
+
  if (stillAlive) {
  fillCircle(maryx,maryy,7);
+
    play();   
   
+
  } else {
  setColour("red");
+
 
  fillCircle(frankx,franky,10);
+
  }
   setColour("yellow");
+
 
  fillCircle(frankx,franky,7);
+
  if (keyIsDown(32)) {
+
   stillAlive=false;  
 +
  }
 +
  draw();
 
}
 
}
  
 
run(move);
 
run(move);
 +
 +
 
</edcode>
 
</edcode>

Latest revision as of 22:51, 6 July 2017

Useful things to refer to.

The API page has a list of some of the functions you can use such as