2
0

main.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from karel.stanfordkarel import *
  2. """
  3. Karel is imitating a somewhat erratically and trippy Mondriaan. (Kareldriaan)
  4. Description: https://codeinplace.stanford.edu/public/forum?post=29efc21e-7f6d-436f-b010-29b9003d20dd
  5. """
  6. """
  7. Helper functions
  8. """
  9. # Turn left thrice
  10. def turn_right():
  11. for i in range(3):
  12. turn_left()
  13. # Turn left twice
  14. def turn_around():
  15. for i in range(2):
  16. turn_left()
  17. # Walk if there is space
  18. def move_if_clear():
  19. if front_is_clear():
  20. move()
  21. # Continue walking where there is space
  22. def move_while_clear():
  23. while front_is_clear():
  24. move()
  25. """
  26. Mondriaan functions
  27. """
  28. # Do not face walls, and sometimes change direction
  29. def orient():
  30. # Perhaps turn
  31. if random(2): # 20% chance to turn
  32. if random(0.5): # 50% chance to go right or left
  33. turn_left()
  34. else:
  35. turn_right()
  36. # Make sure not to face an obstruction
  37. while front_is_blocked():
  38. turn_left()
  39. # Mondriaan colours paint
  40. def paint():
  41. """
  42. For animation purposes, paint before if statements.
  43. For making larger areas of the same colour, paint in else statements - but not for black and gray.
  44. """
  45. paint_corner("black")
  46. if random(0.7):
  47. paint_corner("gray")
  48. if random(0.7):
  49. paint_corner("red")
  50. if random(0.6):
  51. paint_corner("blue")
  52. if random(0.6):
  53. paint_corner("yellow")
  54. if random(0.6):
  55. paint_corner("white")
  56. if random(0.4):
  57. for i in range(4):
  58. move_if_clear()
  59. turn_left()
  60. paint_corner("white")
  61. turn_right()
  62. move_if_clear()
  63. else:
  64. if random(0.4):
  65. for i in range(4):
  66. move_if_clear()
  67. turn_left()
  68. paint_corner("yellow")
  69. turn_right()
  70. move_if_clear()
  71. else:
  72. if random(0.4):
  73. for i in range(4):
  74. move_if_clear()
  75. turn_left()
  76. paint_corner("blue")
  77. turn_right()
  78. move_if_clear()
  79. else:
  80. if random(0.4):
  81. for i in range(4):
  82. move_if_clear()
  83. turn_left()
  84. paint_corner("red")
  85. turn_right()
  86. move_if_clear()
  87. # Make art
  88. def trip_loop():
  89. for i in range(777): # Make 777 artistic loops
  90. paint()
  91. move_if_clear()
  92. orient()
  93. # Sign painting
  94. def signature():
  95. # Go to the uttermost south tile
  96. while not_facing_south():
  97. turn_left()
  98. move_while_clear()
  99. # Go to the uttermost east tile
  100. while not_facing_east():
  101. turn_left()
  102. move_while_clear()
  103. # Sign
  104. turn_around()
  105. for i in range(3):
  106. put_beeper()
  107. move()
  108. turn_around()
  109. # Main program
  110. def main():
  111. trip_loop()
  112. signature()
  113. # Initialization
  114. if __name__ == '__main__':
  115. main()