Skip to content

Instantly share code, notes, and snippets.

@ssophwang
Created June 25, 2015 19:05
Show Gist options
  • Save ssophwang/811929a82f02e13f9c1c to your computer and use it in GitHub Desktop.
Save ssophwang/811929a82f02e13f9c1c to your computer and use it in GitHub Desktop.
hw2a.py
import canvas
from random import random
canvas.set_size(1000, 600)
canvas.draw_image('_backround', 0, 0, 1000, 600)
def draw_tree(rootx, rooty, trunk_height, leaf_height, leaf_width, trunk_width):
canvas.begin_path()
canvas.move_to(rootx-leaf_width/2, rooty+trunk_height)
canvas.add_line(rootx+leaf_width/2, rooty+trunk_height)
canvas.add_line(rootx, rooty+leaf_height+trunk_height)
canvas.close_path()
canvas.set_fill_color(0.00, 0.50, 0.00)
canvas.fill_path()
canvas.set_line_width(trunk_width)
canvas.set_stroke_color(0.50, 0.25, 0.15)
canvas.draw_line(rootx, rooty, rootx, rooty+trunk_height)
def draw_mushroom(center_x, center_y, radius):
import math
canvas.move_to(center_x-radius, center_y)
n = 64
for i in range(n):
canvas.add_quad_curve(center_x - math.cos(math.pi*i/n)*radius,
math.sin(math.pi*i/n)*radius + center_y,
center_x - math.cos(math.pi*(i+1)/n)*radius,
math.sin(math.pi*(i+1)/n)*radius + center_y)
canvas.close_path()
canvas.fill_path()
canvas.set_fill_color(1, 1, 1)
canvas.fill_ellipse(center_x, center_y+radius/6, radius/4, radius/3)
canvas.fill_ellipse(center_x-radius*0.7, center_y+radius*0.07, radius/2, radius/4)
canvas.fill_ellipse(center_x-radius*0.4, center_y+radius/2, radius/3, radius/3)
canvas.fill_ellipse(center_x+radius*0.5, center_y+radius*0.1, radius/4, radius/4)
canvas.fill_ellipse(center_x+radius*0.3, center_y+radius*0.5, radius/3, radius/4)
canvas.set_stroke_color(1.00, 1.00, 0.925)
canvas.set_line_width(radius/1.5)
canvas.draw_line(center_x, center_y-radius, center_x, center_y)
canvas.set_size(1000,600)
canvas.set_fill_color(random(), random(), random())
draw_mushroom(500, 200, 77)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment