How to Draw a Circle in Python Pygame

How to draw different shapes using Python Pygame

In the previous article, we take introduced about the Pygame with total installation process and basic programming concepts. Also mentioned one example of how to draw the rectangle using pygame. In this article, yous will learn how to depict dissimilar shapes using Pygame.

Pygame is a cross-platform 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 arrangement. It includes computer graphics and sound libraries intended to be used with the Python programming language. And then we can easily describe different shapes and create games using this.

Shapes Parameters

Earlier starting to draw unlike shapes, nosotros will introduce to you some parameters that are used equally attributes in cartoon functions.

Surface

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

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

The pygame.brandish.set_mode() office 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 iii r,g,b values that range between 0 and 255.

                      Colour = {r, g, b, a}                  

Where, the parameter "r" sets the crimson value of the colour, "g" sets the greenish value of the color and "b" sets the blue value of the color and the concluding parameter "a" sets the blastoff value of the colour.


width

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

pointlist

A list of an arbitrary amount of points or vectors in pixels (x,y), like [(45, 0), (eighty, 45), (45, 90)]. It is used to depict polygon, lines, anti-aliased lines. The shape volition be drawn by drawing lines betwixt each bespeak.

pos

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

radius

The radius of the circle in pixels.

closed

If information technology sets TRUE, a line between the offset and last point will be drawn and endmost the shape and if, it is prepare FALSE, there volition 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.describe.rect() method is used to draw a rectangle shape. In the given syntax, 'Rect' is either the tuple of iv 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)) colour = (255, 0, 0) pygame.display.set_caption('Draw Rectangle') while True: # main loop     for event in pygame.effect.get():         pygame.draw.rect(screen, color, pygame.Rect(10, 10, 40, 40))          pygame.draw.rect(screen, color, pygame.Rect(lx, x, 40, 40), 5)                  if event.type == QUIT:             pygame.quit()             sys.leave()     pygame.brandish.update()        
Output of the in a higher place code

Pygame Draw Rectangle

Drawing polygon on the screen

The pygame.describe.polygon() method draws a shape with whatsoever number of sides. In the given syntax, pointlist is a list of points. The polygon is fatigued by cartoon lines betwixt each point.

                      pygame.draw.polygon(Surface, colour, 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), (x, 45)] color = (0, 255, 0) pygame.display.set_caption('Draw Polygon') while True: # chief loop     for outcome in pygame.outcome.go():         pygame.draw.polygon(screen, color, points, x)                  if result.type == QUIT:             pygame.quit()             sys.exit()     pygame.brandish.update()                  
Output of the to a higher place code

Pygame Draw Polygon

Drawing circle on the screen

The pygame.depict.circle() method draws a circle around a given number of points.

                      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 Circumvolve') while True: # principal loop     for event in pygame.event.get():         pygame.draw.circle(screen, colour, (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.describe.ellipse(Surface, color, Rect, width=0)                  
Case
          import pygame, sys from pygame.locals import *  pygame.init() screen = pygame.brandish.set_mode((200, 110))  color = (255, 255, 0) pygame.display.set_caption('Describe Ellipse') while True: # chief loop     for event in pygame.event.get():         pygame.draw.ellipse(screen, color, screen.get_rect(), 15)                  if issue.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.depict.arc(Surface, color, Rect, start_angle, stop_angle, width=1)                  
Instance
          import pygame, sys, math from pygame.locals import *  pygame.init() screen = pygame.display.set_mode((300, 200)) pi = iii.141592653   # sets the properties of the arc color = (102, 255, 255)  start_angle = 0 stop_angle = math.pi width = x  while Truthful: # principal loop     for effect in pygame.issue.get():         # draws the arc         pygame.draw.arc(screen, color, screen.get_rect(), start_angle, stop_angle, width)         if event.blazon == QUIT:             pygame.quit()             sys.go out()     pygame.display.update()        
Output of the above lawmaking

Pygame Draw Arc

Drawing line on the screen

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

                      pygame.depict.line(Surface, color, start_pos, end_pos, width=1)                  
Instance
          import pygame, sys from pygame.locals import *  pygame.init() screen = pygame.display.set_mode((200, 120))  colour = (102, 255, 255)  while Truthful: # primary loop     for consequence in pygame.event.become():         # draws the arc         pygame.draw.line(screen, colour, (0,0), (100,threescore), 6)         if result.type == QUIT:             pygame.quit()             sys.exit()     pygame.display.update()        
Output of the higher up code

Pygame Draw Line

Cartoon lines on the screen

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

                      pygame.draw.lines(Surface, colour, closed, pointlist, width=1)                  
Example
          import pygame, sys from pygame.locals import *  pygame.init() screen = pygame.display.set_mode((200, 150)) colour = (204, 0, 204) points = [(45, 0), (80, 45), (45, ninety), (10, 45), (45,xxx)]  while True: # main loop     for event in pygame.event.get():         pygame.draw.lines(screen, color, False, points, 10)         if consequence.type == QUIT:             pygame.quit()             sys.leave()     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 depict a fine anti-aliased line.

                      pygame.draw.aaline(Surface, colour, startpos, endpos, alloy=i)                  
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.result.become():         # draws the antialiased line         pygame.draw.aaline(screen, color, (0,0), (160,100), x)         if outcome.type == QUIT:             pygame.quit()             sys.exit()     pygame.display.update()                  
Output of the in a higher place 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, colour, closed, pointlist, alloy=ane)                  
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,xxx)]  while True: # principal game loop     for event in pygame.event.become():         # draws the antialiased lines         pygame.draw.aalines(screen, color, False, points, 10)         if event.type == QUIT:             pygame.quit()             sys.get out()     pygame.display.update()        
Output of the above code

Pygame AntiAliased Lines

Related Articles

Convert list to dictionary Python
Dictionary inside list Python
Pandas cord 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 listing with alphabetize
Python programme to sum all the numbers in a listing
Python print without newline
Python iterate list with index
Python add listing 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

stetlerdrecur.blogspot.com

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

0 Response to "How to Draw a Circle in Python Pygame"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel