Ejemplo n.º 1
0
def make_surface(array):
    """pygame.numpyarray.make_surface(array): return Surface

    copy an array to a new surface

    Create a new Surface that best resembles the data and format on the
    array. The array can be 2D or 3D with any sized integer values.
    """ 
    # Taken from from Alex Holkner's pygame-ctypes package. Thanks a
    # lot.
    bpp = 0
    r = g = b = 0
    shape = array.shape
    if len (shape) == 2:
        # 2D array
        bpp = 8
        r = 0xFF >> 6 << 5
        g = 0xFF >> 5 << 2
        b = 0xFF >> 6
    elif len (shape) == 3 and shape[2] == 3:
        bpp = 32
        r = 0xff << 16
        g = 0xff << 8
        b = 0xff
    else:
        raise ValueError("must be a valid 2d or 3d array")

    surface = pygame.Surface ((shape[0], shape[1]), 0, bpp, (r, g, b, 0))
    array_to_surface(surface, array)
    return surface
Ejemplo n.º 2
0
 def test_format_newbuf(self):
     Exporter = self.buftools.Exporter
     surface = self.surface
     shape = surface.get_size()
     w, h = shape
     for format in [
         "=i",
         "=I",
         "=l",
         "=L",
         "=q",
         "=Q",
         "<i",
         ">i",
         "!i",
         "1i",
         "=1i",
         "@q",
         "q",
         "4x",
         "8x",
     ]:
         surface.fill((255, 254, 253))
         exp = Exporter(shape, format=format)
         exp._buf[:] = [42] * exp.buflen
         array_to_surface(surface, exp)
         for x in range(w):
             for y in range(h):
                 self.assertEqual(surface.get_at((x, y)), (42, 42, 42, 255))
     # Some unsupported formats for array_to_surface and a 32 bit surface
     for format in ["f", "d", "?", "x", "1x", "2x", "3x", "5x", "6x", "7x", "9x"]:
         exp = Exporter(shape, format=format)
         self.assertRaises(ValueError, array_to_surface, surface, exp)
Ejemplo n.º 3
0
    def render(self):
        """
        Render the environment
        """
        # Clear the screen
        if self.background is not None:
            pixelcopy.array_to_surface(self.screen, self.background)
        else:
            self.screen.fill(self.universe.colour)

        # Draw particles
        for p in self.universe.particles:
            edge = np.maximum(p.colour, (200,200,200))
            self.draw_circle(int(p.x), int(p.y), p.size, p.colour, edgeColor=edge, filled=True)

        # Draw primart target
        #for t in self.universe.targets:
        t = self.primary.target
        self.draw_circle(int(t.x), int(t.y), t.radius, t.color, filled=False)
        self.draw_circle(int(t.x), int(t.y), int(t.radius/4), t.color, filled=True)

        # Draw the primary particle direction
        p = self.primary
        dx, dy = p.get_speed_vector()
        pygame.gfxdraw.line(self.screen, int(p.x), int(p.y), int(p.x+10*dx), int(p.y+10*dy), (0,0,0))
        self.flip_screen()
        time.sleep(0.01)
def make_surface(array):
    """pygame.numpyarray.make_surface(array): return Surface

    copy an array to a new surface

    Create a new Surface that best resembles the data and format on the
    array. The array can be 2D or 3D with any sized integer values.
    """
    # Taken from from Alex Holkner's pygame-ctypes package. Thanks a
    # lot.
    bpp = 0
    r = g = b = 0
    shape = array.shape
    if len(shape) == 2:
        # 2D array
        bpp = 8
        r = 0xFF >> 6 << 5
        g = 0xFF >> 5 << 2
        b = 0xFF >> 6
    elif len(shape) == 3 and shape[2] == 3:
        bpp = 32
        r = 0xff << 16
        g = 0xff << 8
        b = 0xff
    else:
        raise ValueError("must be a valid 2d or 3d array")

    surface = pygame.Surface((shape[0], shape[1]), 0, bpp, (r, g, b, 0))
    array_to_surface(surface, array)
    return surface
Ejemplo n.º 5
0
 def __init__(self, imageBuffer, size):
     width, height = self.get_size(size)
     self.size = size
     self.surface = Surface((width, height), depth=32)
     # Unpack the pixel colors
     pixels = self.unpack_colors(np.fromfile(imageBuffer, dtype=np.uint16))
     pixels = np.swapaxes(np.reshape(pixels, (-1, width)), 0, 1)
     pixelcopy.array_to_surface(self.surface, pixels)
Ejemplo n.º 6
0
    def render(self, mode=None, close=None, background=None):
        """
        Render the environment
        """
        # Clear the screen
        if background is not None:
            pixelcopy.array_to_surface(self.screen, background)
        else:
            pixelcopy.array_to_surface(self.screen, self.universe.map)

        # Draw particles
        for p in self.universe.particles:
            edge = np.maximum(p.color, (255, 255, 255))
            self.draw_circle(int(p.x),
                             int(p.y),
                             p.radius,
                             p.color,
                             edgeColor=edge,
                             filled=True)

        # Draw primary target
        for t in self.universe.targets[:1]:
            color = t.color
            color = (255, 255, 255)
            self.draw_circle(int(t.x), int(t.y), t.radius, color, filled=False)
            self.draw_circle(int(t.x),
                             int(t.y),
                             int(t.radius / 4),
                             color,
                             filled=True)

        # Draw the primary particle orientation
        for p in self.universe.particles:
            dx, dy = p.get_direction_vector(scale=1)
            pygame.gfxdraw.line(self.screen, int(p.x), int(p.y), int(p.x + dx),
                                int(p.y + dy), (0, 0, 0))

        # Draw the primary particle speed
        for p in self.universe.particles:
            dx, dy = p.get_speed_vector(scale=30)
            pygame.gfxdraw.line(self.screen, int(p.x), int(p.y), int(p.x + dx),
                                int(p.y + dy), (204, 204, 0))

        # Draw the control vector
        try:
            p = self.universe.particles[0]
            dx, dy = p.get_control_vector(scale=30)
            pygame.gfxdraw.line(self.screen, int(p.x), int(p.y), int(p.x + dx),
                                int(p.y + dy), (255, 0, 0))
        except AttributeError as e:
            print(e)

        self.flip_screen()
        time.sleep(0.01)
Ejemplo n.º 7
0
    def render(self, mode=None, close=None, background=None):
        """
        Render the environment
        """
        # Clear the screen
        if background is not None:
            pixelcopy.array_to_surface(self.screen, background)
        else:
            self.screen.fill(self.universe.color)

        # Draw penalties
        for xi in range(self.universe.discretization):
            for yi in range(self.universe.discretization):
                xmin,xmax,ymin,ymax = self.universe._get_grid_cell_bounds(xi,yi)
                rect = (xmin, ymin, xmax-xmin, ymax-ymin)
                color = ttl_color(self.universe.penalties[yi,xi])
                pygame.draw.rect(self.screen_buffer, color, rect)
        self.screen.blit(self.screen_buffer, (0,0))

        # Draw particles
        for p in self.universe.particles:
            edge = np.maximum(p.color, (255,255,255))
            self.draw_circle(int(p.x), int(p.y), p.radius, p.color, edgeColor=edge, filled=True)

        # Draw primary target
        t = self.primary.target
        self.draw_circle(int(t.x), int(t.y), t.radius, t.color, filled=False)
        self.draw_circle(int(t.x), int(t.y), int(t.radius/4), t.color, filled=True)

        # Draw the primary particle orientation
        for p in self.universe.particles:
            dx, dy = p.get_direction_vector(scale=1)
            pygame.gfxdraw.line(self.screen, int(p.x), int(p.y), int(p.x+dx), int(p.y+dy), (0,0,0))

        # Draw the primary particle speed
        for p in self.universe.particles:
            dx, dy = p.get_speed_vector(scale=30)
            pygame.gfxdraw.line(self.screen, int(p.x), int(p.y), int(p.x+dx), int(p.y+dy), (204,204,0))

        # Draw the control vector
        try:
            p = self.primary
            dx, dy = p.get_control_vector(scale=30)
            pygame.gfxdraw.line(self.screen, int(p.x), int(p.y), int(p.x+dx), int(p.y+dy), (255,0,0))
        except AttributeError:
            pass

        self.flip_screen()
        time.sleep(0.01)
Ejemplo n.º 8
0
 def __init__(self, imageBuffer, size):
     width, height = self.get_size(size)
     self.size = size
     self.surface = Surface((width, height), depth=8)
     # Read the header
     paletteLength, imageDataLength = self.read_ugar_header(imageBuffer)
     # Read the image palette and unpack
     palette = self.unpack_palette(
         np.fromstring(imageBuffer.read(paletteLength), dtype=np.uint16))
     self.surface.set_palette(palette)
     # Read the pixels
     pixels = np.fromstring(imageBuffer.read(imageDataLength),
                            dtype=np.uint8)
     pixels = np.swapaxes(np.reshape(pixels, (-1, width)), 0, 1)
     pixelcopy.array_to_surface(self.surface, pixels)
