Add drawImage method for rendering images to screen#4
Conversation
Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds a drawImage API to the Graphik class to support rendering image files onto the pygame display surface with explicit positioning and scaling.
Changes:
- Add
Graphik.drawImage(...)that loads an image from disk, scales it to(width, height), and blits it to the display surface. - Add error handling for image load/transform failures with a more descriptive exception message.
- Update
.gitignoreto ignore Python bytecode artifacts (__pycache__,*.pyc).
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/main/python/preponderous/graphik/graphik.py |
Introduces the new drawImage method for loading/scaling/blitting images. |
.gitignore |
Adds Python cache/bytecode ignore patterns. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def drawImage(self, file_path, xpos, ypos, width, height): | ||
| try: | ||
| image = pygame.image.load(file_path) | ||
| image = pygame.transform.scale(image, (width, height)) | ||
| self.gameDisplay.blit(image, (xpos, ypos)) | ||
| except (pygame.error, FileNotFoundError) as e: | ||
| raise type(e)(f"Failed to load image from '{file_path}': {e}") No newline at end of file |
There was a problem hiding this comment.
file_path is the only snake_case parameter name in this class (others use xpos, ypos, colorBox, etc.). To keep the public API consistent, consider renaming it to match the existing style (e.g., filepath or filePath).
| def drawImage(self, file_path, xpos, ypos, width, height): | |
| try: | |
| image = pygame.image.load(file_path) | |
| image = pygame.transform.scale(image, (width, height)) | |
| self.gameDisplay.blit(image, (xpos, ypos)) | |
| except (pygame.error, FileNotFoundError) as e: | |
| raise type(e)(f"Failed to load image from '{file_path}': {e}") | |
| def drawImage(self, filePath, xpos, ypos, width, height): | |
| try: | |
| image = pygame.image.load(filePath) | |
| image = pygame.transform.scale(image, (width, height)) | |
| self.gameDisplay.blit(image, (xpos, ypos)) | |
| except (pygame.error, FileNotFoundError) as e: | |
| raise type(e)(f"Failed to load image from '{filePath}': {e}") |
There was a problem hiding this comment.
Renamed parameter to filePath in commit 9ace29f for consistent naming with other methods in the class.
| image = pygame.transform.scale(image, (width, height)) | ||
| self.gameDisplay.blit(image, (xpos, ypos)) | ||
| except (pygame.error, FileNotFoundError) as e: | ||
| raise type(e)(f"Failed to load image from '{file_path}': {e}") No newline at end of file |
There was a problem hiding this comment.
Re-raising with raise type(e)(...) drops important exception details (e.g., FileNotFoundError's errno/filename) and loses the original traceback context. Prefer raising with explicit exception construction per type and chaining with from e so callers keep the original context and error metadata.
| raise type(e)(f"Failed to load image from '{file_path}': {e}") | |
| if isinstance(e, FileNotFoundError): | |
| # Preserve errno and filename while adding context, and keep traceback via exception chaining. | |
| message = f"Failed to load image from '{file_path}': {e.strerror}" | |
| raise FileNotFoundError(e.errno, message, e.filename) from e | |
| else: | |
| # Preserve the original exception type and chain to keep traceback. | |
| raise pygame.error(f"Failed to load image from '{file_path}': {e}") from e |
There was a problem hiding this comment.
Updated exception handling to use from e for proper exception chaining in commit 9ace29f. This preserves the original traceback context while providing descriptive error messages.
|
@copilot address comments |
…on handling Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
Plan to Add Image Drawing Support
drawImagemethod to Graphik classdrawImage(self, filePath, xpos, ypos, width, height)from efile_pathtofilePathfor naming consistencyChanges Addressing PR Feedback
Comment 2752287803 - Parameter Naming Consistency
file_pathparameter tofilePathto match existing naming style in the class (e.g.,colorBox,colorText,sizeText)Comment 2752287807 - Exception Handling Improvements
from eImplementation Summary
The
drawImagemethod:filePath,xpos,ypos,width,heightSecurity Summary
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.