t = codesters.Teacher()
tm = TestManager()
""" ONLY CHANGE WHAT IS BETWEEN THE GREEN DASHES:
---------------------------------------------------------------"""
# This array represents the board.
# 1's are blocks and 0's are open pathways.
maze_data = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 0, 0, 1, 1],
[1, 1, 1, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
]
# This is where the turtle starts.
start_position = (1, 10)
# This is where the goal is.
goal_position = (9, 2)
# This is the direction the turtle points to begin with
start_direc = 0
# This is the scale factor for the blocks.
block_size = 50
end_result = "incomplete"
# This function iterates through the maze_data array and draws the squares.
def draw_board():
board = maze_data
for j in range(len(board)):
for i in range (len(board[j])):
if board[j][i] == 1:
block_x = (i + 0.5) * block_size - 250
block_y = 250 - (j + 0.5) * block_size
block = codesters.Square(block_x, block_y, block_size, "blue", "black")
def create_goal():
goal_x = (goal_position[0] - 0.5) * block_size - 250
goal_y = 250 - (goal_position[1] - 0.5) * block_size
goal_block = codesters.Square(goal_x, goal_y, block_size, "gold", "black")
draw_board()
create_goal()
txt = codesters.Text("Guide the turtle to the gold box.", 0, 225, "white")
sprite = codesters.Sprite("turtle1")
sprite.set_size(0.3)
sprite.set_speed(2)
# This function puts the turtle at the starting position and rotation.
def send_sprite_home(t):
t.set_x((start_position[0] - 0.5 ) * block_size - 250)
t.set_y(250 - (start_position[1] - .5) * block_size)
t.set_rotation(start_direc)
send_sprite_home(sprite)
collision_state = 0
# This defines what happens when the turtle reaches the goal.
def collision(sprite, hit_sprite):
global collision_state
global goal_position
global block_size
color = hit_sprite.get_color()
if color == "gold":
goal_x = (goal_position[0] - 0.5) * block_size - 250
goal_y = 250 - (goal_position[1] - 0.5) * block_size
sprite.glide_to(goal_x, goal_y)
sprite.reset_animation()
stage.wait(.5)
hit_sprite.set_color("yellow")
# stage.remove_sprite(sprite)
stage.remove_sprite(txt)
if collision_state < 1:
tm.display_success_message("Great job!")
collision_state += 1
elif color == "blue":
sprite.reset_animation()
hit_sprite.set_color("lightblue")
send_sprite_home(sprite)
stage.remove_sprite(txt)
if collision_state < 1:
tm.display_failure_message("You hit a block. Try again.")
collision_state += 1
# txt.set_text("You hit a block. Try Again")
sprite.event_collision(collision)
sprite.pen_down()
for counter in range(3):
sprite.move_forward(50)
sprite.turn_left(90)
sprite.move_forward(50)
sprite.turn_right(90)
sprite.pen_down()