| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 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()
|