Difference between revisions of "Today"

From CodeStuff
Jump to: navigation, search
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
<edcode>
+
Useful things to refer to.
  
var player = {x:300,y:400}  // The starting position of the player
+
*[[Variables]]
 +
*[[If statements]]
 +
*[[Coordinates]]
 +
*[[Keycodes]]
 +
*[[Functions]]
 +
*[[Loops]]
 +
*[[Aiming]]
  
var alien = {x:200,y:100}    // The starting position of the alien
+
The [[API|API page]] has a list of some of the functions you can use such as
  
var bullet = {x:200, y:200}
+
*[[API#setColour.28colourValue.29.3B|setColour()]]
 +
*[[API#clear();|clear()]]
 +
*[[API#fillCircle_.28x.2C_y.2C_radius.29.3B|fillCircle()]] .
 +
*[[API#keyIsDown.28keyCode.29|keyIsDown()]] .
  
  
function move() {
+
<edcode>var c = {x:320, y:240, lastx:320, lasty:240, power:0.1, penSize:4};
  
  //37 is the key code of the left arrow key
+
var gameOver = { image : loadImage("http://fingswotidun.com/images/GameOver.png"),
  if ( keyIsDown(37) ) {
+
                x:0, y: -10000,
    player.x-=2;
+
                endx: 0, endy: 150};
  } 
+
               
 +
var gameAge=0;
 +
var stillAlive=true;
  
  //39 is the key code of the right arrow key
+
var mary = {x:200, y:240, speed:0.35,colour:"blue"};
  if ( keyIsDown(39) ) {
+
var frank = {x:380, y:240, speed:0.2,colour:"cyan"};
    player.x+=2;
+
var bob = {x:580, y:100, speed: 0.25,colour:"lightgreen"};
  }
+
  
 +
var chasers = [mary,frank,bob];
 +
 +
chasers.add({x:100,y:100});
 +
 +
function addChaser(x,y,speed=0.3,colour="yellow") {
 +
  chasers.add({x,y,speed,colour});
 
}
 
}
  
function draw() {
+
addChaser(200,100);
  clear();
+
addChaser(300,400,0.5);
  print("player");
+
addChaser(400,400,0.45,"magenta");
  print(player.x);
+
 
+
  drawPlayerShip(player.x,player.y);
+
  
   drawAlienShip(alien.x,alien.y);
+
function drawSpot(colour,x,y) {
 +
   setColour("red");
 +
  fillCircle(x,y,10);
 +
  setColour(colour);
 +
  fillCircle(x,y,8);
 +
}
  
   drawBullet(bullet.x,bullet.y)
+
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 drawPlayerShip(x,y) {
+
function play() {  
  drawLine(x,y-30, x+10,y+30);
+
  var dx = c.x-c.lastx;
   drawLine(x,y-30, x-10,y+30);
+
  var dy = c.y-c.lasty;
 +
 
 +
  c.lastx=c.x;
 +
  c.lasty=c.y;
 +
 
 +
  c.x=c.x+dx;
 +
  c.y=c.y+dy;
 +
 
 +
    // the arrow keys have key codes 37,38,39 and 40
 +
 
 +
  if (keyIsDown(38)) {
 +
    c.y-=c.power;
 +
  }
 +
 
 +
  if (keyIsDown(40)) {
 +
    c.y+=c.power; 
 +
  }
 +
 
 +
  if (keyIsDown(37)) {
 +
    c.x-=c.power;
 +
  }
 +
 
 +
  if (keyIsDown(39)) {
 +
    c.x+=c.power;
 +
  } 
 +
    
 +
  //keep cx onscreen
 +
  if (c.x>640 ) {
 +
    c.x=640;
 +
  }
 +
 
 +
  if (c.x <0) {
 +
    c.x=0;
 +
  }
 +
 
 +
  //keep cy onScreen
 +
  if (c.y>480) {
 +
    c.y=480;
 +
  }
 +
   
 +
  if (c.y <0) {
 +
    c.y=0;
 +
  }
 +
 
 +
  //move chasers towards c
 +
  for (var meep of chasers) {
 +
    moveTowards(meep,c);
 +
   
 +
    if ( distance(meep,c) < 8) {
 +
      stillAlive=false;
 +
    }
 +
  }
 +
 +
  if ( (gameAge %300) === 0) {
 +
    var newX = Math.random() * 640;
 +
    var newY = Math.random() * 480;
 +
   
 +
    addChaser(newX,newY,0.45+ gameAge/3000,"magenta");
 +
  }
 
}
 
}
  
function drawAlienShip(x,y) {
+
function draw() {
  fillCircle(x,y,20);
+
 
 +
  clear();
 +
   
 +
  setColour("purple");
 +
  fillCircle(c.x,c.y,c.penSize);
 +
 
 +
  for (var q of chasers) {
 +
      drawSpot(q.colour,q.x,q.y);
 +
  }
 +
 
 +
  setColour("black");
 +
  print("the game age is "+gameAge);
 +
  print(gameAge % 300);
 +
 
 +
  drawImage(gameOver.image,gameOver.x,gameOver.y);
 
}
 
}
  
function drawBullet(x,y) {
+
function move() {
   fillCircle(x,y,3);
+
   gameAge+=1;
 +
  if (stillAlive) {
 +
    play(); 
 +
  } else {
 +
 
 +
  }
 +
 
 +
  if (keyIsDown(32)) {
 +
  stillAlive=false;
 +
  }
 +
  draw();
 
}
 
}
run(move,draw);
+
 
 +
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