generated from mwc/project_drawing
I had a lot of fun creating the animation of walking penguins. I initially didn't understand how the frames worked but reading the source code helped me a lot. While creating an animation I realized that I needed to edit my static drawing functions. I still need to edit them to firther my animation. For some strange reason when running my animation, i get the background and i need to hit enter before the animation starts playing. I don't understand why this occurs. Is it something I did or is that just how it runs? Commit 1: Created a static image of a penguin on an iceberg I was struggling how to draw an oval for my penguin. I needed to look up videos/read up how to do it. I also had to look up how to set the size of the image. I found it really difficult to just start the penguin, but once I got the shapes I felt more comfortable. Commit 2: Created an iteration of penguins Created a function penguins which iterates the drawing of the penguin until the end of the screen. I needed to do some debugging for this to work, I managed to solve it, but I don't know how I fixed the issue. For some strange reason it's drawing the first penguin twice and I don't know why. I need to figure that out. This part was much easier than drawing out the shapes.
86 lines
2.3 KiB
Python
86 lines
2.3 KiB
Python
# drawing.py
|
|
# ----------
|
|
# By Ilma Buraira
|
|
#
|
|
# Draws the image of penguins on an iceberg
|
|
# (Briefly describe what this program does.)
|
|
|
|
from turtle import *
|
|
from math import cos, sin, pi, radians
|
|
|
|
# ---------- Setup ----------
|
|
setup(1000, 600)
|
|
screen = Screen()
|
|
screen.title("Penguin on Ice (with real ovals)")
|
|
screen.bgcolor("white")
|
|
screen.tracer(0)
|
|
|
|
pensize(3)
|
|
pencolor("black")
|
|
|
|
# ---------- Helpers ----------
|
|
def move_to(x, y):
|
|
penup(); goto(x, y); pendown()
|
|
|
|
def filled_rect(x1, y1, x2, y2, fill, outline="black"):
|
|
pencolor(outline)
|
|
fillcolor(fill)
|
|
move_to(x1, y1)
|
|
begin_fill()
|
|
goto(x2, y1)
|
|
goto(x2, y2)
|
|
goto(x1, y2)
|
|
goto(x1, y1)
|
|
end_fill()
|
|
|
|
def draw_oval(cx, cy, rx, ry, tilt=0, fill=None, outline=None, steps=180, pen=None):
|
|
"""Parametric ellipse; steps controls smoothness."""
|
|
ang = radians(tilt)
|
|
def R(x, y): # rotate (x,y) by tilt
|
|
return (x*cos(ang) - y*sin(ang), x*sin(ang) + y*cos(ang))
|
|
pensize(pen if pen else 3)
|
|
pencolor(outline if outline else "")
|
|
fillcolor(fill if fill else "")
|
|
move_to(cx + rx, cy) # start at angle 0
|
|
if fill:
|
|
begin_fill()
|
|
for i in range(1, steps + 1):
|
|
theta = 2*pi*i/steps
|
|
x, y = rx*cos(theta), ry*sin(theta)
|
|
xr, yr = R(x, y)
|
|
goto(cx + xr, cy + yr)
|
|
if fill:
|
|
end_fill()
|
|
|
|
def poly(points, fill=None):
|
|
if fill: fillcolor(fill); begin_fill()
|
|
move_to(*points[0])
|
|
for p in points[1:]:
|
|
goto(*p)
|
|
goto(*points[0])
|
|
if fill: end_fill()
|
|
|
|
def right_triangle(ax, ay, leg1, leg2, angle=0, fill=None):
|
|
"""Right angle at (ax, ay). leg1 along 'angle' degrees, leg2 perpendicular."""
|
|
a = radians(angle)
|
|
B = (ax + leg1*cos(a), ay + leg1*sin(a)) # along angle
|
|
C = (ax - leg2*sin(a), ay + leg2*cos(a)) # 90° to the left
|
|
poly([(ax, ay), B, C], fill=fill)
|
|
|
|
# ---------- Scene ----------
|
|
# Ocean (bottom half)
|
|
filled_rect(-500, -100, 500, -300, fill="#3d44c6", outline="#3d44c6")
|
|
|
|
# Iceberg (white with light gray outline)
|
|
pensize(4)
|
|
filled_rect(-500, -60, 120, -150, fill="white", outline="#7f7f7f")
|
|
|
|
def background():
|
|
filled_rect(-500, -100, 500, -300, fill="#3d44c6", outline="#3d44c6")
|
|
|
|
# Iceberg (white with light gray outline)
|
|
pensize(4)
|
|
filled_rect(-500, -60, 120, -150, fill="white", outline="#7f7f7f")
|
|
|
|
|