Difference between revisions of "Game"

From CodeStuff
Jump to: navigation, search
(Moved array bits to api)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
<edcode>
 
<edcode>
 
+
 
 
+
var star=loadImage("PlanetCute_Star.png");
 
+
  
 
// todo
 
// todo
Line 17: Line 16:
 
var ball_4 = {cx:350, cy:140, dx:3, dy:3, colour:'green'};
 
var ball_4 = {cx:350, cy:140, dx:3, dy:3, colour:'green'};
  
var activeBalls = [ball_1,ball_2,ball_3,ball_4];
+
var activeBalls = [];
 
+
   
 +
    addBall(ball_1)
 +
addBall(ball_2);
 +
addBall(ball_3);
 +
addBall(ball_4)     
 +
  
 
var player = {x:320, y:200, speed:3};
 
var player = {x:320, y:200, speed:3};
  
var boxTop=130, boxLeft=120;  //the top left corner of the box is 130,120
+
var boxTop=0, boxLeft=0;  //the top left corner of the box is 130,120
var boxWidth=400, boxHeight=300;
+
var boxWidth=640, boxHeight=480;
  
 
var boxBottom=boxTop+boxHeight, boxRight=boxLeft+boxWidth;  //The bottom right corner of the box can be calculated by adding the width and height to the left and top values
 
var boxBottom=boxTop+boxHeight, boxRight=boxLeft+boxWidth;  //The bottom right corner of the box can be calculated by adding the width and height to the left and top values
 +
 +
function addBall(ball) {
 +
  ball.age=0;
 +
  activeBalls.push(ball);
 +
}
 +
  
 
function moveBall(ball) {
 
function moveBall(ball) {
Line 38: Line 48:
 
   if (ball.cy>boxBottom) ball.dy=-3;
 
   if (ball.cy>boxBottom) ball.dy=-3;
 
    
 
    
 +
 
 +
  ball.dy=ball.dy+0.1;
 
    
 
    
 
   var distance=distanceToPlayer(ball);
 
   var distance=distanceToPlayer(ball);
Line 44: Line 56:
 
     ball.colour = "Black";
 
     ball.colour = "Black";
 
   }
 
   }
 +
 
 +
  if (ball.age > 100) activeBalls.removeObject(ball);
 +
  ball.age=ball.age+1;
 
    
 
    
 
    
 
    
Line 49: Line 64:
  
 
function drawBall(ball) {
 
function drawBall(ball) {
   setColour(ball.colour)
+
   //setColour(ball.colour)
   fillCircle(ball.cx,ball.cy,4);
+
   //fillCircle(ball.cx,ball.cy,4);
 +
 
 +
  drawImage(star,ball.cx-50,ball.cy-85);
 
}
 
}
  
Line 65: Line 82:
 
   return distance;
 
   return distance;
 
}
 
}
 +
 +
function boom(whereX,whereY,howmany) {
 +
 
 +
  var i=0;
 +
  while (i<howmany) { 
 +
    addBall(  {cx:whereX, cy:whereY, dx:Math.random()*6-3, dy:Math.random()*6-3, colour:'green'}  );
 +
    i=i+1;
 +
  }
 +
}
 +
  
 
function move() {
 
function move() {
Line 85: Line 112:
 
   //if (keyIsDown(39)) player.x+=player.speed;
 
   //if (keyIsDown(39)) player.x+=player.speed;
  
   if (keyIsDown(32)) activeBalls.push({cx:350, cy:140, dx:Math.random()*6-3, dy:Math.random()*6-3, colour:'green'});  
+
   if (keyIsDown(32)) boom(Math.random()*600,Math.random()*400,5);
 
+
   
 +
 
   var m = getMousePosition();
 
   var m = getMousePosition();
 
   player.x=m.x;
 
   player.x=m.x;
Line 119: Line 147:
  
 
run(move,draw);
 
run(move,draw);
 
 
</edcode>
 
</edcode>

Latest revision as of 00:14, 31 May 2012