Last active
August 29, 2015 13:57
-
-
Save wkta/9441738 to your computer and use it in GitHub Desktop.
#MonthOfCode day 7 - input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import sys # for exit function | |
import pygame | |
from pygame import Rect | |
# DEFINITIONS | |
################################################ | |
BLACK = ( 0, 0, 0) | |
DISP_COLOR=(230, 190, 230) #gray with a violet touch | |
DISP_COLOR_2=(69, 0, 82) #violet | |
DISP_COLOR_3=(222, 117, 75) #pastel orange | |
CELL_PX_SIZE = 32 | |
NB_CELLS_ROW = 16 | |
def displaysCell( x, y ): | |
if( cell_has_k_lightd_neighb(x,y, 3) ): | |
pygame.draw.rect(window, DISP_COLOR_3, | |
Rect(x*CELL_PX_SIZE+0, y*CELL_PX_SIZE+0, CELL_PX_SIZE, CELL_PX_SIZE) ) | |
return | |
if( cell_has_k_lightd_neighb(x,y, 2) ): | |
pygame.draw.rect(window, DISP_COLOR_2, | |
Rect(x*CELL_PX_SIZE+0, y*CELL_PX_SIZE+0, CELL_PX_SIZE, CELL_PX_SIZE) ) | |
return | |
pygame.draw.rect(window, DISP_COLOR, | |
Rect(x*CELL_PX_SIZE+0, y*CELL_PX_SIZE+0, CELL_PX_SIZE, CELL_PX_SIZE) ) | |
def detCorrepCellCoord( x_screen, y_screen ): | |
""" determining coordinates of the corresponding cell """ | |
corresp_x = 0 | |
for i in xrange(NB_CELLS_ROW): | |
if (i*CELL_PX_SIZE> x_screen): | |
break | |
corresp_x+=1 | |
corresp_y = 0 | |
for i in xrange(NB_CELLS_ROW): | |
if (i*CELL_PX_SIZE> y_screen): | |
break | |
corresp_y+=1 | |
return(corresp_x -1, corresp_y -1) | |
# for displaying lighted cells | |
def refreshScreen(): | |
window.fill( BLACK ) | |
for i in xrange(NB_CELLS_ROW ): | |
for j in xrange( NB_CELLS_ROW): | |
if( bool_matr[j][i] ): | |
displaysCell(j, i) | |
#draw it to the screen | |
pygame.display.flip() | |
def getCellState(x,y ): | |
if(x<0 or x>= NB_CELLS_ROW): | |
return False | |
if(y<0 or y>= NB_CELLS_ROW): | |
return False | |
return bool_matr[x][y] | |
def cell_has_k_lightd_neighb( x,y, k ): | |
cnt = 0 | |
cnt += ( 1 if getCellState( x-1,y ) else 0 ) | |
cnt += ( 1 if getCellState( x,y-1 ) else 0 ) | |
cnt += ( 1 if getCellState( x+1,y ) else 0 ) | |
cnt += ( 1 if getCellState( x,y+1 ) else 0 ) | |
return (cnt>=k ) | |
# MAIN PART | |
################################################ | |
# init. pygame libr; create the screen and diplays a help message in the console | |
pygame.init() | |
window = pygame.display.set_mode((CELL_PX_SIZE*NB_CELLS_ROW, CELL_PX_SIZE*NB_CELLS_ROW)) | |
print 'input by clicking, your goal is to guess rules that determine colors...' | |
# init. a boolean matrix; this aims at representing the state of a cellular automaton | |
bool_matr = list() | |
for i in xrange(NB_CELLS_ROW ): | |
line = list() | |
for j in xrange( NB_CELLS_ROW): | |
line.append( False ) | |
bool_matr.append( line) | |
# diplays the state and then loops for input handling | |
refreshScreen() | |
while True: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
sys.exit(0) | |
if event.type == pygame.MOUSEBUTTONDOWN : | |
clicked_cell_coords = detCorrepCellCoord( *pygame.mouse.get_pos() ) | |
bool_matr[ clicked_cell_coords[0] ][ clicked_cell_coords[1] ] = not \ | |
bool_matr[ clicked_cell_coords[0] ][ clicked_cell_coords[1] ] | |
refreshScreen() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment