jueves, 20 de septiembre de 2018

Unidad 2: Dibujando en Python

Dibujando en Python (Tkinter, canvas)

Figura de Angry Birds
Se utilizo polígono, y óvalos para crear.

#Código par dibujo
from Tkinter import *


def poligono(ventana):
    panel = Canvas(width=500, height=450, bg="gray")
    panel.pack()
    # cara
    panel.create_polygon(200,80, 120, 240, 120, 260, 140, 300, 160, 310, 240, 310, 310, 290, 350, 280, 360, 250, 350, 230,
                         340, 210,250, 80,
                         width=1, fill="yellow", outline="black")
    #cejas
    panel.create_polygon(150, 190, 142, 200, 175, 225, 185, 210, width=1, fill="black", outline="black")
    panel.create_polygon(200, 210, 210, 220, 248, 205, 240, 190, width=1, fill="black", outline="black")
    #boca
    panel.create_polygon(150, 250, 150, 260, 185, 255, 170, 263,
                         173, 275, 205, 263, 200, 240, width=2, fill="orange", outline="black")
    #ojos
    panel.create_oval(210, 215, 255, 235, width=1, fill="white", outline="black")
    panel.create_oval(140, 218, 175, 235, width=1, fill="white", outline="black")
    #pupilas
    panel.create_oval(215, 215, 235, 235, width=1, fill="black", outline="black")
    panel.create_oval(140, 218, 160, 235, width=1, fill="black", outline="black")
    #cabello
    panel.create_polygon(200, 80, 210, 75, 210,60, 220, 50, 220, 70, 230, 50, 240, 50,
                         240, 60, 230, 70, 255, 65, 260, 70, 240, 78,
                         260, 80,230, 88, width=1, fill="black", outline="black")

ventana = Tk()
ventana.title("Poligonos")
ventana.config(bg="gray")
ventana.geometry("600x600")
boton = Button(ventana, text="Crear Poligono", command=lambda: poligono(ventana))
boton.pack()
ventana.mainloop()







CREANDO: 
Dibujo de Plantas vs Zombies: Girasol y Hongo 

Girasol

En la girasol se uso polígono, óvalos y arcos.Al dibujar con create_arc podemos cambiar el estilo de la figura, tenemos tres disponibles, style = PIESLICE, este es el asignado por defecto, así como CHORD en el que dibuja una linea recta que conecta los puntos finales del arco, y por ultimo tenemos ARC para dibujar solo el borde del de la rebanada, se establece con start y el angulo final sera la suma del angulo de inicio más el valor establecido por extent, ambos grados.

extent : anchura de la rebanada en grados
start :Angulo de inicio

