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
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)
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]))
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))
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)
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
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)
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
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
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
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
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)
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)
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
def size(self, val): self._width = int(val[0]) self._height = int(val[1])
def midright(self, val): self._x = int(val[0]) - self._width self._y = int(val[1]) - int(self._height/2)
def midbottom(self, val): self._x = int(val[0]) - int(self._width/2) self._y = int(val[1]) - self._height
def midleft(self, val): self._x = int(val[0]) self._y = int(val[1]) - int(self._height/2)
def midtop(self, val): self._x = int(val[0]) - int(self._width/2) self._y = int(val[1])
def bottomleft(self, val): self._x = int(val[0]) self._y = int(val[1]) - self._height
def topleft(self, val): self._x = int(val[0]) self._y = int(val[1])
def right(self, val): self._x = int(val) - self._width
def left(self, val): self._x = int(val)
def top(self, val): self._y = int(val)
def centery(self, val): self._y = int(val) - int(self._height/2)
def w(self, val): self._width = int(val)
def h(self, val): self._height = int(val)
def topright(self, val): self._x = int(val[0]) - self._width self._y = int(val[1])
def bottom(self, val): self._y = int(val) - self._height
def bottomright(self, val): self._x = int(val[0]) - self._width self._y = int(val[1]) - self._height