# SenseHAT paint by Giles Booth / @blogmywiki
# www.suppertime.co.uk/blogmywiki
# This work is licensed under a Creative Commons
# Attribution-NonCommercial 4.0 International License.
from sense_hat import SenseHat
import pygame, sys
from pygame.locals import *
sense = SenseHat()
b = [0, 0, 0]
green = [0, 255, 0]
red = [255, 0, 0]
white = [255, 255, 255]
yellow = [255, 255, 0]
blue = [0, 0, 255]
def clearscreen():
sense.clear()
global pixels
pixels = [
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
]
clearscreen()
sense.set_pixels(pixels)
pygame.init()
screen = pygame.display.set_mode((300,170))
pygame.display.set_caption('SenseHAT paint')
font = pygame.font.Font(None,16)
screen.fill((200,200,200))
inklabel = font.render("Colour", 1, (0,0,0))
screen.blit(inklabel, (160,20))
keylabel = font.render("Keys: b", 1, (0,0,0))
screen.blit(keylabel, (160,36))
keylabel = font.render("w", 1, white)
screen.blit(keylabel, (205,36))
keylabel = font.render("r", 1, red)
screen.blit(keylabel, (220,36))
keylabel = font.render("g", 1, green)
screen.blit(keylabel, (235,36))
keylabel = font.render("l", 1, blue)
screen.blit(keylabel, (250,36))
keylabel = font.render("y", 1, yellow)
screen.blit(keylabel, (265,36))
pygame.draw.rect(screen, b, (160,55,45,15))
keylabel = font.render("Clear", 1, white)
screen.blit(keylabel, (163,55))
pygame.draw.rect(screen, b, (220,55,45,15))
keylabel = font.render("Dump", 1, white)
screen.blit(keylabel, (223,55))
def drawink():
pygame.draw.rect(screen, ink, (200,20,15,15))
pygame.display.update()
def drawgrid():
z = 0
xpos = 0
ypos = 20
for colours in pixels:
z = z + 1
if z == 9:
xpos = 0
ypos = ypos + 16
if z == 17:
xpos = 0
ypos = ypos + 16
if z == 25:
xpos = 0
ypos = ypos + 16
if z == 33:
xpos = 0
ypos = ypos + 16
if z == 41:
xpos = 0
ypos = ypos + 16
if z == 49:
xpos = 0
ypos = ypos + 16
if z == 57:
xpos = 0
ypos = ypos + 16
xpos = xpos + 16
pygame.draw.rect(screen, colours, (xpos,ypos,15,15))
pygame.display.update()
#pygame.draw.rect(windowObj, r, (10,10,20,20))
ink = white
drawgrid()
drawink()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
ink = white
elif event.key == pygame.K_b:
ink = b
elif event.key == pygame.K_y:
ink = yellow
elif event.key == pygame.K_r:
ink = red
elif event.key == pygame.K_g:
ink = green
elif event.key == pygame.K_l:
ink = blue
drawink()
## elif event.type == pygame.MOUSEMOTION:
## print (pygame.mouse.get_pos())
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
# 1st row
if (x>15 and x<30) and (y>20 and y<35):
pixels[0] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>31 and x<46) and (y>20 and y<35):
pixels[1] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>47 and x<62) and (y>20 and y<35):
pixels[2] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>63 and x<78) and (y>20 and y<35):
pixels[3] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>79 and x<94) and (y>20 and y<35):
pixels[4] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>95 and x<110) and (y>20 and y<35):
pixels[5] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>111 and x<126) and (y>20 and y<35):
pixels[6] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>127 and x<142) and (y>20 and y<35):
pixels[7] = ink
sense.set_pixels(pixels)
drawgrid()
# 2nd row
if (x>15 and x<30) and (y>36 and y<51):
pixels[8] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>31 and x<46) and (y>36 and y<51):
pixels[9] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>47 and x<62) and (y>36 and y<51):
pixels[10] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>63 and x<78) and (y>36 and y<51):
pixels[11] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>79 and x<94) and (y>36 and y<51):
pixels[12] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>95 and x<110) and (y>36 and y<51):
pixels[13] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>111 and x<126) and (y>36 and y<51):
pixels[14] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>127 and x<142) and (y>36 and y<51):
pixels[15] = ink
sense.set_pixels(pixels)
drawgrid()
# 3rd row
if (x>15 and x<30) and (y>52 and y<67):
pixels[16] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>31 and x<46) and (y>52 and y<67):
pixels[17] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>47 and x<62) and (y>52 and y<67):
pixels[18] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>63 and x<78) and (y>52 and y<67):
pixels[19] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>79 and x<94) and (y>52 and y<67):
pixels[20] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>95 and x<110) and (y>52 and y<67):
pixels[21] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>111 and x<126) and (y>52 and y<67):
pixels[22] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>127 and x<142) and (y>52 and y<67):
pixels[23] = ink
sense.set_pixels(pixels)
drawgrid()
# 4th row
if (x>15 and x<30) and (y>68 and y<83):
pixels[24] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>31 and x<46) and (y>68 and y<83):
pixels[25] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>47 and x<62) and (y>68 and y<83):
pixels[26] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>63 and x<78) and (y>68 and y<83):
pixels[27] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>79 and x<94) and (y>68 and y<83):
pixels[28] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>95 and x<110) and (y>68 and y<83):
pixels[29] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>111 and x<126) and (y>68 and y<83):
pixels[30] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>127 and x<142) and (y>68 and y<83):
pixels[31] = ink
sense.set_pixels(pixels)
drawgrid()
# 5th row
if (x>15 and x<30) and (y>84 and y<99):
pixels[32] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>31 and x<46) and (y>84 and y<99):
pixels[33] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>47 and x<62) and (y>84 and y<99):
pixels[34] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>63 and x<78) and (y>84 and y<99):
pixels[35] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>79 and x<94) and (y>84 and y<99):
pixels[36] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>95 and x<110) and (y>84 and y<99):
pixels[37] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>111 and x<126) and (y>84 and y<99):
pixels[38] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>127 and x<142) and (y>84 and y<99):
pixels[39] = ink
sense.set_pixels(pixels)
drawgrid()
# 6th row
if (x>15 and x<30) and (y>100 and y<115):
pixels[40] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>31 and x<46) and (y>100 and y<115):
pixels[41] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>47 and x<62) and (y>100 and y<115):
pixels[42] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>63 and x<78) and (y>100 and y<115):
pixels[43] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>79 and x<94) and (y>100 and y<115):
pixels[44] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>95 and x<110) and (y>100 and y<115):
pixels[45] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>111 and x<126) and (y>100 and y<115):
pixels[46] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>127 and x<142) and (y>100 and y<115):
pixels[47] = ink
sense.set_pixels(pixels)
drawgrid()
# 7th row
if (x>15 and x<30) and (y>116 and y<131):
pixels[48] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>31 and x<46) and (y>116 and y<131):
pixels[49] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>47 and x<62) and (y>116 and y<131):
pixels[50] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>63 and x<78) and (y>116 and y<131):
pixels[51] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>79 and x<94) and (y>116 and y<131):
pixels[52] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>95 and x<110) and (y>116 and y<131):
pixels[53] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>111 and x<126) and (y>116 and y<131):
pixels[54] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>127 and x<142) and (y>116 and y<131):
pixels[55] = ink
sense.set_pixels(pixels)
drawgrid()
# 8th row
if (x>15 and x<30) and (y>132 and y<147):
pixels[56] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>31 and x<46) and (y>132 and y<147):
pixels[57] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>47 and x<62) and (y>132 and y<147):
pixels[58] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>63 and x<78) and (y>132 and y<147):
pixels[59] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>79 and x<94) and (y>132 and y<147):
pixels[60] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>95 and x<110) and (y>132 and y<147):
pixels[61] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>111 and x<126) and (y>132 and y<147):
pixels[62] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>127 and x<142) and (y>132 and y<147):
pixels[63] = ink
sense.set_pixels(pixels)
drawgrid()
elif (x>160 and x<205) and (y>55 and y<70):
clearscreen()
drawgrid()
elif (x>220 and x<265) and (y>55 and y<70):
print
print pixels
pygame.quit()