# Scribble import simplegui # cells width = 15 height = 15 scale = 9 # pixels per cell def make_empty_grid(x, y): grid = [] for r in range(x): row = [] for c in range(y): row.append(0) grid.append(row) return grid # Handler to draw on canvas def draw(canvas): for x in range(width): for y in range(height): alive = world[x][y] col = 'black' if alive else 'white' try: # canvas.draw_circle(center_point, radius, line_width, line_color, fill_color) canvas.draw_circle([x * scale + scale//2, y * scale + scale//2], scale//2, 1, '#ccc', col) except Exception as e: # I don't get this! About one in 1000 says that col is not set. col = 'red' canvas.draw_circle([x * scale + scale//2, y * scale + scale//2], scale//2, 1, col, col) mouse_click_new_val = 1 def mouse_handler_click(position): global mouse_click_new_val x, y = position x = x // scale y = y // scale # 0 <--> 1 world[x][y] = 1 - world[x][y] mouse_click_new_val = world[x][y] def mouse_handler_drag(position): x, y = position x = x // scale y = y // scale world[x][y] = mouse_click_new_val def init(): global world world = make_empty_grid(width, height) def main(): # Create a frame and assign callbacks to event handlers frame = simplegui.create_frame("Scribble", width * scale, height * scale) frame.set_canvas_background('white') frame.set_draw_handler(draw) frame.set_mouseclick_handler(mouse_handler_click) frame.set_mousedrag_handler(mouse_handler_drag) init() # Start the frame animation frame.start() if __name__ == '__main__': main()