Open index.html, click the input field and eat the food with the arrow keys.
Created
October 15, 2018 07:04
-
-
Save fernando-mc/e3ec294a01568a05a5a84b3ecb362362 to your computer and use it in GitHub Desktop.
Vue.js Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<!-- <link rel="stylesheet" href="index.css"> --> | |
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> | |
</head> | |
<body> | |
<div id='game'> | |
<input v-on:keyup.up="moveUp" v-on:keyup.down="moveDown" v-on:keyup.left="moveLeft" v-on:keyup.right="moveRight"> | |
<div | |
v-bind:style="{ backgroundColor: foodColor, position: divPosition, left: foodLocationX + 'px', top: foodLocationY + 'px' }">FOOD | |
</div> | |
<div | |
v-bind:style="{ backgroundColor: divColor, position: divPosition, left: locationX + 'px', top: locationY + 'px' }">/-_-/ | |
</div> | |
<div >{{ userScore }}</div> | |
</div> | |
<script src="index.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict" | |
var app = new Vue({ | |
el: '#game', | |
data: { | |
singleUnusedEl: '', | |
userScore: 0, | |
locationX: 100, | |
locationY: 100, | |
foodLocationX: 160, | |
foodLocationY: 180, | |
divColor: 'red', | |
foodColor: 'green', | |
divPosition: 'absolute' | |
}, | |
methods: { | |
check: function() { | |
if (this.foodLocationX == this.locationX && this.foodLocationY == this.locationY) { | |
this.generateFood(); | |
this.addPoints(); | |
} | |
}, | |
generateFood: function() { | |
console.log('more food!'); | |
this.foodLocationX = Math.ceil(Math.floor((Math.random() * 100) + 1) / 10) * 10; | |
this.foodLocationY = Math.ceil(Math.floor((Math.random() * 100) + 1) / 10) * 10; | |
}, | |
addPoints: function() { | |
console.log('more points!'); | |
this.userScore += 10; | |
}, | |
moveUp: function () { | |
console.log('uped!'); | |
this.locationY -= 10; | |
this.check(); | |
}, | |
moveDown: function () { | |
console.log('downed!'); | |
this.locationY += 10; | |
this.check() | |
}, | |
moveRight: function () { | |
console.log('uped!'); | |
this.locationX += 10; | |
this.check() | |
}, | |
moveLeft: function () { | |
console.log('downed!'); | |
this.locationX -= 10; | |
this.check() | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment