Added chest interaction
[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
70 # function displaying the contents of a container. Object must contain
71 # items list
72 # edgewidth - container edges to avoid drawing items in
73 def get_container_object (surface, obj, bgimage, edgewidth=0):
74
75 # get the number of items
76 numitems = len (obj.objects)
77 # number of rows
78 num_rows = (bgimage.get_height () - edgewidth*2) / 48
79 # number of cols
80 num_cols = (bgimage.get_width () - edgewidth*2) / 48
81
82 objposx = surface.get_width()/2 - bgimage.get_width()/2
83 objposy = surface.get_height()/2 - bgimage.get_height()/2
84
85 selitem = 0
86 selrow = 0
87 selcol = 0
88
89 while 1:
90 # display the background for the container
91 surface.blit (bgimage, (objposx, objposy))
92
93 # display each item in the container
94 i = 0
95 j = 0
96
97 # display all the items in container
98 for item in obj.objects:
99 surface.blit (item.image, (objposx + edgewidth+ j*48, objposy + edgewidth + i*48))
100 j += 1
101 if j >= num_cols:
102 j = 0
103 i += 1
104 # only draw selector if there is at least one item
105 if numitems > 0:
106 pygame.draw.rect (surface, pygame.Color (255,255,255),
107 pygame.Rect(objposx + edgewidth + selcol*48, objposy + edgewidth + selrow*48, 48, 48), 1)
108
109 pygame.display.update ()
110
111 # get events
112 for event in pygame.event.get ():
113 if event.type == pygame.QUIT:
114 sys.exit (0)
115 elif event.type == pygame.KEYDOWN:
116 if event.key == pygame.K_ESCAPE:
117 return None
118 elif event.key == pygame.K_RETURN:
119 if numitems > 0 and selitem >= 0 and selitem < numitems:
120 return obj.objects[selitem]
121 else:
122 return None
123 elif event.key == pygame.K_UP or event.key == pygame.K_LEFT:
124 # go to the prev item
125 selitem -= 1
126 if selitem < 0:
127 selitem = numitems - 1
128 selrow = selitem / num_cols
129 selcol = selitem % num_cols
130
131 elif event.key == pygame.K_DOWN or event.key == pygame.K_RIGHT:
132 # go to the next item
133 selitem += 1
134 if selitem > numitems - 1:
135 selitem = 0
136 selrow = selitem / num_cols
137 selcol = selitem % num_cols