#Kaimiao GalGame Calculator引擎v2.0
# KMGEc v2.0兼容>KMGEc1.6脚本
# Made by 凯喵仔 2026.2
# B站@凯喵仔|cncalc.org@kaimiao|hpcalc.org@kaimiao
# KMGEc新版本，需要固件版本在2025.9.15更新的版本及以后
#KMGEc2.x开始了,纪念HP Prime G2

from hpprime import *
from urandom import *

version="KMGEc2.0"
Information=[
"  Game is:铃音之歌初音之声",
">>>","  Engine made by 凯喵仔",
">>>", "  K M A T 2 0 2 6 ", ">>>",
"  Kaimiao GalGame Engine Calculator",
"  Version 2.0", "  KMGEc2.0"]
text_u=["K","M","A","T","KMGEc","2.0"]
tu_p=[[60,randint(0,100)],[75,randint(0,100)],
[90,randint(0,100)],[105,randint(0,100)],
[160,randint(0,100)],[205,randint(0,100)]]
eval("G1:=AFiles(\""+"black.jpg"+"\")")                          

GT2=0
st_txt="script.txt";st="black.jpg";c_number=0
l_all=0;line_txt=1;txtw="";line_txtc=1
LA="";LB=""
text=[];t2=[];l4=""
LU="";LN=""
LADD2=""
at=1
LAO="";LAO2="";LAO3="";LNO=""
LP=st;LPO="";Plist=[]
lpd=0;seta="";ato1=0
c_text=[];back_text=[];fc_wn=[]
    
fillrect(0,0,0,320,240,0,0)
t_list=["","","","",
"铃音之歌初音之声",
"",
"KMAT/Kaimiao Anthing",
"BiliBili @ 凯喵仔",
"<KMGEc2.0>",
"Script by ......",
""]

for t in range(30):
  t_list[5]+="-"
  t_list[10]+="-"
  
while True:
  pas=0
  fillrect(1,0,0,320,240,0,0)
  for i in range(len(tu_p)):
    if tu_p[i][1]>50:
      tu_p[i][1]-=1
    elif tu_p[i][1]<50:
      tu_p[i][1]+=1
  for i in range(len(tu_p)):
    textout(1,tu_p[i][0],tu_p[i][1],text_u[i],255255255)
  strblit(0,0,0,320,240,1)
  for i in range(len(tu_p)):
    if tu_p[i][1]==50:
      pas+=1
  if pas==len(tu_p):
    break
  
for t in range(len(t_list)):
  textout(0,10,5+t*20,t_list[t],200200200)
  eval("WAIT(0.04)")
  
def Line(x1,y1,x2,y2):
  line(1,x1,y1,x2,y2,0)
  
eval("G2:=AFiles(\""+"black.jpg"+"\")")                           
#pic_file(145ms/260file)
all_f=eval("AFiles")
pic_all=[]
for i in range(len(all_f)):
  if ".jpg" in all_f[i] or ".png" in all_f[i]:
    pic_all.append(all_f[i])
all_f=[]
c_this=""

def wait(n):
  st=ticks()
  nt=ticks()
  while nt-st<=n*1000:
    nt=ticks() 
  
def pic_ns():
  global c_number,c_this
  c_this=""
  eval("G1:=AFiles(\""+"black.jpg"+"\")")                          
  fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7)
  eval("G2:=AFiles(\""+pic_all[0]+"\")")                          
  blit(1,0,0,2)
  fillrect(1,9,221,200,17,0x66000000,0x66FFFFFF)
  textout(1,10,222,pic_all[0]+"(1/"+str(len(pic_all))+")",0)
  for i in range(16):
    t=ticks()
    eval("BLIT_P(G0,0,0,320,240,G1,0,0,320,240,1,"+str(i**2-1)+");")
    wait(((1000/30)-ticks()+t)/1000)
  c_p=0
  while True:
    k=keyboard()
    if k:
      if k==16 or k==1073741824:
        c_this=pic_all[c_p]
        break
      elif k==128:
        if c_p>0:
          c_p-=1
        else:
          c_p=len(pic_all)-1
      elif k==256:
        if c_p<len(pic_all)-1:
          c_p+=1
        else:
          c_p=0       
      fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7)
      eval("G2:=AFiles(\""+pic_all[c_p]+"\")")                          
      blit(1,0,0,2)
      fillrect(1,9,221,200,17,0x66000000,0x66FFFFFF)
      textout(1,10,222,pic_all[c_p]+"("+str(c_p+1)+"/"+str(len(pic_all))+")",0)
      for i in range(16):
        t=ticks()
        eval("BLIT_P(G0,0,0,320,240,G1,0,0,320,240,1,"+str(i**2-1)+");")
        wait(((1000/30)-ticks()+t)/1000)

def pic_sc():
  global c_this
  c_this=""
  eval("G1:=AFiles(\""+"black.jpg"+"\")")                          
  fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7) 
  eval("MSGBOX(\""+"确定后在L0内输入图像名称\n如"+"'"+"test.jpg"+"',要单/双引号"+"\")")            
  eval("{}▶L0")  
  eval("INPUT(L0)")
  putin=eval("L0")     
  try:
    if putin[0] in pic_all:
      fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7)
      eval("G2:=AFiles(\""+putin[0]+"\")")                          
      blit(1,0,0,2)
      fillrect(1,9,221,200,17,0x66000000,0x66FFFFFF)
      textout(1,10,222,putin[0]+"(ESC/Enter)",0) 
      for i in range(16):
        t=ticks
        eval("BLIT_P(G0,0,0,320,240,G1,0,0,320,240,1,"+str(i**2-1)+");")   
        wait(((1000/30)-ticks()+t)/1000) 
      while True:
        k=keyboard()
        if k:
          if k==16 or k==1073741824:
            c_this=putin[0]
            break
    else:
      eval("MSGBOX(\""+"输入有误"+"\")")  
      c_this="err"
  except:
    c_this="err"
    eval("MSGBOX(\""+"输入有误"+"\")")            
     
def c_view():
  global c_number,c_this
  choose_w("自定义场景:",["选择图像","检索图像","退出自定义场景"])
  if c_number==0:
    pic_ns()
  elif c_number==1:
    pic_sc()
  if c_number!=2 and c_this!="err":
    try:
      eval("MSGBOX(\""+"确定后在L0内输入角色名称\n如"+"'"+"Test Name"+"',要单/双引号"+"\")")            
      eval("{}▶L0")  
      eval("INPUT(L0)")
      name=eval("L0")[0]  
      eval("MSGBOX(\""+"确定后在L0内输入对话内容\n如"+"'"+"Test text"+"',要单/双引号"+"\")")            
      eval("{}▶L0")  
      eval("INPUT(L0)")
      text=eval("L0")[0]
      R=1
    except:
      eval("MSGBOX(\""+"输入有误"+"\")")  
      R=0 
    if R==1:
      if type(text)==type(name)==type("test"):
        LNO=name
        LAO=text
        if LNO =="A0":
          LNO=""
          LAO="   "+LAO       
        else:
          LNO="【"+LNO+"】"
          LAO="『"+LAO+"』"
        if LNO!="" and len(LAO)>=22:
          LAO=LAO[:21]+" "+LAO[21:]
        if LNO=="":
          LAO=LAO[:41]+" "+LAO[41:]
        if len(LAO)>=22 :
           LAO2=LAO[22:]
           LAO=LAO[:22]
        else:
          LAO2=""
        if len(LAO2)>=20:
           LAO3=LAO2[20:]
           LAO2=LAO2[:20]
        else:
          LAO3=""             
        eval("G1:=AFiles(\""+c_this+"\")")                                       
        fillrect(1,5,161,310,77,165255102,165255102)
        textout(1,5,164,LNO,0)
        textout(1,13,180,LAO,0)                          
        if LAO2!="":                         
          textout(1,27,195,LAO2,0)                                                                              
        if LAO3!="":  
          textout(1,27,210,LAO3,0)  
        for i in range(16):
          t=ticks()
          eval("BLIT_P(G0,0,0,320,240,G1,0,0,320,240,1,"+str(i**2-1)+");") 
          wait(((1000/30)-ticks()+t)/1000)   
        while True:
          k=keyboard()
          if k:
            if k==16 or k==1073741824:            
              break        
      else:   
        eval("MSGBOX(\""+"输入有误"+"\")")  
               
