Table of contents
1.
Introduction
2.
Drawing Objects and Shapes
3.
Frequently Asked Questions
3.1.
What is Pygame?
3.2.
How many shapes can we draw using Pygame?
3.3.
How do you draw a rectangle in Pygame?
3.4.
How to draw a surface object in Pygame?
4.
Conclusion
Last Updated: Mar 27, 2024

Drawing Objects and Shapes with Pygame

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.
You can also try this code with Online Python Compiler
Run Code

Like Wise, you can create as many shapes as your own by using the above-mentioned methods.

You can refer to more examples through thisthis, and these links.

Frequently Asked Questions

What is Pygame?

Pygame is a python programming language library that allows developers to develop applications on gaming themes. It is a well-developed library that has many useful functions to implement several functionalities of our application.

How many shapes can we draw using Pygame?

In pygame, we are allowed to draw five different shapes Rectangles, lines, polygons, ellipses, and circles. We can use pygame.draw class to draw these shapes using methods like rect(), line(), lines(), polygon(), ellipse(), circle() respectively.

How do you draw a rectangle in Pygame?

In pygame, we can use pygame.draw.rect(surface, color, pygame.Rect(left, top, width, height)) method to draw a rectangle. The arguments explain their properties, such as the surface on which it should be drawn like a window, or on other shapes, its color, its sizes such as height, width, etc.

How to draw a surface object in Pygame?

The surface object may be a window, another shape or object, or even an entire screen. This surface object can be created by using the pygame.display.set_mode() method. After drawing this surface object, we need to update it by using the pygame.display.update() method.

How to draw a hollow circle in Pygame?
Firstly, we need to import the pygame module, and then we need to call init() to initialize the imported modules. Then we need to create a surface object, and then we need to call the method called ‘pygame.draw.circle()’ method, to which we need to pass all their properties. We just need to take care of the width property, which refers to the line thickness; if we mention it to zero, it will become a solid circle.

Conclusion

This article extensively discussed the concept of Drawing Objects and Shapes using pygame. How to use pygame.draw class to draw different shapes and objects.

After reading this article, are you not feeling excited to read/explore more articles on similar problems? Don't worry; Coding Ninjas has you covered. Refer to our Guided Path on Coding Ninjas Studio to improve yourself in Competitive ProgrammingData Structures and AlgorithmsJavaScriptSystem Design, and many more! If you want to test your confidence in coding, you may check out the contests and participate in the mock test series hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview bundle, and interview experiences for placement preparations.

Nevertheless, you can consider our paid courses to give your career improvement an edge over others!

Do upvote our blogs and articles if you find them helpful and engaging!

Happy Learning!

Live masterclass