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.

from pydraw import *
screen = Screen(800, 600)
screen.color(Color('gray'))
face = Oval(screen, 100, 0, 600, 600, Color('yellow'))
face.wedges(40)
eye1 = Oval(screen, 200, 200, 85.71, 100)
eye2 = Oval(screen, 500, 200, 85.71, 100)
eyebrow1 = Rectangle(screen, 157.14, 150, 100, 5, rotation=-16)
eyebrow2 = Rectangle(screen, 517.85, 150, 100, 5, rotation=16)
mouth = Oval(screen, 350, 400, 85.71, face.height() / 109)
screen.stop()
Example #1 Output

Note that we calculated those numbers using these formulas:

eye1 = Oval(screen, face.x() + face.width() / 6, 200, face.width() / 7, face.height() / 6)
eye2 = Oval(screen, (face.x() + face.width()) - (face.width() / 6) * 2, 200, face.width() / 7, face.height() / 6)
eyebrow1 = Rectangle(screen, eye1.x() - eye1.width() / 2, 200 - 50, 100, 5, rotation=-16)
eyebrow2 = Rectangle(screen, eye2.x() - 25 + eye2.width() / 2, 200 - 50, 100, 5, rotation=16)

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):

from pydraw import *
screen = Screen(800, 600)
screen.color(Color('gray'))
face = Oval(screen, 100, 0, 600, 600, Color('yellow'))
face.wedges(40)
eye1 = Oval(screen, face.x() + face.width() / 6, 200, face.width() / 7, face.height() / 6)
eye2 = Oval(screen, (face.x() + face.width()) - (face.width() / 6) * 2, 200, face.width() / 7, face.height() / 6)
eyebrow1 = Rectangle(screen, eye1.x() - eye1.width() / 2, 200 - 50, 100, 5, rotation=-16)
eyebrow2 = Rectangle(screen, eye2.x() - 25 + eye2.width() / 2, 200 - 50, 100, 5, rotation=16)
mouth = Oval(screen, 400 - 50, 400, face.width() / 7, face.height() / 5.5)
count = 0
fps = 30
running = True
while running:
seconds = count / fps
if 1.9 < seconds < 2:
eye1.height(1)
eye2.height(1)
if seconds >= 2.1:
eye1.height(face.height() / 6)
eye2.height(face.height() / 6)
count = 0
count += 1
screen.update()
screen.sleep(1 / fps)
Example #2 Output

Notice how we replaced:

screen.stop()

with this in order to create an animation loop:

fps = 30
running = True
while running:
screen.update()
screen.sleep(1 / fps)

Example #3: Input

Now we will move our emoji's mouth based on the mouse's position:

from pydraw import *
screen = Screen(800, 600)
screen.color(Color('gray'))
face = Oval(screen, 100, 0, 600, 600, Color('yellow'))
face.wedges(40)
eye1 = Oval(screen, face.x() + face.width() / 6, 200, face.width() / 7, face.height() / 6)
eye2 = Oval(screen, (face.x() + face.width()) - (face.width() / 6) * 2, 200, face.width() / 7, face.height() / 6)
eyebrow1 = Rectangle(screen, eye1.x() - eye1.width() / 2, 200 - 50, 100, 5, rotation=-16)
eyebrow2 = Rectangle(screen, eye2.x() - 25 + eye2.width() / 2, 200 - 50, 100, 5, rotation=16)
mouth_default = Location(400 - 50, 400)
mouth = Oval(screen, 400 - 50, 400, face.width() / 7, face.height() / 5.5)
def mousemove(location):
mouth.moveto(mouth_default.x() + (location.x() - mouth_default.x()) / 7,
mouth_default.y() + (location.y() - mouth_default.y()) / 7)
screen.listen()
count = 0
fps = 30
running = True
while running:
seconds = count / fps
if 1.9 < seconds < 2:
eye1.height(1)
eye2.height(1)
if seconds >= 2.1:
eye1.height(face.height() / 6)
eye2.height(face.height() / 6)
count = 0
count += 1
screen.update()
screen.sleep(1 / fps)
Example #3 Output

Notice how a new method was added:

def mousemove(location):
# code

And below that method we tell the screen to listen for input:

screen.listen()