|
|
@@ -25,12 +25,22 @@ colour = colors.rgb2hex((get_random_colour()))
|
|
|
background = colors.rgb2hex((get_random_colour()))
|
|
|
"""
|
|
|
def generate_random_colour():
|
|
|
+ # Generate random RGB values
|
|
|
rgb_values = [random.randrange(256), random.randrange(256), random.randrange(256)]
|
|
|
+
|
|
|
+ # Convert to HEX values
|
|
|
hex_colours = [hex(value)[2:] for value in rgb_values] # Strip the '0x' prefix
|
|
|
+
|
|
|
+ # Add leading "0" for single digit values
|
|
|
for i in range(len(hex_colours)):
|
|
|
if len(str(hex_colours[i])) == 1:
|
|
|
hex_colours[i] = f"0{hex_colours[i]}"
|
|
|
- return '#' + ''.join(hex_colours)
|
|
|
+
|
|
|
+ # Add leading "#" to hex values
|
|
|
+ colour = '#' + ''.join(hex_colours)
|
|
|
+
|
|
|
+ logger.debug(f"Generated random colour: {colour}")
|
|
|
+ return colour
|
|
|
|
|
|
"""
|
|
|
Karel functions
|
|
|
@@ -39,6 +49,7 @@ Karel functions
|
|
|
def erase_asset(asset):
|
|
|
canvas = asset[1][0]
|
|
|
|
|
|
+ # Erase each shape of the asset
|
|
|
for shape in asset[0].values():
|
|
|
canvas.delete(shape)
|
|
|
logger.debug(f"Erased asset: {asset}")
|
|
|
@@ -59,15 +70,17 @@ def relative_move_asset(asset, x:int=0, y:int=0):
|
|
|
asset[1][1] += x
|
|
|
asset[1][2] += y
|
|
|
|
|
|
+ # Replace asset
|
|
|
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}")
|
|
|
+ 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
|
|
|
+ # East
|
|
|
if asset[1][4].startswith("east"):
|
|
|
if direction.lower() == "forward" or direction.lower() == "front":
|
|
|
asset[1][1] += amount
|
|
|
@@ -77,6 +90,8 @@ def orientation_move_asset(asset, direction, amount):
|
|
|
asset[1][2] += amount
|
|
|
if direction.lower() == "backward" or direction.lower() == "back":
|
|
|
asset[1][1] -= amount
|
|
|
+
|
|
|
+ # North
|
|
|
if asset[1][4].startswith("north"):
|
|
|
if direction.lower() == "forward" or direction.lower() == "front":
|
|
|
asset[1][2] -= amount
|
|
|
@@ -86,6 +101,8 @@ def orientation_move_asset(asset, direction, amount):
|
|
|
asset[1][1] += amount
|
|
|
if direction.lower() == "backward" or direction.lower() == "back":
|
|
|
asset[1][2] += amount
|
|
|
+
|
|
|
+ # West
|
|
|
if asset[1][4].startswith("west"):
|
|
|
if direction.lower() == "forward" or direction.lower() == "front":
|
|
|
asset[1][1] -= amount
|
|
|
@@ -95,6 +112,8 @@ def orientation_move_asset(asset, direction, amount):
|
|
|
asset[1][2] -= amount
|
|
|
if direction.lower() == "backward" or direction.lower() == "back":
|
|
|
asset[1][1] += amount
|
|
|
+
|
|
|
+ # South
|
|
|
if asset[1][4].startswith("south"):
|
|
|
if direction.lower() == "forward" or direction.lower() == "front":
|
|
|
asset[1][2] += amount
|
|
|
@@ -105,6 +124,7 @@ def orientation_move_asset(asset, direction, amount):
|
|
|
if direction.lower() == "backward" or direction.lower() == "back":
|
|
|
asset[1][2] -= amount
|
|
|
|
|
|
+ # Replace asset
|
|
|
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])
|
|
|
|
|
|
@@ -117,6 +137,7 @@ def absolute_move_asset(asset, x, y):
|
|
|
asset[1][1] = x
|
|
|
asset[1][2] = y
|
|
|
|
|
|
+ # Replace asset
|
|
|
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])
|
|
|
|
|
|
@@ -183,6 +204,7 @@ def rotate_asset(asset, direction):
|
|
|
else:
|
|
|
logger.error(f"Invalid rotation direction: {direction}")
|
|
|
|
|
|
+ # Repalce asset
|
|
|
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])
|
|
|
|
|
|
@@ -220,13 +242,14 @@ def recolour_asset(asset, colour:str="black", background:str="white"):
|
|
|
asset[1][5] = colour
|
|
|
asset[1][6] = background
|
|
|
|
|
|
+ # Replace asset
|
|
|
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: {asset} with {colour} and {background} to {new_karel}")
|
|
|
return new_karel
|
|
|
|
|
|
- # Draw a random Karel on the canvas
|
|
|
+# Draw a random Karel on the canvas
|
|
|
def generate_random_karel(canvas):
|
|
|
# Base the size on the shortest pane
|
|
|
if canvas.width < canvas.height:
|
|
|
@@ -234,6 +257,7 @@ def generate_random_karel(canvas):
|
|
|
else:
|
|
|
size = random.randint(1, canvas.height)
|
|
|
|
|
|
+ # Generate random values (Inside the canvas geometry)
|
|
|
centre_x = random.randint(int(size / 2), int(canvas.width - size / 2))
|
|
|
centre_y = random.randint(int(size / 2), int(canvas.height - size / 2))
|
|
|
orientation = random.choice(["east", "east-flipped", "north", "north-flipped", "west", "west-flipped", "south", "south-flipped"])
|
|
|
@@ -289,26 +313,29 @@ def draw_karel(
|
|
|
right_operand = operator.sub # Left / Top
|
|
|
bottom_operand = operator.add # Bottom / Right
|
|
|
|
|
|
- # Coords case
|
|
|
+ """ Coords case
|
|
|
+ X and Y need to be swapped in case of a North or South facing Karel, yet the original vlaues have to be returned. (To prevent diagonal relative translation)
|
|
|
+ """
|
|
|
if orientation.lower() == "north" or orientation.lower() == "north-flipped" or orientation.lower() == "south" or orientation.lower() == "south-flipped":
|
|
|
- temp_x = centre_x
|
|
|
- centre_x = centre_y
|
|
|
- centre_y = temp_x
|
|
|
- #pass
|
|
|
+ x = centre_y
|
|
|
+ y = centre_x
|
|
|
+ else:
|
|
|
+ x = centre_x
|
|
|
+ y = centre_y
|
|
|
|
|
|
# Borders
|
|
|
- left = left_operand(centre_x, size / 2)
|
|
|
- top = top_operand(centre_y, size / 2)
|
|
|
- right = right_operand(centre_x, size / 2)
|
|
|
- bottom = bottom_operand(centre_y, size / 2)
|
|
|
+ left = left_operand(x, size / 2)
|
|
|
+ top = top_operand(y, size / 2)
|
|
|
+ right = right_operand(x, size / 2)
|
|
|
+ bottom = bottom_operand(y, size / 2)
|
|
|
margin = size / 8
|
|
|
appendage_multiplier = margin * 0.6
|
|
|
|
|
|
# Body borders
|
|
|
- body_left = left_operand(centre_x, size / 3)
|
|
|
+ body_left = left_operand(x, size / 3)
|
|
|
body_top = top
|
|
|
- body_right = right_operand(centre_x, size / 3)
|
|
|
- body_bottom = bottom_operand(centre_y, size / 2.6)
|
|
|
+ body_right = right_operand(x, size / 3)
|
|
|
+ body_bottom = bottom_operand(y, size / 2.6)
|
|
|
eye_left = right_operand(body_left, margin)
|
|
|
eye_top = bottom_operand(body_top, margin)
|
|
|
eye_right = left_operand(body_right, margin)
|
|
|
@@ -321,7 +348,7 @@ def draw_karel(
|
|
|
right_diagonal_top = eye_right, body_top
|
|
|
right_diagonal_bottom = body_right, bottom_operand(body_top, margin)
|
|
|
bottom_right_corner = body_right, body_bottom
|
|
|
- mouth_left_corner = centre_x, bottom_operand(eye_bottom, margin)
|
|
|
+ mouth_left_corner = x, bottom_operand(eye_bottom, margin)
|
|
|
mouth_right_corner = eye_right, mouth_left_corner[1]
|
|
|
|
|
|
# Left appendage borders
|
|
|
@@ -335,7 +362,7 @@ def draw_karel(
|
|
|
leftFoot_bottom = bottom_operand(leftFoot_top, appendage_multiplier)
|
|
|
|
|
|
# Right appendage borders
|
|
|
- rightLeg_left = centre_x
|
|
|
+ rightLeg_left = x
|
|
|
rightLeg_top = body_bottom
|
|
|
rightLeg_right = right_operand(rightLeg_left, appendage_multiplier)
|
|
|
rightLeg_bottom = bottom
|
|
|
@@ -477,7 +504,6 @@ def draw_karel(
|
|
|
if not transparent:
|
|
|
# Meat
|
|
|
top_fill = canvas.create_rectangle(
|
|
|
-
|
|
|
top_left_corner[1], # Top Y
|
|
|
right_diagonal_top[0], # Right X
|
|
|
eye_top, # Bottom Y
|
|
|
@@ -493,7 +519,6 @@ def draw_karel(
|
|
|
outline = background
|
|
|
)
|
|
|
left_fill = canvas.create_rectangle(
|
|
|
-
|
|
|
eye_top,
|
|
|
eye_left,
|
|
|
left_diagonal_top[1],
|
|
|
@@ -502,7 +527,6 @@ def draw_karel(
|
|
|
background
|
|
|
)
|
|
|
right_fill = canvas.create_rectangle(
|
|
|
-
|
|
|
eye_top,
|
|
|
right_diagonal_bottom[0],
|
|
|
eye_bottom,
|
|
|
@@ -511,7 +535,6 @@ def draw_karel(
|
|
|
background
|
|
|
)
|
|
|
bottom_fill = canvas.create_rectangle(
|
|
|
-
|
|
|
eye_bottom,
|
|
|
bottom_right_corner[0],
|
|
|
bottom_right_corner[1],
|