/
actors.py
644 lines (553 loc) · 26.2 KB
/
actors.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Authors:
# Félix del Rio Benigno <fario@emergya.es>
# Metin Akdere <makdere@emergya.es>
#
# Copyright 2008 Emergya, sca. (www.emergya.es)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
'''
This module will model the actors for pygame and ode and will track their
updates.
An actor for our game is anything with representation on the screen. Can also
have ode representation.
These actors will be mainly, the penguin bars and the ball, but it might include
other actors like effects providers, public, etc...
Actors mainly consist of pygame.sprite and pyode classes:
- Sprites which represent drawings in the screen supplies image and location attributes. Each sprite has an update() method that controls the sprites behavior
- Ode class, the other part of the actors, determines dynamic actions, like collision detection of the objects, kicking the ball, simulating
sliding and turning of the game bars, positioning objects by adding forces to their body attributes etc...
'''
import pygame
import ode
import math
from random import random
from time import gmtime
import globals
import world
'''
Initializing pygame sound system to play sounds in the game. To play multiple sounds,first load sound files from source and then
assign each sound to separate channels in range of mixer.get_num_channels()
'''
if globals.sound:
try:
pygame.mixer.init()
except:
globals.sound = 0
if globals.sound:
num_of_channels = pygame.mixer.get_num_channels()
ambience_channel= pygame.mixer.Channel(1)
goal_channel = pygame.mixer.Channel(2)
kick_off_channel = pygame.mixer.Channel(3)
bounce_channel = pygame.mixer.Channel(4)
# loading sounds
ambience_sound = pygame.mixer.Sound('resources/sounds/ambience/ambience1.ogg')
goal_sound = pygame.mixer.Sound('resources/sounds/goalll/goal.ogg')
kick_off_sound = pygame.mixer.Sound('resources/sounds/kick-off/kickoff3.ogg')
bounce_sound = pygame.mixer.Sound('resources/sounds/ball-kick/kick1.ogg')
class Actor(pygame.sprite.Sprite):
'''An actor is something with representation on the game, can be ode world
representation, screen representation or both. This class is going to be a base class for all other actor classes '''
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.body = None
self.geom = None
self.x, self.y = (0, 0)
self.controller = None
def update(self, args):
'''In the update method is where this actor controller will perform
affect the simulation'''
if self.controller:
self.controller.control(self, args)
class Ball(Actor):
'''Represents the ball in the game. '''
def __init__(self, pos=(0, 0), radious=10, density=800):
'''@pos (x, y) is the pixel position in the screen
@radious of the ball for collision detection, in pixels
@density of the sphere, in kg/m^3'''
Actor.__init__(self)
# set up the sprite
self.image = pygame.Surface((radious * 2, radious * 2),
pygame.SRCALPHA | pygame.HWSURFACE, 32).convert_alpha()
self.rect = self.image.get_rect(center=pos)
image = pygame.image.load('resources/images/ball.png').convert_alpha()
image = pygame.transform.scale(image, self.rect.size)
self.image.blit(image, (0, 0))
# some internal values
self.x, self.y = pos
self.r = radious
# dynamic properties set
self.body = ode.Body(globals.ode_world)
self.mass = ode.Mass()
self.mass.setSphere(density, world.pix_to_dist(radious))
self.body.setMass(self.mass)
# object spatial setup
self.geom = ode.GeomSphere(globals.ode_space, world.pix_to_dist(radious))
self.geom.setBody(self.body)
# custom attributes
self.geom.mu = 0.0
self.geom.bounce = 0.5
# bind it to the 2d plane
self.body.setPosition((0, 0, 0))
self.join = ode.Plane2DJoint(globals.ode_world)
self.join.attach(self.body, ode.environment)
# to simulate friction we set the plane2djoint as if it was a 2d motor
# always trying to reach a 0 velocity
self.join.setXParam(ode.paramVel, 0)
self.join.setXParam(ode.paramFMax, globals.FIELD_FRICTION)
self.join.setYParam(ode.paramVel, 0)
self.join.setYParam(ode.paramFMax, globals.FIELD_FRICTION)
def update(self, delta):
'''Along the game, a constant force being applied to the ball in order to pull it over the middle of the field so that it wont get stuck somewhere
in the game field. '''
x, y, z = self.body.getPosition()
self.rect.center = world.w_to_pix((x, y, z)) # update sprite position
goal = self.rect.collidelist(globals.goals) # check if it's scoring and then play sound effects, draw animation if so.
if goal >= 0:
if globals.sound:
goal_channel.play(goal_sound)
goal_channel.fadeout(3000)
globals.assets.add(GoalAnimation())
globals.score[goal] += 1
self.kill()
return
# position correction: the plane2d joint should keep z around 0 but
# it's inaccurate sometimes
self.body.setPosition((x, y, 0.0))
# we will simulate the curvature of the field applying a force
# proportional to the distance to the center of the field
force_factor = ((globals.DISPLAY_SIZE[0] / 2.0 - self.rect.centerx)
/ (globals.FIELD_SIZE[0] / 2.0))
self.body.addForce((force_factor * globals.FIELD_CONCAVE_FACTOR, 0, 0))
vel = self.body.getLinearVel()[:2]
pos = self.body.getPosition()
if (vel[0]**2 + vel[1]**2) < 0.0025 and abs(pos[0]) < 0.05:
self.kickoff()
#print 'vel ', str((vel[0]**2 + vel[1]**2))
#print 'pos ', str(pos[0])
def kickoff(self):
''' Inits ball position and kicking away with a random force in the middle; at the beginning of the game or after each scoring '''
if globals.sound:
kick_off_channel.play(kick_off_sound)
self.body.setPosition((0, 0, 0))
self.body.addForce((random() * 100 - 50,
random() * 1000 - 500,
0))
@staticmethod
def extra_ball():
b = Ball(radious=15)
globals.balls.add(b)
b.kickoff()
class PenguinBar(Actor):
'''Represents a penguin bar in the game.
It is: a number of penguins joined by a fixed joint to a hinged bar,
which is joined to a sliding bar. So with that, we will be able to simulate moving the bars forward-backward or kicking ball by turning them around hinge'''
PENGUIN_SIZE = 14 # in pixels
BAR_HEIGHT = 0.18 # in world units
bar_image = None # bar image
sprites = None # Team penguin sprites
sprites_k = None # Team penguin goal keepers sprites
num_frames = 0
sprite_size = 0
def __init__(self, pos=0, penguins=1, controller=None, team=0):
'''
@pos is the x position of this bar, 0 is the goal
@penguins is the number of penguins in this bar (1 for keeper, etc...)
@controller is a Controller instance that will control the bar
@team the team number
'''
# for now, testing purpose, the position is hard-coded
#pos = (globals.DISPLAY_SIZE[0] / 2, globals.DISPLAY_SIZE[1] / 2)
pos = (globals.FIELD_TOP_LEFT[0] + pos, globals.DISPLAY_SIZE[1] / 2 + 2)
Actor.__init__(self)
self.controller = controller
self.team = team
if team == 0: self.color = (0, 64, 0) # TODO config?
elif team == 1: self.color = (64, 0, 0)
else: self.color = (128, 128, 128)
# set up the sprite
self.base_image = pygame.Surface((64, globals.FIELD_SIZE[1]),
pygame.SRCALPHA
| pygame.HWSURFACE, 32).convert_alpha()
self.image = self.base_image.copy()
self.rect = self.image.get_rect(center=pos)
self.base_pos = self.rect.topleft
if not PenguinBar.bar_image:
PenguinBar.bar_image = pygame.image.load(
'resources/images/barra.png').convert_alpha()
if not PenguinBar.sprites:
PenguinBar.sprites = (pygame.image.load(
'resources/images/penguinR.png').convert_alpha(),
pygame.image.load(
'resources/images/penguinL.png').convert_alpha())
PenguinBar.sprites_k = (pygame.image.load(
'resources/images/keeperR.png').convert_alpha(),
pygame.image.load(
'resources/images/keeperL.png').convert_alpha())
r = PenguinBar.sprites[0].get_rect()
PenguinBar.sprite_size = r.h
PenguinBar.num_frames = r.w / r.h
# drawing optimization stuff
self.updated = False
# get a position for the bar
bar_pos = world.pix_to_w(pos)
bar_pos = (bar_pos[0], bar_pos[1], self.BAR_HEIGHT) # height over z axis
# init the sliding part
self.slider_bar = ode.Body(globals.ode_world)
self.slider_bar.setPosition(bar_pos)
self.slider = ode.SliderJoint(globals.ode_world)
self.slider.attach(self.slider_bar, ode.environment)
self.slider.setAxis((0, 1, 0))
#self.slider.setParam(ode.ParamVel, 0.0) # add a motor to simulate friction
#self.slider.setParam(ode.ParamFMax, 1000.0)
# the hinged part
self.rot_bar = ode.Body(globals.ode_world)
self.rot_bar.setPosition(bar_pos)
self.hinge = ode.HingeJoint(globals.ode_world)
self.hinge.attach(self.rot_bar, self.slider_bar)
self.hinge.setAxis((0, 1, 0))
self.hinge.setAnchor(bar_pos)
#self.hinge.setParam(ode.ParamVel, 0.0) # add a motor to simulate friction
#self.hinge.setParam(ode.ParamFMax, 100.0)
self.rotating = 0
# penguins
self.num_penguins = penguins
self.bodies = [None] * penguins
penguin_separation = globals.FIELD_SIZE[1] / penguins
first_penguin = globals.FIELD_TOP_LEFT[1] + penguin_separation / 2.0
for i in xrange(penguins):
# set up the ode body with a mass and position
peng = ode.Body(globals.ode_world)
self.bodies[i] = peng
pgpos = world.pix_to_w((pos[0], first_penguin + penguin_separation * i))
peng.setPosition(pgpos)
# glue it to the hinged bar with a fixed joint
peng.fixjoint = ode.FixedJoint(globals.ode_world)
peng.fixjoint.attach(self.bodies[i], self.rot_bar)
peng.fixjoint.setFixed()
# penguins are the only part of the bar that will collide, so they
# need to have a geom representation. can be whatever, but for starters
# we will use a single sphere
peng.geom = ode.GeomSphere(globals.ode_space,
world.pix_to_dist(self.PENGUIN_SIZE))
peng.geom.setBody(self.bodies[i])
# set custom friction and bounce for the penguins
peng.geom.mu = 0
peng.geom.bounce = -0.5 # will not bounce the ball, so we can stop it
h = world.pix_to_dist(globals.FIELD_SIZE[1])
if self.num_penguins == 1: # keeper can only move inside the goal area
self.max_extent = world.pix_to_dist(globals.GOAL_SIZE[1] * 1.5)
else:
self.max_extent = (h / float(self.num_penguins)) / 2
def clear(self, i):
''' Clears the main image only to the actual visible area '''
i.fill((0, 0, 0, 0),
((0, self.rect.y - self.base_pos[1]), self.rect.size))
# pygame.draw.line(i, (128, 128, 128), (50, 0),
# (50, globals.FIELD_SIZE[1]), 5)
i.blit(PenguinBar.bar_image, ((self.rect.w / 2) - 11, 0))
def drawPenguins(self, i):
''' Drawing body attributes of the penguins on the bars;
but before that we are doing a position converting from ode world to pygame world '''
size = self.sprite_size
x_offset = self.base_pos[0]
y_offset = self.base_pos[1]
ntl = None
nbr = None
for b in self.bodies:
bpos = b.getPosition()
dpos = world.w_to_pix(bpos)
## new boundaries
if not ntl:
h = max((y_offset, dpos[1] - self.sprite_size / 2.0))
ntl = (x_offset, h)
nbr = (x_offset + self.sprite_size, dpos[1] + self.sprite_size / 2.0)
dpos = dpos[0] - x_offset, dpos[1] - y_offset
a = -self.hinge.getAngle()
index = int(round((PenguinBar.num_frames/2) / math.pi * a))
if index < 0: index += PenguinBar.num_frames
pos = ((self.rect.w - size) / 2, dpos[1] - size / 2)
if self.num_penguins == 1:
i.blit(PenguinBar.sprites_k[self.team], pos, (index * size, 0, size, size))
else:
i.blit(PenguinBar.sprites[self.team], pos, (index * size, 0, size, size))
# ==== DEBUG
if globals.debug:
dpos = world.w_to_pix((bpos[0], bpos[2], bpos[1]))
pos = (dpos[0] - self.rect.x, dpos[1] - size / 2)
pygame.draw.line(i, (0, 0, 0), (0, 46 + self.rect.h / 2),
(self.rect.w, 46 + self.rect.h / 2))
pygame.draw.circle(i, self.color, pos, self.PENGUIN_SIZE)
# ====
self.rect = pygame.Rect(ntl, (size, nbr[1] - ntl[1]))
def update(self, delta):
''' Handling bars behavior by drawing the bars and penguins over the
bars and then some debug stuff are being rendered over the game field '''
Actor.update(self, delta)
if not self.updated:
self.updated = True
i = self.base_image
self.clear(i)
self.drawPenguins(i)
full = 0
for b in globals.balls:
if abs(b.rect.centerx - self.rect.centerx) < (self.rect.w / 2):
full = 1
if full:
self.rect = self.base_image.get_rect(topleft=self.base_pos)
self.image = self.base_image.subsurface(((0, 0), self.rect.size))
else:
nr = pygame.Rect(self.rect)
nr.topleft = (0, self.rect.y - self.base_pos[1])
nr = nr.clip(self.base_image.get_rect())
#print self.base_image.get_rect(), " -- ", nr
self.image = self.base_image.subsurface(nr)
def rotate(self, proportion):
'''Sets the bar to rotate to the given angle (-1, 1)'''
# will rotate the bar.
if self.rotating:
self.updated = False
return
if self.team == 1:
proportion = -proportion
t = math.pi * proportion / 2
self.hinge.setParam(ode.ParamHiStop, t)
self.hinge.setParam(ode.ParamLoStop, t)
self.updated = False
def hard_turn(self, side):
'''Makes all penguins in the bar spin quickly (currently unused)'''
if side < 0:
side = -1
else:
side = 1
self.rotating = 30
self.hinge.setParam(ode.ParamHiStop, ode.Infinity)
self.hinge.setParam(ode.ParamLoStop, -ode.Infinity)
self.hinge.setParam(ode.ParamFMax, 25.0 * self.num_penguins)
self.hinge.addTorque(25.0 * self.num_penguins)
self.hinge.setParam(ode.ParamVel, 0.025 * side)
self.updated = False
def slide(self, amount):
'''Sets the bar to slide to the given extent (-1, 1)'''
#h = world.pix_to_dist(globals.FIELD_SIZE[1])
#extent = (h / 2.0) * amount
#if extent > self.max_extent:
# extent = self.max_extent
#if extent < -self.max_extent:
# extent = -self.max_extent
extent = amount * self.max_extent
if self.team == 1:
extent = -extent
self.slider.setParam(ode.ParamHiStop, extent + 0.01)
self.slider.setParam(ode.ParamLoStop, extent)
self.updated = False
class ScoreBoard(Actor):
''' Represents the scoreboard in the game. Scoreboard is a rect instance of pygame; that is drawn on the game field, positioned near midline.
Score is blitted over that rect. '''
def __init__(self, position=(0, 0)):
Actor.__init__(self)
#need to init font before using in the pygame
pygame.font.init()
self.font = pygame.font.Font(None, 36)
self.image = self.font.render('0 - 0', 1, (255, 255, 255))
# self.rect = self.image.get_rect(topleft=position)
self.rect = self.image.get_rect(midbottom = (globals.DISPLAY_SIZE[0]/2,
globals.FIELD_TOP_LEFT[1]))
#TODO : scoreboard rect should be placed according to field size parameters
self.score = tuple(globals.score)
self.position = position
def update(self, delta):
'''At first, determine whether score has changed recently; if so, draws last score result '''
Actor.update(self, delta)
sc = tuple(globals.score)
if sc != self.score:
self.image = self.font.render(str(sc[1]) + ' - ' + str(sc[0]), 1,
(255, 255, 255))
self.rect = self.image.get_rect(midbottom = (globals.DISPLAY_SIZE[0]/2,
globals.FIELD_TOP_LEFT[1]))
self.score = sc
class Timer(Actor):
'''Shows a timer accounting the remaining game time'''
def __init__(self, position=(0, 0)):
Actor.__init__(self)
self.position = position
self.rect = pygame.Rect(self.position, (0, 0))
self.ttl = 0
self.image = pygame.Surface((globals.font.length("77:77:77"),
globals.font.line_heigth)).convert_alpha()
self.hidden_img = pygame.Surface((0, 0))
self.hidden_rect = pygame.Rect(0, 0, 0, 0)
self.hiding = 0
def update(self, delta):
Actor.update(self, delta)
remains = globals.time_limit - pygame.time.get_ticks()
updating = 0
if ((remains%60000 > 55000) or (remains < 60000)):
updating = 1
if self.hiding:
self.image, self.hidden_img = self.hidden_img, self.image
self.rect, self.hidden_rect = self.hidden_rect, self.rect
self.hiding = 0
else:
if not self.hiding:
self.image, self.hidden_img = self.hidden_img, self.image
self.rect, self.hidden_rect = self.hidden_rect, self.rect
self.hiding = 1
if updating:
self.image.fill((0, 0, 0, 0))
st = gmtime(remains / 1000.0)
tiempo = "%(m)02d:%(s)02d:%(c)02d" % {'m': st[4],
"s": st[5],
"c": remains%999}
tiempo = tiempo[:-1]
globals.font.render(self.image, str(tiempo), (0, 0))
class GoalAnimation(Actor):
'''Animates a text image that grows from the center of the screen when a
team scores
@steps are the number of steps to animate the growth
@ttl is the time that the animation will last
@repeats is the number of times the image will reapear'''
image = pygame.image.load('resources/images/goal.png')
def __init__(self, steps=10, ttl=3000):
Actor.__init__(self)
self.image = GoalAnimation.image
self.rect = self.image.get_rect(center=(globals.DISPLAY_SIZE[0] / 2.0,
globals.DISPLAY_SIZE[1] / 2.0))
steps = max((1, steps))
ttl = max((500, ttl))
self.advance = 1.0 / steps
self.scale = 0.0
self.ttl = pygame.time.get_ticks() + ttl
def update(self, delta):
Actor.update(self, delta)
if self.scale <= 1:
self.image = pygame.transform.rotozoom(GoalAnimation.image, 0, self.scale)
self.rect = self.image.get_rect(center=(globals.DISPLAY_SIZE[0] / 2.0,
globals.DISPLAY_SIZE[1] / 2.0))
self.scale += self.advance
if pygame.time.get_ticks() > self.ttl - 300:
self.scale *= 0.5
if pygame.time.get_ticks() > self.ttl:
Ball.extra_ball()
self.kill()
class Public(Actor):
'''Sprite for penguins on the public. Implements public animation periodically.
@image Image file to use
@position Position on the screen
@rate ticks between animation update use to slow down the animation
@delay milliseconds between animation loops
@per_loops cycles per loops'''
def __init__(self, image, position, rate=1, delay=3000, per_loops=1):
Actor.__init__(self)
self.__image = pygame.image.load(image)
self.position = position
r = self.__image.get_rect()
self.size = r.h # w and h of each image
self.index = 0 # current index of the sprite
self.rate = rate # ticks to wait to update the state
self.wait_frame = rate # current wait until the next update
self.num_frames = r.w / r.h # number of frames of the sprite
self.loop_delay = delay # delay between sprite loops
self.loop_wait = 0 # current delay
self.cycles_per_loop = per_loops - 1 # each animation cycle will be these sprite cycles
self.cycle_wait = self.cycles_per_loop # current animation cycle
# Sprite class attributes
self.image = self.__image.subsurface((0, 0, self.size, self.size))
self.rect = self.image.get_rect(center = self.position)
def update(self, delta):
Actor.update(self, delta)
if pygame.time.get_ticks() > self.loop_wait: # wait for the next animation loop
if not self.wait_frame: # delay the animation if needed
self.image = self.__image.subsurface((self.index * self.size, 0,
self.size, self.size))
self.rect = self.image.get_rect(center = self.position)
self.index = self.index + 1
if self.index >= self.num_frames:
if not self.cycle_wait: # if we ended the animation loop, wait
self.loop_wait = pygame.time.get_ticks() + self.loop_delay
self.cycle_wait = self.cycles_per_loop
else:
self.cycle_wait -= 1
self.index = 0
self.wait_frame = self.rate
else:
self.wait_frame -= 1
class Font:
'''Works in a similar way to pygame.Font, but it blits surfaces to other
surfaces directly instead of generating a new surface'''
_charmap = None
def __init__(self,
chars=' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO'
+ 'PQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\¡\¿',
font = None,
size=24,
color=(255, 255, 255),
bg_color=(0, 0, 0, 0),
bold = False,
italic = False):
self._charmap = {}
pygame.font.init()
pgf = pygame.font.Font(font, size)
pgf.set_bold(bold)
pgf.set_italic(italic)
self.line_heigth = pgf.get_linesize()
for char in chars:
if bg_color:
self._charmap[char] = pgf.render(char, 1, color, bg_color)
self._charmap[char] = self._charmap[char].convert_alpha()
else:
self._charmap[char] = pgf.render(char, 1, color)
self._charmap[char] = self._charmap[char].convert_alpha()
def render(self, surface, text, pos, zoom=1):
'''Renders the text over the given surface
@surface - is the target surgface to draw on
@text - is the string to be drawn
@pos (x, y) - is the top left corner of the first character
@zoom float - is the relative size to apply to the font, zooming is
discouraged since it doesn't performs any antialias and
it's quite slow'''
f = self._charmap
p = list(pos)
for c in str(text):
rot = 0
if not f.has_key(c):
if c == '\n':
p[1] += self.line_heigth
p[0] = 0
continue
else:
c = '?'
if zoom == 1:
surface.blit(f[c], p)
else:
surface.blit(pygame.transform.rotozoom(f[c], rot, zoom), p)
p[0] += f[c].get_rect().w * zoom
p[1] += self.line_heigth
return [pygame.Rect(pos[0], pos[1], p[0] - pos[0], p[1] - pos[1])]
def length(self, text, zoom=1):
f = self._charmap
w = 0
mw = w
for c in text:
if f.has_key(c):
w += f[c].get_rect().w
elif c == '\n':
if w > mw:
mw = w
w = 0
if w > mw:
mw = w
return mw * zoom