Draw a Circle in Pygame

In this article, we are going to see how to draw an object using Pygame. There tin can be two versions for drawing whatsoever shape, it tin can be a solid 1 or only an outline of it.

Drawing Objects and Shapes in PyGame

You can easily draw basic shapes in pygame using the describe method of pygame.

Drawing Rectangle shape:

To draw a rectangle in your pygame project you tin use draw.rect() role.

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

Parameters:

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

First, import the required module and initialize pygame. At present, 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 colour using the fill() function of pygame. Create a rectangle using the draw.rect() method of pygame. Update the Surface object.

Example i: Cartoon 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.draw.rect(window, ( 0 , 0 , 255 ),

[ 100 , 100 , 400 , 100 ], ii )

pygame.display.update()

Output :

We can create a solid rectangle by setting the width parameter equal to 0 and the rest of the approach remains the same.

Example ii: Drawing a solid rectangle.

Python3

import pygame

from pygame. locals import *

pygame.init()

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

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

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

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

pygame.display.update()

Output :

Drawing Circle Shape:

To depict a circle in your pygame project you tin use draw.circle() function. The entire approach is the same equally above simply the function and parameters are changed accordingly.

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

Parameters :

  • surface :- Here we tin can pass the surface on which we want to draw our circle. In the above example, we created a surface object named 'window'.
  • color :- Here we can pass the color for our circle. Nosotros are using dark-green color in our example.
  • center :- Here we tin pass the ( x, y ) coordinates for the eye of the circumvolve.
  • radius :- Here we can pass the radius of our circumvolve.
  • width :- Here we can laissez passer the line thickness. we can also create a solid circle by irresolute the value of this width parameter. So let's wait at that.

Instance 1: Cartoon a hollow circumvolve.

Python3

import pygame

from pygame. locals import *

pygame.init()

window = pygame.brandish.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 :

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

Example two: drawing a solid circumvolve

Python3

import pygame

from pygame. locals import *

pygame.init()

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

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

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

[ 300 , 300 ], 170 , 0 )

pygame.display.update()

Output :

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

Drawing Polygon Shape:

The desired polygon can exist drawn using polygon() office.

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

Again the arroyo remains the same only the function and the parameters change.

Case 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.describe.polygon(window, ( 255 , 0 , 0 ),

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

[ 100 , 300 ]])

pygame.display.update()

Output :

Instance 2: Drawing a hollow polygon

Python3

import pygame

from pygame. locals import *

pygame.init()

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

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

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

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

[ 100 , 300 ]], 5 )

pygame.display.update()

Output:

Drawing Line Shape:

A line is the about bones drawing entity and tin be fatigued in pygame using line() function.

Syntax : pygame.draw.line(surface, colour, start_pos, end_pos, width)

Example 1: drawing a line

Python3

import pygame

from pygame. locals import *

pygame.init()

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

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

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

[ 100 , 300 ],

[ 500 , 300 ], 5 )

pygame.brandish.update()

Output:

Depict Multiple Shapes:

You tin draw multiple shapes on the same surface object. For that, the first required modules are imported and pygame is initialized. At present, create the surface object of a specific dimension using the display.set_mode() method of pygame. Fill the background of the surface object with white color using the fill up() role of pygame. Create required shapes are described above. Update the Surface object

Case 1: Drawing a circle inside a rectangle.

Python3

import pygame

from pygame. locals import *

pygame.init()

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

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

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

[ 50 , 200 , 500 , 200 ])

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

[ 300 , 300 ], 100 )

pygame.display.update()

Output:

Example 2: Drawing rectangles within i another.

Python3

import pygame

from pygame. locals import *

pygame.init()

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

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

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

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

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

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

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.describe.rect(window, color_list[color_var],

rectangle)

color_var + = 1

pygame.brandish.update()

Output :

Writing your own cartoon functions:

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

This can be washed past following the given procedure. Create a cartoon function that volition accept starting position, width, and summit as parameters. Depict required shapes are described to a higher place. Call the drawfunction()

Python3

import pygame

from pygame. locals import *

def drawingfunction(10, y, width, peak):

pygame.depict.rect(window, ( 0 , 0 , 255 ), [x, y, width, height])

circle_x = width / ii + x

circle_y = height / two + y

if pinnacle < width:

radius = height / 2

else :

radius = width / ii

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

pygame.init()

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

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

drawingfunction( 50 , 200 , 500 , 200 )

pygame.display.update()

Output:

Drawing shapes with the mouse:

Now allow's see how we tin create shapes whenever the user clicks the mouse. Nosotros are going to create circles in the adjacent example but y'all tin can create any shape you desire.

Create a listing to store the position of the shape to be drawn. 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.event.get(). If the type of the effect is quit and then setting the run variable to false. If the blazon of the event is MOUSEBUTTONDOWN ( this event occurs when a user presses the mouse button) then getting the electric current position in a variable so appending that position in our circle_positions list. Iterate over all the positions in of the assortment created using a for a loop. Keep drawing the shape. Update the surface object.

Python3

import pygame

from pygame. locals import *

pygame.init()

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

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

circle_positions = []

circle_radius = 60

color = ( 0 , 0 , 255 )

run = True

while run:

for effect in pygame.event.get():

if issue. type = = QUIT:

run = False

elif event. blazon = = MOUSEBUTTONDOWN:

position = effect.pos

circle_positions.append(position)

for position in circle_positions:

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

circle_radius)

pygame.display.update()

Output:


bearthemnioncy75.blogspot.com

Source: https://www.geeksforgeeks.org/pygame-drawing-objects-and-shapes/

0 Response to "Draw a Circle in Pygame"

แสดงความคิดเห็น

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel