Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on numpy. #274

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions pgzero/screen.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import numpy as np
import pygame
import pygame.draw

Expand Down Expand Up @@ -113,6 +112,29 @@ def textbox(self, *args, **kwargs):
ptext.drawbox(*args, surf=self._surf, **kwargs)


def blit_gradient(start, stop, dest_surface):
"""Blit a gradient into a destination surface.

The function does not return anything as the gradient is written
in-place in the destination surface.

Args:
start: The starting (top) color as a tuple (red, green, blue).
stop: The stopping (bottom) color as a tuple (red, green, blue).
dest_surface: A pygame.Surface to write the gradient into.
Returns:
None."""
surface_compact = pygame.Surface((2, 2))
pixelarray = pygame.PixelArray(surface_compact)
pixelarray[0][0] = start
pixelarray[0][1] = stop
pixelarray[1][0] = start
pixelarray[1][1] = stop
pygame.transform.smoothscale(surface_compact,
pygame.PixelArray(dest_surface).shape,
dest_surface=dest_surface)


class Screen:
"""Interface to the screen."""
def _set_surface(self, surface):
Expand All @@ -132,13 +154,7 @@ def fill(self, color, gcolor=None):
if gcolor:
start = make_color(color)
stop = make_color(gcolor)
pixs = pygame.surfarray.pixels3d(self.surface)
h = self.height

gradient = np.dstack(
[np.linspace(a, b, h) for a, b in zip(start, stop)][:3]
)
pixs[...] = gradient
blit_gradient(start, stop, self.surface)
else:
self.surface.fill(make_color(color))

Expand Down