瀏覽代碼

+rotation & fix move

tBKwtWS 19 小時之前
父節點
當前提交
91dd39124d
共有 1 個文件被更改,包括 77 次插入12 次删除
  1. 77 12
      karel_asset.py

+ 77 - 12
karel_asset.py

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