Javascript required
Skip to content Skip to sidebar Skip to footer

Draw Line With Alpha Pygame

How to draw different shapes using Python Pygame

In the previous article, we have introduced about the Pygame with full installation process and basic programming concepts. Also mentioned one example of how to draw the rectangle using pygame. In this article, you will learn how to draw different shapes using Pygame.

Pygame is a set of Python modules used to create fully-featured multimedia programs and video games. It is very simple and easy to use. It is free, platform independent and run on almost every operating system. So we can easily draw different shapes and create games using this.

Shapes Parameters

Before starting to draw different shapes, we will introduce to you some parameters that are used as attributes in drawing functions.

Surface

The surface to draw the shape on. The syntax to define a screen -

                      SCREEN = pygame.display.set_mode(WIDTH, HEIGHT)                  

The pygame.display.set_mode() function returns the pygame.Surface object for the window and set_mode() function accepts two arguments width and height of the window.


Defining Colors

For defining colors in pygame, we put three r,g,b values that range between 0 and 255.

                      COLOR = {r, g, b, a}                  

where, the parameter "r" sets the red value of the color, "g" sets the green value of the color and "b" sets the blue value of the color and the last parameter "a" sets the alpha value of the color.


width

The width of the line. If we set width = 0, then the shape will be filled.

pointlist

A list of an arbitrary amount of points or vectors in pixels (x,y), like [(45, 0), (80, 45), (45, 90)]. It is used to draw polygon, lines, anti-aliased lines. The shape will be drawn by drawing lines between each point.

pos

The position of the center of the circle in pixels (x,y).

radius

The radius of the circle in pixels.

closed

If it sets TRUE, a line between the first and last point will be drawn and closing the shape and if, it is set FALSE, there will not be a line from the last point in the pointlist parameter to the first point.

start_angle

The initial angle of the arc in radians.

stop_angle

The final angle of the arc in radians.

start_pos

The starting position of the line in pixels.

end_pos

The ending position of the line in pixels.

Drawing rectangle on the screen

The pygame.draw.rect() method is used to draw a rectangle shape. In the given syntax, 'Rect' is either the tuple of four integers or an object of pygame.Rect().

                      pygame.draw.rect(Surface, color, Rect, width=0)                  
Example
          import pygame, sys from pygame.locals import *  pygame.init() screen = pygame.display.set_mode((200, 110)) color = (255, 0, 0) pygame.display.set_caption('Draw Rectangle') while True: # main loop     for event in pygame.event.get():         pygame.draw.rect(screen, color, pygame.Rect(10, 10, 40, 40))          pygame.draw.rect(screen, color, pygame.Rect(60, 10, 40, 40), 5)                  if event.type == QUIT:             pygame.quit()             sys.exit()     pygame.display.update()        
Output of the above code

Pygame Draw Rectangle

Drawing polygon on the screen

The pygame.draw.polygon() method draws a shape with any number of sides. In the given syntax, pointlist is a list of points. The polygon is drawn by drawing lines between each point.

                      pygame.draw.polygon(Surface, color, pointlist, width=0)                  
Example
          import pygame, sys from pygame.locals import *  pygame.init() screen = pygame.display.set_mode((200, 110)) points = [(45, 0), (80, 45), (45, 90), (10, 45)] color = (0, 255, 0) pygame.display.set_caption('Draw Polygon') while True: # main loop     for event in pygame.event.get():         pygame.draw.polygon(screen, color, points, 10)                  if event.type == QUIT:             pygame.quit()             sys.exit()     pygame.display.update()                  
Output of the above code

Pygame Draw Polygon

Drawing circle on the screen

The pygame.draw.circle() method draws a circle around a given number of point.

                      pygame.draw.circle(Surface, color, pos, radius, width=0)                  
Example
          import pygame, sys from pygame.locals import *  pygame.init() screen = pygame.display.set_mode((200, 110)) radius = 35 color = (204, 0, 102) pygame.display.set_caption('Draw Circle') while True: # main loop     for event in pygame.event.get():         pygame.draw.circle(screen, color, (35, 35), radius, 10)          pygame.draw.circle(screen, color, (105, 35), radius)                  if event.type == QUIT:             pygame.quit()             sys.exit()     pygame.display.update()        
Output of the above code

Pygame Draw Circle

Drawing ellipse on the screen

