Difference between revisions of "Game"
From CodeStuff
| Line 1: | Line 1: | ||
<edcode> | <edcode> | ||
| + | /// These go to api. | ||
| + | Array.prototype.remove = function(from, to) { | ||
| + | var rest = this.slice((to || from) + 1 || this.length); | ||
| + | this.length = from < 0 ? this.length + from : from; | ||
| + | return this.push.apply(this, rest); | ||
| + | }; | ||
| − | + | Array.prototype.removeObject =function(object) { | |
| − | + | var index = this.indexOf(object); | |
| + | if (index>=0) this.remove(index); | ||
| + | } | ||
| + | |||
| + | var star=loadImage("PlanetCute_Star.png"); | ||
// todo | // todo | ||
| Line 17: | Line 27: | ||
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 | + | 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 38: | Line 59: | ||
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 67: | ||
ball.colour = "Black"; | ball.colour = "Black"; | ||
} | } | ||
| + | |||
| + | if (ball.age > 100) activeBalls.removeObject(ball); | ||
| + | ball.age=ball.age+1; | ||
| Line 49: | Line 75: | ||
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 93: | ||
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 123: | ||
//if (keyIsDown(39)) player.x+=player.speed; | //if (keyIsDown(39)) player.x+=player.speed; | ||
| − | if (keyIsDown(32)) | + | 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 158: | ||
run(move,draw); | run(move,draw); | ||
| − | |||
</edcode> | </edcode> | ||