gpt4 book ai didi

r - 相当于 R 中的 C getch()

转载 作者:行者123 更新时间:2023-12-04 12:01:07 26 4
gpt4 key购买 nike

我正在开发 2048游戏在 R为了娱乐。我用了 scan(n=1)用户输入他/她的移动键。但与脚本语言一样,它需要按 Enter键继续。

问:有没有什么方法可以制作类似 getch 的东西?在 C申请在R继续购买按下按钮而不按下Enter ?

任何帮助将不胜感激。

最佳答案

我建议您使用 windows/x11 图形环境处理程序 ( LINK )。

这是打开一个窗口的示例,您可以在其中使用键盘箭头移动蓝色方块:

keyboardAwareSquare <- function(){
currPosition <- c(0,0,1,1)
drawRect <- function(){
plot(c(0, 4), c(0, 4), type= "n", xlab = "", ylab = "",xaxt='n',yaxt='n')
rect(currPosition[1],currPosition[2],currPosition[3],currPosition[4],col='lightblue')
}
keydown <- function(key) {
offsetX <- 0
offsetY <- 0
if(key=='Left') offsetX <- -1
if(key=='Right') offsetX <- 1
if(key=='Down') offsetY <- -1
if(key=='Up') offsetY <- 1
if(!any(currPosition[c(1,3)] + offsetX < 0) && !any(currPosition[c(1,3)] + offsetX > 4)){
tmp <- currPosition
tmp[c(1,3)] <- tmp[c(1,3)] + offsetX
currPosition <<- tmp
}
if(!any(currPosition[c(2,4)] + offsetY < 0) && !any(currPosition[c(2,4)] + offsetY > 4)){
tmp <- currPosition
tmp[c(2,4)] <- tmp[c(2,4)] + offsetY
currPosition <<- tmp
}
drawRect()
}
drawRect()
setGraphicsEventHandlers(prompt='use the keyboard arrows to move',onKeybd = keydown)
eventEnv <- getGraphicsEventEnv()
}

# this part works differntly according to your OS
if(.Platform$OS.type == 'unix') x11(type = "Xlib") else x11()

keyboardAwareSquare()
getGraphicsEvent()

关于r - 相当于 R 中的 C getch(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42084296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com