Hangul Quiz Game

1) File

2) main.js

const STATE_INIT = 3000;
const STATE_READY = 3001;
const STATE_PLAYING = 3002;
const STATE_JUDGE = 3004;
const STATE_END = 3005;

const WORD_LINES = [
    'ํ•œ๊ธ€๋‚  ํœด๊ฒŒ์†Œ ํ˜„๊ธฐ์ฆ ํ˜•๊ด‘ํŽœ ํ˜ธ๋‚ ๋‘ ํ—ˆ๋‹ˆ๋ฌธ ํ•˜๋…ธ์ด ํ•ซ๋„๊ทธ ํ™๋‘๊นจ ํ—ค๋“œ์…‹ ํ•ด๋‹์ด ํ•œ๋ผ๋ด‰ ํ•œ๋ผ์‚ฐ ํ˜ธ๋ž‘์ด ํ—ˆ๋ฆฌ๋  ํ˜„๋ฏธ๊ฒฝ ํฐ๋จธ๋ฆฌ ํ™ฉ๋ฌด์ง€ ํ–„๋ฒ„๊ฑฐ ํ•™๋ถ€๋ชจ ํœ˜๋ฐœ์œ  ํ—ˆ๋ฒ…์ง€ ํ•˜์ˆ˜๊ตฌ ํ˜ธ์‹ ์ˆ  ํ™์ˆ˜์•„ ํ™”์Šน์ด ํ—ˆ์Šคํ‚ค ํ–„์Šคํ„ฐ ํ•ด์šด๋Œ€ ํ™œ์ฃผ๋กœ ํœด์ง€ํ†ต ํ™”์žฅํ’ˆ ํšŒ์ดˆ๋ฆฌ ํ•ซํŒฌ์ธ  ํ•˜ํšŒํƒˆ',
    'ํ”ผ๊ทœ์–ด ํŒ”๊ฟˆ์น˜ ํฌ๋„์ฃผ ํ”ผ๋ผ๋ƒ ํ”ผ๋ผ๋ฏธ ํ”ผ๋ขฐ์นจ ํ”„๋ฆฌํ‚ฅ ํ”„๋ผํ•˜ ํฌ๋ฏธ๋‹› ํŒฅ๋น™์ˆ˜ ํ‘œ๋ฐฑ์ œ ํŒ์†Œ๋ฆฌ ํ”ผ์‹œ๋ฐฉ ํŒ”์”จ๋ฆ„ ํ”ผ์•„๋…ธ ํŽธ์˜์  ํŒŒ์ž๋งˆ ํฌ์žฅ์ง€ ํ‘œ์ง€ํŒ ํŒŒ์ถฉ๋ฅ˜ ํŒŒ์ถœ๋ถ€ ํŒŒ์ถœ์†Œ ํฌ์ฒญ์ฒœ ํŒฌํด๋Ÿฝ ํฌ์ผ“๋ณผ ํ”ผ์นด์†Œ ํ”ผ์นด์ธ„ ํญํƒ„์ฃผ ํ”ผํ„ฐํŒฌ',
    'ํƒœ๊ทน๊ธฐ ํƒœ๊ถŒ๋„ ํ„ฑ๊ฑธ์ด ํ…Œ๋‹ˆ์Šค ํŠธ๋ ํฌ ํ„ฐ๋ฏธ๋„ ํ‡ด๋งˆ์‚ฌ ํ† ๋งˆํ†  ํƒฌ๋ฒ„๋ฆฐ ํˆฌ์„๊ธฐ ํ„ฑ์‹œ๋„ ํƒ•์ˆ˜์œก ํ† ์ŠคํŠธ ํƒœ์–‘๊ณ„ ํ‹ฐ์•„๋ผ ํƒ€์ด๋ฐ ํƒˆ์˜์‹ค ํŠธ์œ„ํ„ฐ ํ‡ด์ง๊ธˆ ํ†ต์กฐ๋ฆผ ํƒœ์ง„์•„ ํƒœํ‰์–‘ ํ…Œํ—ค๋ž€',
    '์บฅ๊ฑฐ๋ฃจ ์ฝง๊ตฌ๋ฉ ์ฝ”๋ผ๋ฆฌ ์บ๋‚˜๋‹ค ์ฝฉ๋‚˜๋ฌผ ์ฝ”๋„ˆํ‚ฅ ์ปจ๋””์…˜ ์ฝ”๋Ÿฌ์Šค ์นด๋ฉ”๋ผ ์นด๋ฉ”์˜ค ํ‚ค๋ณด๋“œ ์ฝค๋ฐ”์ธ ์ฝ”๋ฟ”์†Œ ์ผ€์ดํฌ ์ฝ”์ฝ”๋„› ์นดํƒ€๋ฅด ์นตํ…Œ์ผ ์ปคํ”Œ๋ง',
    '์ฐธ๊ธฐ๋ฆ„ ์ฒ ๊ฐ€๋ฐฉ ์ดˆ๋Šฅ๋ ฅ ์ถ•๋†์ฆ ์ทจ๋‘๋ถ€ ์ฒญ๊ณ„์ฒœ ์ฑ…๊ฐˆํ”ผ ์ฑ…๊ฝ‚์ด ์ดˆ๋Šฅ๋ ฅ ์ถ•๋†์ฆ ์ฐฝ๋•๊ถ ์ฐจ๋‘๋ฆฌ ์ตœ๋ฃจํƒ„ ์ตœ๋ฉด์ˆ  ์น ๋ฉด์กฐ ์ฒญ๋ฐ”์ง€ ์ฒญ์†Œ๋…„ ์ถœ์„๋ถ€ ์ฐน์Œ€๋–ก ์ฒญ์™€๋Œ€ ์ฒซ์ธ์ƒ ์น˜์™€์™€ ์ดˆ์ธ์ข… ์ถ”์–ดํƒ• ์ดˆ์ŒํŒŒ ์นจ์ „๋ฌผ ์ฒญ์ฒฉ์žฅ ์ดˆ์ฝœ๋ฆฟ ์น˜ํŠธํ‚ค ์ถœํŒ์‚ฌ ์ฑ”ํ”ผ์–ธ ์นจํŒฌ์ง€ ์ตœํ™๋งŒ',
    '๊ณ„๊ธฐํŒ ๊ฐœ๋‚˜๋ฆฌ ๊ธฐ๋‚ด์‹ ๊ฐ•๋‚ญ์ฝฉ ๊ต๋„๊ด€ ๊ณ ๋“œ๋ฆ„ ๊ณจ๋™ํ’ˆ ๊ธฐ๋Ÿฌ๊ธฐ ๊ฐ€๋กœ๋“ฑ ๊ฐ€๋ž˜๋–ก ๊ธ€๋Ÿฌ๋ธŒ ๊ทธ๋ฆผ์ž ๊ธฐ๋ชจ๋…ธ ๊ธˆ๋ฉ”๋‹ฌ ๊ฑฐ๋จธ๋ฆฌ ๊ต๋ฌด์‹ค ๊ณต๋ฌด์› ๊ฑด๋ง์ฆ ๊ตฌ๋ฏธํ˜ธ ๊น€๋ณ‘๋งŒ ๊ตญ๋ฐฉ๋ถ€ ๊ฑฐ๋ถ์„  ๊ด‘๋ณต์ ˆ ๊ณฑ๋นผ๊ธฐ ๊ฐ€์†๋„ ๊ฐ์„ ๋ฏธ ๊ธฐ์ˆ™์‚ฌ ๊ฐ€์˜ค๋ฆฌ ๊ฑธ์Œ๋งˆ ๊ฐ•์˜์‹ค ๊ฑฐ์ง“๋ง ๊ต์ฐจ๋กœ ๊ณจํ‚คํผ ๊ณผํƒœ๋ฃŒ ๊น€ํƒœ์› ๊น€ํƒœํฌ ๊ฑดํฌ๋„ ๊ณฐํŒก์ด ๊ณจํŒ์ง€ ๊ณตํฌํƒ„ ๊น€ํฅ๊ตญ ๊ด‘ํ™”๋ฌธ ๊ณตํœด์ผ ๊ณ ํ˜„์ • ๊ฝน๊ณผ๋ฆฌ ๊นŒ๋‚˜๋ฆฌ ๊น๋‘๊ธฐ ๊ฝƒ๋‹ค๋ฐœ ๊ฝƒ๋“ฑ์‹ฌ ๊ผฝ๋“ฑ์ด ๊นŒ๋งˆ๊ท€ ๊นŒ์น˜๋ฐœ ๊นํ’๊ธฐ',
    '์ผ€์ฒฉ ํ‚น์นด ํ‚น์ฝฉ ์ปจ๋‹ ์ฑ„์ฐ ์ฐฝ๋ฌธ ์ฐธ๊นจ ์ฒœ๊ตญ ์ถ•๊ตฌ ์ถœ๊ทผ ์นœ๊ตฌ ์น˜๊ณผ ์ทจ๊ถŒ ์ฑ„๊ถŒ ์ดˆ๊ณผ ์กฑ๋ฐœ ์ ˆ๋ฒฝ ์ –๋ณ‘ ์ฃผ๋ถ€ ์ „๋ณต ์ค‘๋ณต ์ •๋ณต ์ ˆ๋ฐ˜ ์กฑ๋ณด ์Ÿ๋ฐ˜ ์ฃผ๋ฒˆ ์ข€๋น„ ์ œ๋น„ ์‚ฌ๊ณผ ์‚ฌ๊ธฐ ์‹œ๊ธ‰ ์‹œ๊ณ„ ์†๊ธˆ ์„ ๊ฑฐ ์ˆ˜๋Šฅ ์„ค๋‚  ์Šค๋‹˜',
    '๋ชจ๋ž˜ ๋ฏธ๋ž˜ ๋ฏธ๋กœ ๋งŒ๋ฃจ ๋–ก๋ฐฅ ๋”ธ๊ธฐ ๋–ก๊ตญ ๋‘์œ  ๋“์Œ ๋‰ด์š• ๋…ธ์„ ๋‚™์—ฝ ๋…น์šฉ ๋…ธ์ธ ๋ˆˆ๋ฌผ ๋ƒ‰๋ฉด ๋‚˜๋น„ ๋‚˜๋ฐฉ ๊ฐ€์œ„ ๊ฑฐ์šธ ๊ทผ์œก ๊ธฐํƒ€ ๊นƒํ„ธ ๊ตฌํ†  ๊ตด๋š ๊ฐœ๋… ๊ทธ๋„ค ๊ตฌ๋ฆ„ ๊ธฐ๋ฆฐ ๊ฒฝ๋งˆ ๊ฒฝ๋งค ๊ณ ๋ง‰ ๊ฐ€๋ฐฉ ๊ณต๋ถ€ ๊ณ ๋ฐฑ ๊ฐ„๋ณ‘ ๊ฐˆ๋น„ ๊น€๋ฐฅ ๊ฑฐ๋ด‰ ๊ตฐ๋ฐค ๊ธฐ๋ถ„ ๊ฑด๋นต ๊ฐ€์‹œ'
];

