I have been given an assignment to create a random race program where 2 different lines race. How the lines travel is determined by a random number generator, but that is not the issue I currently have. I am suppose to create a window that is 500x600, with the x- and y-axises being 2 pixels wide. I am suppose to also include lines along the x- and y-axises at -40, -20, 20, and 40.

I am using graphics.py. My professor will dock points if I use anything else.

One of the problems I am encountering is that python will freeze whenever I try to run the program to see if anything was placed in the window correctly. I do not know if this has anything to with my code, graphics.py, or python itself.

The other issue I am asking for help with is getting the graph created. During the odd occasion where python does not freeze, I am only capable of producing the yellow information box and the x- and y-axises. If I try to add any other code to it, the window fails to display anything, and I am not sure why. I know that I need to implement a loop to efficiently create all the other lines.

The following is my code thus far:

from graphics import*
import math

def buildWindow(win):
    yAxis = Line(Point(0, -50), Point(0, 50))
    yAxis.setFill("black")
    yAxis.draw(win)
    xAxis = Line(Point(-50, 0), Point(50, 0))
    xAxis.setFill("black")
    xAxis.draw(win)
        
    information = Rectangle(Point(-50, -50), Point (50, -40))
    information.setFill("yellow")
    information.draw(win)
    
#def generateData():

def main():
    win = GraphWin("Random Race",500,600)
    win.setCoords(-50,-50,50,50)
    buildWindow(win)
main()

The information box is meant to display the current distance from the origin of each "racer", as well as the maximum distance each racer achieved. However, that is not important at the moment. Getting python to stop freezing and crashing, as well as plotting the graph is what is important. If anyone can, please help me out. Your assistance will be well appreciated.

Recommended Answers

All 6 Replies

If Python is freezing, I suggest you use threading to put the busy work of the program in another thread.

Once you solve the freezing problem with threading, I'm sure you can solve the rest.

First, I must apologize for not listing the version of python I am using and my OS. I am running on a Windows Vista OS and using Python 2.3.2.

I figured out what the freezing problem is caused by. Unfortunately, it is beyond my skill level, as there is a problem with graphics.py that will cause any visual creation programs to freeze during execution after a certain time period has passed. I am working with my professor to figure out why this happens, but until then, I will have to put up with it. However, I have completed the graph.

On second thought, I am going to need some help with the random part.

from graphics import*
import math
from random import*

def buildWindow(win, position):
    # Create the y axis lines
    yAxis = Line(Point(position, -50), Point(position, 50))
    yAxis.setFill("black")
    yAxis.draw(win)

    # Create the x-axis lines
    xAxis = Line(Point(-50, position), Point(50, position))
    xAxis.setFill("black")
    xAxis.draw(win)

    #Create the information Box
    information = Rectangle(Point(-50, -50), Point (50, -40))
    information.setFill("yellow")
    information.draw(win)

def generateData(win):
    #Loop through 500 steps of the race
    redX = redY = greenX = greenY = 0
    for i in range(500):
        #Calculate the angle and distance traveled by Red Racer
        redAngle = random()*2*math.pi
        redX += math.sin(redAngle)
        redY += cosin(redAngle)
        redDistance = math.squrt(redX**2 + redY**2)
        #redMax =

        #Calculate the angle and distance traveled by Green Racer
        greenAngle = random()*2*math.pi
        greenX += math.sin(greenAngle)
        greenY += cosin(greenAngle)
        greenDistance = math.squrt(greenX**2 + greenY**2)
        #greenMax =
        #Write the information from the race in the yellow box
        stepMessage = Text(Point(0, -40), "Step #%f" % (step))
        stepMessage.draw(win)
def main():
    win = GraphWin("Random Race",500,600)
    win.setCoords(-50,-50,50,50)
    position = -40
    for j in range(5):
        buildWindow(win, position)
        position = position + 20


   generateData(win)

main()

if __name__ == '__main__':
    main()

I am getting responses about cosin being undefined and that cosin does not exist in the math module. I cannot even begin to think about trying to graph the race if I cannot get the data to generate...

Because I cannot seem to edit my post...

The cosin problem was me being stupid. I am still encountering other errors, but I hope to fix them by tomorrow. I'll post my progress tomorrow morning if I am still stuck.

Minor fixes, also if you have problems with libraries just google them or look at Python's documentation.
http://docs.python.org/library/math.html

from graphics import*
import math
from random import*

