Python Spritz speed reader模仿者:需要Pyglet用户的帮助吗
链接到更多充实代码的存储库: 通过在应用程序事件循环中返回self.clock.get_sleep_time(False)并使用os.sleep创建暂停和处理器管理,手动消除计算出的空闲时间,可以粗略地修复以下问题 在使用这个程序并强制Pyglet忽略睡眠时间后,我注意到它是处理器密集型的。它真的使我的机器发热,而且我的床上有火灾危险。如果有人能帮我弯曲Pyglets时钟,这样我就可以在不让电脑超速的情况下获得正确的每分钟字数,那我将不胜感激。设置帧速率不起作用。该程序似乎只想在激活事件时加速,奇怪的是,其中包括鼠标滚动(代码中没有处理这一发明) 如果你打算使用下面的代码,请将程序单独放在一个文件夹中,只有一个文本文档(Tkinter不适合我)。每个新文本都会创建一个唯一的书签文件,它们会自动检索该文件。单击窗口的正面以在设置和读卡器之间切换。有时UTF编码会使程序崩溃,在程序关闭之前,您将被标记为书签。将UTF编码添加到UTF列表中Python Spritz speed reader模仿者:需要Pyglet用户的帮助吗,python,pyglet,Python,Pyglet,链接到更多充实代码的存储库: 通过在应用程序事件循环中返回self.clock.get_sleep_time(False)并使用os.sleep创建暂停和处理器管理,手动消除计算出的空闲时间,可以粗略地修复以下问题 在使用这个程序并强制Pyglet忽略睡眠时间后,我注意到它是处理器密集型的。它真的使我的机器发热,而且我的床上有火灾危险。如果有人能帮我弯曲Pyglets时钟,这样我就可以在不让电脑超速的情况下获得正确的每分钟字数,那我将不胜感激。设置帧速率不起作用。该程序似乎只想在激活事件时加速,
import pyglet
from pyglet.gl import *
import os
import pyglet.app.base
for subdir, dirs, files in os.walk('./'):
for file in files:
if ".txt" in file and "SaveSpot" not in file:
filename = file
break
f = open(filename, 'r')
w = f.read()
f.close()
UTF = ["\xef","\xbb","\xbf","\xe2","\x80","\x99","\x00","\xe2","\x80","\x9c","\x9d","\xa6","\x94","\xc3","\xa9","\x98","0x98"]
for elem in UTF:
w = w.replace(elem,"")
words = w.split()
wordslist = []
c = 0
hyphen = False
for word in words:
if "-" in word and hyphen == False:
place = word.find("-")
words.insert(c+1,word[place:])
w = word[:place]
hyphen = True
else:
w = word
if "," in word and hyphen == False: #hyphen is now used for comma detection if revised, add comma and hypen detection at end
hyphen = True
words.insert(c+1, word)
else:
hyphen = False
if ("." in word or "?" in word or "!" in word or ":" in word or ";" in word) and hyphen == False:
words.insert(c+1," ")
words.insert(c+2," ")
wordlist = []
wordlength = len(w)
# these are the rules the roughly seem to mimic spritz
if wordlength > 8:
if w[3] == w[2]:
wordlist = [w[0:2],w[2],w[3:]]
else:
wordlist = [w[0:3],w[3],w[4:]]
elif wordlength < 9 and wordlength > 5 :
if w[2] == w[1]:
wordlist = [w[0:1],w[1],w[2:]]
else:
wordlist = [w[0:2],w[2],w[3:]]
elif wordlength == 5:
wordlist = [w[0],w[1],w[2:]]
elif wordlength == 4:
wordlist = [w[0],w[1],w[2:]]
elif wordlength == 3:
wordlist = [w[0],w[1],w[2]]
elif wordlength == 2:
wordlist = [w[0],w[1],""]
else:
wordlist = ["",w,""]
wordslist.append([wordlist[0],wordlist[1],wordlist[2]])
c += 1
#------------------------------------------------------
states = []
window = pyglet.window.Window(width=600, height=140) # width = 335 Height = 70
pyglet.gl.glClearColor(1, 1, 1, 1)
batch = pyglet.graphics.Batch()
batch2 = pyglet.graphics.Batch()
class PausedState:
def __init__(self):
self.WPM = 500
fontSize = int(window.width * .05)
fontType = 'Times New Roman' # 'Ariel'
center = int(window.width//2.9)
self.save = pyglet.text.Label("Save", font_name=fontType,font_size=fontSize,color=(0, 0, 0, 255),x=window.width/5.2, y=window.height/2, anchor_x='right', anchor_y='center', batch = batch2)
self.load = pyglet.text.Label("Load", font_name=fontType,font_size=fontSize,color=(0, 0, 0, 255),x=window.width/2.5, y=window.height/2, anchor_x='right', anchor_y='center', batch = batch2)
self.back = pyglet.text.Label("<=50", font_name=fontType,font_size=fontSize,color=(0, 0, 0, 255),x=window.width/1.65, y=window.height/2, anchor_x='right', anchor_y='center', batch = batch2)
self.WPMtag = pyglet.text.Label("wpm:", font_name=fontType,font_size=fontSize*.9,color=(0, 0, 0, 255),x=window.width/1.25, y=window.height/2.2, anchor_x='right', anchor_y='center', batch = batch2)
self.WPMdisplay = pyglet.text.Label(str(self.WPM), font_name=fontType,font_size=fontSize*1.2,color=(0, 0, 0, 255),x=window.width/1.015, y=window.height/2.0, anchor_x='right', anchor_y='center', batch = batch2)
self.arrowKeys = pyglet.text.Label("Use Arrow Keys", font_name=fontType,font_size=fontSize*.52,color=(0, 0, 0, 255),x=window.width/1.014, y=window.height/1.07, anchor_x='right', anchor_y='center', batch = batch2)
dist = 0
line1 = (int((window.width/20)+dist), int(window.height/4.5),int((window.width/4.5)+dist), int(window.height/4.5))
line2 = (int((window.width/4.5)+dist),int(window.height-(window.height/4.5)),int((window.width/20)+dist), int(window.height-(window.height/4.5)))
line3 = ((int(window.width/20)+dist), int(window.height-(window.height/4.5)),int((window.width/20)+dist),int(window.height/4.5))
line4 = (int((window.width/4.5)+dist),int(window.height/4.5), int((window.width/4.5)+dist), int(window.height-(window.height/4.5)))
dist = window.width/5
line5 = (int((window.width/20)+dist), int(window.height/4.5),int((window.width/4.5)+dist), int(window.height/4.5))
line6 = (int((window.width/4.5)+dist),int(window.height-(window.height/4.5)),int((window.width/20)+dist), int(window.height-(window.height/4.5)))
line7 = (int((window.width/20)+dist), int(window.height-(window.height/4.5)),int((window.width/20)+dist),int(window.height/4.5))
line8 = (int((window.width/4.5)+dist),int(window.height/4.5), int((window.width/4.5)+dist), int(window.height-(window.height/4.5)))
dist = window.width/2.5
line9 = (int((window.width/20)+dist), int(window.height/4.5),int((window.width/4.5)+dist), int(window.height/4.5))
line10 = (int((window.width/4.5)+dist),int(window.height-(window.height/4.5)),int((window.width/20)+dist), int(window.height-(window.height/4.5)))
line11 = (int((window.width/20)+dist), int(window.height-(window.height/4.5)),int((window.width/20)+dist),int(window.height/4.5))
line12 = (int((window.width/4.5)+dist),int(window.height/4.5), int((window.width/4.5)+dist), int(window.height-(window.height/4.5)))
dist = window.width / 1.3
line13 = (int((window.width/20)+dist), int(window.height/4.5),int((window.width/4.5)+dist), int(window.height/4.5))
line14 = (int((window.width/4.5)+dist),int(window.height-(window.height/4.5)),int((window.width/20)+dist), int(window.height-(window.height/4.5)))
line15 = (int((window.width/20)+dist), int(window.height-(window.height/4.5)),int((window.width/20)+dist),int(window.height/4.5))
line16 = (int((window.width/4.5)+dist),int(window.height/4.5), int((window.width/4.5)+dist), int(window.height-(window.height/4.5)))
batch2.add(2, GL_LINES, None, ('v2i', line1))
batch2.add(2, GL_LINES, None, ('v2i', line2))
batch2.add(2, GL_LINES, None, ('v2i', line3))
batch2.add(2, GL_LINES, None, ('v2i', line4))
batch2.add(2, GL_LINES, None, ('v2i', line5))
batch2.add(2, GL_LINES, None, ('v2i', line6))
batch2.add(2, GL_LINES, None, ('v2i', line7))
batch2.add(2, GL_LINES, None, ('v2i', line8))
batch2.add(2, GL_LINES, None, ('v2i', line9))
batch2.add(2, GL_LINES, None, ('v2i', line10))
batch2.add(2, GL_LINES, None, ('v2i', line11))
batch2.add(2, GL_LINES, None, ('v2i', line12))
batch2.add(2, GL_LINES, None, ('v2i', line13))
batch2.add(2, GL_LINES, None, ('v2i', line14))
batch2.add(2, GL_LINES, None, ('v2i', line15))
batch2.add(2, GL_LINES, None, ('v2i', line16))
def update(self, dt):
pass
def WPMprovider(self, alteration = 0):
self.WPM += alteration
return 60.00/self.WPM
class ReadState:
def __init__(self,wordslist):
center = int(window.width//2.9)
lineGap = window.height/8
pointLineL = int(lineGap * .9)
fontSize = int(window.height * .37)
fontType = 'Times New Roman' # 'Ariel'
self.wordslist = wordslist
self.count = 0
self.label = pyglet.text.Label("", font_name=fontType,font_size=fontSize,color=(0, 0, 0, 255), x=center - int(fontSize/2.5), y=(window.height//2) + 5, anchor_x='right', anchor_y='center',batch = batch)
self.label2 = pyglet.text.Label("", font_name=fontType, font_size=fontSize,color=(250, 65, 65,255), x=center, y=(window.height//2) + 5, anchor_x='center', anchor_y='center',batch = batch)
self.label3 = pyglet.text.Label("", font_name=fontType,font_size=fontSize,color=(0, 0, 0, 255), x= center + int(fontSize/2.5), y=(window.height//2) + 5, anchor_x='left', anchor_y='center',batch = batch)
pyglet.gl.glLineWidth(2)
pyglet.gl.glColor3f(0,0,0)
batch.add(8, GL_LINES, None, ('v2i', (lineGap, window.height - lineGap, window.width-lineGap, window.height - lineGap, lineGap, lineGap, window.width-lineGap,lineGap, center, lineGap, center, lineGap + pointLineL, center, window.height-lineGap, center, window.height - lineGap - pointLineL)))
def update(self,dt):
self.label.text = self.wordslist[self.count][0]
self.label2.text = self.wordslist[self.count][1]
self.label3.text = self.wordslist[self.count][2]
self.count +=1
def savePlace(self):
return self.count-1
def loadPlace(self,count):
if count == -50:
self.count -= 50
else:
self.count = count
@window.event
def on_draw():
if states[1] == ps:
window.clear()
batch2.draw()
else:
window.clear()
batch.draw()
@window.event
def on_mouse_press(x, y, button, modifiers):
if button != 1:
pyglet.EventLoop.exit()
if states[1] == ps:
if (window.width/20 < x < window.width/4.5) and window.height/4.5 < y < window.height-(window.height/4.5):
location = rs.savePlace()
f = open("SaveSpot" + filename,"w")
f.write(str(location))
f.close()
elif (((window.width/20) + (window.width/5)) < x < ((window.width/5)+(window.width/4.5))) and window.height/4.5 < y < window.height-(window.height/4.5):
f = open("SaveSpot" + filename,"r")
place = f.read()
f.close()
rs.loadPlace(int(place))
elif ((window.width/20) + (window.width/2.5) < x < (window.width/2.5)+(window.width/4.5)) and window.height/4.5 < y < window.height-(window.height/4.5):
rs.loadPlace(-50)
else:
states.reverse()
else:
states.reverse()
@window.event
def on_key_press(symbol, modifiers):
ps.arrowKeys.delete()
if symbol == 65362 or symbol == 65363: #up and right arrow keys
ps.WPMprovider(50)
wpm = int(60/ps.WPMprovider())
ps.WPMdisplay.text = (str(wpm))
changeClock()
if symbol == 65364 or symbol == 65361: #down and left arrow keys
wpm = int(60/ps.WPMprovider())
if wpm > 50:
ps.WPMprovider(-50)
wpm = int(60/ps.WPMprovider())
ps.WPMdisplay.text = (str(wpm))
changeClock()
else:
pass
def update(dt):
states[-1].update(dt)
rs = ReadState(wordslist)
ps = PausedState()
states.append(rs)
states.append(ps)
def changeClock():
pyglet.clock.unschedule(update)
WPM = ps.WPMprovider()
pyglet.clock.schedule_interval(update, WPM)
pyglet.clock.schedule_interval(update, 60.0/500)
pyglet.clock.set_fps_limit(20)
class MyEventLoop(pyglet.app.base.EventLoop):
def idle(self):
dt = self.clock.update_time()
redraw_all = self.clock.call_scheduled_functions(dt)
for window in pyglet.app.windows:
if redraw_all or (window._legacy_invalid and window.invalid):
window.switch_to()
window.dispatch_event('on_draw')
window.flip()
window._legacy_invalid = False
return self.clock.get_sleep_time(False)
def exit(self):
pyglet.app.base.EventLoop.exit(self)
def run():
pyglet.app.event_loop = MyEventLoop()
try:
pyglet.app.run()
except UnicodeDecodeError:
print UnicodeDecodeError
location = rs.savePlace()
f = open("SaveSpot" + filename,"w")
f.write(str(location))
f.close()
run()
导入pyglet
从pyglet.gl导入*
导入操作系统
导入pyglet.app.base
对于os.walk(“./”)中的子目录、目录和文件:
对于文件中的文件:
如果文件中有“.txt”而文件中没有“SaveSpot”:
文件名=文件
打破
f=打开(文件名“r”)
w=f.read()
f、 关闭()
UTF=[“\xef”、“\xbb”、“\xbf”、“\xe2”、“\x80”、“\x99”、“\x00”、“\xe2”、“\x80”、“\x9c”、“\x9d”、“\xa6”、“\x94”、“\xc3”、“\xa9”、“\x98”]
对于UTF中的元素:
w=w.替换(元素“”)
words=w.split()
wordslist=[]
c=0
连字符=假
用文字表示:
如果word中的“-”和连字符==False:
place=word.find(“-”)
插入(c+1,单词[位置:)
w=单词[:位置]
连字符=True
其他:
w=字
如果“,”in word and hyphen==False:#如果修改,hyphen现在用于逗号检测,请在末尾添加逗号和hypen检测
连字符=True
字。插入(c+1,字)
其他:
连字符=假
如果(“.”in word或“?”in word或“!”in word或“:”in word或“;”in word)和连字符==False:
插入(c+1,“”)
插入(c+2,“”)
单词表=[]
字长=长度(w)
#这些规则大致上是模仿斯普瑞茨的
如果字长>8:
如果w[3]==w[2]:
词表=[w[0:2],w[2],w[3:]
其他:
词表=[w[0:3],w[3],w[4:]
elif字长<9且字长>5:
如果w[2]==w[1]:
单词表=[w[0:1],w[1],w[2:]
其他:
词表=[w[0:2],w[2],w[3:]
elif wordlength==5:
单词表=[w[0],w[1],w[2:]
elif wordlength==4:
单词表=[w[0],w[1],w[2:]
elif wordlength==3:
单词表=[w[0],w[1],w[2]]
elif wordlength==2:
单词列表=[w[0],w[1],“”]
其他:
单词列表=[“”,w,“]
追加([wordlist[0],wordlist[1],wordlist[2]]))
c+=1
#------------------------------------------------------
国家=[]
窗=pyglet.window.window(宽=600,高=140)#宽=335高=70
pyglet.gl.glClearColor(1,1,1,1)
batch=pyglet.graphics.batch()
batch2=pyglet.graphics.Batch()
类PausedState:
定义初始化(自):
self.WPM=500
fontSize=int(window.width*.05)
fontType=‘泰晤士报新罗马版’#‘Ariel’
中心=整数(窗口宽度//2.9)
self.save=pyglet.text.Label(“保存”,font\u name=fontType,font\u size=fontSize,color=(0,0,0,255),x=window.width/5.2,y=window.height/2,锚定x='right',锚定y='center',批处理=batch2)
self.load=pyglet.text.Label(“load”,font\u name=fontType,font\u size=fontSize,color=(0,0,0,255),x=window.width/2.5,y=window.height/2,锚点x='right',锚点y='center',批次2)
self.back=pyglet.text.Label(“+1代表土生土长的儿子——非常棒的书。你已经为这段代码建立了一个公共存储库了吗?没有,请阅读上面的评论。这个程序在床上是一个潜在的火灾隐患。@JamesBeezho别那么戏剧化。如果这真的是一个问题,你的电脑会关机。如果它值得在这里公开分享,它值得放在Github这样的地方。”。