The pygame.draw.ellipse() method draws an ellipse.

                      pygame.draw.ellipse(Surface, color, Rect, width=0)                  
Example
          import pygame, sys from pygame.locals import *  pygame.init() screen = pygame.display.set_mode((200, 110))  color = (255, 255, 0) pygame.display.set_caption('Draw Ellipse') while True: # main loop     for event in pygame.event.get():         pygame.draw.ellipse(screen, color, screen.get_rect(), 15)                  if event.type == QUIT:             pygame.quit()             sys.exit()     pygame.display.update()        
Output of the above code

Pygame Draw Ellipse

Drawing arc on the screen

The pygame.draw.arc() method draws a partial section of an ellipse.

                      pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1)                  
Example
          import pygame, sys, math from pygame.locals import *  pygame.init() screen = pygame.display.set_mode((300, 200)) pi = 3.141592653   # sets the properties of the arc color = (102, 255, 255)  start_angle = 0 stop_angle = math.pi width = 10  while True: # main loop     for event in pygame.event.get():         # draws the arc         pygame.draw.arc(screen, color, screen.get_rect(), start_angle, stop_angle, width)         if event.type == QUIT:             pygame.quit()             sys.exit()     pygame.display.update()        
Output of the above code

Pygame Draw Arc

Drawing line on the screen

The pygame.draw.line() function draws a straight line segment.

                      pygame.draw.line(Surface, color, start_pos, end_pos, width=1)                  
Example
          import pygame, sys from pygame.locals import *  pygame.init() screen = pygame.display.set_mode((200, 120))  color = (102, 255, 255)  while True: # main loop     for event in pygame.event.get():         # draws the arc         pygame.draw.line(screen, color, (0,0), (100,60), 6)         if event.type == QUIT:             pygame.quit()             sys.exit()     pygame.display.update()        
Output of the above code

Pygame Draw Line

Drawing lines on the screen

The pygame.draw.lines() method is used to draw multiple contiguous line segments.

                      pygame.draw.lines(Surface, color, closed, pointlist, width=1)                  
Example
          import pygame, sys from pygame.locals import *  pygame.init() screen = pygame.display.set_mode((200, 150)) color = (204, 0, 204) points = [(45, 0), (80, 45), (45, 90), (10, 45), (45,30)]  while True: # main loop     for event in pygame.event.get():         pygame.draw.lines(screen, color, False, points, 10)         if event.type == QUIT:             pygame.quit()             sys.exit()     pygame.display.update()        
Output of the above code

Pygame Draw Lines

Drawing Anti aliased line on the screen

The pygame.draw.aaline() method is used to draw a fine anti-aliased line.

                      pygame.draw.aaline(Surface, color, startpos, endpos, blend=1)                  
Example
          import pygame, sys from pygame.locals import *  pygame.init() screen = pygame.display.set_mode((200, 150)) color = (102, 204, 0)  while True: # main loop     for event in pygame.event.get():         # draws the antialiased line         pygame.draw.aaline(screen, color, (0,0), (160,100), 10)         if event.type == QUIT:             pygame.quit()             sys.exit()     pygame.display.update()                  
Output of the above code

Pygame AntiAliased Line

Drawing Anti aliased lines on the screen

The pygame.draw.aalines() method is used to draw a connected sequence of anti-aliased lines.

                      pygame.draw.aalines(Surface, color, closed, pointlist, blend=1)                  
Example
          import pygame, sys from pygame.locals import *  pygame.init() screen = pygame.display.set_mode((200, 150)) color = (102, 204, 0) points = [(45, 0), (80, 45), (45, 90), (10, 45), (45,30)]  while True: # main game loop     for event in pygame.event.get():         # draws the antialiased lines         pygame.draw.aalines(screen, color, False, points, 10)         if event.type == QUIT:             pygame.quit()             sys.exit()     pygame.display.update()        
Output of the above code

Pygame AntiAliased Lines

Related Articles

Convert list to dictionary Python
Dictionary inside list Python
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python add list to list
Python Pandas Dataframe to CSV
Python compare two lists
Remove element from list Python
Python iterate list with index
Python program to sum all the numbers in a list
Python print without newline
Python iterate list with index
Python add list to list
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml

Draw Line With Alpha Pygame

Source: https://www.etutorialspoint.com/index.php/268-how-to-draw-different-shapes-using-pygame