Python:调用';诅咒';两次塞满了我的终端
我在终端游戏(MacOSX,Python2.6.5)中使用Python:调用';诅咒';两次塞满了我的终端,python,terminal,curses,Python,Terminal,Curses,我在终端游戏(MacOSX,Python2.6.5)中使用诅咒。这个难题包括旋转一些刻度盘来打开锁。我的代码有点简化(请原谅我的丑陋): 导入诅咒 拨号=[“|-1 |”、“|-1/3 |”、“|0 |”、“|1/2 |”、“|2/3 |”、“|1 |”] clear=“” 指针=“^” 子菜单=[清除,清除] d={'d0':{},'d1':{} d['d0']={'val':2,'disp':拨号[2]} d['d1']={'val':2,'disp':拨号[2]} def旋转(scr):
诅咒。这个难题包括旋转一些刻度盘来打开锁。我的代码有点简化(请原谅我的丑陋):
导入诅咒
拨号=[“|-1 |”、“|-1/3 |”、“|0 |”、“|1/2 |”、“|2/3 |”、“|1 |”]
clear=“”
指针=“^”
子菜单=[清除,清除]
d={'d0':{},'d1':{}
d['d0']={'val':2,'disp':拨号[2]}
d['d1']={'val':2,'disp':拨号[2]}
def旋转(scr):
尝试:
诅咒。诅咒集(0)
除:
通过
已解决=0
p=0
子表[p]=指针
拨号盘=[d['d0']['disp'],d['d1']['disp']]
maxy,maxx=scr.getmaxyx()
newscr=scr.subwin(10,51,最大15,0)
新闻框(ord(“|”)、ord(“-”)
新增地址(4,8''。连接(拨号))
newscr.addstr(6,8,''.join(子项))
newscr.refresh()
求解时==0:
r=scr.getch()
子刻度[p]=清除
currd='d'+str(p)
如果r==ord('q')或r==ord('q'):
打破
elif r==curses.KEY_左:
如果p>0且p<2:
p-=1
其他:通过
elif r==curses.KEY\u右:
如果p>=0且p<1:
p+=1
其他:通过
elif r==curses.KEY\u UP:
如果d[currd]['val']>=0且d[currd]['val']]<5:
d[货币]['val']+=1
d[currd]['disp']=拨[d[currd]['val']]
其他:通过
elif r==curses.KEY\u向下:
如果d[currd]['val']>0和d[currd]['val']I将Python更新为2.7.3,那么问题就消失了。胡萨 你好,纳沃斯。我试着在我的控制台上运行你的游戏,它立即退出,例如python curses.py立即终止。你是怎么运作的?其次,您可以尝试使用cProfile模块运行代码,以查看是否有过多的函数调用-python-mcprofile curses.py。这可能意味着一个无限循环。@Aaron Newton-谢谢你的回答。我运行python解释器,导入脚本中的所有内容,然后调用box
函数。从长远来看,这就是我打算做的——从我的主游戏脚本中调用函数,而不是独立运行这个脚本。我尝试在这个脚本中添加一行,调用box()
,并使用cProfile运行它–它似乎没有出现任何不好的结果。感谢您将其清除。我尝试了以下操作:>>导入curses\u prog as game>>>运行=game.box>>>运行>>>运行()回溯(最近一次调用):文件“”,第1行,在文件“curses\u prog.py”中,第78行,在框中解决=curses.wrapper(spin)AttributeError:“模块”对象没有属性“wrapper”
(很抱歉这里的格式设置)。这可能只是因为我对Python的实现一无所知,但我无法让它运行。我认为第二行应该是run=game.box()
,带括号?不管它值多少钱,这段代码在FreeBSD(Python2.7)上对我来说都很好。OSX端口闻起来真像个bug。
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