[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Re: Collision bounds smaller than sprite image



Yeah, I'm on http://sourceforge.net/projects/smpltn (or something like
that), but I can't remember off-hand if that has my routine, so I'll
just repeat it here.


import random
import string
import sys
import os, os.path
import pygame
from pygame.locals import *

try:
    import Numeric as N
    import pygame.surfarray as surfarray
except ImportError:
    raise ImportError, "Numeric and Surfarray are required."

...some other defs here...

def loadStrip(filename, width):

    print "loadStrip: ",filename

    #strip_source, asdf = load_image(filename, -1)
    fullname = os.path.join('data', filename)
    try:
        strip_source = pygame.image.load(fullname)
    except pygame.error, message:
        print 'Cannot load image:', fullname
        raise SystemExit, message
    #image = image.convert()
    colorkey = strip_source.get_at((0,0))
    
    strip = surfarray.pixels3d(strip_source)

    print 'len(strip)',len(strip)
    celcount=len(strip)/width
    celstrip = [[]] * celcount    
    print 'celcount:',celcount
    for cels in range(0,celcount):
        print 'cels',cels
        print 'celrange: s: %s e: %s' % (cels*width,(cels+1)*width)
        temp = strip[cels*width:(cels+1)*width]
        celstrip[cels] = surfarray.make_surface(temp)
        celstrip[cels] = celstrip[cels].convert()
        celstrip[cels].set_colorkey(colorkey, RLEACCEL)
        
        
    return celstrip, celstrip[0].get_rect()

It's a bit ugly, but it works.  BTW, I tried creating the
collision_rect in my engine, and it worked like a charm.

On Fri, 31 Dec 2004 23:45:25 +0430, Lee Harr <missive@xxxxxxxxxxx> wrote:
> >Yeah, the more I think about it, the more I think that creating a
> >second rect and storing it as an attribute is the way to go.  All of
> >the graphical adjustments and movements will be performed with the
> >normal rect attribute, and the actual collision and bounds tests will
> >be done with the collision_rect.
> 
> 
> That is how I did it in pygsear:
> http://www.nongnu.org/pygsear/
> 
> (except I called it spriteobj.crect  :o)
> 
> 
> >  I guess it's not really that hard,
> >but I was really exhausted after writing a strip -> cel routine :D
> 
> Interesting ... I have been thinking about writing such a beastie
> for pygsear for the last few weeks... are you releasing your code?
> 
> _________________________________________________________________
> Express yourself instantly with MSN Messenger! Download today it's FREE!
> http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
> 
> 


-- 
Andrew Ulysses Baker
"failrate"