Results 1 to 10 of 10

Thread: Creating traffic simulator ??

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2013
    Posts
    25

    Creating traffic simulator ??

    Hi everybody I am trying to create a traffic simulator where cars enter the screen from the left and go out the right and come back in the left. Also have to create traffic lights with a button for crossing. What should I use to draw traffic lights ? Ovalshape or pictureboxes ?

    I have a .exe demo of the app but the site won't let me upload even if I zip the file.

    Also want to add a button that when clicked it rains into it but don't know how to do that and there is not much tutorials on Google.

    I know the part when the button is clicked for crossing that a timer should start on the lights but I can't seem to even get a start on this

    Any help would be greatly appreciated. Also wish I could upload the .exe file would get a better picture of what I am trying to do.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Creating traffic simulator ??

    no one is going to just run some random exe uploaded. You could upload your source, or upload screen shots.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2013
    Posts
    25

    Re: Creating traffic simulator ??

    I understand but I don't have source code. I am trying to do the source code and recreate the .exe file.

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Creating traffic simulator ??

    So what you want to do is create an application that does basically what another application does. That's fine.

    So... the fact that you're asking about how to draw the traffic lights suggests you have the shell of an application? You can launch the application and a window comes up? Have you got the cars being drawn yet? It's a traffic simulation, so do you have some kind of simulation loop for calculating each car's new position before updating the screen? Show us what you've got and say where you're stuck and we can unstick you, you're unlucky to find someone who will write an entire application for you.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2013
    Posts
    25

    Re: Creating traffic simulator ??

    I have the traffic lights put on as ovalshapes but I'm having a problem I want the light to be just green until a button is clicked in which the timer will kick in for 10 seconds then change to amber and then 10 more seconds change to red. I am a newbie but I'm trying

    HTML Code:
    Public Class cp
    
       
    
        Private Sub cp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.OvalShape1.FillColor = Color.Red
            Me.OvalShape2.FillColor = Color.Yellow
            Me.OvalShape3.FillColor = Color.Green
    
    
        End Sub
    
        Private Sub TmrRain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TmrRain.Tick
            If OvalShape3.Visible Then
    
                OvalShape3.Visible = True
                OvalShape2.Visible = False
                OvalShape1.Visible = False
    
            ElseIf OvalShape2.Visible Then
    
                OvalShape1.Visible = True
                OvalShape2.Visible = False
                OvalShape3.Visible = False
    
            Else
    
                OvalShape1.Visible = True
                OvalShape2.Visible = False
                OvalShape3.Visible = False
            End If
        End Sub
    
        Private Sub btnRain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRain.Click
    
            TmrRain.Enabled = True
    
            OvalShape2.Visible = True
            OvalShape1.Visible = False
            OvalShape3.Visible = False
    
    
    
    
    
        End Sub
    End Class

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2013
    Posts
    25

    Re: Creating traffic simulator ??

    Any Help ??

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Creating traffic simulator ??

    Code:
    If OvalShape3.Visible Then ' if green change to yellow
        OvalShape1.Visible = False
        OvalShape2.Visible = True
        OvalShape3.Visible = False
    ElseIf OvalShape2.Visible Then ' if yellow change to red
        OvalShape1.Visible = True
        OvalShape2.Visible = False
        OvalShape3.Visible = False
    Else ' change to green
        OvalShape1.Visible = False
        OvalShape2.Visible = False
        OvalShape3.Visible = True
    End If

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Nov 2013
    Posts
    25

    Re: Creating traffic simulator ??

    Thanks for the help but one thing I am trying to do is when the button is clicked i.e btnCrossing the timer counts down 10 seconds then changes to Amber then 10 more seconds and changes to red for 10 seconds how is this possible ??
    Thanks in advance

  9. #9
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Creating traffic simulator ??

    Well (where I'm at) normal traffic lights change, green > yellow > red > green > yellow etc, etc,...
    So if I set light to Green, Set timer interval to 10 seconds, and start the timer then in 10 secs it would change to yellow (or amber), then after an additional 10 seconds it would change to red, 10 more seconds and back to green, etc,etc, unless you stop the timer and/or change the current light manually of course.
    Some code I already had, never tried it but the logic looks OK, maybe will help?
    Code:
    Public Class Form1
    
        ' an enum to make logic easier
        Private Enum Lights As Integer
            red = 0
            yellow = 1
            green = 2
        End Enum
    
        ' variable to keep track of which light is on
        Private trafficLight As Lights
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ' set controls for red, yellow, green lights
            pb_Red.BackColor = Color.Red
            pb_Yellow.BackColor = Color.Yellow
            pb_Green.BackColor = Color.Green
            ' at start up show a green light.
            ShowLight(Lights.green)
        End Sub
    
        ' test button..
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            ' start timer to auto toggle traffic lights
            ' set timer
            Timer1.Interval = 2000 ' 2 sec delay between lights
            Timer1.Enabled = True ' auto loop, green > yellow > red > green > yellow, etc, etc...
        End Sub
    
        Private Sub ShowLight(light As Lights)
            ' save "on" light
            trafficLight = light
            ' set all lights off
            pb_Red.Visible = False
            pb_Yellow.Visible = False
            pb_Green.Visible = False
            ' set selected light on
            If trafficLight = Lights.green Then
                pb_Green.Visible = True
            ElseIf trafficLight = Lights.yellow Then
                pb_Yellow.Visible = True
            Else ' red
                pb_Red.Visible = True
            End If
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            If trafficLight = Lights.green Then ' if green change to yellow
                ShowLight(Lights.yellow)
            ElseIf trafficLight = Lights.yellow Then ' if yellow change to red 
                ShowLight(Lights.red)
            Else ' must be red, so set to green
                ShowLight(Lights.green)
            End If
        End Sub
    
    End Class

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Creating traffic simulator ??

    Of course, the times are not usually the same.
    In my neighborhood, one light I go through regularly, I know that if I see it go red, it will go green in 40 seconds. I don't know how long it stays green. If I see it go green, I know that I will make it because it stays green for about a block and 1/2 of travel at the speed limit, and you can only see the light for a block from either direction because of the jogs in the streets at the end of the block from either direction of the light.
    Since the light stays Red in the North/South direction for 40 seconds, and the yellow lasts about 5 seconds, then if both directions were even, then the green should last 35 seconds, assuming no overlap of the Red in both directions. I do believe most lights do overlap with Red in all directions for a second or two to lessen the chance of someone gunning out when the light turns green meeting someone who runs over the tail end of the yellow into the first bit of the Red in the crossing direction.

    So, you could change Edgemeal's code's timer to have a 1 second interval (1000) and a counter to count down the different periods.
    Code:
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Static CountDown as Integer
            CountDown -= 1
            If CountDown <= 0 Then
                If trafficLight = Lights.green Then ' if green change to yellow
                    ShowLight(Lights.yellow)
                    CountDown = 5
                ElseIf trafficLight = Lights.yellow Then ' if yellow change to red 
                    ShowLight(Lights.red)
                    CountDown = 40
                Else ' must be red, so set to green
                    ShowLight(Lights.green)
                    CountDown = 35
                End If
            End If
        End Sub
    Code above not tested, but I believe should work.
    Last edited by passel; Nov 23rd, 2014 at 03:03 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width