Ejemplo n.º 9
0
 def test_format_newbuf(self):
     Exporter = self.buftools.Exporter
     surface = self.surface
     shape = surface.get_size()
     w, h = shape
     for format in ['=i', '=I', '=l', '=L', '=q', '=Q', '<i', '>i',
                    '!i', '1i', '=1i', '@q', 'q', '4x', '8x']:
         surface.fill((255, 254, 253))
         exp = Exporter(shape, format=format)
         exp._buf[:] = [42] * exp.buflen
         array_to_surface(surface, exp)
         for x in range(w):
             for y in range(h):
                 self.assertEqual(surface.get_at((x, y)), (42, 42, 42, 255))
     # Some unsupported formats for array_to_surface and a 32 bit surface
     for format in ['f', 'd', '?', 'x',
                    '1x', '2x', '3x', '5x', '6x', '7x', '9x']:
         exp = Exporter(shape, format=format)
         self.assertRaises(ValueError, array_to_surface, surface, exp)
Ejemplo n.º 10
0
 def test_format_newbuf(self):
     Exporter = self.buftools.Exporter
     surface = self.surface
     shape = surface.get_size()
     w, h = shape
     for format in ['=i', '=I', '=l', '=L', '=q', '=Q', '<i', '>i',
                    '!i', '1i', '=1i', '@q', 'q', '4x', '8x']:
         surface.fill((255, 254, 253))
         exp = Exporter(shape, format=format)
         exp._buf[:] = [42] * exp.buflen
         array_to_surface(surface, exp)
         for x in range(w):
             for y in range(h):
                 self.assertEqual(surface.get_at((x, y)), (42, 42, 42, 255))
     # Some unsupported formats for array_to_surface and a 32 bit surface
     for format in ['f', 'd', '?', 'x',
                    '1x', '2x', '3x', '5x', '6x', '7x', '9x']:
         exp = Exporter(shape, format=format)
         self.assertRaises(ValueError, array_to_surface, surface, exp)
Ejemplo n.º 11
0
 def __init__(self, imageBuffer, size):
     width, height = self.get_size(size)
     self.size = size
     self.surface = Surface((width, height), depth=8)
     # Read the header
     paletteLength, imageDataLength = self.read_ugar_header(imageBuffer)
     # Read the image palette and unpack
     palette = self.unpack_palette(
         np.fromstring(imageBuffer.read(self.round_to_power(paletteLength)),
                       dtype=np.uint16))
     self.surface.set_palette(palette)
     # All pixels with the index of 0 are transparent
     self.surface.set_colorkey(0)
     # Read the pixel data bytes
     pixelData = np.fromstring(imageBuffer.read(imageDataLength),
                               dtype=np.uint8)
     # Split each byte into 2 pixels
     pixels = np.stack((np.bitwise_and(
         pixelData, 0x0f), np.bitwise_and(pixelData >> 4, 0x0f)),
                       axis=-1).flatten()
     pixels = np.swapaxes(np.reshape(pixels, (-1, width)), 0, 1)
     pixelcopy.array_to_surface(self.surface, pixels)
Ejemplo n.º 12
0
def blit_array (surface, array):
    """pygame.surfarray.blit_array(Surface, array): return None

    Blit directly from a array values.

    Directly copy values from an array into a Surface. This is faster than
    converting the array into a Surface and blitting. The array must be the
    same dimensions as the Surface and will completely replace all pixel
    values. Only integer, ascii character and record arrays are accepted.

    This function will temporarily lock the Surface as the new values are
    copied.
    """
    return array_to_surface(surface, array)
Ejemplo n.º 13
0
def blit_array (surface, array):
    """pygame.surfarray.blit_array(Surface, array): return None

    Blit directly from a array values.

    Directly copy values from an array into a Surface. This is faster than
    converting the array into a Surface and blitting. The array must be the
    same dimensions as the Surface and will completely replace all pixel
    values. Only integer, ascii character and record arrays are accepted.

    This function will temporarily lock the Surface as the new values are
    copied.
    """
    if isinstance(array, numpy_ndarray) and array.dtype in numpy_floats:
        array = numpy_rint(array, numpy_empty(array.shape, dtype=numpy_uint32))
    return array_to_surface(surface, array)
Ejemplo n.º 14
0
 def test_array_to_surface_newbuf(self):
     array = self.Array2D(range(0, 15))
     self.assertNotEqual(array.content[0],
                         self.surface.get_at_mapped((0, 0)))
     array_to_surface(self.surface, array)
     self.assertCopy2D(self.surface, array)
Ejemplo n.º 15
0
 def test_array_to_surface_newbuf(self):
     array = self.Array2D(range(0, 15))
     self.assertNotEqual(array.content[0],
                         self.surface.get_at_mapped((0, 0)))
     array_to_surface(self.surface, array)
     self.assertCopy2D(self.surface, array)