#鉴赏模式 
def see_look():
  global c_number    
  c_number=0      
  eval("G1:=AFiles(\""+"black.jpg"+"\")")                          
  fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7)
  while True:
   choose_w("鉴赏模式:",["图像察看","检索图像","自定义场景","退出鉴赏模式"])
   if c_number==0:
     pic_ns()
   elif c_number==1:
     pic_sc()
   elif c_number==2:
     c_view()
   elif c_number==3:
     break          
 
#choose_w
def choose_w(title,text):
  global c_number    
  c_number=0      
  eval("G1:=AFiles(\""+"black.jpg"+"\")")                          
  fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7)
  textout(1,45,25,title,0) 
  for c in range(len(text)):
    textout(1,60,50+30*c,text[c],0) 
  rect(1,60,50+c_number*30,180,20,0)
  for i in range(16):
    t=ticks()
    eval("BLIT_P(G0,0,0,320,240,G1,0,0,320,240,1,"+str(i**2-1)+");")
    wait(((1000/30)-ticks()+t)/1000)
  c_w=1
  while c_w==1:
    ck=keyboard()
    if ck:
      if ck==4:
        if c_number>=1:
          c_number-=1
        else:
          c_number=len(text)-1
      elif ck==4096:
        if c_number<=len(text)-2:
          c_number+=1 
        else:
          c_number=0 
      elif ck==1073741824:            
        c_w=0        
      fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7)
      textout(1,45,25,title,0)            
      for c in range(len(text)):
        textout(1,60,50+30*c,text[c],0) 
      rect(1,60,50+c_number*30,180,20,0)
      strblit(0,0,0,320,240,1) 
      eval("WAIT(0.2)")         

fst=0
#flow_chat
def flow_chat(xy,text,txt,line,pic,drow):
  global seta,ldp,ato1,read_txt,line_txt,LP,fc_drow,lpd,fst,GT2
  c_f=0
  eval("G1:=AFiles(\""+"black.jpg"+"\")")                              
  fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7)   
  for f in range(len(text)):
    if -20<=xy[f][1]<=340: 
      textout(1,xy[f][0],xy[f][1],text[f],0)       
  rect(1,xy[c_f][0],xy[c_f][1],15*len(text[c_f]),15,0)
  textout(1,2,2,"ESC",0) 
  for i in range(len(drow)-1): 
    if -10<=drow[i][1]<=330 or-10<=drow[i+1][1]<=330 :
      x1=drow[i][0];y1=drow[i][1]+15
      x2=drow[i+1][0];y2=drow[i+1][1]
      Line(x1,y1,x2,y2)
  for i in range(16):
    t=ticks()
    eval("BLIT_P(G0,0,0,320,240,G1,0,0,320,240,1,"+str(i**2-1)+");")
    wait(((1000/30)-ticks()+t)/1000)
  fs=1
  y_m=0
  a_f=len(text)
  while fs==1:
    fk=keyboard()
    if fk:
      if fk==1 and xy[c_f][1]+y_m<=220:
        y_m+=15
        eval("WAIT(0.05)")
      elif fk==32 and xy[c_f][1]+y_m>=10:
        y_m-=15
        eval("WAIT(0.05)")
      elif fk==1024:
        seta="out"
        eval("MSGBOX(\""+"存档未保存"+"\")")            
        break
      elif fk==16:     
        ato1=1 
        GT2=1
        fs=0
      elif fk==4 and c_f>=1 and xy[c_f][1]+y_m>=10:
        c_f-=1
        eval("WAIT(0.2)")
      elif fk==4096 and c_f<=a_f-2 and xy[c_f][1]+y_m<=220 :
        c_f+=1 
        eval("WAIT(0.2)")
      elif fk==1073741824:
        textout(1,20,190,"Loading!",0)   
        textout(1,20,210,"Loading Now!",0)   
        strblit(0,0,0,320,240,1)
        lpd=1
        if read_txt!=txt[c_f]: 
          read_txt=txt[c_f]
          line_txt=line[c_f]
          read_text(read_txt)
          LP=pic[c_f]           
          ato1=1
        else:
          line_txt=line[c_f]
          LP=pic[c_f]               
          ato1=1
        fc_read(LP)
        fc_write(LP,1)
        for i in fc_wn:
          fc_write(i,2) 
        eval("WAIT(0.1)")   
        ato1=1 
        blitP(LP,Plist)
        read_lines(line_txt)
        #line_txt+=2
        #read_lines(line_txt)
        break
      fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7)
      for f in range(len(text)):
        if -10<=xy[f][1]+y_m<=330: 
          textout(1,xy[f][0],xy[f][1]+y_m,text[f],0) 
      for i in range(len(drow)-1):
        if -10<=drow[i][1]+y_m<=330 or -10<=drow[i+1][1]<=330 :
          x1=drow[i][0];y1=drow[i][1]+15+y_m
          x2=drow[i+1][0];y2=drow[i+1][1]+y_m
          Line(x1,y1,x2,y2)                        
      textout(1,2,2,"ESC",0) 
      rect(1,xy[c_f][0],xy[c_f][1]+y_m,15*len(text[c_f]),15,0)
      strblit(0,0,0,320,240,1)

#这个函数把立绘列表带背景粘贴到G1
def blitP(BG,PLS):
  eval("G1:=AFiles(\"" + BG + "\")")  
  for i in range(len(PLS)):
    eval("G3:=AFiles(\"" + PLS[i][0] + "\")")
    blit(1,PLS[i][1],PLS[i][2],3)  
  
  
def picB():
 global ato1 
 for sx in range(5):
    blitP(LPO,Plist)            
    for sn in range(20):
      fillrect(1,16*sn,0,sx*4,240,0,0)   
    strblit(0,0,0,320,240,1)
 for sx in range(5):
    blitP(LP,Plist)        
    for sn in range(21):
      fillrect(1,320-16*sn,0,16-sx*4,240,0,0)   
    strblit(0,0,0,320,240,1)
 

def pic():
  global ato1
  eval("G1:=AFiles(\"" + LP + "\")")  
  blitP(LP,Plist)
  fillrect(1,5,161,310,77,165255102,165255102)                                      
  
    
def picA():
  global ato1
  strblit(0,0,0,320,240,1)                    
  blitP(LP,Plist)               
  for i in range(16):    
    t=ticks()                                        
    eval("BLIT_P(G0,0,0,320,240,G1,0,0,320,240,1,"+str(i**2-1)+");")
    wait(((1000/30)-ticks()+t)/1000)
        
  
def picJ():  
  global ato1
  blitP(LP,Plist)
  for i in range(2):
    strblit(0,0-40,0-30,320+80,240+60,1)     
    eval("WAIT(0.25)")
    strblit(0,0,0,320,240,1)  
    eval("WAIT(0.25)")                          
           
  
def picM():  
  global ato1
  blitP(LP,Plist)
  for i in range(21):
    strblit(0,-320+i*16,0,16*i,240,1)      
     
#PPL
def PPL(run):
  global ato1  
  eval(run)
    
fc_text=[];fc_txt=[];fc_xy=[]
fc_line=[];fc_pic=[];fc_u=[]
fc_u2=[];fc_TF=[];fc_drow=[]
with open("flow_chat.txt", 'r',encoding="utf-8") as f_ctxt:
  fc_u=f_ctxt.readlines()
  f_ctxt.close
for fc in range(len(fc_u)):
  if fc==0 or fc%2==0:
    fc_u2=(fc_u[fc].strip()).split(";")
    fc_text.append(fc_u2[0])    
    fc_txt.append(fc_u2[1])
    fc_line.append(int(fc_u2[2]))
    fc_xy.append(fc_u2[3].split())
    fc_pic.append(fc_u2[4])
  else:
    fc_TF.append(fc_u[fc].strip())  
for i in range(len(fc_xy)):
  for j in range(len(fc_xy[i])):
    fc_xy[i][j]=(int(fc_xy[i][j]))     
with open("flow_chat_L.txt", 'r',encoding="utf-8") as f_ctxt2:
  fc_2u=f_ctxt2.readlines()
  f_ctxt2.close
for i in range(len(fc_2u)):
  fc_2u[i]=fc_2u[i].strip()
for i in range(len(fc_2u)):
  fc_2u[i]=fc_2u[i].split(";")
fc_see=[]
for m in range(len(fc_TF)):
  if fc_TF[m]=="T":
    for i in range(len(fc_2u)):
       if fc_text[m] in fc_2u[i]:
         fc_drow.append(fc_xy[m])
         fc_see.append(fc_text[m])  
          
