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 obstacles, interview experiences, and interview package as part of your placement preparations.Do upvote our blogs if you find them helpful and engaging!
Happy Learning!