简体   繁体   中英

How to create click listener for grid?

I'm trying to create grid (for example 5x5). After creating grid I want to click on an item and pass coordinates into my onClick(i,j) method. How can I do this using blessed and blessed-contrib?

This is my code. This is Gomoku game or Five in a row.

let blessed = require('blessed')
    , contrib = require('blessed-contrib')
    , program = blessed.program()

const Gomoku = require('gomoku-js')

let size = 5

let game = new Gomoku(size)

let order = true // 1=X, 0=O

let screen = blessed.screen()

let grid = new contrib.grid({rows: size, cols: size, screen: screen})

for (let i = 0; i < size; i++) {
    for (let j = 0; j < size; j++) {
        grid.set(i, j, 4, 4, blessed.box, {
            content: '',
            bold: 'bold'
        },)
    }
}

function setChess(i, j) {

    if (order === true) {
        grid.set(i, j, 4, 4, blessed.box, {
            content: 'X',
            bold: 'bold'
        },)
    } else {
        grid.set(i, j, 4, 4, blessed.box, {
            content: 'O',
            bold: 'bold'
        },)
        order = false
    }

    try {
        game.setChessOf(order, i, j)
        order = !order
    } catch (e) {
        console.log(e)
    }

}


screen.key(['escape', 'q', 'C-c'], function (ch, key) {
    return process.exit(0)
});

screen.render()

You need to specify the element to add a callback method, like

    button.on('click', () => { /*callback function from blessed element*/ })

in your case

    grid.on('click', () => { /*callback function from blessed element*/ })

Hope this helps:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM