Parcourir la source

+rotation & fix move

tBKwtWS il y a 19 heures
Parent
commit
91dd39124d
1 fichiers modifiés avec 77 ajouts et 12 suppressions
  1. 77 12
      karel_asset.py

+ 77 - 12
karel_asset.py

@@ -12,7 +12,9 @@ Helper functions
 """
 # Return some hex colour
 """
+Waiting for Stanford to fix a bug in the environment
 CIP bug: https://codeinplace.stanford.edu/cip6/report?post=ac7c4ffd-1f90-475a-b4ee-2e5b73c5348f
+
 from matplotlib import colors
 import decimal
 
@@ -52,18 +54,83 @@ def move_karel(canvas, karel, dx, dy):
     logger.debug(f"Moved: {karel} by {dx} horizontally, {dy} vertically")
 """
 def move_karel(karel, dx:int=0, dy:int=0):
-    canvas = karel[1][0]
-
-    erase_karel(karel)
-
     # Update coordinates in list
     karel[1][1] += dx
     karel[1][2] += dy
 
-    new_karel = draw_karel(karel[1][0], )
+    erase_karel(karel)
+    new_karel = draw_karel(karel[1][0], karel[1][1], karel[1][2], karel[1][3], karel[1][4], karel[1][5], karel[1][6], karel[1][7])
+
     logger.debug(f"Moved: {karel} by {dx} horizontally, {dy} vertically, to {new_karel}")
     return new_karel
 
+# Change the orientation of a passed Karel
+def rotate_karel(karel, direction):
+    # Relative turn
+    if direction == "right" or direction == "left":
+        # East
+        if karel[1][4] == "east":
+            if direction == "right":
+                karel[1][4] = "south"
+                print(1)
+            if direction == "left":
+                karel[1][4] = "north"
+        elif karel[1][4] == "east-flipped":
+            if direction == "right":
+                karel[1][4] = "south-flipped"
+            if direction == "left":
+                karel[1][4] = "north-flipped"
+
+        # North
+        elif karel[1][4] == "north":
+            if direction == "right":
+                karel[1][4] = "east"
+            if direction == "left":
+                karel[1][4] = "west"
+        elif karel[1][4] == "north-flipped":
+            if direction == "right":
+                karel[1][4] = "east-flipped"
+            if direction == "left":
+                karel[1][4] = "west-flipped"
+
+        # West
+        elif karel[1][4] == "west":
+            if direction == "right":
+                karel[1][4] = "north"
+            if direction == "left":
+                karel[1][4] = "soutch"
+        elif karel[1][4] == "west-flipped":
+            if direction == "right":
+                karel[1][4] = "north-flipped"
+            if direction == "left":
+                karel[1][4] = "south-flipped"
+
+        # South
+        elif karel[1][4] == "south":
+            if direction == "right":
+                karel[1][4] = "west"
+            if direction == "left":
+                karel[1][4] = "east"
+        elif karel[1][4] == "north-flipped":
+            if direction == "right":
+                karel[1][4] = "west-flipped"
+            if direction == "left":
+                karel[1][4] = "east-flipped"
+    
+    # Absolute rotation
+    elif direction == "east" or direction == "east-flipped" or direction == "north" or direction == "north-flipped" or direction == "west" or direction == "west-flipped" or direction == "south" or direction == "south-flipped":
+        karel[1][4] = direction
+    
+    # Syntax error
+    else:
+        logger.error(f"Invalid rotation direction: {direction}")
+
+    erase_karel(karel)
+    new_karel = draw_karel(karel[1][0], karel[1][1], karel[1][2], karel[1][3], karel[1][4], karel[1][5], karel[1][6], karel[1][7])
+
+    logger.debug(f"Rotated Karel: {karel} by {direction} as {karel[1][4]} to {new_karel}")
+    return new_karel
+
 # Recolour a passed Karel on the canvas
 """
 Waiting for Stanford to fix a bug in the environment
@@ -112,16 +179,16 @@ def draw_karel(
         background:str="white",
         transparent:bool=False
     ):
-
+    
+    # Body constants
+    MARGIN = size / 8
+    APPENDAGE_MULTIPLIER = MARGIN * 0.6
+    
     # Random colours
     if colour == "random":
         colour = generate_random_colour()
     if background == "random":
         background = generate_random_colour()
-    
-    # Body constants
-    MARGIN = size / 8
-    APPENDAGE_MULTIPLIER = MARGIN * 0.6
 
     ''' Flipper case
     In order to be able to flip Karel, the operands in the forumlas must be able to switch around.
@@ -156,7 +223,6 @@ def draw_karel(
         centre_y = temp_x
         #pass
 
-
     # Borders
     left = left_operand(centre_x, size / 2)
     top = top_operand(centre_y, size / 2)
@@ -205,7 +271,6 @@ def draw_karel(
     rigthFoot_right = right_operand(rightLeg_right, appendage_multiplier)
     rightFoot_bottom = bottom
 
-
     # Draw Karel
     match orientation.lower():
         case "east" | "east-flipped" | "west" | "west-flipped":