#f_c_write
def fc_write(name,mod):         
  global fc_u     
  if mod==1:
    for i in range(len(fc_u)):
      if name==(fc_u[i])[:len(name)]:
        fc_u[i+1]="T\n" 
    w_u=""
    for i in fc_u:
      w_u+=i 
    with open("flow_chat.txt", 'w',encoding="utf-8") as f_ctxt_w: 
      f_ctxt_w.write(w_u)
      f_ctxt_w.close
  else:
    for i in range(len(fc_u)):
      if name==(fc_u[i])[:len(name)]:
        fc_u[i+1]="F\n" 
    w_u=""
    for i in fc_u:
      w_u+=i 
    with open("flow_chat.txt", 'w',encoding="utf-8") as f_ctxt_w: 
      f_ctxt_w.write(w_u)
      f_ctxt_w.close
    
  
#fc_read
def fc_read(name): 
   global fc_text,fc_2u,fc_drow,fc_TF,fc_wn
   f_l=0
   for i in range(len(fc_2u)):
     if name in fc_2u[i]:
       for j in range(len(fc_text)):
         
         if name==fc_text[j]:          
           fc_drow[i]=fc_xy[j]
           fc_see[i]=fc_text[j]
           fc_TF[j]="T"
       f_l=i
   fc_wn=[]
   for i in range(len(fc_text)):
     if fc_text[i] not in fc_see:
       fc_TF[i]="F"
       fc_wn.append(fc_text[i])
       fc_write(fc_text[i],2)
   
#行号;文本;背景LP;立绘Plist
def LOAD(number):
  global line_txt,read_txt,LP,Plist
  use=eval("AFiles(\""+"SAVE"+str(number)+"\")")   
  line_txt=int(use[0])
  read_txt=use[1]
  LP=use[2]
  Plist=use[3]
  for i in range(len(Plist)):
    Plist[i][1]=int(Plist[i][1])
    Plist[i][2]=int(Plist[i][2])

def SAVE(number):
  use=[line_txt,read_txt,LP,Plist] 
  use=str(use);use=use.replace("[","{");use=use.replace("]","}");use=use.replace("'",'"')
  eval(use+"▶AFiles(\""+"SAVE"+str(number)+"\")")  
                                         
#START
def START():   
  global line_txt,read_txt,LP,c_number,seta
  choose_w("请选择",["继续游戏","指定存档","重新开始","信息","鉴赏","退出"])
  if c_number==2:
    line_txt=1
    read_txt=st_txt
    LP=st
  elif c_number==0:
    LOAD(0)
  elif c_number==1:
    eval("WAIT(0.15)") 
    choose_w("读取哪个存档?",["存 档 0","存 档 1","存 档 2","存 档 3"])                
    LOAD(c_number)
  elif c_number==4:
    see_look()
    return START()
      
  elif c_number==5:
    seta="out"
    eval("MSGBOX(\""+"KMGEc OFF"+"\")") 
  
  elif c_number==3:
    eval("G1:=AFiles(\""+"black.jpg"+"\")")   
    fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7)          
    for t in range(len(Information)):
      textout(1,40,20+t*20,Information[t],0)
    textout(1,10,2,"按ESC",0)
    for i in range(16):
      t=ticks()
      eval("BLIT_P(G0,0,0,320,240,G1,0,0,320,240,1,"+str(i**2-1)+");")
      wait(((1000/30)-ticks()+t)/1000)
    at9=1
    while at9==1:
     k=keyboard()
     if k and k==16:
       at9=0
    return START() 
             


#2nd
c_n=0 
RText=["RUN","FlowChat","PPL"]

def read_text(txt_n):
  global text 
  text=[] 
  with open(txt_n, 'r') as file:     
    for line in file:
        text.append(line.strip())
    file.close
    
