Difference between revisions of "Today"

From CodeStuff
Jump to: navigation, search
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]]
  
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#clear();|clear()]]
 +
*[[API#fillCircle_.28x.2C_y.2C_radius.29.3B|fillCircle()]] .
 +
*[[API#keyIsDown.28keyCode.29|keyIsDown()]] .
  
  
function move() {
+
<edcode>
  
  //37 is the key code of the left arrow key
+
var cx=320;
  if ( keyIsDown(37) ) {
+
var cy=240;
    player.x-=2;
+
var power=1;
  } 
+
var penSize=4;
  
  //39 is the key code of the right arrow key
+
var lastx=cx;
  if ( keyIsDown(39) ) {
+
var lasty=cy;
    player.x+=2;
+
  } 
+
  
}
+
function move() {   
 +
  var dx = cx-lastx;
 +
  var dy = cy-lasty;
  
function draw() {
+
   lastx=cx;
   clear();
+
   lasty=cy;
   print("player");
+
 
   print(player.x);
+
    // the arrow keys have key codes 37,38,39 and 40
    
+
 
  drawPlayerShip(player.x,player.y);
+
   if (keyIsDown(38)) {
 +
    cy-=power;  
 +
   }
  
   drawAlienShip(alien.x,alien.y);
+
   if (keyIsDown(40)) {
 +
    cy+=power;
 +
  }
  
   drawBullet(bullet.x,bullet.y)
+
   if (keyIsDown(37)) {
}
+
    cx-=power;
 +
  }
  
function drawPlayerShip(x,y) {
+
  if (keyIsDown(39)) {
   drawLine(x,y-30, x+10,y+30);
+
    cx+=power;
  drawLine(x,y-30, x-10,y+30);
+
  }
 +
 
 +
    
 +
  setColour("purple");
 +
  fillCircle(cx,cy,penSize);
 +
 
 
}
 
}
  
function drawAlienShip(x,y) {
+
run(move);
  fillCircle(x,y,20);
+
</edcode>
}
+
  
function drawBullet(x,y) {
+
This program is also available at http://jsbin.com/kizubop/1/edit?js,output
  fillCircle(x,y,3);
+
}
+
run(move,draw);
+
 
+
</edcode>
+

Revision as of 00:58, 23 June 2017

Useful things to refer to.

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


This program is also available at http://jsbin.com/kizubop/1/edit?js,output