Skip to content

Commit

Permalink
Merge pull request #217 from SteveClement/width
Browse files Browse the repository at this point in the history
chg: [screen] Added a width parameter
  • Loading branch information
lordmauve committed Jan 24, 2020
2 parents 2373964 + 257960b commit d42e3e4
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 16 deletions.
12 changes: 6 additions & 6 deletions doc/builtins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ draw images to the screen ("blit" them).
parameter. If ``image`` is a ``str`` then the named image will be
loaded from the ``images/`` directory.

.. method:: draw.line(start, end, (r, g, b))
.. method:: draw.line(start, end, (r, g, b), width=1)

Draw a line from start to end.
Draw a line from start to end with a certain line width.

.. method:: draw.circle(pos, radius, (r, g, b))
.. method:: draw.circle(pos, radius, (r, g, b), width=1)

Draw the outline of a circle.
Draw the outline of a circle with a certain line width.

.. method:: draw.filled_circle(pos, radius, (r, g, b))

Draw a filled circle.

.. method:: draw.rect(rect, (r, g, b))
.. method:: draw.rect(rect, (r, g, b), width=1)

Draw the outline of a rectangle.
Draw the outline of a rectangle with a certain line width.

Takes a :ref:`Rect <rect>`.

Expand Down
18 changes: 9 additions & 9 deletions pgzero/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def round_pos(pos):
try:
return round(x), round(y)
except TypeError:
raise TypeError("Coordinate values must be numbers (not {!r})".format(pos)) from None
raise TypeError("Coordinate values must be numbers (not {!r})".format(pos)) from None # noqa


def make_color(arg):
Expand All @@ -35,16 +35,16 @@ def __init__(self, screen):
def _surf(self):
return self._screen.surface

def line(self, start, end, color):
def line(self, start, end, color, width=1):
"""Draw a line from start to end."""
start = round_pos(start)
end = round_pos(end)
pygame.draw.line(self._surf, make_color(color), start, end, 1)
pygame.draw.line(self._surf, make_color(color), start, end, width)

def circle(self, pos, radius, color):
def circle(self, pos, radius, color, width=1):
"""Draw a circle."""
pos = round_pos(pos)
pygame.draw.circle(self._surf, make_color(color), pos, radius, 1)
pygame.draw.circle(self._surf, make_color(color), pos, radius, width)

def filled_circle(self, pos, radius, color):
"""Draw a filled circle."""
Expand All @@ -56,7 +56,7 @@ def polygon(self, points, color):
try:
iter(points)
except TypeError:
raise TypeError("screen.draw.filled_polygon() requires an iterable of points to draw") from None
raise TypeError("screen.draw.filled_polygon() requires an iterable of points to draw") from None # noqa
points = [round_pos(point) for point in points]
pygame.draw.polygon(self._surf, make_color(color), points, 1)

Expand All @@ -65,15 +65,15 @@ def filled_polygon(self, points, color):
try:
iter(points)
except TypeError:
raise TypeError("screen.draw.filled_polygon() requires an iterable of points to draw") from None
raise TypeError("screen.draw.filled_polygon() requires an iterable of points to draw") from None # noqa
points = [round_pos(point) for point in points]
pygame.draw.polygon(self._surf, make_color(color), points, 0)

def rect(self, rect, color):
def rect(self, rect, color, width=1):
"""Draw a rectangle."""
if not isinstance(rect, RECT_CLASSES):
raise TypeError("screen.draw.rect() requires a rect to draw")
pygame.draw.rect(self._surf, make_color(color), rect, 1)
pygame.draw.rect(self._surf, make_color(color), rect, width)

def filled_rect(self, rect, color):
"""Draw a filled rectangle."""
Expand Down
Binary file added test/images/expected_circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/images/expected_filled_circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/images/expected_filled_rect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/images/expected_line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/images/expected_rect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 69 additions & 1 deletion test/test_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,49 @@ def test_fill_gradient(self):
images.expected_gradient
)

def test_line(self):
yellow = (255, 255, 0)
"""We can draw a line."""
self.screen.draw.line(start=(0, 50), end=(100, 30), color=yellow, width=9)
self.assertImagesAlmostEqual(
self.screen.surface,
images.expected_line
)

def test_line_errors(self):
"""draw.line raises errors as expected."""
yellow = (255, 255, 0)
with self.assertRaises(TypeError):
self.screen.draw.line(2, yellow)
with self.assertRaises(TypeError):
self.screen.draw.line([2], yellow)

def test_circle(self):
yellow = (255, 255, 0)
"""We can draw a circle."""
self.screen.draw.circle(pos=(50, 50), radius=50, color=yellow, width=9)
self.assertImagesAlmostEqual(
self.screen.surface,
images.expected_circle
)

def test_filled_circle(self):
yellow = (255, 255, 0)
"""We can draw a filled circle."""
self.screen.draw.filled_circle(pos=(50, 50), radius=50, color=yellow)
self.assertImagesAlmostEqual(
self.screen.surface,
images.expected_filled_circle
)

def test_circle_errors(self):
"""draw.circle raises errors as expected."""
yellow = (255, 255, 0)
with self.assertRaises(TypeError):
self.screen.draw.circle(2, yellow)
with self.assertRaises(TypeError):
self.screen.draw.circle([2], yellow)

def test_polygon(self):
poly = [(0, 99), (49, 0), (99, 99)]
yellow = (255, 255, 0)
Expand All @@ -96,7 +139,7 @@ def test_polygon(self):
def test_filled_polygon(self):
poly = [(0, 99), (49, 0), (99, 99)]
yellow = (255, 255, 0)
"""We can draw a polygon."""
"""We can draw a filled polygon."""
self.screen.draw.filled_polygon(poly, yellow)
self.assertImagesAlmostEqual(
self.screen.surface,
Expand All @@ -111,6 +154,31 @@ def test_polygon_errors(self):
with self.assertRaises(TypeError):
self.screen.draw.polygon([2], yellow)

def test_rect(self):
yellow = (255, 255, 0)
"""We can draw a rectangle."""
self.screen.draw.rect(rect=Rect((0, 0), (100, 100)), color=yellow, width=9)
self.assertImagesAlmostEqual(
self.screen.surface,
images.expected_rect
)

def test_filled_rect(self):
yellow = (255, 255, 0)
"""We can draw a filled rectangle."""
self.screen.draw.filled_rect(rect=Rect((0, 0), (100, 100)), color=yellow)
self.assertImagesAlmostEqual(
self.screen.surface,
images.expected_filled_rect
)

def test_rect_errors(self):
"""draw.rect raises errors as expected."""
yellow = (255, 255, 0)
with self.assertRaises(TypeError):
self.screen.draw.rect(2, yellow)
with self.assertRaises(TypeError):
self.screen.draw.rect([2], yellow)

def test_wrapped_gradient_text(self):
"""We can draw wrapped gradient text.
Expand Down

0 comments on commit d42e3e4

Please sign in to comment.