Keyboard input simultaneously (diagonal game movement)

I am working on a 2D game in Node where the character needs to move diagonally. This is a top-down, text-based game exclusively in a Node environment (no browser, so I don't have any good keydown / keyup events at my disposal).

I use the keypress library to read user input, but I don’t know how to capture two keys at once in order to trigger a diagonal movement (for example, the down arrow and the right arrow). Here is the code that I now have for horizontal and vertical movement:

game = new Game()
game.print()

keypress(process.stdin)
process.stdin.setRawMode(true)
process.stdin.resume()
process.stdin.on('keypress', (ch, key) ->
  return unless key
  return process.stdin.pause() if key.ctrl and key.name is 'c'

  player = game.player
  switch key.name
    when 'up' 
      if key.shift then player.turnUp() else player.moveUp()
    when 'right' 
      if key.shift then player.turnRight() else player.moveRight()
    when 'down' 
      if key.shift then player.turnDown() else player.moveDown()
    when 'left' 
      if key.shift then player.turnLeft() else player.moveLeft()
    when 'enter' 
      player.repeatMove()
    when 'b' 
      player.placeBlock()

  game.update()
  game.print()
)

, . , , , .

?

+4
3

, , . keypress .

, , , 100 , . , 40 , . , 40 . , .

EventEmitter = require('events').EventEmitter
keypress = require('keypress')
_ = require('underscore')

# This module allows for reading simultaneous key events in the terminal.
module.exports = class Keyboard extends EventEmitter
    _keyTimes: {}

    constructor: ->
        keypress(process.stdin)
        process.stdin.setRawMode(true)
        process.stdin.resume()

        @_emitKeyStatus = _.throttle(@_emitKeyStatus, 40, leading: false)
        process.stdin.on('keypress', (ch, key) => @_processKey(key))

    # Batch-emits any keys recently pressed.
    _emitKeyStatus: ->
        currentTime = @_tuple2time(process.hrtime())
        millisecond = 1000000
        keys = {}

        for own keyName, time of @_keyTimes
            if currentTime - time < 100 * millisecond
                keys[keyName] = true

        @emit('keypress', keys)

    _tuple2time: (tuple) ->
        (tuple[0] * 1e9) + tuple[1]

    _processKey: (key) ->
        return unless key
        return @emit('quit') if key.ctrl and key.name is 'c'

        time = @_tuple2time(process.hrtime())

        # Treat ctrl, shift and meta as distinct keys.
        @_keyTimes.shift = time if key.shift 
        @_keyTimes.ctrl = time if key.ctrl
        @_keyTimes.meta = time if key.meta

        @_keyTimes[key.name] = time
        @_emitKeyStatus()
0

:

. , . .

tools.KeyboardController.keyPressed = function(e) {
    var evtobj = window.event? event : e;
    var key = evtobj.keyCode;

    //don't need to change anything if it already pressed
    if(tools.KeyboardController.keyStatus[key] === true) return;
    //we store the key in an object which describe the keyboard status
    tools.KeyboardController.keyStatus[key] = true;
    //send an event to signal the touch is pressed 
    EventManager.fire("tools.KeyboardController.keyPressed."+key);
}

tools.KeyboardController.keyReleased = function(e) {
    var evtobj = window.event? event : e;
    var key = evtobj.keyCode;
    //if key is not already realese, noting to do
    if(tools.KeyboardController.keyStatus[key] === false) return;
    //set the key as not pushed
    tools.KeyboardController.keyStatus[key] = false;
    //send an event to signal the touch is released
    EventManager.fire("tools.KeyboardController.keyReleased."+key);
}

tools.KeyboardController.keyStatus = {};

document.onkeydown  = tools.KeyboardController.keyPressed;
document.onkeyup    = tools.KeyboardController.keyReleased;
+1
checkInput: function(){
    if (this.isPlayerOne == false) {
    if (myInput.isKeyDown(Input.KEY_UP)) {
    if (this.y > 0) {
    this.y = this.y - 10;
    }
    }
    else
    if (myInput.isKeyDown(Input.KEY_DOWN)) {
    // x,y are taken from the left corner
    if (this.y < game_height - this.height)
    this.y = this.y + 10;
    }
    }
    else {
    if (myInput.isKeyDown(65)) { // 'A'
    if (this.y > 0) {
    this.y = this.y - 10;
    }
    }
    else
    if (myInput.isKeyDown(90)) { // 'Z'
    // x,y are taken from the left corner
    if (this.y < game_height - this.height)
    this.y = this.y + 10;
    }
    }
    }
0

Source: https://habr.com/ru/post/1533016/


All Articles