Skip to content

Commit

Permalink
add draw_rectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
noamzaks committed Nov 16, 2024
1 parent 840e914 commit f994fa4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## [Unreleased]

### Added

- The `GameCanvas` has a new `draw_rectangle` method which can fill/stroke a given rectangle.

## [1.6.0] - 2024-11-15

### Added
Expand Down
27 changes: 27 additions & 0 deletions code_battles/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,33 @@ def draw_line(
self.context.lineTo(end_x, end_y)
self.context.stroke()

def draw_rectangle(
self,
start_x: int,
start_y: int,
width: int,
height: int,
fill="black",
stroke="transparent",
stroke_width=2,
board_index=0,
):
"""
Draws the given rectangle with the top-left corner at `(start_x, start_y)` (in map pixels) and with the specified `width` and `height` (in map pixels) with the given stroke and fill.
"""

start_x, start_y = self._translate_position(board_index, start_x, start_y)
width *= self._scale
height *= self._scale

self.context.fillStyle = fill
self.context.strokeStyle = stroke
self.context.lineWidth = stroke_width * self._scale
self.context.beginPath()
self.context.rect(start_x, start_y, width, height)
self.context.stroke()
self.context.fill()

def draw_circle(
self,
x: int,
Expand Down

0 comments on commit f994fa4

Please sign in to comment.