| 1 | # $Id: libgame.py 1.1 2004/12/30 03:56:21 jimb Exp $ |
|---|
| 2 | # Python game library. |
|---|
| 3 | # Author: Jim Brooks http://www.jimbrooks.org |
|---|
| 4 | # Date: 2004/08 |
|---|
| 5 | # License: GNU General Public License (GPL) |
|---|
| 6 | # Requires: Python 2.3, PyGame, SDL. |
|---|
| 7 | # ============================================================================== |
|---|
| 8 | |
|---|
| 9 | import pygame |
|---|
| 10 | from pygame.locals import * |
|---|
| 11 | |
|---|
| 12 | # Return true if one object has collided into another. |
|---|
| 13 | def Collided( obj1, obj2 ): |
|---|
| 14 | if obj1.valid and obj2.valid: |
|---|
| 15 | return obj1.rect.colliderect( obj2.rect ) |
|---|
| 16 | else: |
|---|
| 17 | return False |
|---|
| 18 | |
|---|
| 19 | # ------------------------------------------------------------------------------ |
|---|
| 20 | # Text messages using PyGame fonts. |
|---|
| 21 | # ------------------------------------------------------------------------------ |
|---|
| 22 | |
|---|
| 23 | # Every message is a tuple consisting of (text,x,y,rgb,center). |
|---|
| 24 | # Add() adds message(s) to the message list. |
|---|
| 25 | # Del() removes matching message(s) from the list. |
|---|
| 26 | # Render() will display all message(s) in the list. |
|---|
| 27 | |
|---|
| 28 | class Msg: |
|---|
| 29 | |
|---|
| 30 | def __init__( self, screen, font ): |
|---|
| 31 | self.messages = [] |
|---|
| 32 | self.screen = screen # PyGame screen object |
|---|
| 33 | self.font = font # PyGame font object |
|---|
| 34 | return |
|---|
| 35 | |
|---|
| 36 | def Add( self, msgTuples ): |
|---|
| 37 | for m in msgTuples: |
|---|
| 38 | self.messages.append( m ) |
|---|
| 39 | return |
|---|
| 40 | |
|---|
| 41 | def Del( self, msgTuples ): |
|---|
| 42 | # For every message tuple to delete. |
|---|
| 43 | for mdel in msgTuples: |
|---|
| 44 | # For every message in messages[], delete it if it matches. |
|---|
| 45 | tmpList = self.messages[:] |
|---|
| 46 | i = -1 |
|---|
| 47 | for m in tmpList: |
|---|
| 48 | i += 1 |
|---|
| 49 | if m == mdel: |
|---|
| 50 | del self.messages[i] |
|---|
| 51 | i -= 1 |
|---|
| 52 | return |
|---|
| 53 | |
|---|
| 54 | # Show a message immediately (won't be linked into list). |
|---|
| 55 | def Show( self, (text,x,y,rgb,center) ): |
|---|
| 56 | if pygame.font: |
|---|
| 57 | f = self.font.render( text, 1, rgb ) |
|---|
| 58 | r = f.get_rect() |
|---|
| 59 | if center: |
|---|
| 60 | r.centerx = x |
|---|
| 61 | r.centery = y |
|---|
| 62 | else: |
|---|
| 63 | r.left = x |
|---|
| 64 | r.top = y |
|---|
| 65 | self.screen.blit( f, r ) |
|---|
| 66 | return |
|---|
| 67 | |
|---|
| 68 | def Render( self ): |
|---|
| 69 | if pygame.font: |
|---|
| 70 | for msg in self.messages: |
|---|
| 71 | self.Show( msg ) |
|---|
| 72 | return |
|---|