1 # utility functions for the game
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
))
12 # function to draw several lines of text, centered horizontally and vertically on surface
13 def put_lines (surface
, text_lines
):
16 harisfont
= os
.path
.join ("font", "harisgamefont.ttf")
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
))
21 height
= height
+ s
.get_height()*1.5
24 scrwidth
= surface
.get_width ()
25 scrheight
= surface
.get_height ()
28 surface
.blit (s
, (scrwidth
/2 - s
.get_width()/2, scrheight
/2 - height
/2+ i
* (s
.get_height()*1.5)))
31 # function to ask a question and return answer
32 def ask_question (surface
, question
, answers
, bgscreen
):
37 textarray
= [ [ 10, 128, 0, 0, question
] ]
40 for answer
in answers
:
45 textarray
.append ( [10, r
, g
, b
, answer
] )
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
)
53 pygame
.display
.update ()
55 for event
in pygame
.event
.get ():
56 if event
.type == pygame
.QUIT
:
58 elif event
.type == pygame
.KEYDOWN
:
59 if event
.key
== pygame
.K_UP
:
63 elif event
.key
== pygame
.K_DOWN
:
65 if sel_answer
> len(answers
):
66 sel_answer
= len(answers
)
67 elif event
.key
== pygame
.K_RETURN
:
70 # function displaying the contents of a container. Object must contain
72 # edgewidth - container edges to avoid drawing items in
73 def get_container_object (surface
, obj
, bgimage
, edgewidth
=0):
75 # get the number of items
76 numitems
= len (obj
.objects
)
78 num_rows
= (bgimage
.get_height () - edgewidth
*2) / 48
80 num_cols
= (bgimage
.get_width () - edgewidth
*2) / 48
82 objposx
= surface
.get_width()/2 - bgimage
.get_width()/2
83 objposy
= surface
.get_height()/2 - bgimage
.get_height()/2
90 # display the background for the container
91 surface
.blit (bgimage
, (objposx
, objposy
))
93 # display each item in the container
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))
104 # only draw selector if there is at least one item
106 pygame
.draw
.rect (surface
, pygame
.Color (255,255,255),
107 pygame
.Rect(objposx
+ edgewidth
+ selcol
*48, objposy
+ edgewidth
+ selrow
*48, 48, 48), 1)
109 pygame
.display
.update ()
112 for event
in pygame
.event
.get ():
113 if event
.type == pygame
.QUIT
:
115 elif event
.type == pygame
.KEYDOWN
:
116 if event
.key
== pygame
.K_ESCAPE
:
118 elif event
.key
== pygame
.K_RETURN
:
119 if numitems
> 0 and selitem
>= 0 and selitem
< numitems
:
120 return obj
.objects
[selitem
]
123 elif event
.key
== pygame
.K_UP
or event
.key
== pygame
.K_LEFT
:
124 # go to the prev item
127 selitem
= numitems
- 1
128 selrow
= selitem
/ num_cols
129 selcol
= selitem
% num_cols
131 elif event
.key
== pygame
.K_DOWN
or event
.key
== pygame
.K_RIGHT
:
132 # go to the next item
134 if selitem
> numitems
- 1:
136 selrow
= selitem
/ num_cols
137 selcol
= selitem
% num_cols