2
0
tBKwtWS 3 өдөр өмнө
parent
commit
14ec376b0b
1 өөрчлөгдсөн 129 нэмэгдсэн , 0 устгасан
  1. 129 0
      main.py

+ 129 - 0
main.py

@@ -0,0 +1,129 @@
+from karel.stanfordkarel import *
+"""
+Karel is imitating a somewhat erratically and trippy Mondriaan. (Kareldriaan)
+Description: https://codeinplace.stanford.edu/public/forum?post=29efc21e-7f6d-436f-b010-29b9003d20dd
+"""
+
+"""
+Helper functions
+"""
+# Turn left thrice
+def turn_right():
+    for i in range(3):
+        turn_left()
+
+# Turn left twice
+def turn_around():
+    for i in range(2):
+        turn_left()
+
+# Walk if there is space
+def move_if_clear():
+    if front_is_clear():
+        move()
+
+# Continue walking where there is space
+def move_while_clear():
+    while front_is_clear():
+        move()
+
+"""
+Mondriaan functions
+"""
+# Do not face walls, and sometimes change direction
+def orient():
+    # Perhaps turn
+    if random(2): # 20% chance to turn
+        if random(0.5): # 50% chance to go right or left
+            turn_left()
+        else:
+            turn_right()
+    
+    # Make sure not to face an obstruction
+    while front_is_blocked():
+        turn_left()
+
+# Mondriaan colours paint:
+def paint():
+    """
+    For animation purposes, paint before if statements.
+    For making larger areas of the same colour, paint in else statements - but not for black and gray.
+    """
+    paint_corner("black")
+    if random(0.7):
+        paint_corner("gray")
+        if random(0.7):
+            paint_corner("red")
+            if random(0.6):
+                paint_corner("blue")
+                if random(0.6):
+                    paint_corner("yellow")
+                    if random(0.6):
+                        paint_corner("white")
+                        if random(0.4):
+                            for i in range(4):
+                                move_if_clear()
+                                turn_left()
+                                paint_corner("white")
+                            turn_right()                                
+                            move_if_clear()
+                    else:
+                        if random(0.4):
+                            for i in range(4):
+                                move_if_clear()
+                                turn_left()
+                                paint_corner("yellow")
+                            turn_right()
+                            move_if_clear()
+                else:
+                    if random(0.4):
+                        for i in range(4):
+                            move_if_clear()
+                            turn_left()
+                            paint_corner("blue")
+                        turn_right()
+                        move_if_clear()
+            else:
+                if random(0.4):
+                    for i in range(4):
+                        move_if_clear()
+                        turn_left()
+                        paint_corner("red")
+                    turn_right()
+                    move_if_clear()
+
+# Make art
+def trip_loop():
+    for i in range(777):    # Make 777 artistic loops
+        paint()
+        move_if_clear()
+        orient()
+
+# Sign painting
+def signature():
+    # Go to the uttermost south tile
+    while not_facing_south():
+        turn_left()
+    move_while_clear()
+
+    # Go to the uttermost east tile
+    while not_facing_east():
+        turn_left()
+    move_while_clear()
+
+    # Sign
+    turn_around()
+    for i in range(3):
+        put_beeper()
+        move()
+    turn_around()
+
+# Main program
+def main():
+    trip_loop()
+    signature()
+
+
+# Initialization
+if __name__ == '__main__':
+    main()