Introduction
We know that the gaming industry is one of the major developing industries in the current world using current developing technologies. And on the parallel side, Python is also one of the famous programming languages which are stretching its hands in all the fields due to its simplicity and a full-fledged pack of libraries. Gaming is really a fun way of learning things. Game programming also includes many concepts such as math, computer programming, logic development, and many more. In the current developing technology, AI is also showing some support for game programming. Python can be used for Game Programming by using the Pygame library. Pygame library is one of the python programming libraries which has very efficient functionalities such as building custom events, taking input from the user through the keyboard, creating custom input boxes for taking inputs, etc. You can learn how to add custom events and get keyboard input in pygame using this link and this link. This article will try to discuss how to draw objects and shapes in our pygame application. Hope you will enjoy the article!
Drawing Objects and Shapes
Pygame supports many different shapes and objects to be present in its applications. For example, circles, polygons, rectangles, etc. It has a class called pygame.draw, which has several built-in methods to draw different shapes and objects. You can just call the name of the shape or object to be drawn in our pygame application.
Example: pygame.draw.rect() to draw a rectangle with the specific dimensions that are to be passed as arguments.
There are several other methods as follows:
- pygame.draw.rect - to draw a rectangle
- pygame.draw.polygon - to draw a polygon
- pygame.draw.circle - to draw a circle
- pygame.draw.ellipse - to draw an ellipse
- pygame.draw.arc - to draw an elliptical arc
- pygame.draw.line - to draw a straight line
- pygame.draw.lines - to draw multiple contiguous straight line segments
- pygame.draw.aaline - to draw a straight antialiased line
- pygame.draw.aalines - to draw multiple contiguous straight antialiased line segments.
You can try this on your own pygame application.
Steps to be followed:
- First, install all the necessary packages and call pygame.init() to initialize all the imported modules.
- Set the size of the screen using pygame.display.set_mode().
- Next, you need to create objects and shapes by using the above-mentioned methods.
- Lastly, you need to update the screen by using the pygame.display.update() method to get the object or shape on the screen.
You can get a few more detailed information about the methods mentioned to draw shapes and objects through their official documentation.
Example:
To draw a rectangle:
import pygame
pygame.init()
window = pygame.display.set_mode((600, 600)) #Set the size of the screen using the pygame.display.set_mode().
window.fill((255, 255, 255)) #to fill the screen with white colour.
pygame.draw.rect(window, (0, 0, 255), [100, 100, 400, 100], 2) #to draw rectangle.
pygame.display.update() #to update the screen with the rectangle shape.
Like Wise, you can create as many shapes as your own by using the above-mentioned methods.
You can refer to more examples through this, this, and these links.