X-Git-Url: https://harishankar.org/repos/?p=butaba-adventures.git;a=blobdiff_plain;f=utility.py;h=5ef54281db4cda7d772136da8e4d3f8b98310d14;hp=5d8c45d0e3e24bbbe6c95ffcc066c4212d7489dc;hb=9457edf26f8499b7fbeb9921fdd91a762bdfb8c4;hpb=7dbb8e9b6766b7c0c17433bea3c88a321036b837 diff --git a/utility.py b/utility.py index 5d8c45d..5ef5428 100644 --- a/utility.py +++ b/utility.py @@ -1,29 +1,69 @@ # utility functions for the game import pygame +import sys import os.path # function to draw text on surface def put_text (surface, x, y, size, (r,g,b), text): - harisfont = os.path.join ("font", "HarisComic-2.ttf") + harisfont = os.path.join ("font", "harisgamefont.ttf") textsurf = pygame.font.Font (harisfont, size).render (text, True, pygame.Color (r,g,b)) surface.blit (textsurf, (x, y)) +# function to draw several lines of text, centered horizontally and vertically on surface +def put_lines (surface, text_lines): + textsurfs = [] + height = 0 + harisfont = os.path.join ("font", "harisgamefont.ttf") + for size, r, g, b, text in text_lines: + s = pygame.font.Font (harisfont, size).render (text, True, pygame.Color (r,g,b)) + # add spacing + height = height + s.get_height()*1.5 + textsurfs.append (s) -# function to ask a question and return answer -def ask_question (surface, question, answers): - pygame.draw.rect (surface, pygame.Color (255, 255, 128), pygame.Rect (40, 40, 480-40, 480-40)) + scrwidth = surface.get_width () + scrheight = surface.get_height () + i = 0 + for s in textsurfs: + surface.blit (s, (scrwidth/2 - s.get_width()/2, scrheight/2 - height/2+ i* (s.get_height()*1.5))) + i += 1 - put_text (surface, 60, 60, 22, (0, 0, 192), question) +# function to ask a question and return answer +def ask_question (surface, question, answers, bgscreen): - i = 1 - for answer in answers: - put_text (surface, 60, i*20+60, 22, (0, 0, 128), "%d" % i) - put_text (surface, 80, i*20+60, 22, (0, 0, 0), answer) + sel_answer = 1 while 1: + textarray = [ [ 10, 128, 0, 0, question ] ] + + i = 1 + for answer in answers: + if sel_answer == i: + r, g, b = 0, 0, 216 + else: + r, g, b = 0, 0, 0 + textarray.append ( [10, r, g, b, answer] ) + + i += 1 + + surface.blit (bgscreen, (surface.get_width()/2 - bgscreen.get_width()/2, + surface.get_height()/2 - bgscreen.get_height()/2)) + put_lines (surface, textarray) + + pygame.display.update () + for event in pygame.event.get (): if event.type == pygame.QUIT: sys.exit (0) elif event.type == pygame.KEYDOWN: - print event.key + if event.key == pygame.K_UP: + sel_answer -= 1 + if sel_answer < 1: + sel_answer = 1 + elif event.key == pygame.K_DOWN: + sel_answer += 1 + if sel_answer > len(answers): + sel_answer = len(answers) + elif event.key == pygame.K_RETURN: + return sel_answer +