From 321b9403e5690af32a3d5717cca9fff51b1523fd Mon Sep 17 00:00:00 2001 From: root Date: Fri, 21 May 2010 12:49:26 +0000 Subject: Add Matrix GUI --- diff --git a/color.py b/color.py new file mode 100755 index 0000000..b685e78 --- /dev/null +++ b/color.py @@ -0,0 +1,159 @@ +#!/usr/bin/env python + +import os, pygame,math +import pygame.font, pygame.event, pygame.draw, string +from matriz import * +from pygame.locals import * +from math import sin, cos, pi, sqrt + +class pyGrafic: + def __init__(self, matriz): + self.matriz=matriz + self.filas=matriz.Filas + self.columnas=matriz.Columnas + + def init(self): + #pygame.init() + self.screen = pygame.display.set_mode((600, 600),1) + pygame.display.set_caption('Pintando Fracciones') + self.cargaImgs() + self.dibujar() + self.gridRect = pygame.Rect(0,0,50,45) + self.palet() + self.color=self.back + self.colorName='white' + + def cargaImgs(self): + self.purple= pygame.image.load("./purple.png").convert() + self.purple.set_colorkey((0x80, 0x00, 0x80), RLEACCEL) + + self.brown= pygame.image.load("./brown.png").convert() + self.brown.set_colorkey((0x80, 0x00, 0x80), RLEACCEL) + + self.pink= pygame.image.load("./pink.png").convert() + self.pink.set_colorkey((0x80, 0x00, 0x80), RLEACCEL) + + self.orange= pygame.image.load("./orange.png").convert() + self.orange.set_colorkey((0x80, 0x00, 0x80), RLEACCEL) + + self.gray= pygame.image.load("./gray.png").convert() + self.gray.set_colorkey((0x80, 0x00, 0x80), RLEACCEL) + + self.yellow= pygame.image.load("./yellow.png").convert() + self.yellow.set_colorkey((0x80, 0x00, 0x80), RLEACCEL) + + self.blue = pygame.image.load("./blue.png").convert() + self.blue.set_colorkey((0x80, 0x00, 0x80), RLEACCEL) + + self.green = pygame.image.load("./green.png").convert() + self.green.set_colorkey((0x80, 0x00, 0x80), RLEACCEL) + + self.red = pygame.image.load("./red.png").convert() + self.red.set_colorkey((0x80, 0x00, 0x80), RLEACCEL) + + self.back = pygame.image.load("./back.png").convert() + self.back.set_colorkey((0x80, 0x00, 0x80), RLEACCEL) + + def dibujar(self): + self.mapimg = pygame.Surface((600,600),1) + self.mapimg= self.mapimg.convert() + self.mapimg.fill((0,0,0)) + + for y in range(self.columnas): + for x in range(self.filas): + pixelX,pixelY=x*38,y*41 + self.mapimg.blit(self.back,(pixelX,pixelY)) + + def setcolor(self, x, y): + if(x > 561): + if(y>=0 and y<41): + self.color=self.red + self.colorName='red' + if(y>=41 and y<82): + self.color=self.green + self.colorName='green' + if(y>=82 and y<123): + self.color=self.blue + self.colorName='blue' + if(y>=123 and y<164): + self.color=self.brown + self.colorName='brown' + if(y>=164 and y<205): + self.color=self.purple + self.colorName='purple' + if(y>=205 and y<246): + self.color=self.yellow + self.colorName='yellow' + if(y>=246 and y<287): + self.color=self.pink + self.colorName='pink' + if(y>=287 and y<328): + self.color=self.orange + self.colorName='orange' + if(y>=328 and y<349): + self.color=self.gray + self.colorName='gray' + + c=-1 + f=-1 + for fila in range(self.filas+1): + if (x<=(fila*38)): + f=fila-1 + print "columna:" + str(f) + break + + for columna in range(self.columnas+1): + if (y<=(columna*41)): + c=columna-1 + print "fila:" + str(c) + break + + if c-1 and f>-1: + self.matriz.Tabla[c][f].PintarCelda(self.colorName) + print "Color:" + self.matriz.Tabla[c][f].ObtenerColor() + + c=c*41 + f=f*38 + if (c>-1 and f>-1): + self.mapimg.blit(self.color,(f,c)) + pygame.display.flip() + + def palet (self): + self.mapimg.blit(self.red,(562,0)) + self.mapimg.blit(self.green,(562,41)) + self.mapimg.blit(self.blue,(562,82)) + self.mapimg.blit(self.brown,(562,123)) + self.mapimg.blit(self.purple,(562,164)) + self.mapimg.blit(self.yellow,(562,205)) + self.mapimg.blit(self.pink,(562,246)) + self.mapimg.blit(self.orange,(562,287)) + self.mapimg.blit(self.gray,(562,328)) + + def loop(self): + pygame.init() + self.init() + while 1: + for event in pygame.event.get(): + if event.type == QUIT: + return + elif event.type == KEYDOWN: + if event.key == K_ESCAPE: + return + elif event.type is MOUSEBUTTONDOWN: + if event.button == 1: + self.setcolor(event.pos[0],event.pos[1]) + + self.screen.blit(self.mapimg, (0, 0)) + pygame.display.flip() + +def main(): + + pygame.init() + matriz=CrearMatriz(60) + #se inicia el controlador de pygame + + objeto=pyGrafic(matriz) + objeto.loop() + +#se llama a la funcion main cuando se ejecute el scrip +if __name__ == '__main__': main() diff --git a/images/back.png b/images/back.png new file mode 100755 index 0000000..ac2df99 --- /dev/null +++ b/images/back.png Binary files differ diff --git a/images/blue.png b/images/blue.png new file mode 100755 index 0000000..0c01dfd --- /dev/null +++ b/images/blue.png Binary files differ diff --git a/images/brown.png b/images/brown.png new file mode 100755 index 0000000..c81b76e --- /dev/null +++ b/images/brown.png Binary files differ diff --git a/images/cursor.png b/images/cursor.png new file mode 100755 index 0000000..1b2b0fa --- /dev/null +++ b/images/cursor.png Binary files differ diff --git a/images/gray.png b/images/gray.png new file mode 100755 index 0000000..6468cc3 --- /dev/null +++ b/images/gray.png Binary files differ diff --git a/images/green.png b/images/green.png new file mode 100755 index 0000000..7795b33 --- /dev/null +++ b/images/green.png Binary files differ diff --git a/images/orange.png b/images/orange.png new file mode 100755 index 0000000..457f0ce --- /dev/null +++ b/images/orange.png Binary files differ diff --git a/images/pink.png b/images/pink.png new file mode 100755 index 0000000..f619ac2 --- /dev/null +++ b/images/pink.png Binary files differ diff --git a/images/purple.png b/images/purple.png new file mode 100755 index 0000000..645b3ff --- /dev/null +++ b/images/purple.png Binary files differ diff --git a/images/red.png b/images/red.png new file mode 100755 index 0000000..835e5a9 --- /dev/null +++ b/images/red.png Binary files differ diff --git a/images/yellow.png b/images/yellow.png new file mode 100755 index 0000000..ee3772a --- /dev/null +++ b/images/yellow.png Binary files differ diff --git a/matrix.py b/matrix.py new file mode 100755 index 0000000..2506229 --- /dev/null +++ b/matrix.py @@ -0,0 +1,85 @@ +from math import sqrt + +#Cell: represents a single cell of the matrix +class Cell: + def __init__(self): + self.Color = 'white' + + def GetColor(self): + return self.Color + + def PaintCell(self, color): + self.Color = color + +#Matrix: represent the cell's matrix. Defines constructor +class Matrix: + def __init__(self, rows, columns): + self.Rows = rows + self.Columns = columns + self.Values = [] + for i in range(rows): + a=[0]*columns + self.Values.append(a) + for i in range(rows): + for j in range(columns): + self.Values[i][j] = Cell() + +#Cells: Number of matrix's cells. +def CreateMatrix(Cells): + + #Generate the number of rows and columns + #so matrix can be similar as possible to a square matrix + + root = int(sqrt(Cells)) + l = list(range(1,root+1)) + l.reverse() + for i in l: + if((Cells%i)==0): + rows=i + break + columns = Cells / rows + + #Got the matrix's dimension + + matrix = Matrix(rows,columns) + return matrix; + +#Parameters: matrix, list of colors, and how many cells has to be painted in each color +#Returns: True if the painting is correct, False if is not +def CheckMatrix(matrix, colors, number_color): + painted = number_color + + #Each color got no cells painted initially + for item in painted: + item = 0 + + rows = matriz.rows + columns = matriz.columns + + #Walk over the matrix + for i in range(rows): + for j in range(columns): + #Walk over the list of colors + cont=0 + for color in colors: + if matriz.Cells[i][j].GetColor() == color: + painted[cont]=painted[cont] + 1 + + cont=cont+1 + + #Check + #Controlar si la cantidad de celdas pintadas por color coinciden + #con las cantidades solicitadas + success=True + cont=0 + for item in painted: + if painted[cont] != number_color[cont]: + success=False + break + + cont=cont+1 + + return success + +#probar que funcione +#CrearMatriz(24) -- cgit v0.9.1