|
| 1 | +/* |
| 2 | +ASTEROIDS |
| 3 | +Jeff Thompson | 2013 | jeffreythompson.org |
| 4 | +
|
| 5 | +https://freeasteroids.org |
| 6 | +
|
| 7 | +See the other Javascript files for more details! |
| 8 | +
|
| 9 | +CHALLENGES |
| 10 | +1. Can you draw a more interesting shape for our |
| 11 | + asteroids? (Hint: try using randomized vertices, |
| 12 | + created in the constructor)) |
| 13 | +2. Can you make the asteroids move across the |
| 14 | + screen? Can you remove them if they go offscreen? |
| 15 | +3. Could you limit the number of bullets available? |
| 16 | +
|
| 17 | +*/ |
| 18 | + |
| 19 | +let ship; |
| 20 | +let bullets; |
| 21 | +let enemies; |
| 22 | +let score = 0; |
| 23 | + |
| 24 | +function setup() { |
| 25 | + createCanvas(windowWidth, windowHeight); |
| 26 | + noCursor(); |
| 27 | + |
| 28 | + // create our ship in the center |
| 29 | + ship = new Ship(); |
| 30 | + |
| 31 | + // and an empty list of bullets |
| 32 | + bullets = []; |
| 33 | + |
| 34 | + // make a few enemies too |
| 35 | + enemies = []; |
| 36 | + for (let i=0; i<3; i++) { |
| 37 | + let e = new Enemy(); |
| 38 | + enemies.push(e); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function draw() { |
| 43 | + background(220); |
| 44 | + |
| 45 | + fill(0); |
| 46 | + noStroke(); |
| 47 | + text('SCORE: ' + score, 15,20); |
| 48 | + |
| 49 | + // use left/right to rotate the ship, |
| 50 | + // up to move forward |
| 51 | + // note: keyIsDown() lets us check for |
| 52 | + // multiple key presses at once! |
| 53 | + if (keyIsDown(UP_ARROW)) { |
| 54 | + ship.update(MOVE); |
| 55 | + } |
| 56 | + if (keyIsDown(LEFT_ARROW)) { |
| 57 | + ship.update(LEFT); |
| 58 | + } |
| 59 | + if (keyIsDown(RIGHT_ARROW)) { |
| 60 | + ship.update(RIGHT); |
| 61 | + } |
| 62 | + if (keyIsDown(32)) { // 32 = space in ASCII |
| 63 | + let b = new Bullet(ship); |
| 64 | + bullets.push(b); |
| 65 | + } |
| 66 | + ship.display(); |
| 67 | + |
| 68 | + // update and draw the bullets |
| 69 | + for (let i=0; i<bullets.length; i++) { |
| 70 | + bullets[i].update(); |
| 71 | + bullets[i].display(); |
| 72 | + } |
| 73 | + |
| 74 | + // and finally go through the enemies! |
| 75 | + // note we do this in reverse order; this is |
| 76 | + // so we can remove one that is hit (trying |
| 77 | + // this in normal order would cause an error, |
| 78 | + // since the length of the list would change!) |
| 79 | + for (let i=enemies.length-1; i>=0; i-=1) { |
| 80 | + enemies[i].update(); |
| 81 | + |
| 82 | + // the isHit variable will be set to true |
| 83 | + // in the update() function if it hits an enemy |
| 84 | + if (enemies[i].isHit) { |
| 85 | + // remove the current enemy from the list! |
| 86 | + // (and update the score) |
| 87 | + enemies.splice(i, 1); |
| 88 | + score += 1; |
| 89 | + } |
| 90 | + // if not hit, display as usual |
| 91 | + else { |
| 92 | + enemies[i].display(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // randomly generate some new enemies |
| 97 | + // (or when we've destroyed them all!) |
| 98 | + if (random(0, 100) < 0.5 || enemies.length === 0) { |
| 99 | + enemies.push(new Enemy()); |
| 100 | + } |
| 101 | +} |
| 102 | + |
0 commit comments