GT2=0      
GT=1    
def read_lines(s_line):      
    global LAO,LNO,LP,LADD2,seta,LAO2,LAO3,line_txt,ato1,LP,LPO,lpd,read_txt,c_text
    global c_n,sblit,back_text,fc_wn,GT,GT2,Plist
    try:       
      if len(text)!=0:                                                                                                                                  
          LA=text[s_line]
          LN=text[s_line-1]                                   
          if LN =="RUN":
            read_txt=LA
            line_txt=1
            read_text(read_txt)
            #lpd=1
            RT()
            back_text=[]        
          elif LN[:6]=="CHOOSE":
            c_u=LA.split(";")
            c_n=int(LN[6])
            c_text=[]
            for c in range(1,len(c_u)):
              c_text.append(c_u[c])
            #lpd=1
            back_text=[]
            choose(c_n,c_text,c_u[0])
            GT2=1
            read_text(read_txt)
          elif LN[:3]=="PIC":
            Plist=[]
            LPO=LP
            LP=LA            
            if LN[3:]=="B":
              lpd=1                                  
              picB()
            elif LN[3:]=="N":
              lpd=1             
              pic() 
            elif LN[3:]=="J":
              lpd=1              
              picJ()   
            elif LN[3:]=="M":
              lpd=1              
              picM()         
            else:
              lpd=1             
              picA()   
          elif LN=="FlowChat":           
            fc_write(LA,1)
            fc_read(LA)
            for i in fc_wn:
              fc_write(i,2) 
            #lpd=1           
          elif LN=="PPL":
            lpd=1        
            PPL(LA)
          elif LN[:3]=="OBT":
            use=LA
            use=use.split(";")
            for i in range(len(use)):     
              use[i]=use[i].split(",")
              use[i][-1]=int(use[i][-1])
              use[i][-2]=int(use[i][-2])
            Plist=use
            lpd=1  
            blitP(LP,Plist)
            for i in range(16):    
              t=ticks()                                        
              eval("BLIT_P(G0,0,0,320,240,G1,0,0,320,240,1,"+str(i**2-1)+");")
              wait(((1000/30)-ticks()+t)/1000)
          elif LN=="WAIT":
            wait(int(LA)/1000)
            lpd=1
          elif LN=="OBTremove":
            use=LA.split(";")
            for i in range(len(use),0,-1):
              Plist.pop(int(use[i]))
            lpd=1
            blitP(LP,Plist)
            for i in range(16):    
              t=ticks()                                        
              eval("BLIT_P(G0,0,0,320,240,G1,0,0,320,240,1,"+str(i**2-1)+");")
              wait(((1000/30)-ticks()+t)/1000)
          elif LN=="TEXT":
            use=LA.split(";")
            textout(0,use[0],int(use[1]),int(use[2]),0)
            lpd=1
          elif LN =="A0":
            LNO=""
            LAO="   "+LA
            GT=1
          elif LN =="A1":
            LNO="Another View"
            LAO="   "+LA
            GT=1
          else:
            LNO="【"+LN+"】"
            LAO="『"+LA+"』"
            GT=1
          if LNO!="" and len(LAO)>=22:
             LAO=LAO[:21]+" "+LAO[21:]
          if LNO=="":
             LAO=LAO[:41]+" "+LAO[41:]
          if len(LAO)>=22 :
             LAO2=LAO[22:]
             LAO=LAO[:22]
          else:
            LAO2=""
          if len(LAO2)>=20:
             LAO3=LAO2[20:]
             LAO2=LAO2[:20]
          else:
            LAO3=""
                   
          if seta!="r" and lpd==0:
            if LP!="" and GT2==1:
              blitP(LP,Plist)
                         
            fillrect(1,5,161,310,77,165255102,165255102)
            textout(1,5,164,LNO,0)
            textout(1,13,180,LAO,0)                          
            if LAO2!="":              
              if GT==1:
                textout(1,27,195,LAO2,0)                                                                              
            if LAO3!="":   
              if GT==1:
                textout(1,27,210,LAO3,0) 
            if sblit==1:                        
              strblit(0,0,0,320,240,1)
          lpd=0
          sblit=1        
          GT=0
          if LN in RText or "PIC" in LN or "OBT" in LN:
            line_txt+=2
            return read_lines(line_txt)      
          elif "CHOOSE" in LN or LN=="WAIT":
            line_txt+=2
            return read_lines(line_txt)   
          else:
            if GT2==0:
              back_text.append(LN)
              back_text.append(LAO)
              back_text.append(LAO2)
              back_text.append(LAO3)
            GT2=0
            return 0                                                  
    except:        
      if line_txt+1>=len(text):
        eval("MSGBOX(\""+"文本已尽未续"+"\")") 
      else:
        eval("MSGBOX(\""+"ERROR:"+text[line_txt]+text[line_txt+1]+"\")")              
      seta="out"
    
#rt
def RT():
  global line_txt,GT
  GT=0
  line_txt+=2
  read_lines(line_txt)
          
