banner



How To Draw The Robot Event

In this article, we are going to come across how to describe an object using Pygame. At that place tin be two versions for cartoon any shape, it can exist a solid ane or merely an outline of it.

Drawing Objects and Shapes in PyGame

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

Drawing Rectangle shape:

To describe a rectangle in your pygame project yous tin can utilize draw.rect() function.

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

Parameters:

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

Showtime, import the required module and initialize pygame. At present, 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 colour using the fill() function of pygame. Create a rectangle using the describe.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.brandish.update()

Output :

We can create a solid rectangle by 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.brandish.set_mode(( 600 , 600 ))

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

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

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

pygame.display.update()

Output :

Drawing Circumvolve Shape:

To draw a circle in your pygame project y'all can use draw.circumvolve() function. The entire approach is the same as in a higher place merely the role and parameters are changed accordingly.

Syntax : pygame.depict.circumvolve(surface, colour, center, radius, width)

Parameters :

  • surface :- Hither nosotros tin laissez passer the surface on which nosotros want to draw our circle. In the above case, we created a surface object named 'window'.
  • color :- Here we can pass the color for our circle. We are using dark-green color in our example.
  • center :- Hither we can pass the ( x, y ) coordinates for the middle of the circle.
  • radius :- Here we tin can laissez passer the radius of our circle.
  • width :- Hither nosotros can pass the line thickness. we tin also create a solid circle past changing the value of this width parameter. Then let's await 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.fill(( 255 , 255 , 255 ))

pygame.describe.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.

Case 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 can draw other basic shapes using the draw module of pygame.

Drawing Polygon Shape:

The desired polygon tin can be drawn using polygon() function.

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

Again the arroyo remains the same but the role and the parameters change.

Example one: 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: Drawing a hollow 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 ]], 5 )

pygame.display.update()

Output:

Drawing Line Shape:

A line is the most basic drawing entity and tin can be fatigued in pygame using line() function.

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

Example 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.draw.line(window, ( 0 , 0 , 0 ),

[ 100 , 300 ],

[ 500 , 300 ], 5 )

pygame.display.update()

Output:

Draw Multiple Shapes:

Yous can describe multiple shapes on the same surface object. For that, the first required modules are imported and pygame is initialized. Now, 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() office of pygame. Create required shapes are described to a higher place. Update the Surface object

Example i: Drawing a circle inside a 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 ),

[ fifty , 200 , 500 , 200 ])

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

[ 300 , 300 ], 100 )

pygame.display.update()

Output:

Example 2: Drawing rectangles inside ane another.

Python3

import pygame

from pygame. locals import *

pygame.init()

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

window.make full(( 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 , fourscore ),

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

]

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 + = ane

pygame.display.update()

Output :

Writing your ain cartoon functions:

You tin can hands create your ain specialized cartoon functions in pygame.

This tin exist done by post-obit the given process. Create a drawing role that will take starting position, width, and height as parameters. Describe required shapes are described above. Call the drawfunction()

Python3

import pygame

from pygame. locals import *

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

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

circle_x = width / 2 + ten

circle_y = summit / ii + y

if summit < width:

radius = superlative / 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:

At present let's see 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 whatsoever shape you want.

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 volition utilise to run the while loop and Create a while loop. Iterate over all the events received from pygame.event.go(). If the type of the event is quit and then setting the run variable to fake. If the type of the event is MOUSEBUTTONDOWN ( this result occurs when a user presses the mouse push) so getting the current position in a variable and so appending that position in our circle_positions listing. Iterate over all the positions in of the assortment 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.brandish.set_mode(( 600 , 600 ))

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

circle_positions = []

circle_radius = 60

color = ( 0 , 0 , 255 )

run = True

while run:

for event in pygame.result.get():

if issue. type = = QUIT:

run = False

elif event. type = = MOUSEBUTTONDOWN:

position = outcome.pos

circle_positions.append(position)

for position in circle_positions:

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

circle_radius)

pygame.display.update()

Output:


How To Draw The Robot Event,

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

Posted by: leetabefore.blogspot.com

0 Response to "How To Draw The Robot Event"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel