Пример #1
0
 def clamp_ip(self, rect):
     """
     Move this rect within rect.
     """
     if self._width < rect._width:
         if self._x < rect._x:
             x = rect._x
         elif self._x + self._width > rect._x + rect._width:
             x = rect._x + rect._width - self._width
         else:
             x = self._x
     else:
         x = rect._x - int((self._width - rect._width)/2)
     if self._height < rect._height:
         if self._y < rect._y:
             y = rect._y
         elif self._y + self._height > rect._y + rect._height:
             y = rect._y + rect._height - self._height
         else:
             y = self._y
     else:
         y = rect._y - int((self._height - rect._height)/2)
     self._x = x
     self._y = y
     return None
Пример #2
0
 def __setitem__(self, index, val):
     if index == 0:
         self._x = int(val)
     elif index == 1:
         self._y = int(val)
     elif index == 2:
         self._width = int(val)
     elif index == 3:
         self._height = int(val)
Пример #3
0
def _bitset_set(bitset, index, value):
    data = bitset._data._data
    if value:
        data[int(index /
                 bitset._bit)] = (data[int(index / bitset._bit)]
                                  | bitset._bitmask[index % bitset._bit])
    else:
        data[int(index /
                 bitset._bit)] = (data[int(index / bitset._bit)]
                                  & ~(bitset._bitmask[index % bitset._bit]))
Пример #4
0
 def __init__(self, size):
     """
     Return a Mask object.
     The size argument is (width, height) of the mask.
     The mask is represented by a list of Bitset.
     """
     self.width = int(size[0])
     self.height = int(size[1])
     self.bit = []
     for bitset in range(self.height):
         self.bit.append(BitSet(self.width))
Пример #5
0
 def inflate(self, *offset):
     """
     Return Rect at same position but size offset by x,y.
     """
     if len(offset) == 2:
         x = offset[0]
         y = offset[1]
     else:
         _offset = offset[0]
         x = _offset[0]
         y = _offset[1]
     return Rect(self._x - int(x/2), self._y - int(y/2),
                 self._width + x, self._height + y)
Пример #6
0
 def move_ip(self, *offset):
     """
     Moves this rect to position offset by x,y.
     """
     if len(offset) == 2:
         x = offset[0]
         y = offset[1]
     else:
         _offset = offset[0]
         x = _offset[0]
         y = _offset[1]
     self._x = self._x + int(x)
     self._y = self._y + int(y)
     return None
Пример #7
0
 def __call__(self, sprite1, sprite2):  #__call__ not implemented in pyjs
     if hasattr(sprite1, 'radius'):
         radius1 = sprite1.radius * self.ratio
     else:
         radius1 = (((((sprite1.rect.width)**2) +
                      ((sprite1.rect.height)**2))**0.5) * 0.5 * self.ratio)
     if hasattr(sprite2, 'radius'):
         radius2 = sprite2.radius * self.ratio
     else:
         radius2 = (((((sprite2.rect.width)**2) +
                      ((sprite2.rect.height)**2))**0.5) * 0.5 * self.ratio)
     sx1 = (sprite1.rect.x + int(sprite1.rect.width * 0.5))
     sy1 = (sprite1.rect.y + int(sprite1.rect.height * 0.5))
     sx2 = (sprite2.rect.x + int(sprite2.rect.width * 0.5))
     sy2 = (sprite2.rect.y + int(sprite2.rect.height * 0.5))
     return ((sx1 - sx2)**2 + (sy1 - sy2)**2) < (radius1**2 + radius2**2)
Пример #8
0
 def inflate_ip(self, *offset):
     """
     Change size of this rect offset by x,y.
     """
     if len(offset) == 2:
         x = offset[0]
         y = offset[1]
     else:
         _offset = offset[0]
         x = _offset[0]
         y = _offset[1]
     self._x = self._x - int(x/2)
     self._y = self._y - int(y/2)
     self._width = self._width + int(x)
     self._height = self._height + int(y)
     return None
Пример #9
0
def rotate(surface, angle):
    """
    Return Surface rotated by the given angle.
    """
    if not angle:
        return surface.copy()
    theta = angle * _deg_rad
    width_i = surface.width
    height_i = surface.height
    cos_theta = _fabs(_cos(theta))
    sin_theta = _fabs(_sin(theta))
    width_f = int((width_i * cos_theta) + (height_i * sin_theta))
    height_f = int((width_i * sin_theta) + (height_i * cos_theta))
    surf = Surface((width_f, height_f))
    surf.saveContext()
    surf.translate(width_f / 2.0, height_f / 2.0)
    surf.rotate(-theta)
    surf.drawImage(surface.canvas, -width_i / 2, -height_i / 2)
    surf.restoreContext()
    return surf
Пример #10
0
def rotozoom(surface, angle, size):
    """
    Return Surface rotated and resized by the given angle and size.
    """
    if not angle:
        width = int(surface.width * size)
        height = int(surface.height * size)
        return scale(surface, (width, height))
    theta = angle * _deg_rad
    width_i = int(surface.width * size)
    height_i = int(surface.height * size)
    cos_theta = _fabs(_cos(theta))
    sin_theta = _fabs(_sin(theta))
    width_f = int(_ceil((width_i * cos_theta) + (height_i * sin_theta)))
    if width_f % 2:
        width_f += 1
    height_f = int(_ceil((width_i * sin_theta) + (height_i * cos_theta)))
    if height_f % 2:
        height_f += 1
    surf = Surface((width_f, height_f))
    surf.saveContext()
    surf.translate(width_f / 2.0, height_f / 2.0)
    surf.rotate(-theta)
    surf.drawImage(surface.canvas, 0, 0, surface.width, surface.height,
                   -width_i / 2, -height_i / 2, width_i, height_i)
    surf.restoreContext()
    return surf
Пример #11
0
def _bitset_get(bitset, index, toIndex):
    data = bitset._data._data
    _bitset = _bitsetPool_get(toIndex - index)
    ix = 0
    if toIndex > bitset._width:
        toIndex = bitset._width
    for i in range(index, toIndex):
        _bitset_set(
            _bitset, ix,
            bool(data[int(i / bitset._bit)]
                 & bitset._bitmask[i % bitset._bit]))
        ix += 1
    return _bitset
Пример #12
0
def collide_circle(sprite1, sprite2):
    """
    **pyjsdl.sprite.collide_circle**
    
    Check two sprites intersect by checking by intersection of circle around their centers.
    Will use sprite radius attribute or circle will encompass rect attribute.
    Can be used as spritecollide callback function.
    """
    if hasattr(sprite1, 'radius'):
        radius1 = sprite1.radius
    else:
        radius1 = (((((sprite1.rect.width)**2) +
                     ((sprite1.rect.height)**2))**0.5) * 0.5)
    if hasattr(sprite2, 'radius'):
        radius2 = sprite2.radius
    else:
        radius2 = (((((sprite2.rect.width)**2) +
                     ((sprite2.rect.height)**2))**0.5) * 0.5)
    sx1 = (sprite1.rect.x + int(sprite1.rect.width * 0.5))
    sy1 = (sprite1.rect.y + int(sprite1.rect.height * 0.5))
    sx2 = (sprite2.rect.x + int(sprite2.rect.width * 0.5))
    sy2 = (sprite2.rect.y + int(sprite2.rect.height * 0.5))
    return (((sx1 - sx2)**2 + (sy1 - sy2)**2)) < (radius1**2 + radius2**2)
Пример #13
0
 def clamp(self, rect):
     """
     Return Rect of same dimension as this rect moved within rect.
     """
     if self._width < rect._width:
         if self._x < rect._x:
             x = rect._x
         elif self._x + self._width > rect._x + rect._width:
             x = rect._x + rect._width - self._width
         else:
             x = self._x
     else:
         x = rect._x - int((self._width - rect._width)/2)
     if self._height < rect._height:
         if self._y < rect._y:
             y = rect._y
         elif self._y + self._height > rect._y + rect._height:
             y = rect._y + rect._height - self._height
         else:
             y = self._y
     else:
         y = rect._y - int((self._height - rect._height)/2)
     return Rect(x, y, self._width, self._height)
Пример #14
0
 def __call__(self, sprite1, sprite2):  #__call__ not implemented in pyjs
     r = sprite1.rect
     x = (r.width * self.ratio) - r.width
     y = (r.height * self.ratio) - r.height
     rect1 = rectPool.get(r.x - int(x * 0.5), r.y - int(y * 0.5),
                          r.width + int(x), r.height + int(y))
     r = sprite2.rect
     x = (r.width * self.ratio) - r.width
     y = (r.height * self.ratio) - r.height
     rect2 = rectPool.get(r.x - int(x * 0.5), r.y - int(y * 0.5),
                          r.width + int(x), r.height + int(y))
     collide = (rect1._x < (rect2._x + rect2._width) and rect2._x <
                (rect1._x + rect1._width) and rect1._y <
                (rect2._y + rect2._height) and rect2._y <
                (rect1._y + rect1._height))
     rectPool.append(r1)
     rectPool.append(r2)
     return collide
Пример #15
0
 def size(self, val):
     self._width = int(val[0])
     self._height = int(val[1])
Пример #16
0
 def midright(self, val):
     self._x = int(val[0]) - self._width
     self._y = int(val[1]) - int(self._height/2)
Пример #17
0
 def midbottom(self, val):
     self._x = int(val[0]) - int(self._width/2)
     self._y = int(val[1]) - self._height
Пример #18
0
 def midleft(self, val):
     self._x = int(val[0])
     self._y = int(val[1]) - int(self._height/2)
Пример #19
0
 def midtop(self, val):
     self._x = int(val[0]) - int(self._width/2)
     self._y = int(val[1])
Пример #20
0
 def bottomleft(self, val):
     self._x = int(val[0])
     self._y = int(val[1]) - self._height
Пример #21
0
 def topleft(self, val):
     self._x = int(val[0])
     self._y = int(val[1])
Пример #22
0
 def right(self, val):
     self._x = int(val) - self._width
Пример #23
0
 def left(self, val):
     self._x = int(val)
Пример #24
0
 def top(self, val):
     self._y = int(val)
Пример #25
0
 def centery(self, val):
     self._y = int(val) - int(self._height/2)
Пример #26
0
 def w(self, val):
     self._width = int(val)
Пример #27
0
 def h(self, val):
     self._height = int(val)
Пример #28
0
 def topright(self, val):
     self._x = int(val[0]) - self._width
     self._y = int(val[1])
Пример #29
0
 def bottom(self, val):
     self._y = int(val) - self._height
Пример #30
0
 def bottomright(self, val):
     self._x = int(val[0]) - self._width
     self._y = int(val[1]) - self._height