#CHOOSE_txt
def choose(n,text,txt):
  global read_txt,line_txt,ato1,GT,GT2
  pxy=0      
  cg_s=1 
  eval("G1:=AFiles(\""+"black.jpg"+"\")")                           
  fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7)
  for s in range(n):
    textout(1,60,20+30*s,text[s],0) 
  rect(1,60,20+pxy*30,180,20,0)
  for i in range(16):
    t=ticks()
    eval("BLIT_P(G0,0,0,320,240,G1,0,0,320,240,1,"+str(i**2-1)+");")   
    wait(((1000/30)-ticks()+t)/1000)
  while cg_s==1:
    ck=keyboard()
    if ck:
      if ck==4:
        if pxy>=1:
          pxy-=1
          eval("WAIT(0.2)")
        else:
          pxy=n-1
          eval("WAIT(0.2)")         
      elif ck==4096:
        if pxy<=n-2:
          pxy+=1 
          eval("WAIT(0.2)")
        else:
          pxy=0
          eval("WAIT(0.2)")
      elif ck==1073741824:
        textout(1,20,190,"Loading!",0)   
        textout(1,20,210,"Loading Now!",0)   
        strblit(0,0,0,320,240,1)
        read_txt=txt+str(pxy+1)+".txt"
        line_txt=1        
        cg_s=0
      fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7)
      for s in range(n):
        textout(1,60,20+30*s,text[s],0) 
      rect(1,60,20+pxy*30,180,20,0)
      strblit(0,0,0,320,240,1) 
 
#back_text
def btext_blit(): 
  global back_text,ato1,GT2
  eval("G1:=AFiles(\""+"black.jpg"+"\")")                           
  fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7)
  try: 
    for s in range(14):
      textout(1,20,220-s*18,back_text[len(back_text)-1-s],0) 
  except:
    a=0
  bs=0      
  if len(back_text)+bs>=-5:
          textout(1,2,2,"↑",0)
  for i in range(16):
    t=ticks()
    eval("BLIT_P(G0,0,0,320,240,G1,0,0,320,240,1,"+str(i**2-1)+");") 
    wait(((1000/30)-ticks()+t)/1000)
  while True:
    bk=keyboard()
    if bk:
      if bk==4 and len(back_text)+bs>=-5:
        bs-=2
        eval("WAIT(0.08)")
      if bk==4096 and bs<=-2:
        bs+=2
        eval("WAIT(0.08)")
      elif bk==1024:
        seta="off"        
        eval("MSGBOX(\""+"存档未保存"+"\")") 
        break
      elif bk==16:
        GT2=0                     
        ato1=1
        eval("WAIT(0.15)") 
        break
      fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7)
      if len(back_text)+bs>=-5 and bs<=-5:
        textout(1,2,2,"↑",0) 
        textout(1,2,222,"↓",0) 
      elif len(back_text)+bs>=-5 and bs>=-5:      
        textout(1,2,2,"↑",0) 
      else:
        textout(1,2,222,"↓",0)      
      try: 
        for s in range(14):
          textout(1,20,220-s*18,back_text[len(back_text)-1-s+bs],0) 
      except:
        a=0
      strblit(0,0,0,320,240,1)
      
#info_gm
def info_gm():                        
  global seta
  eval("G1:=AFiles(\""+"black.jpg"+"\")")   
  fillrect(1,0,0,320,240,0x0036e2d7,0x0036e2d7)          
  textout(1,40,20,"当前脚本:"+read_txt,0)
  textout(1,40,40,"行号/总行数:"+str(line_txt)+"/"+str(len(text)),0)
  textout(1,10,2,"ESC返回",0)
  for i in range(16):
    t=ticks()
    eval("BLIT_P(G0,0,0,320,240,G1,0,0,320,240,1,"+str(i**2-1)+");")
    wait(((1000/30)-ticks()+t)/1000)
  textn="统 记 中"
  for i in range(len(textn)):
    textout(0,50+12*i,60,textn[i],0)
    eval("WAIT(0.05)")
  ct_all=[0,"是",0,"否"]
  for i in range(len(text)):
    if "PIC"==text[i][:3]:
      ct_all[0]+=1
    elif "CHOOSE"==text[i][:6] or "RUN"==text[i]:
      ct_all[1]="否"
      if "CHOOSE"==text[i][:6]:
        ct_all[3]=(text[i+1].split(";"))[0]+"/C.txt/"
      else:
        ct_all[3]=text[i+1]
    elif "A0"==text[i]:
      ct_all[2]+=1 
  textout(0,40,80,"完成!",0)
  textout(0,60,100,"PIC:"+str(ct_all[0]),0)
  textout(0,60,120,"是否终章:"+str(ct_all[1]),0)
  textout(0,60,140,"下一章节:"+ct_all[3],0)
  textout(0,60,160,"A0:"+str(ct_all[2]),0)
  textout(0,40,180,">>>",0)
  textout(0,60,200,version,0)
  while True:
    k=keyboard()
    if k and k==16:
      eval("WAIT(0.15)")
      break 

  
                                                                    
