2

curses在终端游戏(Mac OSX,Python 2.6.5)中使用拼图。这个谜题涉及旋转一些表盘来打开锁。我的代码,有些简化(请原谅丑陋):

import curses

dial = ["| -1  |","|-1/3 |","|  0  |","| 1/2 |","| 2/3 |","|  1  |"]

clear = "       "
pointer = "   ^   "
subdials = [clear,clear]

d = {'d0':{},'d1':{}}
d['d0'] = {'val':2,'disp':dial[2]}
d['d1'] = {'val':2,'disp':dial[2]}

def spin(scr):
    try:
        curses.curs_set(0)
    except:
        pass

    solved = 0
    p = 0
    subdials[p] = pointer
    dials = [d['d0']['disp'],d['d1']['disp']]

    maxy,maxx = scr.getmaxyx()

    newscr = scr.subwin(10,51,maxy-15,0)
    newscr.box(ord('|'),ord('-'))
    newscr.addstr(4,8,''.join(dials))
    newscr.addstr(6,8,''.join(subdials))
    newscr.refresh()

    while solved == 0:
        r = scr.getch()
        subdials[p] = clear
        currd = 'd'+str(p)

        if r == ord('q') or r == ord('Q'):
            break
        elif r == curses.KEY_LEFT:
            if p > 0 and p < 2:
                p -= 1
            else: pass
        elif r == curses.KEY_RIGHT:
            if p >= 0 and p < 1:
                p += 1
            else: pass
        elif r == curses.KEY_UP:
            if d[currd]['val'] >= 0 and d[currd]['val'] < 5:
                d[currd]['val'] += 1
                d[currd]['disp'] = dial[d[currd]['val']]
            else: pass
        elif r == curses.KEY_DOWN:
            if d[currd]['val'] > 0 and d[currd]['val'] <= 5:
                d[currd]['val'] -= 1
                d[currd]['disp'] = dial[d[currd]['val']]
            else: pass
        else: pass

        subdials[p] = pointer
        dials = [d['d0']['disp'],d['d1']['disp']]

        newscr.addstr(4,8,''.join(dials))
        newscr.addstr(6,8,''.join(subdials))
        newscr.refresh()

        if d['d0']['val'] == 5 and d['d1']['val'] == 3:
            solved = 1

    if solved == 0:
        scr.addstr(maxy-1,0,"You can't figure out the lock.")
    else:
        scr.addstr(maxy-1,0,"The lock is open!")
    scr.getch()
    scr.clear()
    return solved

def box():
    solved = curses.wrapper(spin)
    return solved

退出“盒子”功能有两种方式:按“q”退出,或解谜。第一次退出诅咒(无论哪种方式)都没有问题。但是,如果我再次调用“box”,很快就会出现问题。

首先,在 curses 窗口中,我的函数被指示输入的任何键输入pass都会回显到屏幕上。其次,更糟糕的是,当你第二次退出谜题时——无论是退出还是获胜——noecho并且cbreak没有关闭,所以我的终端被塞满了,必须重新设置。

(注意:如果我继续调用“box”,则键盘输入不再在 curses 窗口本身中回显,但我的终端不会恢复正常。)

我一生都无法弄清楚导致这种行为的原因。帮助!

4

1 回答 1

1

我将 Python 更新到 2.7.3,问题就消失了。嘘!

于 2012-05-20T09:39:10.260 回答