Difference between revisions of "Game"
From CodeStuff
| Line 1: | Line 1: | ||
<edcode> | <edcode> | ||
| + | |||
| + | |||
// todo | // todo | ||
| Line 8: | Line 10: | ||
| − | var ball_1 = {cx:320, cy:240, dx:3, dy:-3}; | + | var ball_1 = {cx:320, cy:240, dx:3, dy:-3, colour: 'red'}; |
| − | var ball_2 = {cx:330, cy:340, dx:-3, dy:-3}; | + | var ball_2 = {cx:330, cy:340, dx:-3, dy:-3, colour:'brown'}; |
| − | var ball_3 = {cx:340, cy:440, dx:-3, dy:3}; | + | var ball_3 = {cx:340, cy:440, dx:-3, dy:3, colour:'blue'}; |
| − | var ball_4 = {cx:350, cy:140, dx:3, dy:3}; | + | var ball_4 = {cx:350, cy:140, dx:3, dy:3, colour:'green'}; |
var player = {x:320, y:200, speed:3}; | var player = {x:320, y:200, speed:3}; | ||
| Line 29: | Line 31: | ||
// 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) ball.dy=-3; | + | if (ball.cy>boxBottom)gb ball.dy=-3; |
| + | } | ||
| + | |||
| + | function drawBall(ball) { | ||
| + | setColour(ball.colour) | ||
| + | fillCircle(ball.cx,ball.cy,4); | ||
| + | } | ||
| + | |||
| + | 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; | ||
} | } | ||
| Line 48: | Line 68: | ||
//draw the balls | //draw the balls | ||
| − | + | ||
| − | + | drawBall(ball_1); | |
| − | + | drawBall(ball_2); | |
| − | + | drawBall(ball_3); | |
| − | + | drawBall(ball_4); | |
| + | |||
//draw the box | //draw the box | ||
setColour('red'); | setColour('red'); | ||
| Line 63: | Line 84: | ||
run(move,draw); | run(move,draw); | ||
| + | |||
</edcode> | </edcode> | ||