Draw a Circle or Ellipse
Drawing rectangles, ellipses, and circles¶
All drawing functions in the PyGame library begin with pg.depict
. Depending on what shape we desire to draw, we telephone call dissimilar functions. In the explanations that follow, the pregnant of the parameters is:
-
The sail parameter is the area in which we draw. In this guide, programs will already have a formed variable (more specifically object) canvass, obtained as the issue from a call of the
pygamebg.open_window
part. -
The color parameter is the colour we employ to draw. As information technology was said earlier, a color can be specified past its name (for example
pg.Color("black")
), or as a tuple or a list of 3 elements (for instance[255, 0, 0]
for red). -
The rectangle parameter is a tuple or a list of four elements \((ten, y, due west, h)\) or \([10, y, due west, h]\), that describes a rectangle, every bit explained earlier (coordinates of the acme-left vertex, rectangle width and height).
-
The center parameter represents a point. Every bit mentioned earlier, a point can be specified every bit a tuple (or list) of 2 elements, which stand for the coordinates of the point in the window in which nosotros draw.
-
The thickness parameter is thickness of the lines we apply to draw. In the functions we explain here, this parameter is optional and can be omitted.
Nosotros will at present see more than detailed descriptions of the functions for cartoon rectangles, ellipses, and circles. A brief example of ane or 2 lines of code is given afterward each office clarification. You can run each of these examples by copying it to the program below (which doesn't depict annihilation for at present). The pictures that follow the examples are obtained in that aforementioned mode.
Drawing a rectangle¶
To draw a rectangle we utilise the function pg.draw.rect
, which has two forms:
pg . draw . rect ( canvas , color , rectangle , thickness ) pg . draw . rect ( canvas , color , rectangle )
Nosotros use the form without the thickness parameter when we want the rectangle's interior to be filled with the indicated colour as well.
For case, the first of the following 2 statements means:
-
draw a rectangle (function rect)
-
paint information technology black (parameter (0, 0, 0) specifies blackness color)
-
The top-left vertex of the rectangle has coordinates (40, 80)
-
the width of the rectangle is fifty and the height is 30 pixels
-
only frame of the rectangle is to be drawn and the lines should exist 3 pixels thick
The 2d argument means:
-
describe a rectangle (office rect)
-
paint it blackness (parameter pg.Color("blackness") also specifies black color)
-
The top-left vertex of the rectangle has coordinates (140, 80)
-
the rectangle is to be twenty pixels both wide and high, so information technology volition actually be a square
-
The square will exist filled with color since there is no thickness parameter
Drawing an ellipse¶
To draw an ellipse, nosotros utilise the office pg.depict.ellipse
, with or without the thickness parameter:
pg . draw . ellipse ( canvas , colour , rectangle , thickness ) pg . describe . ellipse ( canvas , color , rectangle )
The rectangle parameter represents the rectangle the ellipse is inscribed in, and the other parameters take the aforementioned meaning every bit earlier. If nosotros need it, nosotros can calculate the centre and major and minor semi-axes of the ellipse using the tuple \((10, y, w, h)\) or listing \([10, y, w, h]\) that defines the rectangle. The coordinates of the eye of the rectangle, which is also the heart of the ellipse, are \((x + w/two, y + h/2)\), and the major and modest semi-axes of the ellipse are \(due west/two\) and \(h/2\). So, for example, statement
draws a yellow filled ellipse. The center of the ellipse is the center of the specified rectangle, which is at point (130, 180). The horizontal semi-centrality of the ellipse is 30 pixels long, and the vertical xx.
Drawing a circle¶
To draw a circumvolve, we utilise the function pg.describe.circumvolve
, with or without the thickness parameter:
pg . draw . circle ( canvas , color , center , radius , thickness ) pg . draw . circle ( canvas , color , center , radius )
The eye parameter is a point representing the center of the circle, and the radius parameter is a number representing the radius of the circumvolve in pixels. For example, the following statement draws a red circle, 3 pixels thick, of radius fifty pixels, whose heart is at point (100, 100):
If the terminal parameter (stroke width 3) had been omitted, the interior of the circle would have been red also.
Drawing rectangles, ellipses and circles - questions¶
Cheque how much y'all understand and remember well-nigh these drawing functions:
- Pinnacle-left vertex coordinates
- Top-left vertex coordinates are specified when drawing an ellipse or a rectangle
- Radius
- Correct
- Center coordinates
- Correct
- Width and peak
- Width and meridian are specified when drawing an ellipse or a rectangle
- Colour
- Correct
Q-26: What needs to be specified when drawing a circle?
- pg.draw.circle(canvas, colour, 100, 100, 30, 5)
- Try again
- pg.draw.circumvolve(canvas, colour, (100, 100), 30, five)
- Correct
- pg.draw.circle(canvass, color, (100, 100, 30, 5))
- Try over again
- pg.describe.circle(sail, color, (100, 100), (30, 5))
- Endeavour again
Q-27: To draw a circle centered at point \((100, 100)\), whose radius is \(xxx\) pixels, using line \(5\) pixel wide, which part telephone call needs to exist made?
- the latter draws an ellipse whose semi-major and semi-small axes equal r and 1.
- Endeavor once more
- the latter fills the circle interior with color.
- Try again
- the former draws a disk (filled circumvolve), and the latter a circular line.
- Correct
- the former draws a circular line, and the latter a deejay (filled circle).
- Try over again
Q-28: The deviation between pg.draw.circle(sheet, color, (cx, cy), r) and pg.depict.circle(canvas, colour, (cx, cy), r, i) is that:
- Elevation-left vertex coordinates
- Try again
- Stroke width
- Effort again
- Width
- Endeavor over again
- Superlative
- Endeavour again
- Center coordinates
- Correct
Q-29: What is NOT specified when drawing a rectangle?
- pg.depict.rect(canvas, colour, 100, 100, xxx, 50)
- Endeavour again
- pg.draw.rect(canvas, colour, (100, 100), (xxx, 50))
- Attempt over again
- pg.draw.rect(canvas, color, (100, 100), 30, l)
- Effort again
- pg.draw.rect(sail, colour, (100, 100, 30, l))
- Right
Q-thirty: To depict a rectangle whose top-left vertex is at point :math: (100, 100), \(30\) pixels broad and \(50\) pixels high, which part call needs to exist made?
- pg.draw.rect(canvas, colour, (80, 80, 50, 80))
- Correct
- pg.depict.rect(canvas, color, (80, lxxx), (130, 160))
- Endeavor again
- pg.draw.rect(sail, color, (80, 80, 130, 160))
- Endeavour again
- pg.draw.rect(canvas, colour, (80, 80), (50, 80))
- Attempt once again
Q-31: To draw a rectangle whose tiptop-left vertex is at point \((fourscore, 80)\), and bottom-correct vertex at signal \((130, 160)\), which function call needs to be made:
Drawing by instructions¶
In the following tasks, you lot tin see what your programme should draw past clicking the "Play chore" button. To provide you necessary data to write the statements you need, detailed instructions with descriptions of the parameters are likewise given.
Proceed in mind that before drawing you should paint the background with the appropriate color, for which you use the argument canvas.fill(pg.Colour(...))
(instead of the dots specify a color).
Task - target:
Depict a target on a white background using 3 filled circles. The centers of all three circles should be at the eye of the window and all circles should exist filled with colour. Get-go, draw a red circle of radius 100, and so a blue one of radius 75, and and so a green circle of radius 50 pixels.
What exercise you think, could these circles exist fatigued in a different order? If y'all are not sure what would happen if the order changed, give it a try.
Task - duckling:
On a dark-green background, describe a duckling equally a cartoon grapheme. The cartoon consists of the following parts:
-
Head: a yellow filled ellipse, inscribed in a 320 x 300 pixel rectangle, with the peak left vertex at point (40, fifty)
-
Head edge: a blackness ellipse framing the previous ellipse with a line of width 1
-
Left eye: a black filled ellipse, inscribed in a xl 10 xl pixel rectangle with the pinnacle left vertex at point (130, 130)
-
Right eye: a black filled ellipse, inscribed in a forty ten 40 pixel rectangle, with the pinnacle left vertex at point (280, 120)
-
Rima oris (beak): a cerise filled ellipse, inscribed in a 120 x 140 pixel rectangle, with the meridian left vertex at point (200, 170)
-
Mouth border: a black ellipse framing the previous ellipse with a line of width 1
Here nosotros have more freedom with the drawing social club, but we still need to follow some gild. Try to explain which parts of the image need to exist drawn exactly in this order, and which demand not.
Note that the eyes are inscribed in rectangles that are really squares. How (thanks to this) can we draw the same optics in a unlike way?
Source: https://petlja.github.io/TxtProgInPythonEng/03_PyGame/03_PyGame_11a_Drawing_CircleRectEllipse.html
Belum ada Komentar untuk "Draw a Circle or Ellipse"
Posting Komentar