Advertisement
here2share

# MultipleChoice.py

Nov 29th, 2014
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # MultipleChoice.py
  2.  
  3. import os,appuifw,e32,sensor,sys,graphics,codecs,sysinfo,time,re,dir_iter,key_codes,math,random,audio
  4.  
  5. EXT_DIR='e:\\data\\python\\'
  6. sys.path.append(EXT_DIR)
  7.  
  8. img=None
  9.  
  10. def exit_key_handler():
  11.     global main_loop
  12.     main_loop=0
  13. appuifw.app.exit_key_handler=exit_key_handler
  14.  
  15. def handle_redraw(rect):
  16.     if img: ### Skips only at first pass as 'None'
  17.         canvas.blit(img)
  18. def gfx_on(mode): # actually took me a long while to figure out how to come up with this
  19.     global canvas,txt
  20.     if mode == 1:
  21.         appuifw.app.body=canvas
  22.     else:
  23.         txt=appuifw.Text()
  24.         appuifw.app.body=txt
  25.  
  26. def pad():
  27.     txt.font=("dense",4)
  28.     txt.add(u'\n\n')
  29.     txt.font=("dense",24)
  30.  
  31. appuifw.app.directional_pad=False
  32. appuifw.app.orientation='portrait'
  33. appuifw.app.screen='large'
  34. txt=appuifw.Text()
  35. appuifw.app.body=txt
  36. canvas=appuifw.Canvas(redraw_callback=handle_redraw)
  37. appuifw.app.body=canvas
  38. img=graphics.Image.new(canvas.size)
  39. timer=e32.Ao_timer()
  40. gfx_on(0)
  41.  
  42. txt.clear()
  43. txt.style=(appuifw.STYLE_BOLD)
  44. COLOR=txt.color
  45.  
  46. intro=u'\n\nThis is a Multiple Choice application which is still very much in the works that I decided to write as a mean to study in hopes of becoming, in a few years from now, an expert mobile developer.\n\n -- Kirk Lawrence'
  47.  
  48. txt.font=("dense",32)
  49. txt.add(intro)
  50. capture = graphics.screenshot()
  51. img.blit(capture, target=(0,0))
  52. gfx_on(1)
  53. canvas.blit(img)
  54.  
  55. COLOR_BLACK=(0,0,0)
  56. COLOR_GRAY=(128,128,128)
  57. COLOR_WHITE=(255,255,255)
  58. COLOR_GREEN=(0,204,0)
  59. COLOR_YELLOW=(255,255,0)
  60. COLOR_RED=(255,0,0)
  61. COLOR_ORANGE=(255,165,0)
  62. COLOR_BLOODRED=(102,0,0)
  63.  
  64. def z_tdown(pos):
  65.     global xm,ym
  66.     xm,ym=pos[0],pos[1]
  67.  
  68. def z_notouch(pos):
  69.     pass
  70.  
  71. canvas.bind(key_codes.EButton1Up,z_notouch)
  72. canvas.bind(key_codes.EButton1Down,z_tdown)
  73.  
  74. def opt_1():
  75.     global opt
  76.     opt=0
  77. def opt_2():
  78.     global opt
  79.     opt=1
  80. def opt_3():
  81.     global opt
  82.     opt=2
  83. def opt_4():
  84.     global opt
  85.     opt=3
  86. def opt_5():
  87.     global opt
  88.     opt=4
  89.  
  90. opt= -1
  91.  
  92. flashcard=0
  93. init_loop=0
  94. main_loop=1
  95.  
  96. def open_x():
  97.     global init_loop
  98.     init_loop=1
  99.  
  100. options5=[(u"A",opt_1),(u"B",opt_2),(u"C",opt_3),(u"D",opt_4),(u"E",opt_5)]
  101. appuifw.app.menu=[(u"Select A File",open_x)]
  102. mainmenu=appuifw.app.menu
  103.  
  104. '''
  105. appuifw.note(u"Cancelled",'info')
  106. appuifw.app.menu=mainmenu
  107. exit=True
  108. txt.clear()
  109. '''
  110.  
  111. while main_loop:
  112.     if init_loop == 1:
  113.         init_loop = 0
  114.         score=0
  115.         opt= -1
  116.         filenames=[]
  117.         for filename in os.listdir(EXT_DIR):
  118.             if filename.endswith('_multi.txt'):
  119.                 filename=filename.decode('utf-8')
  120.                 filename=filename.replace(u'_multi', '')
  121.                 filename=filename.replace(u'.txt', '')
  122.                 filename=filename.replace(u'_', ' ')
  123.                 filenames.append(filename)
  124.         filenames.reverse()
  125.         index=appuifw.popup_menu(filenames,u'Select Quiz')
  126.         YorN=False
  127.         try:
  128.             YorN=appuifw.query(u'Open '+filenames[index]+'?','query')
  129.         except:
  130.             appuifw.note(u"Cancelled",'info')
  131.         if YorN == True:
  132.             selected=filenames[index]
  133.             f=open(unicode(EXT_DIR+selected+'_multi.txt'),'r+')
  134.             appuifw.app.title=unicode(selected)
  135.             lines=f.read()
  136.             f.close()
  137.             try:
  138.                 lines=unicode(lines)
  139.                 flashcard=1
  140.             except:
  141.                 appuifw.note(u"Data Is Incompatible","error")
  142.             QA=[]
  143.             QuesAns=[]
  144.             k=1
  145.             for line in lines.split('\n'):
  146.                 if line == '':
  147.                     k=0
  148.                 else:
  149.                     QA.append(line)
  150.                     if k > 5:
  151.                         QuesAns.append(QA)
  152.                         QA=[]
  153.                         k=0            
  154.                 k+=1
  155.             qCount=str(len(QuesAns))
  156.             random.shuffle(QuesAns)
  157.             cc=0
  158.  
  159.     if flashcard:
  160.         opt = -1
  161.         if cc > int(qCount)-1:
  162.             gfx_on(0)
  163.             txt.clear()
  164.             appuifw.app.menu=mainmenu
  165.             msg=u'You Scored %s Out Of %s' % (score, qCount)
  166.             appuifw.note(msg,'info')
  167.             txt.font=("dense",32)
  168.             txt.add(msg)
  169.             b=time.time()
  170.             msg2=u'the elapsed time is: %d' %(b-a)
  171.             appuifw.note(msg2,'info')
  172.             capture = graphics.screenshot()
  173.             img.blit(capture, target=(0,0))
  174.             gfx_on(1)
  175.             canvas.blit(img)
  176.         else:
  177.             k=QuesAns[cc]
  178.             gfx_on(0)
  179.             txt.clear()
  180.             txt.font=("dense",32)
  181.             txt.add(u"Question "+str(cc+1)+" out of "+qCount)
  182.             pad()
  183.             corr=k[1] # The Correct Answer
  184.             txt.add(k[0]) # Questions
  185.             k.pop(0)
  186.             a=time.time()
  187.             ### random.shuffle(k) # not needed anymore
  188.             k=sorted(k)
  189.             txt.color=COLOR_ORANGE
  190.             for line in k: # Choices
  191.                 pad()
  192.                 pad()
  193.                 txt.add(unicode(line))
  194.             txt.color=COLOR
  195.             capture = graphics.screenshot()
  196.             img.blit(capture, target=(0,0))
  197.             gfx_on(1)
  198.             canvas.blit(img)
  199.             appuifw.app.menu=options5
  200.         flashcard=0
  201.         cc+=1
  202.     if opt!= -1:
  203.         if k[opt] == corr:
  204.             score+=1
  205.             appuifw.note(u"That Is Correct","conf")
  206.         else:
  207.             appuifw.note(u"Sorry... Wrong Answer","error")
  208.         flashcard=1
  209.  
  210.     e32.ao_yield()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement