Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # MultipleChoice.py
- import os,appuifw,e32,sensor,sys,graphics,codecs,sysinfo,time,re,dir_iter,key_codes,math,random,audio
- EXT_DIR='e:\\data\\python\\'
- sys.path.append(EXT_DIR)
- img=None
- def exit_key_handler():
- global main_loop
- main_loop=0
- appuifw.app.exit_key_handler=exit_key_handler
- def handle_redraw(rect):
- if img: ### Skips only at first pass as 'None'
- canvas.blit(img)
- def gfx_on(mode): # actually took me a long while to figure out how to come up with this
- global canvas,txt
- if mode == 1:
- appuifw.app.body=canvas
- else:
- txt=appuifw.Text()
- appuifw.app.body=txt
- def pad():
- txt.font=("dense",4)
- txt.add(u'\n\n')
- txt.font=("dense",24)
- appuifw.app.directional_pad=False
- appuifw.app.orientation='portrait'
- appuifw.app.screen='large'
- txt=appuifw.Text()
- appuifw.app.body=txt
- canvas=appuifw.Canvas(redraw_callback=handle_redraw)
- appuifw.app.body=canvas
- img=graphics.Image.new(canvas.size)
- timer=e32.Ao_timer()
- gfx_on(0)
- txt.clear()
- txt.style=(appuifw.STYLE_BOLD)
- COLOR=txt.color
- 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'
- txt.font=("dense",32)
- txt.add(intro)
- capture = graphics.screenshot()
- img.blit(capture, target=(0,0))
- gfx_on(1)
- canvas.blit(img)
- COLOR_BLACK=(0,0,0)
- COLOR_GRAY=(128,128,128)
- COLOR_WHITE=(255,255,255)
- COLOR_GREEN=(0,204,0)
- COLOR_YELLOW=(255,255,0)
- COLOR_RED=(255,0,0)
- COLOR_ORANGE=(255,165,0)
- COLOR_BLOODRED=(102,0,0)
- def z_tdown(pos):
- global xm,ym
- xm,ym=pos[0],pos[1]
- def z_notouch(pos):
- pass
- canvas.bind(key_codes.EButton1Up,z_notouch)
- canvas.bind(key_codes.EButton1Down,z_tdown)
- def opt_1():
- global opt
- opt=0
- def opt_2():
- global opt
- opt=1
- def opt_3():
- global opt
- opt=2
- def opt_4():
- global opt
- opt=3
- def opt_5():
- global opt
- opt=4
- opt= -1
- flashcard=0
- init_loop=0
- main_loop=1
- def open_x():
- global init_loop
- init_loop=1
- options5=[(u"A",opt_1),(u"B",opt_2),(u"C",opt_3),(u"D",opt_4),(u"E",opt_5)]
- appuifw.app.menu=[(u"Select A File",open_x)]
- mainmenu=appuifw.app.menu
- '''
- appuifw.note(u"Cancelled",'info')
- appuifw.app.menu=mainmenu
- exit=True
- txt.clear()
- '''
- while main_loop:
- if init_loop == 1:
- init_loop = 0
- score=0
- opt= -1
- filenames=[]
- for filename in os.listdir(EXT_DIR):
- if filename.endswith('_multi.txt'):
- filename=filename.decode('utf-8')
- filename=filename.replace(u'_multi', '')
- filename=filename.replace(u'.txt', '')
- filename=filename.replace(u'_', ' ')
- filenames.append(filename)
- filenames.reverse()
- index=appuifw.popup_menu(filenames,u'Select Quiz')
- YorN=False
- try:
- YorN=appuifw.query(u'Open '+filenames[index]+'?','query')
- except:
- appuifw.note(u"Cancelled",'info')
- if YorN == True:
- selected=filenames[index]
- f=open(unicode(EXT_DIR+selected+'_multi.txt'),'r+')
- appuifw.app.title=unicode(selected)
- lines=f.read()
- f.close()
- try:
- lines=unicode(lines)
- flashcard=1
- except:
- appuifw.note(u"Data Is Incompatible","error")
- QA=[]
- QuesAns=[]
- k=1
- for line in lines.split('\n'):
- if line == '':
- k=0
- else:
- QA.append(line)
- if k > 5:
- QuesAns.append(QA)
- QA=[]
- k=0
- k+=1
- qCount=str(len(QuesAns))
- random.shuffle(QuesAns)
- cc=0
- if flashcard:
- opt = -1
- if cc > int(qCount)-1:
- gfx_on(0)
- txt.clear()
- appuifw.app.menu=mainmenu
- msg=u'You Scored %s Out Of %s' % (score, qCount)
- appuifw.note(msg,'info')
- txt.font=("dense",32)
- txt.add(msg)
- b=time.time()
- msg2=u'the elapsed time is: %d' %(b-a)
- appuifw.note(msg2,'info')
- capture = graphics.screenshot()
- img.blit(capture, target=(0,0))
- gfx_on(1)
- canvas.blit(img)
- else:
- k=QuesAns[cc]
- gfx_on(0)
- txt.clear()
- txt.font=("dense",32)
- txt.add(u"Question "+str(cc+1)+" out of "+qCount)
- pad()
- corr=k[1] # The Correct Answer
- txt.add(k[0]) # Questions
- k.pop(0)
- a=time.time()
- ### random.shuffle(k) # not needed anymore
- k=sorted(k)
- txt.color=COLOR_ORANGE
- for line in k: # Choices
- pad()
- pad()
- txt.add(unicode(line))
- txt.color=COLOR
- capture = graphics.screenshot()
- img.blit(capture, target=(0,0))
- gfx_on(1)
- canvas.blit(img)
- appuifw.app.menu=options5
- flashcard=0
- cc+=1
- if opt!= -1:
- if k[opt] == corr:
- score+=1
- appuifw.note(u"That Is Correct","conf")
- else:
- appuifw.note(u"Sorry... Wrong Answer","error")
- flashcard=1
- e32.ao_yield()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement