Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: JavaScript Game

  1. #1
    Join Date
    Oct 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JavaScript Game

    Hi everyone,

    I have a homework assignment for JavaScript that I need some help with:
    Create a game using the following rules:
    Two players play the game. Each player takes a turn that lasts 30 seconds. On each turn, the player is to "tag" an asterisk(*) on the screen that starts on the left-hand side of the screen and travels across the screen and back. The asterisk begins at the far left of the screen and travels across ten different points on the screen until it rach the far right side of the screen, then return to the left of the screen. The player "tags" the asterisk by clicking a button or clicking text "Tag". The further the asterisk is to the right of the screen, the more points are scored when a player "tags" the asterisk. When the asterisk is "tagged" the player receives the point value associated with the position on the screen. Each point value shoul dbe clearly displayed next to its position.

    The game is started when a "start" button or text(as in a P tag) is clicked. The game should go for 5 turns each player, then end and display the winner. It should be clear at all times whose turn it is: player 1 or player 2. You can restart the game by either refreshing the browser, or clicking a "new game" button, then clicking "start".

    When the browser loads, the asterisks will start at the left-hand side of the screen and travel 10 positions to the right and then travels 10 positions back. Position 1 is worth 1 point, poistion 2 is worth 2 points, etc. up to position 10 which is worth 10 points. The player must click the "Tag" button to get points, so it is worth more point to click the button the further to the right the asterisk is. The goal is to get the most points within 30 seconds.

    Hints:

    * Use a one second timer to count down the game time(30 seconds per turn)
    * Use a faster timer to change the position of the asterisks
    * Use P tag or DIV tags to hold and update the score for each player

    Optional (worth 25 points of Extra Credit each)

    * Allow the players to set the difficulty level from one to three, which controls the speed of the asterisks
    * Allow the players to select normal play - or random play, in which the asterisk is randomly positioned across the screen.
    So far I have written this:

    <HTML>
    <HEAD>
    <TITLE>Homework Assignment #6 Part 2 - Game</TITLE>

    <SCRIPT LANGAUGE = "JavaScript">

    function moveAsterisk()
    {
    asterisk.style.left += 50;
    }

    function start()
    {
    window.setInterval("moveAsterisk()", 100);
    }



    </SCRIPT>

    </HEAD>
    <BODY ONLOAD = "start()">
    <Div ID = "asterisk" STYLE = "position: absolute; left: 0px, top: 200px">*</div>
    <font color = "red"><P><strong><em><br><br><br>Click Refresh on the Browser or Press F5 to run the script again.</em></strong></P></font>
    </BODY>
    </HTML>
    But the timer only goes through one loop, then gets an error....but I have no idea why!

    Any help is appreciated

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Try this:

    Code:
    var upto=0;
    function moveAsterisk()
    {
    asterisk.style.left = upto+"px";
    upto=upto+50;
    }
    
    function start()
    {
    window.setInterval("moveAsterisk()", 100);
    }
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Oct 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thank you

  4. #4
    Join Date
    Oct 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    oh and just so you guys know, I am mainly a VB programmer, but I am just now taking JavaScript in college (last test Thursday!), and will be taking C# starting next week

    but anywho, I like this place, so I plan to hang out a bit here and there

  5. #5
    Join Date
    Oct 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    and remind me how I can break out of a function? or end a timer?

  6. #6
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    and remind me how I can break out of a function? or end a timer?
    Code:
    var upto=0;
    function moveAsterisk()
    {
    asterisk.style.left = upto+"px";
    upto=upto+50;
    }
    var mytimer; //Preset the variable
    function start()
    {
    mytimer = window.setInterval("moveAsterisk()", 100); //Associate the timer with a variable
    }
    function stop()
    {
    clearInterval(mytimer); //Stop the timer
    }
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  7. #7
    Join Date
    Oct 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    clearInterval

    that's it!

  8. #8
    Join Date
    Oct 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    but I still can't find how to exit a fuction

    my function keeps looping, even if I end the timer

  9. #9
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Try removing the start() function and only putting the setInterval, like this:
    Code:
    var upto=0;
    function moveAsterisk()
    {
    asterisk.style.left = upto+"px";
    upto=upto+50;
    }
    var mytimer = window.setInterval("moveAsterisk()", 100); //Associate the timer with a variable
    function stop()
    {
    clearInterval(mytimer); //Stop the timer
    }
    Oh, and remove the onload attribute in the <body> tag as well.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  10. #10
    Join Date
    Oct 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    is there a way to exit a function?

Bookmarks

Posting Permissions

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