Skip to content Skip to sidebar Skip to footer

Draw a Circle on Pygame

In this commodity, we are going to see how to draw an object using Pygame. At that place tin be two versions for drawing any shape, information technology can exist a solid ane or only an outline of information technology.

Drawing Objects and Shapes in PyGame

You can easily depict bones shapes in pygame using the draw method of pygame.

Drawing Rectangle shape:

To depict a rectangle in your pygame project you lot can apply depict.rect() function.

Syntax: pygame.describe.rect(surface, color, rect, width)

Parameters:

  • surface :- Here we tin can pass the surface on which we want to draw our rectangle. In the above case, we created a surface object named 'window'.
  • color :- Here nosotros can pass the color for our rectangle. We are using bluish color in our instance.
  • rect :- Here nosotros tin can pass the rectangle, position, and dimensions.
  • width :- Hither we can pass the line thickness. we can also create a solid rectangle past changing the value of this width parameter. So let'southward look at that.

Outset, import the required module and initialize pygame. Now, Create the surface object of a specific dimension using the display.set_mode() method of pygame. Make full the background of the surface object with white color using the fill() office of pygame. Create a rectangle using the draw.rect() method of pygame. Update the Surface object.

Example 1: Drawing outlined rectangle using pygame.

Python3

import pygame

from pygame. locals import *

pygame.init()

window = pygame.display.set_mode(( 600 , 600 ))

window.fill(( 255 , 255 , 255 ))

pygame.describe.rect(window, ( 0 , 0 , 255 ),

[ 100 , 100 , 400 , 100 ], 2 )

pygame.display.update()

Output :

We tin create a solid rectangle past setting the width parameter equal to 0 and the rest of the approach remains the aforementioned.

Example 2: Drawing a solid rectangle.

Python3

import pygame

from pygame. locals import *

pygame.init()

window = pygame.display.set_mode(( 600 , 600 ))

window.fill(( 255 , 255 , 255 ))

pygame.draw.rect(window, ( 0 , 0 , 255 ),

[ 100 , 100 , 400 , 100 ], 0 )

pygame.brandish.update()

Output :

Drawing Circumvolve Shape:

To depict a circle in your pygame projection yous tin use draw.circumvolve() office. The entire approach is the same as above merely the function and parameters are changed accordingly.

Syntax : pygame.draw.circle(surface, colour, center, radius, width)

Parameters :

  • surface :- Here we tin can pass the surface on which we want to describe our circle. In the above case, we created a surface object named 'window'.
  • color :- Here we can laissez passer the color for our circumvolve. We are using green colour in our example.
  • middle :- Here nosotros can pass the ( x, y ) coordinates for the center of the circle.
  • radius :- Here we tin can pass the radius of our circle.
  • width :- Here nosotros can laissez passer the line thickness. we can also create a solid circle by irresolute the value of this width parameter. So let'due south look at that.

Instance 1: Drawing a hollow circle.

Python3

import pygame

from pygame. locals import *

pygame.init()

window = pygame.display.set_mode(( 600 , 600 ))

window.make full(( 255 , 255 , 255 ))

pygame.draw.circle(window, ( 0 , 255 , 0 ),

[ 300 , 300 ], 170 , 3 )

pygame.display.update()

Output :

We can create a solid circle by setting the width parameter equal to 0.

Instance 2: drawing a solid circle

Python3

import pygame

from pygame. locals import *

pygame.init()

window = pygame.brandish.set_mode(( 600 , 600 ))

window.fill(( 255 , 255 , 255 ))

pygame.depict.circle(window, ( 0 , 255 , 0 ),

[ 300 , 300 ], 170 , 0 )

pygame.display.update()

Output :

Similarly, you tin depict other basic shapes using the draw module of pygame.

Drawing Polygon Shape:

The desired polygon can be fatigued using polygon() role.

Syntax: polygon(surface, color, points, width)

Again the approach remains the aforementioned merely the function and the parameters change.

Example 1: drawing a solid polygon

Python3

import pygame

from pygame. locals import *

pygame.init()

window = pygame.display.set_mode(( 600 , 600 ))

window.fill(( 255 , 255 , 255 ))

pygame.draw.polygon(window, ( 255 , 0 , 0 ),

[[ 300 , 300 ], [ 100 , 400 ],

[ 100 , 300 ]])

pygame.display.update()

Output :

Example 2: Cartoon a hollow polygon

Python3

import pygame

from pygame. locals import *

pygame.init()

window = pygame.brandish.set_mode(( 600 , 600 ))

window.fill(( 255 , 255 , 255 ))

pygame.draw.polygon(window, ( 255 , 0 , 0 ),

[[ 300 , 300 ], [ 100 , 400 ],

[ 100 , 300 ]], 5 )

pygame.display.update()

Output:

Drawing Line Shape:

