GramAPP / app.js
Merlintxu's picture
Update app.js
2ec5dec
raw
history blame
2.31 kB
import Phaser from 'phaser';
import axios from 'axios';
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
scene: {
preload: preload,
create: create,
update: update
}
};
const game = new Phaser.Game(config);
let score = 0;
let scoreText;
function preload() {
// load assets
}
function create() {
// create game objects{
"link": "https://phaser.io/examples/v3/category/physics/arcade",
"user_has_request": false
}
scoreText = this.add.text(16, 16, 'Score: 0', { fontSize: '32px', fill: '#000' });
// create word objects
words = this.physics.add.group();
// add words to the group
sentence.split(' ').forEach((word, index) => {
let wordObject = this.physics.add.sprite(100 + index * 50, 100, 'word');
wordObject.setBounce(0.2);
wordObject.setCollideWorldBounds(true);
wordObject.word = word;
words.add(wordObject);
});
// create categories
categories = this.physics.add.staticGroup();
// add categories to the group
['sustantivo', 'verbo', 'adjetivo', 'artículo'].forEach((category, index) => {
let categoryObject = this.physics.add.sprite(100 + index * 200, 500, 'category');
categoryObject.category = category;
categories.add(categoryObject);
});
// check for correct categorization
this.physics.add.collider(words, categories, checkCategorization, null, this);
}
function checkCategorization(wordObject, categoryObject) {
// check if the word belongs to the category
axios.post('/api/game_logic', {
sentence: sentence,
user_word: wordObject.word,
user_pos: categoryObject.category
})
.then(function (response) {
let correct = response.data.correct;
if (correct) {
increaseScore(10);
wordObject.destroy();
} else {
// give a hint
let hint = response.data.hint;
displayHint(hint);
}
})
.catch(function (error) {
console.log(error);
});
}
function update() {
// update game logic
scoreText.setText(`Score: ${score}`);
}
function increaseScore(points) {
score += points;
}
function displayHint(hint) {
// display hint on screen
}