Creating a Running Game in VueJS - Part 4 - The Finish Line
February 20, 2021
This post follows on from a series on creating a game in Vue. If you’d like to start from the beginning, then go here.
In the previous post we refactored the single file index.html that we’d been working on into multiple component files, and created a new Vue templated application.
In this post, we’ll do some further refactoring, and we’ll add a finish line.
Reusing Components
Since we now have components, we should re-use them; otherwise, there’s not much point in them being components. Our Player object is just a game object, and since we now need a second game object, let’s rename Player to GameObject. We can also add an additional property; for now, it will simply allow us to see which object is which:
As you can see, we’ve changed the data a little: instead of location we now have two pieces of computed data (in fact one isn’t really computed, but we’ll come back to that later):
If you run that now, you’ll see the finish line; but alas we can “run” straight past the finish line and nothing happens!
Collision - have we won, or lost?
The first thing we’ll do here is create a new component: GameFinished. This will simply display some text for now saying whether you won or lost. The code inside the component will be very straightforward:
[code lang=“html”]
Game is finished, you {{ wonOrLost }}
We'll need to both **import** and **register** the component. If you don't, you'll start getting errors such as
> error 'GameFinished' is defined but never used no-unused-vars
Or:
> error 'GameFinished' is not defined no-undef
To import the component, you'll need this in App.vue:
[code lang="html"]
<script>
import GameObject from './components/GameObject.vue';
import Timer from './components/Timer.vue';
import GameFinished from './components/GameFinished.vue';
And registering is done slightly further down:
components:{
GameObject,
Timer,
GameFinished
},
We’ll add a new data property:
data:function(){return{...
isGameFinished:false
We’ll add a new computed function that will tell us whether we’ve won: