Treehouse: Grow your CSS skills. Land your dream job.

car game

  • # January 6, 2015 at 1:30 am

    hi i am facing a problem in the car game

    *when u click the turns are working properly
    when the dice is rolled
    * the problem is how to get the cars reach the finish line an how do i check who has won

    learning js trying to build stuff an learn as i am on my own there is no guidance if u guys help me it will help me in my carrier thanks

    http://jsfiddle.net/codemasters14/f3fLy137/

    Regards,
    mark

    # January 6, 2015 at 5:45 am

    Seems to me that you need to set an end value for the finish line, keep track of each player’s position value…then when the position value is higher than the finish value…boom “We have a winner!”.

    # January 6, 2015 at 10:52 am

    Here’s a working example of the game. ;-)

    Old link

    New link

    Like Paulie_D mentioned, you could check the bottom (or top) position of each car for when any of them pass the position of the finish…

    Edit – link updated.

    # January 6, 2015 at 10:52 am

    Not that the pen I posted has very logical code, I didn’t go in too deep but wanted to show that a loop would be easiest and that the previous position of the car needed to be accounted for.

    For throwing the dice this isn’t really accurate either – I think you could end up with a zero. Then again with the first fiddle you could both do that and you couldn’t throw a 6 (Math.floor rounds down). But with the shuffle function from the other topic you could do something like this :

    shuffle(dice);
    
    var rolled = dice[0];
    

    This was a quick fix by the way :

    position = position || 0;
    

    For some reason it doesn’t return a bottom position on the first click.
    I updated the link in the previous post with the shuffle approach…

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.