#ATTO
t_one=2
t_st=0
atto=0
def ATTO(): 
  global t_one,c_number,atto,t_st
  choose_w("时间:",["0s","1s","2s","3s","4s","5s"])
  t_one=c_number
  t_st=0
  atto=1 

def RUN():
  global line_txt,GT2,atto,ato1,rk,at,c_number,seta,t_st
  rk=0
  while True:
    if seta=="out":
      break        
                  
    if ato1==1:      
      read_lines(line_txt) 
      ato1=0    
                            
    key = keyboard()
    
    if key == 16 : #ESC 或 CAS 键保存并退出      
      at-=1         
      choose_w("保存到哪个存档?",["存 档 0","存 档 1","存 档 2","存 档 3","返回","主界面"])
      sio="T"      
      if c_number==4:
        sio="F"   
      elif c_number==5:
        sio="Out" 
      if sio=="T":  
        SAVE(c_number)     
        seta="out"                  
        break
      if sio=="Out":       
        break
      GT2=1
      read_lines(line_txt)      
      
    if atto==1:      
      if (ticks()-tko)/1000>=t_one:
        line_txt+=2
        read_lines(line_txt)
        textout(0,15,225,"AUTO",0)
        tko=ticks()     
      
    elif key ==1024:
      seta="out"
      eval("MSGBOX(\""+"行号："+str(line_txt)+"但未保存"+"\")") 
      break
        
    if key == 1073741824 or key ==128: 
     if rk!=1:
        line_txt+=2
        eval("WAIT(0.2)")
        read_lines(line_txt)
        atto=0
     else:               
        eval("WAIT(0.2)")
        read_lines(line_txt)
        rk=0  
      
    if key == 256:      
      rk=1
      eval("G1:=AFiles(\""+LP+"\")") 
      strblit(0,0,0,320,240,1)                               
                  
    if key == 4 and line_txt>1:
      line_txt-=2      
      eval("WAIT(0.2)")
      read_lines(line_txt)
      
    if key == 4096:
      if atto==0:
        atto=1
        eval("WAIT(0.2)")
        textout(0,15,225,"AUTO",0)
        tko=ticks() 
    
    if key==8:     
      choose_w("菜单:",["流程图","对话","自动","状态","返回","退出"])
      if c_number==5:        
        seta="out"
        eval("MSGBOX(\""+"行号:"+str(line_txt)+"但未保存"+"\")") 
        break
      if c_number==0:      
        flow_chat(fc_xy,fc_text,fc_txt,fc_line,fc_pic,fc_drow)
      elif c_number==1:
        btext_blit()
      elif c_number==2:
        ATTO()  
      elif c_number==3:
        info_gm()
      if seta=="out":
        break
      GT2=1
      read_lines(line_txt)
      if atto==1:
        textout(0,15,225,"AUTO",0)
        tko=ticks()
      
#RUN game
while True:
  if seta=="out":
    break 
  START()
  if seta!="out":       
    textout(1,20,190,"Loading!",0)   
    textout(1,20,210,"Loading Now!",0)   
    strblit(0,0,0,320,240,1)         
    blitP(LP,Plist)    
    read_text(read_txt)      
  if seta!="out": 
    sblit=0          
    read_lines(line_txt)
    for i in range(16):
      t=ticks()
      eval("BLIT_P(G0,0,0,320,240,G1,0,0,320,240,1,"+str(i**2-1)+");")
      wait(((1000/30)-ticks()+t)/1000)
    RUN()
                    
#KMGEc1.9
#KMAT 2025
#2025.10.25