Difference between revisions of "Game"
From CodeStuff
(Moved array bits to api) |
|||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
<edcode> | <edcode> | ||
| − | + | ||
| − | + | var star=loadImage("PlanetCute_Star.png"); | |
// todo | // todo | ||
// make player stay in box | // make player stay in box | ||
// make player hit balls | // make player hit balls | ||
| + | |||
| Line 14: | Line 15: | ||
var ball_3 = {cx:340, cy:440, dx:-3, dy:3, colour:'blue'}; | 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 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 player = {x:320, y:200, speed:3}; | ||
| − | var boxTop= | + | var boxTop=0, boxLeft=0; //the top left corner of the box is 130,120 |
| − | var boxWidth= | + | 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 31: | Line 46: | ||
// and if cy has crossed the top or bottom edges of the box | // and if cy has crossed the top or bottom edges of the box | ||
if (ball.cy<boxTop) ball.dy=3; | if (ball.cy<boxTop) ball.dy=3; | ||
| − | if (ball.cy>boxBottom) | + | 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) { | 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 51: | 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() { | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | if (keyIsDown(38)) player.y-=player.speed; | + | //moveBall(ball_1); |
| − | if (keyIsDown(40)) player.y+=player.speed; | + | //moveBall(ball_2); |
| − | if (keyIsDown(37)) player.x-=player.speed; | + | //moveBall(ball_3); |
| − | if (keyIsDown(39)) player.x+=player.speed; | + | //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 69: | Line 126: | ||
//draw the balls | //draw the balls | ||
| − | drawBall(ball_1); | + | //drawBall(ball_1); |
| − | drawBall(ball_2); | + | //drawBall(ball_2); |
| − | drawBall(ball_3); | + | //drawBall(ball_3); |
| − | drawBall(ball_4); | + | //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 | ||
| Line 84: | Line 147: | ||
run(move,draw); | run(move,draw); | ||
| − | |||
| − | |||
| − | |||
</edcode> | </edcode> | ||