1. schlangen
  2. pygamedraw

Commits

Marcus von Appen  committed 9a8acea

Added erase() workaround for masks.

  • Participants
  • Parent commits 529cadd
  • Branches default

Comments (0)

Files changed (1)

File lib/utils.py

View file
 small helper functions for pygamedraw
 """
 import pygame2
+import pygame2.mask
+
 
 def get_slope(p1, p2):
     """
         return 0
     return float(y2-y1) / float(x2-x1)
 
+def erase (mask1, mask2, offset):
+    """
+    Erases the set pixels from mask2 on mask1 and returns the result
+    """
+    opx, opy = offset
+    mx, my = 0, 0
+    if opx > mask1.width or opy > mask1.height:
+        return
+    newmask = pygame2.mask.Mask (mask1.size)
+
+    maxw, maxh = mask2.width, mask2.height
+    if opx < 0:
+        mx = abs (opx)
+    if opy < 0:
+        my = abs (opy)
+
+    for x in xrange (mask1.width):
+        for y in xrange (mask1.height):
+            isset1 = mask1.get_at (x, y)
+            if (x < opx or y < opy) and isset1:
+                newmask.set_at (x, y)
+            elif (x >= maxw or y >= maxh) and isset1:
+                newmask.set_at (x, y)
+            elif (mx >= maxw or my >= maxh) and isset1:
+                newmask.set_at (x, y)
+            else:
+                # Own index for relative movement.
+                isset2 = mask2.get_at (mx, my)
+                if isset1 and not isset2:
+                    newmask.set_at (x, y)
+            my += 1
+        if opy < 0:
+            my = abs (opy)
+        else:
+            my = 0
+        mx += 1
+
+    return newmask
+
 def intersection(p1a, p1b, p2a, p2b):
     """
     p1a, p1b: two points defining the first line
                 inside = not inside
             elif inside:
                 mask.set_at((x, y))
-