How to draw different shapes in pyleap?
Table of Contents
There are two drawing methods to draw shapes in pyleap:
- draw() – will draw complete graphic on the canvas.
- stroke() – will draw only the stroke line of the shape.
Now, let’s look into drawing different types of shapes in pyleap:
1. Line
Line(x1=100, y1=100, x2=200, y2=200, line_width=1, color= “orange”)
# OR
Line(100,100,200,200,1,"orange")
x1 = x-coordinate of the start point of the line
y1 = y-coordinate of the start point of the line
x2 = x-coordinate of the endpoint of the line
y2 = y-coordinate of the endpoint of the line
line_width = line width of the line
color = color of the line
2. Rectangle
Rectangle(x=100, y=100, w=100, h=50, color= “orange” )
# OR
Rectangle(100,100,100,50,"orange")
x= x-coordinate of lower-left vertex of the rectangle
y = y-coordinate of lower-left vertex of the rectangle
w = width of the rectangle
h = height of the rectangle
color = color of the rectangle
3. Circle
Circle(x=100, y=100, r=30, color= “orange”)
# OR
Circle(100,100,30,"orange")
x = x-coordinate of center of circle
y = y-coordinate of the center of the circle
r= radius of the circle
color = color of the circle
4. Ellipse
Ellipse(x=100, y=100, r_x=50, r_y=30, color= “orange” )
# OR
Ellipse(100,100,50,30,"orange")
x = x-coordinate of the center of the ellipse
y = y-coordinate of the center of the ellipse
r_x = radius along the x-axis of the ellipse
r_y = radius along the y-axis of the ellipse
color = color of the ellipse
5. Triangle
Triangle(x1, y1, x2, y2, x3, y3, color)
# OR
Triangle(100,100,300,300,400,400,"red")
x1 = x-coordinate of first vertex of triangle
y1 = y-coordinate of the first vertex of a triangle
x2 = x-coordinate of second vertex of triangle
y2 = y-coordinate of the second vertex of a triangle
x3 = x-coordinate of the third vertex of a triangle
y3 = y-coordinate of the third vertex of a triangle
color = color of the triangle
6. Polygon
Used to draw different types of shapes. Example hexagon, octagon, etc… which does not have a particular method in pyleap.
Polygon(x1,y1,x2,y2,x3,y3,…… xn, yn, color)
xn = x-coordinate of a vertex of the polygon
yn = y-coordinate of a vertex of the polygon
color = color of the polygon
Note: In Polygon(), you can create as many vertices as you prefer but you should provide the correct x and y coordinates of the vertices of the Polygon.
Project: Creative art with the message
I am sharing one of the projects done by Ms. Dechen Wangmo using a shape method in pyleap.
Output:
Code:
from pyleap import*
window.set_size(1000,700)
window.set_caption("Coding-Bhutan")
#background
bg1=Rectangle(0,0,1000,700,'bisque')
#table
table=Rectangle(0,20,1000,100,'bole')
#std1
s1_body=Ellipse(100,100,50,80,'tomato')
s1_head=Circle(100,200,20,'apricot')
s1_bangs1=Ellipse(95,215,3,10,'black')
s1_bangs1.rotation=110
s1_bangs2=Ellipse(105,215,3,10,'black')
s1_bangs2.rotation=80
s1_eye1=Ellipse(90,200,2,5,'black')
s1_eye2=Ellipse(110,200,2,5,'black')
s1_mouth=Ellipse(100,190,6,2,'black')
s1_gho1=Line(90,178,100,165,3,'black')
s1_gho2=Line(110,178,75,120,3,'black')
#std2
s2_hair=Ellipse(300,195,30,35,'black')
s2_bangs=Rectangle(280,210,40,10,'black')
s2_body=Ellipse(300,100,50,80,'ultramarine')
s2_head=Circle(300,200,20,'burlywood')
s2_eye1=Ellipse(290,200,2,5,'black')
s2_eye2=Ellipse(310,200,2,5,'black')
s2_mouth=Ellipse(300,190,6,2,'black')
s2_wonju1=Ellipse(295,170,5,10,'urobilin')
s2_wonju1.rotation=50
s2_wonju2=Ellipse(305,170,5,10,'urobilin')
s2_wonju2.rotation=140
#std3
s3_body=Ellipse(500,100,50,80,'stormcloud')
s3_head=Circle(500,200,20,'burlywood')
s3_bangs1=Ellipse(495,215,4,12,'black')
s3_bangs1.rotation=100
s3_eye1=Ellipse(490,200,2,5,'black')
s3_eye2=Ellipse(510,200,2,5,'black')
s3_mouth=Ellipse(500,190,6,2,'black')
s3_gho1=Line(490,178,500,165,3,'black')
s3_gho2=Line(510,178,475,120,3,'black')
#std4
s4_hair=Ellipse(700,195,30,35,'black')
s4_hair2=Ellipse(700,185,30,40,'black')
s4_bangs1=Ellipse(695,215,5,15,'black')
s4_bangs1.rotation=120
s4_bangs2=Ellipse(705,215,5,15,'black')
s4_bangs2.rotation=60
s4_body=Ellipse(700,100,50,80,'salmon')
s4_head=Circle(700,200,20,'burlywood')
s4_eye1=Ellipse(690,200,2,5,'black')
s4_eye2=Ellipse(710,200,2,5,'black')
s4_mouth=Ellipse(700,190,6,2,'black')
s4_wonju1=Ellipse(695,170,5,10,'shampoo')
s4_wonju1.rotation=50
s4_wonju2=Ellipse(705,170,5,10,'shampoo')
s4_wonju2.rotation=140
#std5
s5_body=Ellipse(900,100,50,80,'pistachio')
s5_head=Circle(900,200,20,'burlywood')
s5_hair1=Line(897,218,895,225,2,'black')
s5_hair2=Line(900,218,900,225,2,'black')
s5_hair3=Line(903,218,905,225,2,'black')
s5_eye1=Ellipse(890,200,2,5,'black')
s5_eye2=Ellipse(910,200,2,5,'black')
s5_mouth=Ellipse(900,190,6,2,'black')
s5_gho1=Line(890,178,900,165,3,'black')
s5_gho2=Line(910,178,875,120,3,'black')
#greenboard
gb1=Rectangle(50,250,900,420,'chocolate')
gb2=Rectangle(65,265,870,390,'amazon')
#text
hmq=Text('"We have always steered our coungry in the right direction. We have successfully navigated through great geo-political and socio-',70,640,11.5,'white','Eras Light ITC')
hmq2=Text('economic changes of the past. This decade requires us to yet again skillfully navigate the new digital and technological landscape."',70,628,11.5,'white','Eras Light ITC')
hmq3=Text('-His Majesty King Jigme Khesar Namgyal Wangchuck',310,615,10,'white','Eras Light ITC')
t1=Text("Why you should learn coding ?",280,570,20,'white','Stencil')
#pointsborders
pb1=Line(205,280,205,530,2,'white')
pb2=Line(395,280,395,530,2,'white')
pb3=Line(605,280,605,530,2,'white')
pb4=Line(765,280,765,530,2,'white')
#pointtitle
p1=Text('Coding is fun',70,520,15,'white','Bradley Hand ITC')
p2=Text('Coding boosts your',210,520,15,'white','Bradley Hand ITC')
p2a=Text('Thinking',250,505,15,'white','Bradley Hand ITC')
p3=Text('Coding expands your',400,520,15,'white','Bradley Hand ITC')
p3a=Text('Creativity',460,505,15,'white','Bradley Hand ITC')
p4=Text('Coding is the',620,520,15,'white','Bradley Hand ITC')
p4a=Text('FUTURE',635,505,15,'white','Bradley Hand ITC')
p5=Text('Coding supports',778,520,15,'white','Bradley Hand ITC')
p5a=Text('Mathematics',800,505,15,'white','Bradley Hand ITC')
#booktitle
p6a=Text('PYLEAP COURSE',388,45,15,'white','Bradley Hand ITC')
p6b=Text('ICT-Bhutan',420,65,15,'white','Bradley Hand ITC')
#pointdetails
pd1=Rectangle(85,300,100,200,'gray')
pd1a=Rectangle(95,380,80,100,'celeste')
pd1b=Line(110,315,110,360,10,'red')
pd1c=Line(87,337,135,337,10,'red')
pd1d=Circle(165,345,10,'yellow')
pd1e=Circle(155,320,10,'ao')
pd2a=Circle(300,420,60,'yellow')
pd2b=Line(300,365,300,350,50,'yellow')
pd2c=Line(300,350,300,335,50,'white')
pd2d=Line(275,345,325,345,2,'black')
pd2e=Line(275,340,325,340,2,'black')
pd2f=Line(300,350,275,420,2,'black')
pd2g=Line(300,350,315,420,2,'black')
pd2h=Line(270,415,320,415,1,'black')
pd3a=Rectangle(420,380,80,80,'blueberry')
pd3b=Circle(460,473,15,'blueberry')
pd3c=Circle(513,422,15,'blueberry')
pd3d=Rectangle(500,380,80,80,'red')
pd3e=Rectangle(420,300,80,80,'green')
pd3f=Rectangle(500,300,80,80,'yellow')
pd3g=Circle(540,390,15,'yellow')
pd3h=Circle(540,290,15,'yellow')
pd3i=Circle(490,340,15,'yellow')
pd4a=Rectangle(660,330,60,120,'black')
pd4b=Triangle(660,450,720,450,690,480,'black')
pd4c=Circle(690,445,10,'white')
pd4d=Circle(690,410,10,'white')
pd4e=Circle(690,380,10,'white')
pd4f=Polygon(652,295,726,300,720,330,660,330,652,295)
pd4g=Triangle(666,312,668,278,683,309)
pd4h=Triangle(678,297,699,278,712,300)
pd4i=Polygon(720,380,730,370,730,325,720,330,'white')
pd4j=Polygon(660,380,650,370,650,325,660,330,'white')
pd5a=Line(790,465,850,465,10,'byzantine')
pd5b=Line(840,440,900,400,8,'amaranth')
pd5c=Line(900,440,840,400,8,'amaranth')
pd5d=Line(800,345,850,345,8,'capri')
pd5e=Circle(825,365,7,'capri')
pd5f=Circle(825,325,7,'capri')
#laptop
l1=Rectangle(60,90,80,60,'charcoal')
l2=Rectangle(260,90,80,60,'charcoal')
l3=Rectangle(460,90,80,60,'charcoal')
l4=Rectangle(660,90,80,60,'charcoal')
l5=Rectangle(860,90,80,60,'charcoal')
la1=Triangle(140,115,150,115,140,90,'gray')
la2=Triangle(340,115,350,115,340,90,'gray')
la3=Triangle(540,115,550,115,540,90,'gray')
la4=Triangle(740,115,750,115,740,90,'gray')
la5=Triangle(940,115,950,115,940,90,'gray')
#books
book1=Rectangle(385,40,170,20,'coral')
book2=Rectangle(395,60,170,20,'cerulean')
def task6(dt):
bg1.draw()
gb1.draw()
gb2.draw()
s1_body.draw()
s1_head.draw()
s1_bangs1.draw()
s1_bangs2.draw()
s1_eye1.draw()
s1_eye2.draw()
s1_mouth.draw()
s1_gho1.draw()
s1_gho2.draw()
s2_hair.draw()
s2_body.draw()
s2_head.draw()
s2_bangs.draw()
s2_eye1.draw()
s2_eye2.draw()
s2_mouth.draw()
s2_wonju1.draw()
s2_wonju2.draw()
s3_body.draw()
s3_head.draw()
s3_bangs1.draw()
s3_eye1.draw()
s3_eye2.draw()
s3_mouth.draw()
s3_gho1.draw()
s3_gho2.draw()
s4_hair.draw()
s4_hair2.draw()
s4_body.draw()
s4_head.draw()
s4_bangs1.draw()
s4_bangs2.draw()
s4_eye1.draw()
s4_eye2.draw()
s4_mouth.draw()
s4_wonju1.draw()
s4_wonju2.draw()
s5_body.draw()
s5_head.draw()
s5_hair1.draw()
s5_hair2.draw()
s5_hair3.draw()
s5_eye1.draw()
s5_eye2.draw()
s5_mouth.draw()
s5_gho1.draw()
s5_gho2.draw()
table.draw()
hmq.draw()
hmq2.draw()
hmq3.draw()
t1.draw()
pb1.stroke()
pb2.stroke()
pb3.stroke()
pb4.stroke()
p1.draw()
p2.draw()
p2a.draw()
p3.draw()
p3a.draw()
p4.draw()
p4a.draw()
p5.draw()
p5a.draw()
pd1.draw()
pd1a.draw()
pd1b.draw()
pd1c.draw()
pd1d.draw()
pd1e.draw()
pd2a.draw()
pd2b.draw()
pd2c.draw()
pd2d.draw()
pd2e.draw()
pd2f.draw()
pd2g.draw()
pd2h.draw()
pd3a.draw()
pd3b.draw()
pd3c.draw()
pd3d.draw()
pd3e.draw()
pd3f.draw()
pd3g.draw()
pd3h.draw()
pd3i.draw()
pd4a.draw()
pd4b.draw()
pd4c.draw()
pd4d.draw()
pd4e.draw()
pd4f.draw()
pd4g.draw()
pd4h.draw()
pd4i.draw()
pd4j.draw()
pd5a.stroke()
pd5b.stroke()
pd5c.stroke()
pd5d.draw()
pd5e.draw()
pd5f.draw()
l1.draw()
l2.draw()
l3.draw()
l4.draw()
l5.draw()
la1.draw()
la2.draw()
la3.draw()
la4.draw()
la5.draw()
book1.draw()
book2.draw()
p6a.draw()
p6b.draw()
repeat(task6)
run()
Related article: