]> git.cworth.org Git - zombocom-ai/blobdiff - interpret-cairo-to-svg.py
Reword Coda's final message
[zombocom-ai] / interpret-cairo-to-svg.py
index bc0c6b1053ab8eaf406ea95f5c55d7f33a534bf3..9c90702a631ab6a12ee75dffe3ae88863c4c4cdf 100755 (executable)
@@ -4,6 +4,9 @@ import cairo
 import tempfile
 import os
 import sys
+import gi
+gi.require_version('Rsvg', '2.0')
+from gi.repository import Rsvg
 
 import math
 import random as rand
@@ -26,10 +29,18 @@ if "import" in input:
 os.close(fd)
 os.chmod(filename, 0o644)
 
+if len(sys.argv) < 2:
+    sys.stderr.write("This script requires a single argument (value, 1 - 4)")
+    sys.exit(1)
+
+user = int(sys.argv[1])
+stderr = sys.stderr
+
 # Also delete our import for some more safety
 del tempfile
 del os
 del sys
+del gi
 
 # Note: We'll let the following "safe" imports remain:
 #
@@ -791,10 +802,44 @@ COLORS = {
     'yellowgreen': (0x9a/0xff, 0xcd/0xff, 0x32/0xff)
 }
 
+hint = [
+    "WAT", "ER ", "PLA", "NET"
+]
+
+code = [
+    "304", "570", "325", "6"
+]
+
+r = 0
+g = 0
+b = 0
+a = 1.0
+
 with cairo.SVGSurface(filename, 512, 512) as surface:
+
     cr = cairo.Context(surface);
+    cr.set_font_size(190)
+    cr.select_font_face("sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
+    # Setup RSVG rendering
+    rsvg = Rsvg.Handle()
+
+    answer_shown = False
+
     del cairo
+    del Rsvg
 
+    def random_within(max):
+        return max * random()
+
+    def wiggle():
+        delta = 200
+        cr.rel_curve_to(-delta/2 + random_within(delta),
+                        -delta/2 + random_within(delta),
+                        -delta/2 + random_within(delta),
+                        -delta/2 + random_within(delta),
+                        -delta/2 + random_within(delta),
+                        -delta/2 + random_within(delta))
     def arc(x, y, r, a1, a2):
         cr.arc(x, y, r, a1, a2)
 
@@ -804,18 +849,92 @@ with cairo.SVGSurface(filename, 512, 512) as surface:
     def fill():
         cr.fill()
 
+    def stroke():
+        cr.stroke()
+
     def set_color(color):
+        global r, g, b, a
         if color in COLORS:
             (r,g,b) = COLORS[color]
         else:
             (r,g,b) = (0,0,0)
-        cr.set_source_rgb(r, g, b)
+        cr.set_source_rgba(r, g, b, a)
 
-    cr.set_line_width(6)
+    def set_random_color():
+        global r, g, b, a
+        (r,g,b) = rand.choice(list(COLORS.values()))
+        cr.set_source_rgba(r, g, b, a)
+
+    def set_opacity(opacity):
+        global a
+        a = opacity
+        cr.set_source_rgba(r, g, b, a)
+
+    def move_to(x, y):
+        cr.move_to(x, y)
+
+    def line_to(x, y):
+        cr.line_to(x, y)
+
+    def line(x, y, dx, dy):
+        cr.move_to(x, y)
+        cr.rel_line_to(dx, dy)
+
+    def set_line_width(width):
+        cr.set_line_width(width)
+
+    def show_answer():
+        global answer_shown
+        answer_shown = True
+        # After the submitted code, draw the solution in white, (so it will
+        # only be clearly visible if they've made their drawing big enough).
+        cr.set_source_rgb(1, 1, 1)
+        cr.move_to(0,250)
+        cr.show_text(hint[user])
+        cr.set_font_size(240)
+        cr.move_to(0,480)
+        cr.show_text(code[user])
+
+    def fingers():
+        show_answer()
+        svg = rsvg.new_from_file("hand.svg")
+        cr.scale(0.2,0.2)
+        for i in range(16):
+            cr.save()
+            cr.translate(i%4 * 500, i/4 * 500)
+            cr.rotate(2 * math.pi * random())
+            svg.render_cairo(cr)
+            cr.restore()
+
+    def mouths():
+        show_answer()
+        svg = rsvg.new_from_file("mouth.svg")
+        cr.scale(0.5,0.5)
+        for i in range(16):
+            cr.save()
+            cr.translate(i%4 * 150, i/4 * 150)
+            cr.rotate(2 * math.pi * random())
+            svg.render_cairo(cr)
+            cr.restore()
+
+    def eyes():
+        show_answer()
+        svg = rsvg.new_from_file("eye.svg")
+        cr.scale(0.2,0.2)
+        for i in range(16):
+            cr.save()
+            cr.translate(i%4 * 600, i/4 * 600)
+            cr.rotate(2 * math.pi * random())
+            svg.render_cairo(cr)
+            cr.restore()
 
     # Run the submitted code
     exec(input)
 
+    # If the answer hasn't been shown yet, show it now
+    if not answer_shown:
+        show_answer()
+
 web_file = filename.removeprefix(OUTPUT_DIR_PREFIX)
 
 print(web_file)