Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Keyboard input in Javascript(Rhino)

+2 votes
asked Oct 30, 2022 by Adam Alexander (140 points)
Hello! Novice programmer teaching middle school students. Have spent three weeks teaching my students how to program in Javascript(Rhino), and would love to finish out our unit with some cheesy little games, but I can't figure out how to make any kind of active keyboard listener work in that language. Rerouting to a different flavor of javascript would be a little hard on them. Any Rhino masters out there? Goal is simple console games like below but that the user can interact with using WASD keys Thanks in advance!

importPackage(java.io);
importPackage(java.lang);

var screen = [[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]]
var x
var y
var z
function setPosition(x, y, z)
{
    screen[8-y][x+1]=z
}

function clear()
{
    var i
    var j
    for(i=1; i<9; i++)
    {
        for(j=1; j<29; j++)
        {
            screen[i][j]=" "
        }
    }
}

function draw()
{
    var k
    for (k = 0; k<10; k++)
    {
        System.out.println(screen[k])
    }
}

function sendBorders()
{
    var i
    var j
    for(i=0; i<10; i++)
    {
        for(j=0; j<30; j++)
        {
            if (((i==0) && (j==0)) || ((i==9) && (j==29)))
            {
                screen[i][j]="/"
            }
            if (((i==9) && (j==0)) || ((i==0) && (j==29)))
            {
                screen[i][j]="\\"
            }          
            if (((i==0) || (i==9)) && ((j>0) && (j<29)))
            {
                screen[i][j]="-"
            }
            if (((j==0) || (j==29)) && ((i>0) && (i<9)))
            {
                screen[i][j]="|"
            }
            
        }
    }
}

clear()
sendBorders()

var t
for(t=0; t<60; t++)
{
    right = Math.floor(t/(60/27))
    up = Math.floor(-(1/130)*((t-30)*(t-30))+7)
    clear()
    setPosition(right,up,"X")
    Thread.sleep(50)
    System.out.print("\033[H\033[2J")
    draw()
}

1 Answer

+1 vote
answered Dec 12, 2022 by Boyina Sri satya sai kumar (160 points)
var a=prompt("enter any value");
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...