Much simplified the tile picker in level editor
[butaba-adventures.git] / utility.py
1 # utility functions for the game
2 import pygame
3 import sys
4 import os.path
5
6 # function to draw text on surface
7 def put_text (surface, x, y, size, (r,g,b), text):
8 harisfont = os.path.join ("font", "harisgamefont.ttf")
9 textsurf = pygame.font.Font (harisfont, size).render (text, True, pygame.Color (r,g,b))
10 surface.blit (textsurf, (x, y))
11
12 # function to draw several lines of text, centered horizontally and vertically on surface
13 def put_lines (surface, text_lines):
14 textsurfs = []
15 height = 0
16 harisfont = os.path.join ("font", "harisgamefont.ttf")
17
18 for size, r, g, b, text in text_lines:
19 s = pygame.font.Font (harisfont, size).render (text, True, pygame.Color (r,g,b))
20 # add spacing
21 height = height + s.get_height()*1.5
22 textsurfs.append (s)
23
24 scrwidth = surface.get_width ()
25 scrheight = surface.get_height ()
26 i = 0
27 for s in textsurfs:
28 surface.blit (s, (scrwidth/2 - s.get_width()/2, scrheight/2 - height/2+ i* (s.get_height()*1.5)))
29 i += 1
30
31 # function to ask a question and return answer
32 def ask_question (surface, question, answers, bgscreen):
33
34 sel_answer = 1
35
36 while 1:
37 textarray = [ [ 10, 128, 0, 0, question ] ]
38
39 i = 1
40 for answer in answers:
41 if sel_answer == i:
42 r, g, b = 0, 0, 216
43 else:
44 r, g, b = 0, 0, 0
45 textarray.append ( [10, r, g, b, answer] )
46
47 i += 1
48
49 surface.blit (bgscreen, (surface.get_width()/2 - bgscreen.get_width()/2,
50 surface.get_height()/2 - bgscreen.get_height()/2))
51 put_lines (surface, textarray)
52
53 pygame.display.update ()
54
55 for event in pygame.event.get ():
56 if event.type == pygame.QUIT:
57 sys.exit (0)
58 elif event.type == pygame.KEYDOWN:
59 if event.key == pygame.K_UP:
60 sel_answer -= 1
61 if sel_answer < 1:
62 sel_answer = 1
63 elif event.key == pygame.K_DOWN:
64 sel_answer += 1
65 if sel_answer > len(answers):
66 sel_answer = len(answers)
67 elif event.key == pygame.K_RETURN:
68 return sel_answer
69