File size: 2,313 Bytes
4130016
2ec5dec
4130016
abd9a21
 
 
 
 
 
 
 
 
 
4130016
 
 
abd9a21
 
4130016
abd9a21
 
 
4130016
abd9a21
2ec5dec
 
 
 
abd9a21
2ec5dec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abd9a21
4130016
abd9a21
 
 
 
4130016
abd9a21
 
 
4130016
2ec5dec
 
abd9a21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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 
}