Quickstart Examples
Some examples to quickly begin creating amazing things with pyDraw!

Example #1: Drawing

We can draw a simple emoji onto the screen with Ovals. We will mimic: "
😯
" with a gray background.
1
from pydraw import *
2
​
3
screen = Screen(800, 600)
4
screen.color(Color('gray'))
5
​
6
face = Oval(screen, 100, 0, 600, 600, Color('yellow'))
7
face.wedges(40)
8
​
9
eye1 = Oval(screen, 200, 200, 85.71, 100)
10
eye2 = Oval(screen, 500, 200, 85.71, 100)
11
​
12
eyebrow1 = Rectangle(screen, 157.14, 150, 100, 5, rotation=-16)
13
eyebrow2 = Rectangle(screen, 517.85, 150, 100, 5, rotation=16)
14
​
15
mouth = Oval(screen, 350, 400, 85.71, face.height() / 109)
16
​
17
screen.stop()
Copied!
Example #1 Output
Note that we calculated those numbers using these formulas:
1
eye1 = Oval(screen, face.x() + face.width() / 6, 200, face.width() / 7, face.height() / 6)
2
eye2 = Oval(screen, (face.x() + face.width()) - (face.width() / 6) * 2, 200, face.width() / 7, face.height() / 6)
3
​
4
eyebrow1 = Rectangle(screen, eye1.x() - eye1.width() / 2, 200 - 50, 100, 5, rotation=-16)
5
eyebrow2 = Rectangle(screen, eye2.x() - 25 + eye2.width() / 2, 200 - 50, 100, 5, rotation=16)
Copied!

Example #2: Animation

We can animate our face now by adding an animation loop and having our eyes blink every 3 seconds (note that we've now switched to the proportions-based calculations):
1
from pydraw import *
2
​
3
screen = Screen(800, 600)
4
screen.color(Color('gray'))
5
​
6
face = Oval(screen, 100, 0, 600, 600, Color('yellow'))
7
face.wedges(40)
8
​
9
eye1 = Oval(screen, face.x() + face.width() / 6, 200, face.width() / 7, face.height() / 6)
10
eye2 = Oval(screen, (face.x() + face.width()) - (face.width() / 6) * 2, 200, face.width() / 7, face.height() / 6)
11
​
12
eyebrow1 = Rectangle(screen, eye1.x() - eye1.width() / 2, 200 - 50, 100, 5, rotation=-16)
13
eyebrow2 = Rectangle(screen, eye2.x() - 25 + eye2.width() / 2, 200 - 50, 100, 5, rotation=16)
14
​
15
mouth = Oval(screen, 400 - 50, 400, face.width() / 7, face.height() / 5.5)
16
​
17
count = 0
18
fps = 30
19
running = True
20
while running:
21
seconds = count / fps
22
​
23
if 1.9 < seconds < 2:
24
eye1.height(1)
25
eye2.height(1)
26
if seconds >= 2.1:
27
eye1.height(face.height() / 6)
28
eye2.height(face.height() / 6)
29
count = 0
30
count += 1
31
​
32
screen.update()
33
screen.sleep(1 / fps)
Copied!
Example #2 Output
Notice how we replaced:
1
screen.stop()
Copied!
with this in order to create an animation loop:
1
fps = 30
2
running = True
3
while running:
4
screen.update()
5
screen.sleep(1 / fps)
Copied!

Example #3: Input

Now we will move our emoji's mouth based on the mouse's position:
1
from pydraw import *
2
​
3
screen = Screen(800, 600)
4
screen.color(Color('gray'))
5
​
6
face = Oval(screen, 100, 0, 600, 600, Color('yellow'))
7
face.wedges(40)
8
​
9
eye1 = Oval(screen, face.x() + face.width() / 6, 200, face.width() / 7, face.height() / 6)
10
eye2 = Oval(screen, (face.x() + face.width()) - (face.width() / 6) * 2, 200, face.width() / 7, face.height() / 6)
11
​
12
eyebrow1 = Rectangle(screen, eye1.x() - eye1.width() / 2, 200 - 50, 100, 5, rotation=-16)
13
eyebrow2 = Rectangle(screen, eye2.x() - 25 + eye2.width() / 2, 200 - 50, 100, 5, rotation=16)
14
​
15
mouth_default = Location(400 - 50, 400)
16
mouth = Oval(screen, 400 - 50, 400, face.width() / 7, face.height() / 5.5)
17
​
18
def mousemove(location):
19
mouth.moveto(mouth_default.x() + (location.x() - mouth_default.x()) / 7,
20
mouth_default.y() + (location.y() - mouth_default.y()) / 7)
21
​
22
screen.listen()
23
​
24
count = 0
25
fps = 30
26
running = True
27
while running:
28
seconds = count / fps
29
​
30
if 1.9 < seconds < 2:
31
eye1.height(1)
32
eye2.height(1)
33
if seconds >= 2.1:
34
eye1.height(face.height() / 6)
35
eye2.height(face.height() / 6)
36
count = 0
37
count += 1
38
​
39
screen.update()
40
screen.sleep(1 / fps)
Copied!
Example #3 Output
Notice how a new method was added:
1
def mousemove(location):
2
# code
Copied!
And below that method we tell the screen to listen for input:
1
screen.listen()
Copied!
Last modified 7mo ago