Python help (urgent) thx

This is the place for queries that don't fit in any of the other categories.

Python help (urgent) thx

Postby tommy0805 » Sat May 11, 2013 3:36 pm

Code: Select all
"""
    Turtle Graphics - Game Programming
"""

import turtle
import random

"""
    Constants and variables
"""

# Window size
window_height = 600
window_width = 600

# The screen update interval
update_interval = 25

# Parameters for controlling the width of the river
river_width = 300
minimum_width = 50

# Border parameters
border_height = 600

# Parameters for gradually decreasing river width
river_width_update = 0.5

# How far should we stay away from the borders
safe_distance_from_border = border_height / 2 + 3

# Parameters for crocodiles
crocodile_number = 10
crocodiles = []
crocodile_speeds = []
crocodile_width = 100
crocodile_height = 40
crocodile_speed_min, crocodile_speed_max = 1, 25

# How far should we stay away from the crocodiles
safe_distance_from_crocodile = 15

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

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



# Part 1 (2 of 2)
# 1.2 Event handler for the turtle.ondrag() event
def 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() event
def 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.2
    # Decrease the width of the river by river_width_update
   
    if river_width >minimum_width :
        upper_border.sety(upper_border.ycor() - river_width_update)
        lower_border.sety(lower_border.ycor() + river_width_update)
        river_width = river_width_update * 2
   
    # 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
       
        # Part 4.3
        # Check collision

    # Part 2 (3-4 of 4)

    # 2.3. Update the screen
    turtle.update()
    # 2.4. Schedule an update in 'update_interval' milliseconds
    turtle.ontimer(updatescreen, update_interval)
 



"""
    Here is the entry point of the game
   
    First of all, we create turtles for each component
    in the game with turtle.Turtle().
    The components are:
        1. The player turtle
        2. Two big boxes used as borders of the river
        3. Ten crocodiles
   
    Then we set up the event handlers for:
        1. The ondrag handler to handle the player's control
        2. The ontimer handler to handle timer event for
           regular screen update

    After all the components are ready, start the game
"""

turtle.setup(window_width, window_height) # Set the window size
turtle.bgcolor("DarkBlue")

# Part 2 (1 of 4)
# 2.1. Turn off the tracer here
turtle.tracer(False)

# Part 3.1
# 3.1.1. Create the big boxes for upper border and lower border
upper_border = turtle.Turtle()
upper_border.up()
lower_border = turtle.Turtle()
lower_border.up()

# 3.1.2. Set the shape of the borders to "square"
upper_border.shape("square")
lower_border.shape("square")

# 3.1.3. Set the colour of the borders to "DarkOrange4"
upper_border.color("DarkOrange4")
lower_border.color("DarkOrange4")

# 3.1.4. Set the size of the borders
upper_border.shapesize(30, 40)
lower_border.shapesize(30, 40)

# 3.1.5. Set the initial y position of the borders
upper_border.sety((border_height + river_width) / 2)
lower_border.sety(-(border_height + river_width) / 2)

# Part 4.1
# Add the crocodile image to the pool of shapes
turtle.addshape("crocodile.gif")

# Create ten crocodiles
for _ in range(crocodile_number):

    # a. Create a new turtle instance which is facing left
    crocodile = turtle.Turtle()

    # b. Set the shape
    crocodile.shape("crocodile.gif")

    # c. Rotate the crocodile
    crocodile.left(180)
   
    # d. Place the crocodile in the right hand side randomly
    crocodile.up()
    x = (window_width + crocodile_width) / 2
    y = random.uniform(-(river_width-crocodile_height)/2, (river_width-crocodile_height)/2)
    crocodile.goto(x, y)

    # 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 turtle
turtle.shape("turtle")
turtle.color("GreenYellow")
turtle.up()

# Part 1 (1 of 2)
# 1.1 Set up event handlers
#    The event handler for turtle.ondrag
turtle.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 game
updatescreen()
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.
tommy0805
 
Posts: 3
Joined: Sat May 11, 2013 3:24 pm

Re: Python help (urgent) thx

Postby micseydel » Sat May 11, 2013 8:11 pm

Don't make another post without reading this.

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.)
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1361
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Python help (urgent) thx

Postby tommy0805 » Sun May 12, 2013 3:35 am

This is the instruction and the end program should be like this: https://www.youtube.com/watch?feature=p ... Khlyy-2yKg


2. Controlling Animation Using the Timer Event
turtle.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() explicitly
For example, in the given code, turtle.update() has been used in the moveturtle function to show the movement of the turtle
However, we will now delete the line containing turtle.update() in moveturtle
Instead of updating the screen whenever you drag the turtle, the idea is to use a timer event to do the regular screen update
To do the screen update we run turtle.update() when the timer event fires
We 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 time
The unit of update_interval is in milliseconds, i.e. if update_interval is 1000, the timer will fire after 1 second
Because 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 game
The function updates everything inside the game such as moving the turtle and crocodiles
At the end of the function turtle.update() is called to update the screen
To run this function continuously we use turtle.ontimer to schedule a new timer event before the function exits
If 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.
tommy0805
 
Posts: 3
Joined: Sat May 11, 2013 3:24 pm

Re: Python help (urgent) thx

Postby micseydel » Sun May 12, 2013 3:58 am

micseydel wrote:I tried running your code, but you need to include all the dependencies (images) in order for that to happen.

We don't have the image files required to run your program. You should upload them.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1361
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Python help (urgent) thx

Postby tommy0805 » Sun May 12, 2013 4:19 am

I am sorry here is the only file attachment attachment .
Attachments
crocodile.gif
I am sorry here is the only file attachment
crocodile.gif (1.82 KiB) Viewed 468 times
tommy0805
 
Posts: 3
Joined: Sat May 11, 2013 3:24 pm


Return to General Coding Help

Who is online

Users browsing this forum: Baldyr, Bing [Bot], umarmughal824, Yoriz and 2 guests