from Tkinter import *
#codigo para girasol
def poligono(ventana):
    panel = Canvas(width=500, height=450, bg="skyblue")
    panel.pack()
    # cara
    panel.create_oval(120, 80, 320, 200,  width=2, fill="chocolate", outline="black")

    #ojos
    panel.create_oval(180, 100, 200, 130, width=1, fill="black", outline="black")
    panel.create_oval(240, 100, 260, 130,  width=1, fill="black", outline="black")

    #pupila
    panel.create_oval(185, 105, 190, 115, width=1, fill="white", outline="black")
    panel.create_oval(245, 105, 250, 115, width=1, fill="white", outline="black")

    #boca
    panel.create_arc(170, 140, 270, 180, extent = 180, style = CHORD ,start = 180, width=2, fill="black", outline="black")

    #sonrisa rosybrown
    panel.create_arc(155, 145, 190, 160, extent = 100, style = ARC, start = 180, width=2, fill="black", outline="black")
    panel.create_arc(255, 145, 290, 160, extent = 100, style = ARC, start = 255, width=2, fill="black", outline="black")
    panel.create_oval(195, 170, 245, 180,  width=2, fill="pink2", outline="black")

    #Petalos
    panel.create_arc(200, 30, 240, 130, extent = 180, start = 0, style = CHORD , width=2, fill="yellow2", outline="black")
    panel.create_arc(240, 40, 280, 130, extent = 180, start = 353, style = CHORD , width=2, fill="yellow2", outline="black")
    panel.create_arc(275, 58, 315, 145, extent = 185, start = 340, style=CHORD, width=2, fill="yellow2", outline="black")
    panel.create_arc(255, 100, 365, 140, extent = 190, start = 280, style=CHORD, width=2, fill="yellow2", outline="black")
    panel.create_arc(260, 130, 360, 170, extent = 180, start = 268, style=CHORD, width=2, fill="yellow2", outline="black")
    panel.create_arc(245, 200, 360, 165, extent = 220, start = 225, style=CHORD, width=2, fill="yellow2", outline="black")
    panel.create_arc(255, 163, 295, 233, extent=215, start=178, style=CHORD, width=2, fill="yellow2", outline="black")
    panel.create_arc(225, 155, 265, 245, extent=190, start=180, style=CHORD, width=2, fill="yellow2", outline="black")
    panel.create_arc(170, 155, 215, 245, extent=190, start=170, style=CHORD, width=2, fill="yellow2", outline="black")
    panel.create_arc(135, 158, 175, 233, extent=210, start=150, style=CHORD, width=2, fill="yellow2", outline="black")
    panel.create_arc(80, 155, 192, 195, extent=198, start=105, style=CHORD, width=2, fill="yellow2", outline="black")
    panel.create_arc(65, 120, 172, 160, extent=193, start=84, style=CHORD, width=2, fill="yellow2", outline="black")

    panel.create_arc(128, 50, 168, 133, extent=200, start=10, style=CHORD, width=2, fill="yellow2", outline="black")

    panel.create_arc(80, 83, 170, 123, extent=233, start=35, style=CHORD, width=2, fill="yellow2", outline="black")
    panel.create_arc(160, 40, 200, 125, extent=190, start=2, style=CHORD, width=2, fill="yellow2", outline="black")

    #tallo
    panel.create_polygon(210, 200, 210, 250, 220, 290, 235, 330, 230, 380, 230, 410,
                         245, 410, 245, 380, 250, 330, 235, 290, 225, 250, 225, 200,  width=1, fill="forestgreen", outline="black")
    #hoja derecha
    panel.create_arc(230, 250, 310, 300, extent = 270, start = 0, style=CHORD, width=2, fill="forestgreen", outline="darkgreen")
    panel.create_arc(230, 265, 300, 295, extent=105, start=75, style=ARC, width=2, outline="darkgreen")
    #hoja izquierda
    panel.create_arc(125, 250, 215, 300, extent=270, start=270, style=CHORD, width=2, fill="forestgreen", outline="darkgreen")
    panel.create_arc(130, 265, 215, 295, extent=105, start=0, style=ARC, width=2, outline="darkgreen")

    #fondo derecho
    panel.create_arc(245, 300, 360, 410, extent=180, start=0, style=CHORD, width=1, fill="darkgreen", outline="black")
    panel.create_arc(245, 325, 380, 440, extent = 170, start = 0, style=CHORD, width=2, fill="forestgreen", outline="darkgreen")
    panel.create_arc(240, 340, 365, 460, extent=105, start=35, style=ARC, width=2, fill="forestgreen", outline="darkgreen")
    #fondo izquierdo
    panel.create_arc(115, 310, 235, 400, extent=195, start=345, style=CHORD, width=1, fill="darkgreen", outline="black")
    panel.create_arc(105, 340, 235, 470, extent=170, start=20, style=CHORD, width=2, fill="forestgreen", outline="darkgreen")
    panel.create_arc(135, 350, 240, 490, extent=130, style=ARC, start=8, width=2, fill="black", outline="darkgreen")
    #fondo medio
    panel.create_arc(185, 345, 350, 500, extent = 180, start = 0, style=CHORD, width=2, fill="forestgreen", outline="darkgreen")
    panel.create_arc(185, 365, 315, 500, extent = 130, style = ARC, start = 40, width=2, outline="darkgreen")

ventana = Tk()
ventana.title("Poligonos")
ventana.config(bg="gray")
ventana.geometry("600x600")
boton = Button(ventana, text="Crear Poligono", command=lambda: poligono(ventana))
boton.pack()
ventana.mainloop()



Girasol (PvZ)



Hongo

Figuras create_ oval, polygon y arc.

from Tkinter import *
#codigo para hongo
def poligono(ventana):
    panel = Canvas(width=600, height=800, bg="linen")
    panel.pack()
    # cara
    panel.create_polygon(200, 300, 200, 450, 210, 470, 390, 470, 400, 450, 400, 300, width=2, fill="lemonchiffon", outline="black")
    #cabeza
    panel.create_arc(100, 80, 500, 520, extent = 180, start = 0, style = CHORD , width=2, fill="gold", outline="black")
    #manchas

    panel.create_oval(290, 100, 380, 140, width=1, fill="goldenrod", outline="khaki")#1
    panel.create_oval(170, 110, 275, 190, width=1, fill="goldenrod", outline="khaki")  #2
    panel.create_oval(130, 220, 180, 270, width=1, fill="goldenrod", outline="khaki")#3
    panel.create_oval(210, 200, 300, 290, width=1, fill="goldenrod", outline="khaki")#4
    panel.create_oval(320, 160, 350, 210, width=1, fill="goldenrod", outline="khaki")#5
    panel.create_oval(325, 235, 375, 275, width=1, fill="goldenrod", outline="khaki")#6
    panel.create_oval(390, 140, 465, 285, width=1, fill="goldenrod", outline="khaki")#7


    #ojos
    panel.create_oval(240, 320, 270, 390, width=1, fill="black", outline="black")
    panel.create_oval(330, 320, 360, 390, width=1, fill="black", outline="black")
    #pupila
    panel.create_oval(245, 325, 265, 355, width=1, fill="white", outline="black")
    panel.create_oval(335, 325, 355, 355, width=1, fill="white", outline="black")
    #boca
    panel.create_arc(260, 390, 340, 440, extent = 180, style = ARC, start = 180, width=2, fill="black", outline="black")

ventana = Tk()
ventana.title("Poligonos")
ventana.config(bg="gray")
ventana.geometry("600x600")
boton = Button(ventana, text="Crear Poligono", command=lambda: poligono(ventana))
boton.pack()
ventana.mainloop()







No hay comentarios:

Publicar un comentario