Przeglądaj źródła

Update 'karel_asset.py'

Renamed karel variables to asset in anticipation of more shapes.
tBKwtWS 1 miesiąc temu
rodzic
commit
57e5984f52
1 zmienionych plików z 105 dodań i 47 usunięć
  1. 105 47
      karel_asset.py

+ 105 - 47
karel_asset.py

@@ -36,12 +36,12 @@ def generate_random_colour():
 Karel functions
 """
 # Erase a passed Karel from the canvas
-def erase_karel(karel):
-    canvas = karel[1][0]
+def erase_asset(asset):
+    canvas = asset[1][0]
 
-    for shape in karel[0].values():
+    for shape in asset[0].values():
         canvas.delete(shape)
-    logger.debug(f"Erased karel: {karel}")
+    logger.debug(f"Erased asset: {asset}")
 
 # Move a passed Karel on the canvas
 """
@@ -53,82 +53,140 @@ def move_karel(canvas, karel, x, y):
         canvas.move(shape, x, y)
     logger.debug(f"Moved: {karel} by {x} horizontally, {y} vertically")
 """
-def move_karel(karel, x:int=0, y:int=0):
+# Move a passed Karel relative to previous position
+def relative_move_asset(asset, x:int=0, y:int=0):
+    # Update coordinates in list
+    asset[1][1] += x
+    asset[1][2] += y
+
+    erase_asset(asset)
+    new_asset = draw_karel(asset[1][0], asset[1][1], asset[1][2], asset[1][3], asset[1][4], asset[1][5], asset[1][6], asset[1][7])
+
+    logger.debug(f"Moved: {asset} by {x} horizontally, {y} vertically, to {new_asset}")
+    return new_asset
+
+# Move a passed Karel in relation to her orientation
+def orientation_move_asset(asset, direction, amount):
+    # Direction translation
+    if asset[1][4].startswith("east"):
+        if direction.lower() == "forward" or direction.lower() == "front":
+            asset[1][1] += amount
+        if direction.lower() == "left":
+            asset[1][2] -= amount
+        if direction.lower() == "right":
+            asset[1][2] += amount
+        if direction.lower() == "backward" or direction.lower() == "back":
+            asset[1][1] -= amount
+    if asset[1][4].startswith("north"):
+        if direction.lower() == "forward" or direction.lower() == "front":
+            asset[1][2] -= amount
+        if direction.lower() == "left":
+            asset[1][1] -= amount
+        if direction.lower() == "right":
+            asset[1][1] += amount
+        if direction.lower() == "backward" or direction.lower() == "back":
+            asset[1][2] += amount
+    if asset[1][4].startswith("west"):
+        if direction.lower() == "forward" or direction.lower() == "front":
+            asset[1][1] -= amount
+        if direction.lower() == "left":
+            asset[1][2] += amount
+        if direction.lower() == "right":
+            asset[1][2] -= amount
+        if direction.lower() == "backward" or direction.lower() == "back":
+            asset[1][1] += amount
+    if asset[1][4].startswith("south"):
+        if direction.lower() == "forward" or direction.lower() == "front":
+            asset[1][2] += amount
+        if direction.lower() == "left":
+            asset[1][1] += amount
+        if direction.lower() == "right":
+            asset[1][1] -= amount
+        if direction.lower() == "backward" or direction.lower() == "back":
+            asset[1][2] -= amount
+
+    erase_asset(asset)
+    new_karel = draw_karel(asset[1][0], asset[1][1], asset[1][2], asset[1][3], asset[1][4], asset[1][5], asset[1][6], asset[1][7])
+
+    logger.debug(f"Moved: {asset} {direction} by {amount} as {new_karel}")
+    return new_karel
+
+# Move a passed Karel to new coordinates
+def absolute_move_karel(asset, x:int=0, y:int=0):
     # Update coordinates in list
-    karel[1][1] += x
-    karel[1][2] += y
+    asset[1][1] = x
+    asset[1][2] = y
 
-    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])
+    erase_asset(asset)
+    new_karel = draw_karel(asset[1][0], asset[1][1], asset[1][2], asset[1][3], asset[1][4], asset[1][5], asset[1][6], asset[1][7])
 
-    logger.debug(f"Moved: {karel} by {x} horizontally, {y} vertically, to {new_karel}")
+    logger.debug(f"Moved: {asset} by {x} horizontally, {y} vertically, to {new_karel}")
     return new_karel
 
 # Change the orientation of a passed Karel
-def rotate_karel(karel, direction):
+def rotate_karel(asset, direction):
     # Relative turn
     if direction == "right" or direction == "left":
         # East
-        if karel[1][4] == "east":
+        if asset[1][4] == "east":
             if direction == "right":
-                karel[1][4] = "south"
-                print(1)
+                asset[1][4] = "south"
             if direction == "left":
-                karel[1][4] = "north"
-        elif karel[1][4] == "east-flipped":
+                asset[1][4] = "north"
+        elif asset[1][4] == "east-flipped":
             if direction == "right":
-                karel[1][4] = "south-flipped"
+                asset[1][4] = "south-flipped"
             if direction == "left":
-                karel[1][4] = "north-flipped"
+                asset[1][4] = "north-flipped"
 
         # North
-        elif karel[1][4] == "north":
+        elif asset[1][4] == "north":
             if direction == "right":
-                karel[1][4] = "east"
+                asset[1][4] = "east"
             if direction == "left":
-                karel[1][4] = "west"
-        elif karel[1][4] == "north-flipped":
+                asset[1][4] = "west"
+        elif asset[1][4] == "north-flipped":
             if direction == "right":
-                karel[1][4] = "east-flipped"
+                asset[1][4] = "east-flipped"
             if direction == "left":
-                karel[1][4] = "west-flipped"
+                asset[1][4] = "west-flipped"
 
         # West
-        elif karel[1][4] == "west":
+        elif asset[1][4] == "west":
             if direction == "right":
-                karel[1][4] = "north"
+                asset[1][4] = "north"
             if direction == "left":
-                karel[1][4] = "soutch"
-        elif karel[1][4] == "west-flipped":
+                asset[1][4] = "soutch"
+        elif asset[1][4] == "west-flipped":
             if direction == "right":
-                karel[1][4] = "north-flipped"
+                asset[1][4] = "north-flipped"
             if direction == "left":
-                karel[1][4] = "south-flipped"
+                asset[1][4] = "south-flipped"
 
         # South
-        elif karel[1][4] == "south":
+        elif asset[1][4] == "south":
             if direction == "right":
-                karel[1][4] = "west"
+                asset[1][4] = "west"
             if direction == "left":
-                karel[1][4] = "east"
-        elif karel[1][4] == "north-flipped":
+                asset[1][4] = "east"
+        elif asset[1][4] == "north-flipped":
             if direction == "right":
-                karel[1][4] = "west-flipped"
+                asset[1][4] = "west-flipped"
             if direction == "left":
-                karel[1][4] = "east-flipped"
+                asset[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
+        asset[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])
+    erase_asset(asset)
+    new_karel = draw_karel(asset[1][0], asset[1][1], asset[1][2], asset[1][3], asset[1][4], asset[1][5], asset[1][6], asset[1][7])
 
-    logger.debug(f"Rotated Karel: {karel} by {direction} as {karel[1][4]} to {new_karel}")
+    logger.debug(f"Rotated Karel: {asset} by {direction} as {asset[1][4]} to {new_karel}")
     return new_karel
 
 # Recolour a passed Karel on the canvas
@@ -151,7 +209,7 @@ def recolour_karel(canvas, karel, colour:str="black", background:str="white"):
                 canvas.set_color(shape, colour)
                 canvas.set_outline_color(shape, colour)
 """
-def recolour_karel(karel, colour:str="black", background:str="white"):
+def recolour_asset(asset, colour:str="black", background:str="white"):
     # Random colours
     if colour == "random":
         colour = generate_random_colour()
@@ -159,13 +217,13 @@ def recolour_karel(karel, colour:str="black", background:str="white"):
         background = generate_random_colour()
 
     # Update colours in list
-    karel[1][5] = colour
-    karel[1][6] = background
+    asset[1][5] = colour
+    asset[1][6] = background
 
-    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])
+    erase_asset(asset)
+    new_karel = draw_karel(asset[1][0], asset[1][1], asset[1][2], asset[1][3], asset[1][4], asset[1][5], asset[1][6], asset[1][7])
 
-    logger.debug(f"Re-coloured Karel: {karel} with {colour} and {background} to {new_karel}")
+    logger.debug(f"Re-coloured Karel: {asset} with {colour} and {background} to {new_karel}")
     return new_karel
 
 # Draw a Karel
@@ -571,6 +629,6 @@ def draw_karel(
             "mouth": mouth
         }
 
-    arguments = [canvas, centre_x, centre_y, size, orientation, colour, background, transparent]
+    arguments = [canvas, centre_x, centre_y, size, orientation.lower(), colour, background, transparent]
     logger.debug(f"Created Karel: {shapes, arguments}")
     return [shapes, arguments]