comparison gamelib/state.py @ 263:3b4a78422201

Shuffled a bunch of stuff into more appropriate places.
author Jeremy Thurgood <firxen@gmail.com>
date Fri, 27 Aug 2010 19:29:37 +0200
parents dfc89bc64fdb
children 3cedc4f95925
comparison
equal deleted inserted replaced
262:5f58da9eeb52 263:3b4a78422201
3 from albow.resource import get_image 3 from albow.resource import get_image
4 from albow.utils import frame_rect 4 from albow.utils import frame_rect
5 from widgets import BoomLabel 5 from widgets import BoomLabel
6 from pygame.locals import BLEND_ADD 6 from pygame.locals import BLEND_ADD
7 from pygame.rect import Rect 7 from pygame.rect import Rect
8 from pygame.surface import Surface
9 from pygame.color import Color 8 from pygame.color import Color
10 9
11 import constants 10 import constants
12 from sound import get_sound 11 from sound import get_sound
13 from cursor import HAND 12 from cursor import HAND
343 342
344 def get_detail_size(self): 343 def get_detail_size(self):
345 return self._background.get_size() 344 return self._background.get_size()
346 345
347 346
348 class Interact(object):
349
350 def __init__(self, image, rect, interact_rect):
351 self.image = image
352 self.rect = rect
353 self.interact_rect = interact_rect
354
355 def set_thing(self, thing):
356 pass
357
358 def draw(self, surface):
359 if self.image is not None:
360 surface.blit(self.image, self.rect, None)
361
362 def animate(self):
363 return False
364
365
366 class InteractNoImage(Interact):
367
368 def __init__(self, x, y, w, h):
369 super(InteractNoImage, self).__init__(None, None, Rect(x, y, w, h))
370
371
372 class InteractText(Interact):
373 """Display box with text to interact with -- mostly for debugging."""
374
375 def __init__(self, x, y, text, bg_color=None):
376 if bg_color is None:
377 bg_color = (127, 127, 127)
378 label = BoomLabel(text)
379 label.set_margin(5)
380 label.border_width = 1
381 label.border_color = (0, 0, 0)
382 label.bg_color = bg_color
383 label.fg_color = (0, 0, 0)
384 image = Surface(label.size)
385 rect = Rect((x, y), label.size)
386 label.draw_all(image)
387 super(InteractText, self).__init__(image, rect, rect)
388
389
390 class InteractRectUnion(Interact):
391
392 def __init__(self, rect_list):
393 # pygame.rect.Rect.unionall should do this, but is broken
394 # in some pygame versions (including 1.8, it appears)
395 rect_list = [Rect(x) for x in rect_list]
396 union_rect = rect_list[0]
397 for rect in rect_list[1:]:
398 union_rect = union_rect.union(rect)
399 super(InteractRectUnion, self).__init__(None, None, union_rect)
400 self.interact_rect = rect_list
401
402
403 class InteractImage(Interact):
404
405 def __init__(self, x, y, image_name):
406 super(InteractImage, self).__init__(None, None, None)
407 self._pos = (x, y)
408 self._image_name = image_name
409
410 def set_thing(self, thing):
411 self.image = get_image(thing.folder, self._image_name)
412 self.rect = Rect(self._pos, self.image.get_size())
413 self.interact_rect = self.rect
414
415
416 class InteractAnimated(Interact):
417 """Interactive with an animation rather than an image"""
418
419 # FIXME: Assumes all images are the same size
420 # anim_seq - sequence of image names
421 # delay - number of frames to wait between changing images
422
423 def __init__(self, x, y, anim_seq, delay):
424 self._pos = (x, y)
425 self._anim_pos = 0
426 self._names = anim_seq
427 self._frame_count = 0
428 self._anim_seq = None
429 self._delay = delay
430
431 def set_thing(self, thing):
432 self._anim_seq = [get_image(thing.folder, x) for x in self._names]
433 self.image = self._anim_seq[0]
434 self.rect = Rect(self._pos, self.image.get_size())
435 self.interact_rect = self.rect
436
437 def animate(self):
438 if self._anim_seq:
439 self._frame_count += 1
440 if self._frame_count > self._delay:
441 self._frame_count = 0
442 self._anim_pos += 1
443 if self._anim_pos >= len(self._anim_seq):
444 self._anim_pos = 0
445 self.image = self._anim_seq[self._anim_pos]
446 # queue redraw
447 return True
448 return False
449
450
451 class Thing(StatefulGizmo): 347 class Thing(StatefulGizmo):
452 """Base class for things in a scene that you can interact with.""" 348 """Base class for things in a scene that you can interact with."""
453 349
454 # name of thing 350 # name of thing
455 NAME = None 351 NAME = None