# How far should we stay away from the crocodilessafe_distance_from_crocodile = 15

""" Helper function and event handler functions"""

# Part 5# Show a message when the game enddef gameover(message): print(message)

# Part 1 (2 of 2)# 1.2 Event handler for the turtle.ondrag() eventdef moveturtle(x, y): # Allow the turtle to move within the window only if x > -window_width / 2 and x < window_width / 2: turtle.goto(x, y) # You should delete this line of code after # you finish the updatescreen function

# Event handler for the turtle.ontimer() eventdef updatescreen(): """ This function does: 1. Decrease the width of the river 2. Check if the player has won the game 3. Check if the player has hit the borders 4. Move the crocodiles 5. Check if the player has collided with a crocodile 6. Update the screen 7. Schedule the next update """

# Global variables that will be changed in the function global river_width

# Part 3.3 # Check if the player has won the game # If the player survives until the river gets to its narrowest # width, he/she will win the game if river_width <= minimum_width: gameover("You survived the crocodile river!") return

# Part 3.4 # Check if the player has hit the borders # The vertical distance between the player's turtle and the # borders must be large enough, otherwise the player loses # the game

# Part 4.2 # Move the crocodiles # For every crocodile in crocodiles

# 4.2.1. Move the crocodile to the left # 4.2.2. If the crocodile moves out of the screen, move it # to the right hand side and generate its speed and # position randomly

# e. Add the new crocodile to the list 'crocodiles' crocodiles.append(crocodile)

# 4.1.2. Generate a random speed and store it in 'crocodile_speeds' crocodile_speeds.append(random.uniform(crocodile_speed_min, crocodile_speed_max))

# Prepare the player turtleturtle.shape("turtle")turtle.color("GreenYellow")turtle.up()

# Part 1 (1 of 2)# 1.1 Set up event handlers# The event handler for turtle.ondragturtle.ondrag(moveturtle)

# Part 2 (2 of 4)# The event handler for turtle.ontimer# 2.2. Schedule the first update# It starts the main loop and starts the game# Start the main loop, start the gameupdatescreen()turtle.update()turtle.done()

I wonder why the turtle couldn't be dragged and the two squares wouldn't move inward.Thank you for your help

Last edited by micseydel on Sat May 11, 2013 8:12 pm, edited 2 times in total.
Reason:Added code tags.

As for your problem, I tried running your code, but you need to include all the dependencies (images) in order for that to happen. Also, I have no idea what you mean when you say

tommy0805 wrote:I wonder why the turtle couldn't be dragged and the two squares wouldn't move inward.

Perhaps it would make sense if I ran the code, but you should give as precise of instructions as you can manage as to what happens in your code and what you would want to happen in your code, as well as what user interactions would be required. (This will help us to help you urgently.)

2. Controlling Animation Using the Timer Eventturtle.tracer(False) has been used to disable any movement/drawing animation in the given Python code,As you know, after running the above code you cannot see any update of the screen unless you call turtle.update() explicitlyFor example, in the given code, turtle.update() has been used in the moveturtle function to show the movement of the turtleHowever, we will now delete the line containing turtle.update() in moveturtleInstead of updating the screen whenever you drag the turtle, the idea is to use a timer event to do the regular screen updateTo do the screen update we run turtle.update() when the timer event firesWe can set up the timer event using this code: turtle.ontimer(updatescreen, update_interval)The above code means the program will run updatescreen() after update_interval amount of timeThe unit of update_interval is in milliseconds, i.e. if update_interval is 1000, the timer will fire after 1 secondBecause the screen has to be updated quite frequently, update_interval has been set as 25

updatescreen(), which is used by the timer event, is the main procedure of the gameThe function updates everything inside the game such as moving the turtle and crocodilesAt the end of the function turtle.update() is called to update the screenTo run this function continuously we use turtle.ontimer to schedule a new timer event before the function exitsIf you set up the timer event in updatescreen() correctly you will be able to drag the turtle again

However, the program didn't work as what the instructions said. I wonder which step is wrong.