Difference between revisions of "Game"

From CodeStuff
Jump to: navigation, search
(Created page with "<edcode> var ball_1 = {cx:320, cy:240, dx:3, dy:-3}; var ball_2 = {cx:330, cy:340, dx:-3, dy:-3}; var ball_3 = {cx:340, cy:440, dx:-3, dy:3}; var ball_4 = {cx:350, cy:140, dx...")
 
(Moved array bits to api)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
<edcode>
 
<edcode>
 +
 
 +
var star=loadImage("PlanetCute_Star.png");
  
var ball_1 = {cx:320, cy:240, dx:3, dy:-3};
+
// todo
var ball_2 = {cx:330, cy:340, dx:-3, dy:-3};
+
// make player stay in box
var ball_3 = {cx:340, cy:440, dx:-3, dy:3};
+
// make player hit balls
var ball_4 = {cx:350, cy:140, dx:3, dy:3};
+
  
var boxTop=130, boxLeft=120;  //the top left corner of the box is 130,120
+
 
var boxWidth=400, boxHeight=300;
+
 
 +
 
 +
 
 +
var ball_1 = {cx:320, cy:240, dx:3, dy:-3, colour: 'red'};
 +
var ball_2 = {cx:330, cy:340, dx:-3, dy:-3, colour:'brown'};
 +
var ball_3 = {cx:340, cy:440, dx:-3, dy:3, colour:'blue'};
 +
var ball_4 = {cx:350, cy:140, dx:3, dy:3, colour:'green'};
 +
 
 +
var activeBalls = [];
 +
   
 +
    addBall(ball_1)
 +
addBall(ball_2);
 +
addBall(ball_3);
 +
addBall(ball_4)     
 +
 +
 
 +
var player = {x:320, y:200, speed:3};
 +
 
 +
var boxTop=0, boxLeft=0;  //the top left corner of the box is 130,120
 +
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 21: Line 47:
 
   if (ball.cy<boxTop) ball.dy=3;
 
   if (ball.cy<boxTop) ball.dy=3;
 
   if (ball.cy>boxBottom) ball.dy=-3;
 
   if (ball.cy>boxBottom) ball.dy=-3;
 +
 
 +
 
 +
  ball.dy=ball.dy+0.1;
 +
 
 +
  var distance=distanceToPlayer(ball);
 +
 
 +
  if (distance < 20) {
 +
    ball.colour = "Black";
 +
  }
 +
 
 +
  if (ball.age > 100) activeBalls.removeObject(ball);
 +
  ball.age=ball.age+1;
 +
 
 +
 
 
}
 
}
 +
 +
function drawBall(ball) {
 +
  //setColour(ball.colour)
 +
  //fillCircle(ball.cx,ball.cy,4);
 +
 
 +
  drawImage(star,ball.cx-50,ball.cy-85);
 +
}
 +
 +
function distanceToPlayer(ball) {
 +
  var dx= ball.cx - player.x;
 +
  var dy= ball.cy - player.y;
 +
 
 +
  var xa = dx*dx;
 +
  var ya = dy*dy;
 +
 
 +
 
 +
  var distance= Math.sqrt(xa+ya);
 +
 
 +
  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() {
   moveBall(ball_1);
+
 
   moveBall(ball_2);
+
   //moveBall(ball_1);
   moveBall(ball_3);
+
   //moveBall(ball_2);
   moveBall(ball_4);
+
   //moveBall(ball_3);
 +
   //moveBall(ball_4);
 +
 
 +
  var i = 0;
 +
  for (i=0; i<activeBalls.length;i++) {
 +
    var ourBall = activeBalls[i];
 +
    moveBall(ourBall);
 +
  }
 +
 
 +
 
 +
  ///if (keyIsDown(38)) player.y-=player.speed;
 +
  //if (keyIsDown(40)) player.y+=player.speed;
 +
  //if (keyIsDown(37)) player.x-=player.speed;
 +
  //if (keyIsDown(39)) player.x+=player.speed;
 +
 
 +
  if (keyIsDown(32)) boom(Math.random()*600,Math.random()*400,5);
 +
   
 +
 +
  var m = getMousePosition();
 +
  player.x=m.x;
 +
  player.y=m.y;
 +
 
 
}
 
}
  
Line 34: Line 125:
  
 
   //draw the balls  
 
   //draw the balls  
   fillCircle(ball_1.cx,ball_1.cy,4);
+
    
   fillCircle(ball_2.cx,ball_2.cy,4);
+
  //drawBall(ball_1);
   fillCircle(ball_3.cx,ball_3.cy,4);
+
   //drawBall(ball_2);
   fillCircle(ball_4.cx,ball_4.cy,4);
+
   //drawBall(ball_3);
 
+
   //drawBall(ball_4);
 +
 
 +
  var i = 0;
 +
  for (i=0; i<activeBalls.length;i++) {
 +
    var ourBall = activeBalls[i];
 +
    drawBall(ourBall);
 +
  }
 +
 
 
   //draw the box
 
   //draw the box
 
   setColour('red');
 
   setColour('red');
 
   drawRectangle(boxLeft,boxTop,boxWidth,boxHeight);
 
   drawRectangle(boxLeft,boxTop,boxWidth,boxHeight);
 +
 
 +
 
 +
  drawCircle(player.x,player.y,20);
 +
 
 
}
 
}
  
 
run(move,draw);
 
run(move,draw);
 
 
</edcode>
 
</edcode>

Latest revision as of 00:14, 31 May 2012