|
16 | 16 |
|
17 | 17 | <script>
|
18 | 18 |
|
19 |
| -// BALL BOUNCES |
20 |
| -//BUT GOES OVER EDGES. FIX IT |
| 19 | +// this is a function (from canvas.js) that sets you the canvas |
| 20 | +// so you don't need to do any of the heavy lifting and can |
| 21 | +// start playing straight away |
| 22 | +// the function also lets you access the screen width as "width" or "w" |
| 23 | +// and the screen height as "height" or "h" |
21 | 24 |
|
22 | 25 | var ctx = createCanvas("canvas1");
|
23 |
| -var block_size = 20; |
24 |
| -var x = width/2; |
25 |
| -var y = height/2; |
26 |
| -var speed_x = random(-5, 5); |
27 |
| -var speed_y = random(-5, 5); |
28 |
| - |
29 |
| -// much like processing this is the drawing loop that plays repeatedly |
30 |
| -// the actual syntax is a shortcut for requestAnimationFrame |
| 26 | +var ball_size = 20; |
| 27 | +var ball_x = width/2; |
| 28 | +var ball_y = height/2; |
| 29 | +var speed_x = randomInt(-15, 15); |
| 30 | +var speed_y = randomInt(-15, 15); |
| 31 | +ctx.fillStyle = rgb(randomInt(255),randomInt(255),0); |
31 | 32 |
|
32 | 33 | function draw(){
|
33 | 34 |
|
34 |
| - // shortcut for clearing the screen, |
35 |
| - // takes a rgb value 255,255,255 or a grey value of 0-255 |
36 |
| - ctx.background(250); |
| 35 | + //ctx.background(255, 0.2); |
| 36 | + //ctx.fillStyle = rgb(255,0,0); |
| 37 | + moveBall(); |
| 38 | + drawBall(); |
| 39 | +} |
37 | 40 |
|
38 |
| - x = x + speed_x; |
39 |
| - y = y + speed_y; |
40 | 41 |
|
41 |
| - if (x > width || x < 0 ) { |
42 |
| - speed_x = speed_x *-1; |
43 |
| - } |
| 42 | +function moveBall(){ |
| 43 | + ball_x = ball_x + speed_x; |
| 44 | + ball_y = ball_y + speed_y; |
44 | 45 |
|
45 |
| - if (y > height || y < 0 ) { |
46 |
| - speed_y = speed_y *-1; |
| 46 | + if (bounce(ball_x, 0, w, ball_size)) { |
| 47 | + speed_x *=-1; |
| 48 | + ball_size = random(10, 50); |
| 49 | + ctx.fillStyle = rgb(randomInt(255),randomInt(255),0); |
47 | 50 | }
|
48 | 51 |
|
49 |
| - ctx.fillStyle = "#00aeef"; |
50 |
| - ctx.fillRect(x, y, block_size, block_size); |
| 52 | + if (bounce(ball_y, 0 ,h, ball_size)) { |
| 53 | + speed_y *=-1; |
| 54 | + ball_size = random(10, 50); |
| 55 | + ctx.fillStyle = rgb(0, randomInt(255),randomInt(255)); |
| 56 | + } |
| 57 | +} |
51 | 58 |
|
52 |
| -} // end draw |
| 59 | +function drawBall(){ |
| 60 | + ctx.fillEllipse(ball_x, ball_y, ball_size, ball_size); |
| 61 | +} |
53 | 62 |
|
54 | 63 |
|
55 | 64 | </script>
|
56 |
| -<!-- <script language="javascript" src="js/soundcloud.js"></script> --> |
| 65 | + |
57 | 66 | </body>
|
58 | 67 |
|
59 | 68 | </html>
|
0 commit comments