let WORDS = [];

let _state = STATE_INIT;
let _stateTimer = 0;
let _timer = 0;
let _choanswer = '';
let _answer = '';
let _start = false;
let _widget = null; // using for contents UI
let _players = App.player;
let _result = '';

for(let w in WORD_LINES)
    WORDS =  WORDS.concat(WORD_LINES[w].trim().split(' '));

function cho_hangul(str) {
    cho = ["ใ„ฑ","ใ„ฒ","ใ„ด","ใ„ท","ใ„ธ","ใ„น","ใ…","ใ…‚","ใ…ƒ","ใ……","ใ…†","ใ…‡","ใ…ˆ","ใ…‰","ใ…Š","ใ…‹","ใ…Œ","ใ…","ใ…Ž"];
    result = "";
    for (let i = 0; i < str.length; ++i ) {
      code = str.charCodeAt(i)-44032;
      if(code>-1 && code<11172) result += cho[Math.floor(code/588)];
      else result += str.charAt(i);
    }
    return result;
}

App.onStart.Add(function(){
    startState(STATE_INIT);
});

// when chatting event
// player : person who chatted
// text : chat text
// return : enter chatting box
// return false or true : not appear in chatting box
App.onSay.add(function(player, text) {
    if(_state == STATE_PLAYING)
    {
        if(_answer == text)
        {
            _result = player.name + '๋‹˜ ์ •๋‹ต!\n์ •๋‹ต์€ ' + _answer;

            startState(STATE_JUDGE);
        }
    }
});

