Pen Settings

HTML

CSS

CSS Base

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

Vue

              
                <template>
  <div id="app">
    <div class="top">
      <p class="instructions">Use arrow keys to move the rocket</p>
      <p class="score">SCORE: {{ score }} | LEVEL: {{ level }}</p>
      <p class="highest-score">HIGHEST SCORE: {{ highestScore }}</p>
    </div>

    <div
      class="rocket-area"
      :style="{ 'animation-play-state': isGameOver ? 'paused' : 'running' }"
    >
      <img
        ref="rocket"
        class="rocket"
        src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-1/rocket.svg"
        alt="Rocket ship"
        :style="{ bottom: `${rocketBottom}px`, left: `${rocketLeft}px` }"
      />

      <img
        v-for="(asteroid, index) in asteroids"
        :key="index"
        src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3500482/asteroid.svg"
        alt="Asteroid"
        ref="asteroids"
        class="asteroid"
        :style="{
          top: `${asteroid.top}%`,
          left: `${asteroid.left}%`,
          width: `${asteroid.size}px`,
          height: `${asteroid.size}px`
        }"
      />
    </div>

    <div class="game-over" v-if="isGameOver">
      <h1>GAME OVER</h1>
      <button @click="restartGame">START NEW GAME</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rocketBottom: 50,
      rocketLeft: 350,
      asteroids: [],
      isGameOver: false,
      score: 0,
      highestScore: localStorage.getItem("highestRocketScore") | 0,
      level: 1,
      keys: [],
      velY: 0,
      velX: 0,
      speed: 2,
      friction: 0.68,
      animFrameId: null
    };
  },
  methods: {
    move() {
      this.animFrameId = requestAnimationFrame(this.move);

      this.moveRocket();
      this.moveAsteroids();
    },
    moveRocket(e) {
      var length = Object.keys(this.keys).length;

      for (let i = 0; i < length; i++) {
        if (this.keys["ArrowLeft"]) {
          if (this.velX > -this.speed) {
            this.velX--;
          }
        }

        if (this.keys["ArrowDown"]) {
          if (this.velY > -this.speed) {
            this.velY--;
          }
        }

        if (this.keys["ArrowRight"]) {
          if (this.velX < this.speed) {
            this.velX++;
          }
        }

        if (this.keys["ArrowUp"]) {
          if (this.velY < this.speed) {
            this.velY++;
          }
        }

        this.velX *= this.friction;
        this.rocketLeft += this.velX;

        this.velY *= this.friction;
        this.rocketBottom += this.velY;

        if (this.rocketLeft <= 0) this.rocketLeft = 0;
        if (this.rocketLeft >= 750) this.rocketLeft = 750;
        if (this.rocketBottom <= 0) this.rocketBottom = 0;
      }
    },
    moveAsteroids() {
      for (let i = 0; i < this.asteroids.length; i++) {
        this.asteroids[i].top += 0.75 * this.asteroids[i].speed;

        if (this.isCollide(this.$refs.rocket, this.$refs.asteroids[i])) {
          this.gameOver();
        } else if (this.asteroids[i].top >= 100) {
          this.asteroidFinished(i);
        }
      }
    },
    asteroidFinished(i) {
      this.score++;

      if (this.score >= this.level * 20) {
        this.level++;
      }

      this.asteroids[i] = this.createAsteroid(i);
    },
    createAsteroid(i) {
      return {
        top: this.getRndNumber(10, 500) * -1,
        left: this.getRndNumber(i * 10 + 1, (i + 1) * 10),
        size: this.getRndNumber(30, 100),
        speed: this.getRndNumber(1, 1.5 + this.level * 0.5)
      };
    },
    createAsteroids() {
      const asteroids = [];
      const numOfAsteroids = 10;

      for (let i = 0; i < numOfAsteroids; i++) {
        asteroids.push(this.createAsteroid(i));
      }

      this.$set(this, "asteroids", asteroids);
    },
    gameOver() {
      this.isGameOver = true;
      cancelAnimationFrame(this.animFrameId);

      if (this.score > this.highestScore) {
        this.highestScore = this.score;
        localStorage.setItem("highestRocketScore", this.highestScore);
      }
    },
    restartGame() {
      this.rocketBottom = 50;
      this.rocketLeft = 350;
      this.asteroids = [];
      this.isGameOver = false;
      this.score = 0;
      this.level = 1;
      this.keys = [];
      this.velY = 0;
      this.velX = 0;
      this.animFrame = null;

      Vue.nextTick(() => {
        this.createAsteroids();

        Vue.nextTick(() => {
          this.animFrameId = requestAnimationFrame(this.move);
        });
      });
    },
    isCollide(a, b) {
      const aRect = a.getBoundingClientRect();
      const bRect = b.getBoundingClientRect();

      return !(
        aRect.top + aRect.height < bRect.top ||
        aRect.top > bRect.top + bRect.height ||
        aRect.left + aRect.width < bRect.left ||
        aRect.left > bRect.left + bRect.width
      );
    },
    getRndNumber(min, max) {
      return Math.floor(Math.random() * (max - min)) + min;
    }
  },
  created() {
    this.createAsteroids();
  },
  mounted() {
    this.animFrameId = requestAnimationFrame(this.move);

    window.addEventListener("keyup", (e) => (this.keys[e.key] = false));
    window.addEventListener("keydown", (e) => (this.keys[e.key] = true));
  }
};
</script>