A line is the most basic cartoon entity and tin be fatigued in pygame using line() role.

Syntax : pygame.describe.line(surface, color, start_pos, end_pos, width)

Case ane: drawing a line

Python3

import pygame

from pygame. locals import *

pygame.init()

window = pygame.display.set_mode(( 600 , 600 ))

window.fill(( 255 , 255 , 255 ))

pygame.depict.line(window, ( 0 , 0 , 0 ),

[ 100 , 300 ],

[ 500 , 300 ], 5 )

pygame.brandish.update()

Output:

Depict Multiple Shapes:

Y'all tin describe multiple shapes on the same surface object. For that, the kickoff required modules are imported and pygame is initialized. Now, create the surface object of a specific dimension using the brandish.set_mode() method of pygame. Fill the background of the surface object with white color using the fill() function of pygame. Create required shapes are described above. Update the Surface object

Case 1: Drawing a circumvolve inside a rectangle.

Python3

import pygame

from pygame. locals import *

pygame.init()

window = pygame.display.set_mode(( 600 , 600 ))

window.fill up(( 255 , 255 , 255 ))

pygame.draw.rect(window, ( 0 , 0 , 255 ),

[ l , 200 , 500 , 200 ])

pygame.draw.circle(window, ( 0 , 255 , 0 ),

[ 300 , 300 ], 100 )

pygame.display.update()

Output:

Example 2: Drawing rectangles inside one another.

Python3

import pygame

from pygame. locals import *

pygame.init()

window = pygame.display.set_mode(( 600 , 600 ))

window.fill up(( 255 , 255 , 255 ))

rectangle_list = [pygame.Rect( l , 100 , 500 , 200 ),

pygame.Rect( 70 , 120 , 460 , 160 ),

pygame.Rect( 90 , 140 , 420 , 120 ),

pygame.Rect( 110 , 160 , 380 , 80 ),

pygame.Rect( 130 , 180 , 340 , 40 )

]

color_list = [( 0 , 0 , 0 ),

( 255 , 255 , 255 ),

( 0 , 0 , 255 ),

( 0 , 255 , 0 ),

( 255 , 0 , 0 )

]

color_var = 0

for rectangle in rectangle_list:

pygame.depict.rect(window, color_list[color_var],

rectangle)

color_var + = 1

pygame.display.update()

Output :

Writing your ain drawing functions:

You can easily create your own specialized drawing functions in pygame.

This can be done past following the given procedure. Create a cartoon role that will accept starting position, width, and pinnacle as parameters. Draw required shapes are described in a higher place. Call the drawfunction()

Python3

import pygame

from pygame. locals import *

def drawingfunction(ten, y, width, height):

pygame.draw.rect(window, ( 0 , 0 , 255 ), [ten, y, width, summit])

circle_x = width / 2 + 10

circle_y = meridian / 2 + y

if height < width:

radius = pinnacle / 2

else :

radius = width / 2

pygame.draw.circle(window, ( 0 , 255 , 0 ), [circle_x, circle_y], radius)

pygame.init()

window = pygame.display.set_mode(( 600 , 600 ))

window.fill(( 255 , 255 , 255 ))

drawingfunction( 50 , 200 , 500 , 200 )

pygame.display.update()

Output:

Drawing shapes with the mouse:

Now let's encounter how we can create shapes whenever the user clicks the mouse. We are going to create circles in the next example but you can create any shape yous want.

Create a list to store the position of the shape to be fatigued. Create a variable to store the color of the shape. Create a variable which nosotros will use to run the while loop and Create a while loop. Iterate over all the events received from pygame.result.get(). If the type of the outcome is quit then setting the run variable to false. If the type of the event is MOUSEBUTTONDOWN ( this outcome occurs when a user presses the mouse push button) then getting the current position in a variable and then appending that position in our circle_positions list. Iterate over all the positions in of the array created using a for a loop. Keep cartoon the shape. Update the surface object.

Python3

import pygame

from pygame. locals import *

pygame.init()

window = pygame.display.set_mode(( 600 , 600 ))

window.make full(( 255 , 255 , 255 ))

circle_positions = []

circle_radius = 60

color = ( 0 , 0 , 255 )

run = True

while run:

for event in pygame.result.get():

if event. type = = QUIT:

run = Faux

elif event. type = = MOUSEBUTTONDOWN:

position = event.pos

circle_positions.append(position)

for position in circle_positions:

pygame.draw.circle(window, colour, position,

circle_radius)

pygame.display.update()

Output:


ortizoblee1979.blogspot.com

Source: https://www.geeksforgeeks.org/pygame-drawing-objects-and-shapes/#:~:text=To%20draw%20a%20circle%20in,and%20parameters%20are%20changed%20accordingly.

Postar um comentário for "Draw a Circle on Pygame"