How to draw a rectangle with rounded corner in PyGame?
Pygame is a Python library designed to develop video games. Pygame adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the python language. In this article, we will see how can we draw a rectangle with rounded corners in Pygame.
- pygame.display.set_mode(): This function is used to initialize a surface for display. This function takes the size of the display as a parameter.
- pygame.display.flip(): This function is used to update the content of the entire display surface of the screen.
- pygame.draw.rect(): This function is used to draw a rectangle. It takes surface, color, and pygame Rect objects as input parameters and draws a rectangle on the surface.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
rect(surface, color, rect, width=0, border_radius=0, border_top_left_radius=-1, border_top_right_radius=-1, border_bottom_left_radius=-1, border_bottom_right_radius=-1)
The border_radius parameters were only added to PyGame version 2.0.0.dev8.
- Import module
- Initialize Pygame
- Draw a rectangle with rounded borders
- Display shape
Example 1: This example draws a rectangle with all corner rounded
Not just this, Pygame can be used to round even only one corner as per requirement. Given below is the implementation using the above approach.
Example 2: This example draws a rectangle with only the top right corner rounded.
Example 3: This example uses keyword arguments to draw a rectangle with the bottom right corner rounded.