def buildWindow(win, position):
    # Create the y axis lines
    yAxis = Line(Point(position, -50), Point(position, 50))
    yAxis.setFill("black")
    yAxis.draw(win)

    # Create the x-axis lines
    xAxis = Line(Point(-50, position), Point(50, position))
    xAxis.setFill("black")
    xAxis.draw(win)

    #Create the information Box
    information = Rectangle(Point(-50, -50), Point (50, -40))
    information.setFill("yellow")
    information.draw(win)

def generateData(win):
    #Loop through 500 steps of the race
    redX = redY = greenX = greenY = 0
    stepMessage = Text(Point(0, -40),"")
    stepMessage.draw(win)
    for step in range(500):
        #Calculate the angle and distance traveled by Red Racer
        redAngle = random()*2*math.pi
        redX += math.sin(redAngle)
        redY += math.cos(redAngle)
        redDistance = math.sqrt(redX**2 + redY**2)
        #redMax =

        #Calculate the angle and distance traveled by Green Racer
        greenAngle = random()*2*math.pi
        greenX += math.sin(greenAngle)
        greenY += math.cos(greenAngle)
        greenDistance = math.sqrt(greenX**2 + greenY**2)
        #greenMax =
        #Write the information from the race in the yellow box
        stepMessage.setText("Step #%f" % (step+1))
def main():
    win = GraphWin("Random Race",500,600)
    win.setCoords(-50,-50,50,50)
    position = -40
    for j in range(5):
        buildWindow(win, position)
        position = position + 20
    generateData(win)

if __name__ == '__main__':
    main()

All right, I believe I completed the program. I am not encountering any problems anymore. I will, however, post my final code anyways.

from graphics import*
import math
from random import*

def buildWindow(win, position):
    # Create the y axis lines
    yAxis = Line(Point(position, -50), Point(position, 50))
    yAxis.setFill("black")
    yAxis.draw(win)

    # Create the x-axis lines
    xAxis = Line(Point(-50, position), Point(50, position))
    xAxis.setFill("black")
    xAxis.draw(win)

    #Create the information Box
    information = Rectangle(Point(-50, -50), Point (50, -40))
    information.setFill("yellow")
    information.draw(win)

def generateData(win):
    #Draw the information messages
    stepMessage = Text(Point(0, -42), "Step #0")
    stepMessage.draw(win)
    redTravel = Text(Point(0, -45), "Red Distance = 0 Red Maximum Distance = 0")
    redTravel.draw(win)
    greenTravel = Text(Point(0, -47), "Green Distance = 0 Green Maximum Distance = 0")
    greenTravel.draw(win)

    #each value of x and y starts at the origin
    redX = redY = greenX = greenY = redX2 = redY2 = greenX2 = greenY2 = 0
    redMax = greenMax = 0   #sets the starting values of the max distance from the origin
    
    for i in range(500):    #Loop through 500 steps of the race
        #Calculate the angle and distance traveled by Red Racer
        #all comments for red apply the same to green
        redAngle = random()*2*math.pi
        redX2 += math.cos(redAngle)
        redY2 += math.sin(redAngle)
        
        redLine = Line(Point(redX, redY), Point(redX2, redY2))
        redLine.setFill("red")
        redLine.draw(win)   #draw the lines and colors it red
        redX = redX2
        redY = redY2
        
        redDistance = math.sqrt(redX**2 + redY**2)  #calculate distance
        redMax = max(redMax, redDistance)   #calculate max distance

        
        greenAngle = random()*2*math.pi
        greenX2 += math.cos(greenAngle)
        greenY2 += math.sin(greenAngle)
        greenLine = Line(Point(greenX, greenY), Point(greenX2, greenY2))
        greenLine.setFill("green")
        greenLine.draw(win)
        greenX = greenX2
        greenY = greenY2

        greenDistance = math.sqrt(greenX**2 + greenY**2)
        greenMax = max(greenMax, greenDistance)
        
        #Write the information from the race in the yellow box
        stepMessage.setText("Step #%f" % (i+1))
        redTravel.setText("Red Distance = %f Red Maximum Distance = %f" % (redDistance, redMax))
        greenTravel.setText("Green Distance = %f Green Maximum Distance = %f" %\
                            (greenDistance, greenMax))
        
        #if the race is over, instruct user on how to close the window
        if i == 499:    stepMessage.setText("Click to Close the Window")
def main():
    #Draw the window.
    win = GraphWin("Random Race",500,600)
    win.setCoords(-50,-50,50,50)
    position = -40 #Starting point for the drawing of the grid lines
    for j in range(5):
        buildWindow(win, position)
        position = position + 20 #Draw the lines of the grid.

    generateData(win)   #gather the race information
    win.getMouse()
    win.close() #closes the window by clicking.
if __name__ == '__main__':
    main()

Thanks TKpanther, this helped debug similar code!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.