Sunday, September 9, 2018

Sunday Funday - Code Kata - MS Paint program

Code Kata is a quick software challenge you can do to warm up your brain.  You are only supposed to work on the challenge for 30 minutes or less.  If you need more time, continue the challenge the next day.  I wanted to make a drawing program with an old school spray paint brush like MS paint.  I made one in less than 30 minutes and 50 lines of code (including white space and comments).  

I loved MS Paint.  I thought I was going to be an online artist using it.  But then I quickly found out its limitations (and my own).  The spray can only let you have one color.  I wanted 3 or more all mixed together for better blending.  So that is the focus of my code kata today.

I picked out three colors in the same spectrum that blended well.  Then every time I click and hole it starts adding dots in random points around my mouse.  The slower I drag my mouse the denser the dots like a spray can.  

Try out making up your own code kata, this one or send me your ideas.  I'll try them out as well!


Aaaaaannnnnd some source code.  I made the paint dots 4 pixels wide and my spray brush 32 coverage 32 pixels.  Copy and paste this into your own python file.

# Have a paintbrush you can control with your mouse that sprays like the old
# MS Paint spray paint brush.  But instead of one color, use three different
# colors all at once.  That way there is no dominant color.  This will help
# blend colors together.

from tkinter import *
import random

class View:

    def __init__(self, master):
        self.root = master
        self.frame = Frame(self.root, width = 800, height = 800)
        self.canvas = Canvas(self.frame, width = 800, height = 800)
        self.colors = [ '#113d84', '#68768e', '#609bff' ]  # blues
        self.brush_size = 16
        self.dot = 2
        self.num = len(self.colors) * 3
        self.draw = False
        self.canvas.bind('', self.mouse_button_1_click)
        self.canvas.bind('', self.mouse_button_1_release)
        self.canvas.bind('', self.mouse_button_1_motion)
        self.root.bind('', self.key_pressed)
        self.canvas.pack()
        self.frame.pack()
        
    def add_points(self, x, y):
        for i in range(0, self.num):
            x1 = random.randint(-self.brush_size, self.brush_size) + x
            y1 = random.randint(-self.brush_size, self.brush_size) + y
            self.canvas.create_oval(x1-self.dot, y1-self.dot, x1+self.dot, y1+self.dot, outline = self.colors[i%3], fill = self.colors[i%3], tags = 'dot')
        
    def mouse_button_1_click(self, event):
        self.draw=True
        self.add_points(event.x, event.y)
        
    def mouse_button_1_motion(self, event):
        if self.draw == True:
            self.add_points(event.x, event.y)

    def mouse_button_1_release(self, event):
        self.draw = False

    def key_pressed(self, event):
        if key == 'R':
            self.canvas.delete('dot')

master = Tk()
view = View(master)
master.mainloop()


No comments:

Post a Comment