全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
1548 1
2020-08-18
写了俄罗斯方块的程序,想使用getGraphicsEvent等函数键入积木移动、变换的指令,但是程序总是陷入空白的循环中,任何键盘指令没有用。希望大佬能指出其中的错误。我会发红包作为感谢!

Tetris <- function(diff = 1)    #diff 难度(下落时间间隔)
{
    par(mai=rep(0,4),oma=rep(0,4))
    plot(0, 0, type = "n", xlim = c(0.5,10.5), ylim = c(1,20),
         xlab = "", ylab = "")
    draw <- function(z)points(z, pch = 15 , cex = 4, col = "#1A4D80")
    clean <- function(z)points(z, pch = 15 , cex = 4, col = "white")

    ##积木
    Smashboy = c(5+20i,6+20i,5+19i,6+19i)    #方块
    Hero = c(5+20i,4+20i,6+20i,7+20i)    #长条
    Teewee = c(5+19i,5+20i,4+19i,6+19i)    #小T
    Ricky = c(4+20i,5+19i,4+19i,6+19i)    #右手L
    Licky = c(4+19i,6+20i,5+19i,6+19i)    #左手L
    Cleveland = c(5+20i,4+20i,5+19i,6+19i)    #小Z
    Rhodeland = c(5+20i,6+20i,5+19i,4+19i)    #反Z

    block = list(Smashboy, Hero, Teewee, Ricky, Licky, Cleveland, Rhodeland)

    #旋转变换
    spin <-function(A) A = (A-A[1])*1i + A[1]
    #左平移
    Lmove <- function(A) A = A - 1
    #右平移
    Lmove <- function(A) A = A + 1
    #下平移
    Dmove <- function(A) A = A - 1i
    #随机出现积木
    toss <- function()floor(runif(1, 1, 8))
    #距离函数
    dist <- function(A)
    {
        I = Im(A); R = Re(A)
        J = c() ; K = c()
        for(i in (min(R):max(R)))
        {
            J = c(J, min(I[which(R == i)]))
            K = c(K, max(Im(Bmat[,i])))
        }      
        min(J - K)
    }
    #积木记录表
    Bmat <- matrix(rep(0,200),20)
    #积木高度函数
    height <- function(Bmat)
    {
        h = which(Bmat!=0)%%20
        if(any(h == 0))h[which(h == 0)] = 20
        22 - min(h)
    }
    keydown <- function(key)
    {
        if (key == "right") A = Lmove(A)
        if (key == "left")  A = Rmove(A)
        if (key == "down")  A = Dmove(A)
        if (key == "up")    A = spin(A)
        draw(A)
    }
    H = 1
    while(H < 21)
    {
        A = block[[toss()]]
        L = dist(A)
        while(L > 1.1)
        {
            draw(A)
            Sys.sleep(diff)
            clean(A)
            getGraphicsEvent(onKeybd = keydown)
            L = L - 1
            A = Dmove(A)
        }
        draw(A)
        for(i in A)Bmat[21 - Im(i),Re(i)] = i
        H = height(Bmat)
    }
}
Tetris(0.2)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2020-8-18 19:27:18
三川啦啦啦 发表于 2020-8-18 17:35
写了俄罗斯方块的程序,想使用getGraphicsEvent等函数键入积木移动、变换的指令,但是程序总是陷入空白的循 ...
感谢楼主分享
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群