JavaScript - Hi Everyone, How Do I Use A Code I Have For A Simple Game With An Image Map?
Basically, I have a Simon Says game with very simple functions but I added an image map with buttons that I now want to use, rather than a table with images as buttons. The problem is, now I can't get the game to work with the image map. Can anyone help me?
Similar TutorialsI am trying to create a simple shoot em up on java and it is due by tomorrow 5/2. I need help making the gun I have created, shoot up to 10 bullets using the space bar. Also, I need some kind of either bullet countdown bar or something to show the shooter the number of bullets that he has left to shoot out of 10. So far I have created the Ufo's that bounce around the screen randomly and the gun that moves along the bottom using the arrow keys. When a Ufo huts the gun the game ends and goes to a blank screen. Here is what I have so far: Code: //Shoot.java import acm.graphics.*; import acm.program.*; import java.awt.event.*; import java.awt.*; import acm.util.*; public class Shoot extends GraphicsProgram { public static final int APPLICATION_WIDTH = 800; public static final int APPLICATION_HEIGHT = 500; final int AW=APPLICATION_WIDTH; final int AH=APPLICATION_HEIGHT; final int WAIT=5; final int MV_AMT=5; final int JUMP_AMT=100; final int U_SIZE=30; int xMove,yMove; UFO u1,u2,u3; public void init() { u1=new UFO(); u2=new UFO(); u3=new UFO(); RandomGenerator rg= new RandomGenerator(); int rand1= rg.nextInt(0,AW-50); int rand2= rg.nextInt(0,AW-75); int rand3= rg.nextInt(0,AW-100); add(u1,rand1,0); add(u2,rand2,0); add(u3,rand3,0); xMove=yMove=0; addKeyListeners(); }//init public void keyPressed(KeyEvent e) { int key = e.getKeyCode( ); if (key == KeyEvent.VK_RIGHT) { xMove = MV_AMT ; } else if (key == KeyEvent.VK_LEFT) { xMove = -MV_AMT ; } } //keyPressed public void run() { RandomGenerator rg =new RandomGenerator(); GRectangle u1Box, u2Box, u3Box, g1Box; int xMove1=1,xMove2=1,xMove3=1; int yMove1=1,yMove2=0,yMove3=1/4; boolean u1Done=false, u2Done=false, u3Done=false; GUN g1=new GUN(); add(g1,300,480); xMove=yMove=0; addKeyListeners(); waitForClick(); while(true) { u1.move(xMove1,yMove1); u2.move(xMove2,yMove2); u3.move(xMove3,yMove3); u1Box=u1.getBounds(); u2Box=u2.getBounds(); u3Box=u3.getBounds(); g1Box=g1.getBounds(); pause(WAIT); g1.move(xMove,yMove); xMove=yMove=0; //intersections if(u1Box.intersects(g1Box)==true) {g1.setVisible(false); u1.setVisible(false); u2.setVisible(false); u3.setVisible(false); break; } if(u2Box.intersects(g1Box)==true) {g1.setVisible(false); u1.setVisible(false); u2.setVisible(false); u3.setVisible(false); break; } if(u3Box.intersects(g1Box)==true) {g1.setVisible(false); u1.setVisible(false); u2.setVisible(false); u3.setVisible(false); break; } //at top of window if (u1.getY( ) == 0) { yMove1 = -yMove1 ; } if (u2.getY( ) == 0) { yMove2 = -yMove2 ; } if (u3.getY( ) == 0) { yMove3 = -yMove3 ; } //at left or right side if ((u1.getX( ) <= 0) || (u1.getX( ) + U_SIZE >= AW)) { xMove1 = -xMove1 ; } if ((u2.getX( ) <= 0) || (u2.getX( ) + U_SIZE >= AW)) { xMove2 = -xMove2 ; } if ((u3.getX( ) <= 0) || (u3.getX( ) + U_SIZE >= AW)) { xMove3 = -xMove3 ; } //at bottom of window if (u1.getY( ) == 450) { yMove1 = -yMove1 ; } if (u2.getY( ) == 450) { yMove2 = -yMove2 ; } if (u3.getY( ) == 450) { yMove3 = -yMove3 ; } } } } Thank you so much Courtney Okay I'm writing a JavaScript application that would prompt the user for a number between 0 and 2, generate a random number for the computer, then take that input and display two images (in this case two hands making rock, paper, or scissor symbols at each other) using an array, and displaying text indicating a win, draw, or loss. I'm having a lot of problems getting this to work as I am new to JavaScript and am wondering if anyone could help me fix this problem. I just want the input from the prompt AND the random number to translate to what in the array should be displayed and what text is then shown at the bottom. Any comments/advice/help is greatly appreciated! By the way for some reason it doesn't let the letters H T M L show up together so that is where the ellipses are showing up! Code: <script type = "text/javascript"> selection = prompt("Enter 0 for rock, 1 for paper, and 2 for scissors") userChoice = parseInt(selection) arrayHolder = new Array(); arrayHolder[0] = "rock.jpg"; arrayHolder[1] = "paper.jpg"; arrayHolder[2] = "scissors.jpg"; compchoice = Math.floor(Math.random()*arrayHolder.length) function playGame(idholder1) { document.images["userpic"].src = arrayHolder[userchoice]; document.images["comppic"].src = arrayHolder[compchoice]; if (userChoice == 0 && compchoice == 0) { document.getElementbyId(idholder1).innerHTML = "DRAW!" } else if (userChoice == 0 && compchoice == 1) { document.getElementbyId(idholder1).innerHTML = "Paper beats Rock, you lose!" } else if (userChoice == 0 && compchoice == 2) { document.getElementbyId(idholder1).innerHTML = "Rock beats Scissors, you win!" } else if (userChoice == 1 && compchoice == 0) { document.getElementbyId(idholder1).innerHTML = "Paper beats Rock, you win!" } else if (userChoice == 1 && compchoice == 1) { document.getElementbyId(idholder1).innerHTML = "Draw!" } else if (userChoice == 1 && compchoice == 2) { document.getElementbyId(idholder1).innerHTML = "Scissors beats Paper, you lose!" } else if (userChoice == 2 && compchoice == 0) { document.getElementbyId(idholder1).innerHTML = "Rock beats Scissors, you lose!" } else if (userChoice == 2 && compchoice == 1) { document.getElementbyId(idholder1).innerHTML = "Scissors beat Paper, you win!" } else if (userChoice == 2 && compchoice == 2) { document.getElementbyId(idholder1).innerHTML = "Draw!" } } </script> </head> <body> <p> <center><img src="rock.jpg" width="197" height="171" alt="rock" id="userpic" /><img src="paper.jpg" width="210" height="95" id="comppic" /><br /> <h1 id="gametext" onLoad="playGame('gametext')">ROCK PAPER SCISSORS SHOOT!</h1></center> </p> I'm developing a simple game that involves a system of interconnected nodes with unidirectional travel between nodes (similar to the circulation system!). The goal of the game is to get from a starting node to an ending node, which can be a variable number of nodes away. The program picks a random starting point, then randomly chooses one of its connecting nodes (cNodes) and pushes it onto a pathArray. A cNode is randomly chosen from this new node and it is pushed onto the pathArray. This continues for a designated number of turns, thus generating a pathArray (invisible to the player). The last element in the pathArray is the endNode and the goal of the puzzle. At each node the player is given two options of travel (though there may be more than two ways to go). One of these options MUST be the correct way if the player has not deviated from the path up until that point. If the player has deviated, this option can be any cNode. The other node is any cNode that does not lead to the endNode. The following code contains a simplified list of nodes that represents the content in my game. The function, however, is taken word for word. In this snippet, the pathArray & startNode have already been generated and I am trying to resolve how to assign "nodeChoice" as either the correct direction of travel (for a player on the correct path) or any random cNode (for a player who has deviated from the path). Keep in mind that the pathArray and cNodes lengths can be any size. Code: <script> //NODES: var nodeA = {name:"A"}; var nodeB = {name:"B"}; var nodeC = {name:"C"}; var nodeD = {name:"D"}; var nodeE = {name:"E"}; var nodeF = {name:"F"}; var nodeG = {name:"G"}; var nodeH = {name:"H"}; var nodeI = {name:"I"}; var nodeJ = {name:"J"}; var nodeK = {name:"K"}; //An array of all nodes in the system: var systemArray = [nodeA, nodeB, nodeC, nodeD, nodeE, nodeF, nodeG, nodeH, nodeI, nodeJ, nodeK]; //Connecting Nodes (cNodes): //(uni-directional, but cyclical) nodeA.cNodes = [nodeB, nodeC]; nodeB.cNodes = [nodeD, nodeE, nodeF]; nodeC.cNodes = [nodeF, nodeG]; nodeD.cNodes = [nodeI, nodeH]; nodeE.cNodes = [nodeJ]; nodeF.cNodes = [nodeK]; nodeG.cNodes = [nodeK]; nodeJ.cNodes = [nodeA]; nodeK.cNodes = [nodeA]; nodeI.cNodes = [nodeA]; nodeH.cNodes = [nodeA]; //The path chosen (generated from code not included here) var pathArray = [nodeA, nodeB, nodeE, nodeJ]; //nodeChoice will represent a cNode from any given node var nodeChoice; //chooseNode is supposed to assign nodeChoice the next element in pathArray if the player on on the right path (if at nodeB, nodeChoice = nodeE). //However, if the user has taken a different path, its cNodes will not be in pathArray in which case a random cNode is assigned to nodeChoice function chooseNode(_node) { //check each cNode to see if any are in pathArray for (var j = 0; j < _node.cNodes.length; j++) { //if a cNode is in pathArray, then we know to assign it nodeChoice... if (_node.cNodes[j] in pathArray) { nodeChoice = _node.cNodes[j]; console.log("choiceNode CORRECT: " + nodeChoice.name); //(for debugging purposes only) } //...otherwise don't do anything in this forLoop/ifStatement }; //if by this point nodeChoice is still undefined, meaning none of the current node's cNodes are in pathArray, assign it any one of its cNodes. if (nodeChoice == undefined) { nodeChoice = _node.cNodes[Math.floor(Math.random()* _node.cNodes.length)]; console.log("choiceNode INCORRECT: " + nodeChoice.name);//(for debugging purposes only) }; }; //Runtime: chooseNode(nodeB); //Result should be only nodeE.name since nodeD is not in the pathArray... console.log(nodeChoice.name); </script> ...however, nodeChoice is assigned either D, E or F randomly and we are given the troubleshooting statement "choiceNode INCORRECT: D (or) E (or) F", indicating that the if-in statement is always ignored. I know that the if-in statement doesn't work but am not sure how else to write it so that each cNode is compared the each element in pathArray, both of which can be of variable lengths... Hi everyone, I was wondering if anyone could help me figure out how to overlay a javascript game with buttons and such onto a background image? Let me know if that's too vague a description.
hi everyone. i have to create the 15 puzzle game and im having a difficult time getting started. can anyone help? i need to create a table (4x4) that has one empty space. All the other spaces must have a random generated value between 1 and 15. All i got so far is a onclick image swapping function.... need help! thanks
Hello everyone, well i have asked for alot of issues earlier but none completely got resolved i hope that i get answer for this one. I want something a script or anything that shows loading as we have seen .gif images, before the game loads. I went through google and tried alot of scripts but failed maybe i don't know the exact method how to implement it as i am weak in jquery or scripting. I would like some one to help me out as i don't know what to do thank you in advance. I've created a javascript version of the classic game SOKOBAN. Basically what happens is you move the man around the "warehouse" and he must push the boxes into the target area. Copy the code and try it yourself to see what I have so far (note-he can't move the boxes yet). My question is: Right now, my map consists of solid color blocks. What I WANT them to be is image files (for example: box.png, wall.png, floor.png) to add a little versatility and life to the map. I am not sure how to make this switch though. I suspect I make the change somewhere in the tile array in the initialize function, but I am not sure what.. Also, how can I get the boxes to actually MOVE? I.e. when my man pushes them, they move around in the direction he pushes? Any help would be much appreciated. Thank you kindly Code: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Sokoban</title> <style type="text/css"> body { font-family: Arial; font-size: 48px; } #warehouse { width: 680px; height: 400px; position: relative; } .box { background-size: contain; } .box, #man { margin: 0; padding: 0; display: inline-block; width: 40px; height: 40px; float: left; } </style> <script type="text/javascript"> var map_width = 17; var map_height = 10; var grid; var player_x; var player_y; var tile; function initialize() { grid = new Array(); grid[0] = new Array(0,0,0,0,0,5,5,5,5,5); grid[1] = new Array(0,0,0,0,0,5,2,5,2,5); grid[2] = new Array(0,0,0,0,0,5,2,2,2,5); grid[3] = new Array(0,0,0,0,0,5,1,1,1,5); grid[4] = new Array(0,0,0,0,0,5,1,1,1,5); grid[5] = new Array(0,0,0,0,0,5,1,1,1,5); grid[6] = new Array(0,0,0,0,0,5,1,1,1,5); grid[7] = new Array(0,0,0,0,0,5,5,1,5,5); grid[8] = new Array(5,5,5,5,5,5,5,1,5,0); grid[9] = new Array(5,1,1,1,5,1,1,1,5,0); grid[10] = new Array(5,1,3,1,1,1,3,1,5,0); grid[11] = new Array(5,1,5,1,1,1,3,1,5,0); grid[12] = new Array(5,1,1,1,3,5,3,1,5,0); grid[13] = new Array(5,1,1,1,1,1,1,1,5,0); grid[14] = new Array(5,1,5,5,5,5,1,1,5,0); grid[15] = new Array(5,5,5,0,0,5,1,1,5,0); grid[16] = new Array(0,0,0,0,0,5,5,5,5,0); player_x = 14; player_y = 1; tile = new Array( 'green', /* grass */ '#202020', /* interior floor */ 'red', /* target */ 'brown', /* crate */ 'brown', /* crate sitting on target */ 'orange' /* brick */ ); draw_background(); position_man(); } function draw_background() { for (var y = 0; y < map_height; ++y) { for (var x = 0; x < map_width; ++x) { var box = document.getElementById('box_' + x + '_' + y); box.style.backgroundColor = tile[grid[x][y]]; } } } function position_man() { var man = document.getElementById('man'); man.style.position = 'absolute'; man.style.left = 40 * player_x + 'px'; man.style.top = 40 * player_y + 'px'; } function safe_check(x, y) { if (x >= 0 && x < map_width && y >= 0 && y < map_height) return grid[x][y]; return -1; } function handle_key(e) { var move_x = 0; var move_y = 0; switch(String.fromCharCode(e.which).toLowerCase()) { case 'w': move_y = -1; break; case 'a': move_x = -1; break; case 's': move_y = 1; break; case 'd': move_x = 1; break; default: return false; } var s = safe_check(player_x + move_x, player_y + move_y); if (s > 2) { return true; } player_x += move_x; player_y += move_y; position_man(); return true; } </script> </head> <body onkeypress="handle_key(event)"> <div id="warehouse"> <img src="man.png" id="man" /> </div> <table> <tr> <th>up</th> <th>left</th> <th>down</th> <th>right</th> </tr> <tr> <th>W</th> <th>A</th> <th>S</th> <th>D</th> </tr> </table> <script type="text/javascript"> var warehouse = document.getElementById('warehouse'); for (var y = 0; y < map_height; ++y) { for (var x = 0; x < map_width; ++x) { var box = document.createElement('div'); box.id = "box_" + x + "_" + y; box.className = "box"; warehouse.appendChild(box); } } initialize(); </script> </body> </html> This is a simple basic game i want to make. Its a "What Am I" Riddle... The questions is (In HTML which i already know): I'm as light as a feather but no man can hold me for long. What am I? the answer is: Your Breath. (This is where the JAVASCRIPT comes in) Lets say they guess a rock, have a pop up say: Wrong Answer. Please try again. Lets say they guess Your breath -OR- Breath, have a pop up say: Correct! I want a text field where you can enter your answer, and if they choose ANYTHING but the 2 correct answers above, it will display please try again. Can anyone code this please? Thanks for reading. Ok so i want to work on an EASY basic game. As simple as Guess what number... But there are some twist... Ok So here is my example... (HTML, i know this.)I'm Guessing a number 1-99. What is it? (java) Lets say its 66 and they guess 53, have the text rusult be in a pop up window be: Too low! (java) Lets say they guess 78, have the text rusult be in a pop up window be: Too high! (java) Lets say they guess 100+, have the text rusult be in a pop up window be: Out of range! (java)But lets say they guess 66, have a text result be in a pop up window be: You've won! (java) I want a text field where you can enter your answer, and it will do the actions above. Can anyone code this? Thanks for reading! The code I commeted out dosen't work I want to check if its null and if so alert them. { // PART 2: YOUR CODE STARTS AFTER THIS LINE correctAnswer = Math.floor((Math.random() * 100) + 1); // For testing purposes. alert("Testing purposes, correct answer is: " + correctAnswer); var number = 0; //// Calculations //if (isNaN(number) = true) { // alert("Thats not a number"); //} //document.write(number); while (number != correctAnswer) { number = prompt("Wrong Number, guess again."); number = Number(number); if (number < correctAnswer) { document.writeln("Small guess: " + number); } if (number > correctAnswer) { document.writeln("Large guess: " + number); } if (number == correctAnswer) { document.writeln("Correct guess: " + number); } } } Simple random pic script that I found on the some other forum (I forget what it was) [CODE]var aryimages = new Array('images/pic/01.jpg', 'images/pic/37.jpg', 'images/pic/02.jpg', 'images/pic/family/08.jpg','images/pic/08.jpg', 'images/pic/food/03.jpg'); randompic.src = aryimages[Math.floor(Math.random() * aryimages.length)]; [CODE] code anchors to HTML markup [CODE]<img name="randompic" id="bg" />[CODE] it is working perfectly for Safari and Chrome. Nothing is showing for FF. Any suggestions appreciated. I just started learning JavaScript. I'm sure it's something really simple but I cannot figure out what is missing from my code. No matter what year I enter into the text box, I keep getting "The year you entered, 2080393, is a leap year." It's not returning false...ever. Can someone please tell me what I'm doing wrong so I can stop pulling my hair out? Thanks! <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>Is It A Leap Year?</title> <script type="text/javascript"> /* <![CDATA[ */ function isLeapYear(year) { year = parseInt(year); var leapYear = true; var remainder = 0; if(isDivisibleBy(year, 4)) { leapYear = true; } if(isDivisibleBy(year, 100)) { leapYear = false; } if(isDivisibleBy(year, 400)) { leapYear = true; } if(leapYear) { window.alert("The year you entered, " + year + ", is a leap year."); } else { window.alert("The year you entered, " + year + ", is not a leap year."); } } function isDivisibleBy(year, divisor) { var remainder = 0; remainder = year%divisor; if(remainder == 0) { return true; } else { return false; } } /* ]]> */ </script> </head> <body> <form action="" id="GetLeapYear"> <p><strong>Is It a Leap Year?</strong></p> <p><strong>Enter a valid 4-digit year below to find out!</strong><br /></p> <p> <input type="text" name="year" size="20" style="color: black; border-style: solid; border-color: inherit; border-width: medium; background-color: Transparent" value="" /> <input type="button" value="Leap Year?" onclick="isLeapYear(document.getElementById('GetLeapYear').year.value);" /> </p> </form> </body> index.html Code: <html> <head> <title>:: wtmp</title> </head> <body> <p>Welcome to my page</p> <script type="text/javascript" src="file.js"></script> </body> </html> file.js Code: function one(p1, p2) { var j_text=p1+" "+p2; return j_text; } function two() { var rslt=one("Hi", "there!"); } document.write(rslt); var mainscrpt=one("Hello", "world!"); window.alert(mainscrpt); Here's the basic concept, its somewhat of a counter, and I think javascripts the way to go... Basically I'm looking to generate a running number... x + 11 = print the number The tricky part is, I need it to never stop, somewhat like a timer (example http://www.hashemian.com/tools/javascript-countdown.htm) but not a timer... Think the tally mark on McDonald's 99 billion served.. Any suggestions or ideas? I would like to compare my work to the work of others, can you please solve this and post your result? Declare an 8 x 8 matrix and an array of length 22. 1.- Populate the matrix. 2.- Copy the elements of the first row of the matrix, the anti-diagonal, and the last row of the matrix into the array, to form a Z shape. 3.- Sort the array. 4.- Assuming that the data in the arrays are grades, compute the average of the grades stored in the even locations of the array. 5.- Copy the array back into the matrix.(back into the Z) 6.- Print out the matrix values. Hi... i have this code: <script type='text/javascript'> cookie_name = GetCookie("href_location"); if(cookie_name){ alert("Cookie found, redirecting to stored cookie."); document.location.href=cookie_name; } </script> I will like to create a iframe using the info from the "cookie_name" value <iframe name="FRAME1" src="cookie_name" width="350" height="320" frameborder="0" scrolling="auto"></iframe> I have a script that set a cookie with the value of a chosed website, what i need now is to create an iframe with that value. Any help will be apriciated. Sorry for my poor enlish I'm getting a runtime error on the code in red. Can anyone see anything that is wrong with it? These functions are called by an onBlur by the way. <script type="text/javascript"> //THIS GENERATES QUANTITY OF MATERIALS function generate3() { var tot3 = document.ComplianceForm.materialamount.value; var tbl3 = document.getElementById("quantitymaterials"); if (tot3 > 28) { alert("16 is total amount of materials for now."); } else { for(var i =1; i<=tot3; i++) { tbl3.innerHTML = tbl3.innerHTML + '<tr><td colspan=\"2\"><hr /></td></tr><tr><td>'+i+') Quantity: <input type=\"text\" name=\"qty'+i+'\" size=\"3\"\/></td><td>Description: <input type=\"text\" name=\"material'+i+'\" /></td></tr><tr><td colspan=\'2\' \">Your Truck<input type=\"checkbox\" \/> Other truck<input type=\"checkbox\" \/> Cash<input type=\"checkbox\" \/> Credit Card<input type=\"checkbox\" \/> P.O.<input type=\"checkbox\" \/></td></tr>'; } tbl3.innerHTML = tbl3.innerHTML + '<br>'; } } function remove3() { var tot3 = document.ComplianceForm.materialamount.value; var tbl3 = document.getElementById("quantitymaterials"); for(var i =1;i<=tot3; i++) { tbl3.innerHTML = ""; document.ComplianceForm.materialamount.value = ""; tbl3.innerHTML = ""; } } </script> I'm pretty new to Javascript so don't lay into me too hard. haha thanks. -Ty Hi everyone, I've been coding Perl for quite some time, but I'm new to Javascript and can't quite figure this out. I want to call a Javascript function that is sent to the browser via a perl script. When I hard code the string "Fargo" into the code it works just fine, when I pass the word Fargo via a variable the script will not call the function what so ever. Is it possible to call a javascript function via a Perl script with Perl providing all the necessary data? Here is my code: #!/usr/local/bin/perl use CGI qw(:all); use CGI::Carp qw(fatalsToBrowser); use Cwd; print header; print "top<br><br>"; $data = "fargo"; print <<html; <html><head></head>my heading is here<br><body></body> <script type="text/javascript"> function testz(inbound) { document.write("im in ", inbound); } //the script will only work if I uncomment the line below and comment out two lines below //var data1="fargo"; var data1 = $data; testz(data1); </script> </html> html Thanks to everyone in advance for your help. Hello, What I need is a simple service area, zip code validation form that redirects to a certain URL when a valid zip code is submitted and a different URL when an invalid zip code is submitted. I found a form script example that works well with only a single zip code. My problem is I can't figure out how to modify it so that multiple zip codes are valid. Here is the head part. 60016 is one of about 50 valid zip codes I need the form to accept as valid - [code] <script> var correctCode = "60016"; function validateCode() { var code = document.getElementById("codeTextBox").value; if (code == correctCode) { window.location.href = "/ggc/test1"; } else { window.location.href = "/ggc/test2"; } } </script> [code] Here is the body part - [code] Please enter your zip code: <input type="text" name="codeTextBox" id="codeTextBox" /> <input type="submit" name="Submit" value="Submit" onclick="validateCode()" /> [code] |