forked from sugarlabs/followme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
291 lines (243 loc) · 7.72 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
Follow Me activity for Sugar
Copyright (C) 2010 Peter Hewitt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import sys
import random
import logging
import pygame
import g
import load_save
# constants
RED, BLUE, GREEN = (255, 0, 0), (0, 0, 255), (0, 255, 0)
BLACK, WHITE = (0, 0, 0), (255, 255, 255)
CYAN, ORANGE, CREAM = (0, 255, 255), (255, 165, 0), (255, 255, 192)
YELLOW = (255, 255, 0)
def exit():
save()
pygame.display.quit()
pygame.quit()
sys.exit()
def save():
directory = ''
directory = os.environ.get('SUGAR_ACTIVITY_ROOT')
if directory is None:
directory = ''
fname = os.path.join(directory, 'data', 'follow.dat')
f = open(fname, 'w')
load_save.save(f)
f.close
def load():
directory = ''
directory = os.environ.get('SUGAR_ACTIVITY_ROOT')
if directory is None:
directory = ''
fname = os.path.join(directory, 'data', 'follow.dat')
try:
f = open(fname, 'r')
except Exception as e:
logging.error('Could not open %s: %s' % (fname, e))
return None
try:
load_save.load(f)
except Exception as e:
logging.error('load_save failed: %s' % (e))
f.close
def version_display():
g.message = g.app + ' ' + g.ver
g.message += ' ' + str(g.screen.get_width()) + ' x ' + \
str(g.screen.get_height())
message(g.screen, g.font1, g.message)
# loads an image (eg pic.png) from the data subdirectory
# converts it for optimum display
# resizes it using the image scaling factor, g.imgf
# so it is the right size for the current screen resolution
# all images are designed for 1200x900
def load_image(file1, alpha=False, subdir=''): # eg subdir = 'glow'
data = 'data'
if subdir != '':
data = os.path.join('data', subdir)
fname = os.path.join(data, file1)
try:
img = pygame.image.load(fname)
except IOError:
print("Peter says: Can't find " + fname)
exit()
if alpha:
img = img.convert_alpha()
else:
img = img.convert()
if abs(g.imgf - 1.0) > .1: # only scale if factor <> 1
w = img.get_width()
h = img.get_height()
try:
img = pygame.transform.smoothscale(
img, (int(g.imgf * w), int(g.imgf * h)))
except IndexError:
img = pygame.transform.scale(
img, (int(g.imgf * w), int(g.imgf * h)))
return img
# eg new_list = copy_list(old_list)
def copy_list(l):
new_list = []
new_list.extend(l)
return new_list
def shuffle(lst):
l1 = lst
lt = []
for i in range(len(lst)):
ln = len(l1)
r = random.randint(0, ln - 1)
lt.append(lst[r])
l1.remove(lst[r])
return lt
def centre_blit(screen, img, position, angle=0): # rotation is clockwise
(cx, cy) = position
img1 = img
if angle != 0:
img1 = pygame.transform.rotate(img, -angle)
rect = img1.get_rect()
screen.blit(img1, (cx - rect.width / 2, cy - rect.height / 2))
def text_blit(screen, s, font, position, color):
(cx, cy) = position
(r, g, b) = color
text = font.render(s, True, (0, 0, 0))
rect = text.get_rect()
rect.centerx = cx + 2
rect.centery = cy + 2
screen.blit(text, rect)
text = font.render(s, True, (r, g, b))
rect = text.get_rect()
rect.centerx = cx
rect.centery = cy
screen.blit(text, rect)
return rect
def text_blit1(screen, s, font, position, color):
(x, y) = position
(r, g, b) = color
text = font.render(s, True, (r, g, b))
rect = text.get_rect()
rect.x = x
rect.y = y
screen.blit(text, rect)
# m is the message
# d is the # of pixels in the border around the text
# (cx, cy) = coords centre - (0, 0) means use screen centre
def message(screen, font, m, position=(0, 0), d=20):
(cx, cy) = position
if m != '':
if pygame.font:
text = font.render(m, True, (255, 255, 255))
shadow = font.render(m, True, (0, 0, 0))
rect = text.get_rect()
if cx == 0:
cx = screen.get_width() / 2
if cy == 0:
cy = screen.get_height() / 2
rect.centerx = cx
rect.centery = cy
bgd = pygame.Surface((rect.width + 2 * d, rect.height + 2 * d))
bgd.fill((0, 255, 255))
bgd.set_alpha(128)
screen.blit(bgd, (rect.left - d, rect.top - d))
screen.blit(
shadow, (rect.x + 2, rect.y + 2, rect.width, rect.height))
screen.blit(text, rect)
def mouse_on_img(img, position): # x, y = top left
(x, y) = position
w = img.get_width()
h = img.get_height()
mx, my = g.pos
if mx < x:
return False
if mx > (x + w):
return False
if my < y:
return False
if my > (y + h):
return False
try: # in case out of range
col = img.get_at((int(mx - x), int(my - y)))
except IndexError:
return False
if col[3] < 10:
return False
return True
def mouse_on_img1(img, position):
(cx, cy) = position
xy = centre_to_top_left(img, (cx, cy))
return mouse_on_img(img, xy)
def mouse_on_img_rect(img, position):
(cx, cy) = position
w2 = img.get_width() / 2
h2 = img.get_height() / 2
x1 = cx - w2
y1 = cy - h2
x2 = cx + w2
y2 = cy + h2
return mouse_in(x1, y1, x2, y2)
def mouse_in(x1, y1, x2, y2):
mx, my = g.pos
if x1 > mx:
return False
if x2 < mx:
return False
if y1 > my:
return False
if y2 < my:
return False
return True
def mouse_in_rect(rect): # x, y, w, h
return mouse_in(rect[0], rect[1], rect[0] + rect[2], rect[1] + rect[3])
def display_score():
if pygame.font:
text = g.font2.render(str(g.score), True, ORANGE, BLUE)
w = text.get_width()
h = text.get_height()
x = g.sx(5.7)
y = g.sy(18.8)
d = g.sy(.3)
pygame.draw.rect(g.screen, BLUE,
(x - d - g.sy(.05), y - d, w + 2 * d, h + 2 * d))
g.screen.blit(text, (x, y))
centre_blit(g.screen, g.sparkle,
(x - d + g.sy(.05), y + h / 2 - g.sy(.2)))
def display_number(n, position, font, colour=BLACK, bgd=None,
outline_font=None):
(cx, cy) = position
if pygame.font:
if bgd is None:
text = font.render(str(n), True, colour)
else:
text = font.render(str(n), True, colour, bgd)
if outline_font is not None:
outline = outline_font.render(str(n), True, BLACK)
centre_blit(g.screen, outline, (cx, cy))
centre_blit(g.screen, text, (cx, cy))
def display_number1(n, position, font, colour=BLACK):
(x, cy) = position
if pygame.font:
text = font.render(str(n), True, colour)
y = cy - text.get_height() / 2
g.screen.blit(text, (x, y))
def top_left_to_centre(img, position):
(x, y) = position
cx = x + img.get_width() / 2
cy = y + img.get_height() / 2
return (cx, cy)
def centre_to_top_left(img, position):
(cx, cy) = position
x = cx - img.get_width() / 2
y = cy - img.get_height() / 2
return (x, y)