<style lang="scss">
@import url("https://fonts.googleapis.com/css?family=Roboto:400,400i,700");
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 2rem;
  height: 100vh;
  font-family: Roboto, sans-serif;
  letter-spacing: 2px;
}

#app {
  display: grid;
  grid-template-rows: auto 1fr;
  justify-content: center;
  height: 100%;
}

.rocket-area {
  width: 800px;
  background: #222;
  background-image: radial-gradient(
      2px 2px at 20px 30px,
      #eee,
      rgba(0, 0, 0, 0)
    ),
    radial-gradient(2px 2px at 40px 70px, #fff, rgba(0, 0, 0, 0)),
    radial-gradient(2px 2px at 50px 160px, #ddd, rgba(0, 0, 0, 0)),
    radial-gradient(2px 2px at 90px 40px, #fff, rgba(0, 0, 0, 0)),
    radial-gradient(2px 2px at 130px 80px, #fff, rgba(0, 0, 0, 0)),
    radial-gradient(2px 2px at 160px 120px, #ddd, rgba(0, 0, 0, 0));
  background-repeat: repeat;
  background-size: 200px 200px;
  border: 5px solid #ddd;
  box-shadow: inset 0 0 12px black;
  border-radius: 16px;
  display: grid;
  position: relative;
  overflow: hidden;
  animation: move-sky 200s infinite;

  @keyframes move-sky {
    from {
      background-position: 0 0;
    }
    to {
      background-position: 0 10000px;
    }
  }
}

.rocket {
  position: absolute;
  height: 100px;
}

.asteroid {
  fill: #fff;
  width: 75px;
  height: 75px;
  position: absolute;
}

.top {
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
}

.instructions {
  font-size: 0.8rem;
}

.score {
  width: 400px;
  margin: 0 auto;
  font-size: 1.5rem;
}

.highest-score {
  font-size: 1rem;
}

.game-over {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: rgba(black, 0.8);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: #fff;

  button {
    padding: 0.5rem 2rem;
    letter-spacing: 2px;
    cursor: pointer;
    box-shadow: 0 0 5px rgba(white, 0.4);
    border-radius: 16px;
    border: none;

    &:hover {
      opacity: 0.95;
    }
  }
}
</style>

<!-- Asteroid icon made by Freepik -->

              
            
!
999px

Console