function startState(state) {
    _state = state;
    _stateTimer = 0;

    switch(_state)
    {
        case STATE_INIT:
            if(_widget)
            {
                _widget.destroy();
                _widget = null;
            }
            _answer = WORDS[Math.floor(Math.random() * WORDS.length)];
            _timer = 60;
    
            _choanswer = cho_hangul(_answer);
    
            // called html UI
            // param1 : file name
            // param2 : position 
            // [ top, topleft, topright, middle, middleleft, middleright, bottom, bottomleft, bottomright, popup ]
            // param3 : width size
            // param4 : height size
            _widget = App.showWidget('widget.html', 'top', 200, 300);
            
            _widget.sendMessage({
                state: _state,
                timer: _timer,
                answer: _choanswer,
            });

            startState(STATE_READY);
            break;
        case STATE_READY:
            _start = true;
            startState(STATE_PLAYING);
            break;
        case STATE_PLAYING:
            App.showCenterLabel('๋ชฉํ‘œ: ์ดˆ์„ฑํžŒํŠธ๋กœ ๋‹จ์–ด๋ฅผ ์ฐพ์•„๋‚ด์„ธ์š”.',0xFFFFFF, 0x000000, 115);
            _widget.sendMessage({
                state: _state,
                timer: _timer,
                answer: _choanswer,
            });
            break;
        case STATE_JUDGE:
            break;
        case STATE_END:
            if(_widget)
            {
                _widget.destroy();
                _widget = null; // must to do for using again
            }

            _start = false;
            break;
    }
}

App.onLeavePlayer.Add(function(p) {
    p.title = null;
    p.sprite = null;
    p.moveSpeed = 80;
    p.sendUpdated();
});

App.onDestroy.Add(function() {
    _start = false;
    
    if(_widget)
    {
        _widget.destroy();
        _widget = null;
    }
});

App.onUpdate.Add(function(dt) {
    if(!_start)
        return;

    _stateTimer += dt;

    switch(_state)
    {
        case STATE_INIT:
            break;
        case STATE_READY:
            _start = true;
            break;
        case STATE_PLAYING:
            if(_stateTimer >= 1)
            {
                _stateTimer = 0;
                _timer -= 1;
            }

            if(_timer == 0)
            {
                _result = '์ •๋‹ต์€ ' + _answer + ' ์ž…๋‹ˆ๋‹ค.';
                startState(STATE_JUDGE);
            }
            break;
        case STATE_JUDGE:
            App.showCenterLabel(_result, 0xFFFFFF, 0x000000, 115);

            if(_stateTimer >= 3)
                startState(STATE_END);
            break;
        case STATE_END:
            break;
    }
});

Last updated