Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Pygame
3.
Implementation
4.
OUTPUT
5.
Frequently Asked Questions.
5.1.
How do you make a pentagon in Python?
5.2.
What is Blit in pygame?
5.3.
Is pygame a GUI?
5.4.
How do I create a pop-up in pygame?
5.5.
Which pygame function is used to make changes to the window?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Drawing different shapes in Pygame window

Introduction

Pygame is a Python multimedia toolkit that may be used to create games and multimedia applications. We'll explore how to use the pygame module to develop various forms on the screen while considering their height, width, and position in the pygame window in this tutorial.

SDL stands for Simple DirectMedia Layer, and pygame is a Python wrapper for it. SDL gives you cross-platform access to your system's multimedia hardware, including sound, video, mouse, keyboard, and joystick. pygame began as a substitute for the now-defunct PySDL project. Because SDL and pygame are cross-platform, you can create games and rich multimedia Python programs for any platform that supports them!

Pygame

Pygame is a suite of cross-platform Python modules for creating video games. It offers sound and graphics libraries that may be utilized with the Python programming language. Now it's up to the developer's imagination or requirement to decide what kind of game to make using this toolbox.

To install pygame, type:

pip install pygame
You can also try this code with Online Python Compiler
Run Code

The fundamental procedures for showing graphics in the pygame window are as follows:

  • Using the pygame display.set mode() function, create a display surface object.
  • Using the fill() method of the pygame, show the surface object and fill the surface object with white.
  • Using pygame's Primitive Drawing Functions, draw various shapes onto a surface object.
  • The display. update() function of pygame is used to show the display surface object on the pygame window.

Implementation

# import the module pygame
import pygame

# activation of the library pygame.
# initiated pygame and gave permission
# to use pygame's functionality.

pygame.init()

# define the RGB value
# for white, green,
# blue, black, red
# colour respectively.
wht = (255, 255, 255)
grn = (0, 255, 0)
blu = (0, 0, 128)
blck = (0, 0, 0)
rd = (255, 0, 0)

# assigning values to X and Y variable
X = 400
Y = 400

# created the display surface object
# of specific dimension..e(X,Y).
dsply_surfc = pygame.display.set_mode((X, Y ))

# set the pygame window name
pygame.display.set_caption('Drawing')

# completely fill the surface object
# with white colour
dsply_srfc.fill(wht)

# draw a polygon using draw.polygon()
# method of pygame.
# pygame.draw.polygon(surface, color, pointlist, thickness)
# thickness of line parameter is optional.
pygame.draw.polygon(dsply_srfc, blue,
[(146, 0), (291, 106),
(236, 277), (56, 277), (0, 106)])

# draw a line using draw.line()
# method of pygame.
# pygame.draw.line(surface, color,
# start point, end point, thickness)
pygame.draw.line(dsply_srfc, green,
(60, 300), (120, 300), 4)

# draw a circle using draw.circle()
# method of pygame.
# pygame.draw.circle(surface, color,
# center point, radius, thickness)
pygame.draw.circle(dsply_srfc,
green, (300, 50), 20, 0)

# draw a ellipse using draw.ellipse()
# method of pygame.
# pygame.draw.ellipse(surface, color,
# bounding rectangle, thickness)
pygame.draw.ellipse(dsply_srfc, black,
(300, 250, 40, 80), 1)

# draw a rectangle using draw.rect()
# method of pygame.
# pygame.draw.rect(surface, color,
# rectangle tuple, thickness)
# thickness of line parameter is optional.
pygame.draw.rect(dsply_srfc, black,
(150, 300, 100, 50))

# infinite loop
while True :

# iterate over the list of Event objects
# that was returned by pygame.event.get() method.
for vnt in pygame.event.get() :

# if event object type is QUIT
# then quitting the pygame
# and program both.
if vnt.type == pygame.QUIT :

# deactivates the pygame library
pygame.quit()

# quit the program.
quit()

# Draws the surface object to the screen.
pygame.display.update()
You can also try this code with Online Python Compiler
Run Code

OUTPUT

Frequently Asked Questions.

How do you make a pentagon in Python?

The side of a pentagon is assumed to be 100 units long. As a result, we will move the turtle forward by 100 units. Then spin it 72 degrees in a clockwise manner. A pentagon's outer angle is 72 degrees. To achieve Pentagon, these two statements are repeated five times.

What is Blit in pygame?

blit(), which stands for Block Transfer, copies the contents of one Surface to another. As a result, blit() will place that rectangle Surface on top of the screen.

Is pygame a GUI?

Pygame GUI is a package that allows you to create graphical user interfaces for pygame games. The module is developed for Pygame 2 and Python 3 and is strongly forward-looking.

How do I create a pop-up in pygame?

In Pygame, you may treat the popup window like any other sprite. Create a class with draw(), update(), and handle event() and use it in the same way as Player and Enemy are used. You may also utilize one of PyGame's GUI modules. However, some may require code modifications because they have their event loop.

Which pygame function is used to make changes to the window?

"Pygame window" and "pygame icon" are Pygame's default titles and logos for the pygame window. Our window's name and icon may be changed using the set caption() and set icon() methods, respectively.

Conclusion

So that's the end of the article Drawing different shapes in Pygame window

After reading about the Drawing different shapes in the Pygame window, are you not feeling excited to read/explore more articles on the topic of pygame? Don't worry; Coding Ninjas has you covered. 

However, you may want to pursue our premium courses to give your job an advantage over the competition!

Upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and more with our Coding Ninjas Studio Guided Path! If you want to put your coding skills to the test, check out the mock test series and enter the contests on Coding Ninjas Studio! But if you've only recently started your schooling and are looking for answers to issues presented by digital titans like Amazon, Microsoft, Uber, and others. In this situation, you must consider the obstaclesinterview experiences, and interview package as part of your placement preparations.Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass