473,396 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Game In JavaScript

Hi all I have a small query in preparing this game the game is about shotting the balloons means the whole page will display balloons motion from bottom to up and each balloon will have a particular arrow key(means the image of the balloon will have any of the four arrow keys ) once clicked on the arrow keys on the keyboard the balloon should be blown off or become invisible. i have managed the image of the balloons to move from bottom to top and need help on how can i get the balloons invisible when the arrow keys are pressed....

the script is written below which is used to display the balloons to move from bottom to top
Expand|Select|Wrap|Line Numbers
  1.  
  2. <script language="JavaScript1.2">
  3. var no = 5; //15 image number or falling rate
  4. var speed = 5; // the lower the number the faster the image moves
  5. var snow = new Array();
  6. snow[0] = "baloon1.gif"
  7. snow[1] = "baloon2.gif"
  8. snow[2] = "baloon3.gif"
  9. snow[3] = "baloon4.gif"
  10.  
  11. var ns4up = (document.layers) ? 1 : 0;  // browser sniffer
  12. var ie4up = (document.all) ? 1 : 0;
  13. var ns6up = (document.getElementById&&!document.all) ? 1 : 0;
  14. var dx, xp, yp;    // coordinate and position variables
  15. var am, stx, sty;  // amplitude and step variables
  16. var i, doc_width = 1800, doc_height = 3000;
  17.  
  18. if (ns4up||ns6up) {
  19.         doc_width = self.innerWidth;
  20.         doc_height = self.innerHeight;
  21. } else if (ie4up) {
  22.         doc_width = document.body.clientWidth;
  23.         doc_height = document.body.clientHeight;
  24. }
  25.  
  26. dx = new Array();
  27. xp = new Array();
  28. yp = new Array();
  29. am = new Array();
  30. stx = new Array();
  31. sty = new Array();
  32. j = 0;
  33.  
  34. for (i = 0; i < no; ++ i) {
  35.         dx[i] = 0;                        // set coordinate variables
  36.         xp[i] = Math.random()*(doc_width-50);  // set position variables
  37.         yp[i] = Math.random()*doc_height;
  38.         am[i] = Math.random()*20;         // set amplitude variables
  39.         stx[i] = 0.02 + Math.random()/10; // set step variables
  40.         sty[i] = 0.7 + Math.random();     // set step variables
  41.         if (ns4up) {                      // set layers
  42.                 if (i == 0) {
  43. /*                        document.write("<layer name=\"dot"+ i +"\" left=\"15\" top=\"15\" visibility=\"show\"><img src=\""+ snow[j] + "\" border=\"0\"></layer>");
  44. */                        document.write("<layer name=\"dot"+ i +"\"  left=\"20\" top=\"20\" visibility=\"show\"><img src=\""+snow[j]+"\" border=\"0\"></layer>");
  45.                 } else {
  46.                         document.write("<layer name=\"dot"+ i +"\" left=\"15\" top=\"15\" visibility=\"show\"><img src=\""+ snow[j] + "\" border=\"0\"></layer>");
  47.                 }        } else if (ie4up||ns6up) {                if (i == 0) 
  48. {
  49.                         document.write("<div id=\"dot"+ i +"\" style=\"POSITION: absolute; Z-INDEX: "+ i +"VISIBILITY: visible; TOP: 15px; LEFT: 15px; width:1;\"><img src=\"" + snow[j] + "\" border=\"0\"></div>");
  50.                 } else {
  51.                         document.write("<div id=\"dot"+ i +"\" style=\"POSITION: absolute; Z-INDEX: "+ i +"VISIBILITY: visible; TOP: 15px; LEFT: 15px; width:1;\"><img src=\"" + snow[j] + "\" border=\"0\"></div>");
  52.                 }
  53.         }
  54.         if (j == (snow.length-1)) { j = 0; } else { j += 1; }
  55. }
  56.  
  57. function snowNS() {  // Netscape main animation function
  58.         for (i = 0; i < no; ++ i) {  // iterate for every dot
  59.                 yp[i] -= sty[i];     
  60.                        if (yp[i] < -50) {
  61.                         xp[i] = Math.random()*(doc_width-am[i]-30);
  62.                         yp[i] = doc_height;
  63.                         stx[i] = 0.02 + Math.random()/10;
  64.                         sty[i] = 0.7 + Math.random();
  65.                         doc_width = self.innerWidth;
  66.                         doc_height = self.innerHeight;                }
  67.                 dx[i] += stx[i];
  68.                 document.layers["dot"+i].top = yp[i]+pageYOffset;
  69.                 document.layers["dot"+i].left = xp[i] + 
  70. am[i]*Math.sin(dx[i]);
  71.         }
  72.         setTimeout("snowNS()", speed);
  73. }
  74.  
  75. function snowIE_NS6() {  // IE main animation function
  76.         for (i = 0; i < no; ++ i) {  // iterate for every baloon
  77.                 yp[i] -= sty[i];
  78.                 if (yp[i] < -50) {
  79. <!--                        xp[i] = Math.random()*(doc_width-am[i]-30);-->
  80.                         xp[i] = Math.random()*(doc_width-am[i]-15);
  81.                         yp[i] = doc_height;
  82.                         stx[i] = 0.02 + Math.random()/10;
  83.                         sty[i] = 0.7 + Math.random();
  84.                         doc_width = ns6up?window.innerWidth-5:document.body.clientWidth;
  85.                         doc_height = ns6up?window.innerHeight-5:document.body.clientHeight;
  86.                 }
  87.                 dx[i] += stx[i];
  88.                 if (ie4up){
  89.                 document.all["dot"+i].style.pixelTop = yp[i]+document.body.scrollTop;
  90.                 document.all["dot"+i].style.pixelLeft = xp[i] + am[i]*Math.sin(dx[i]);
  91.                 }
  92.                 else if (ns6up){
  93.                 document.getElementById("dot"+i).style.top=yp[i]+pageYOffset;
  94.                 document.getElementById("dot"+i).style.left=xp[i] + am[i]*Math.sin(dx[i]);
  95.                 }
  96.         }
  97.         setTimeout("snowIE_NS6()", speed);
  98. }
  99.  
  100. if (ns4up) {
  101.         snowNS();
  102. } else if (ie4up||ns6up) {
  103.         snowIE_NS6();
  104. }
  105.  
  106.  
  107. </script>
  108.  
Oct 20 '08 #1
1 1458
acoder
16,027 Expert Mod 8TB
The code is old. Did you write it or is it taken from somewhere? If you want cross-browser code which is cleaner and dropping support for old, buggy browsers like Netscape 4, then you'll have to start again, though you can use a lot of the code you already have.

For key events, you need to use the onkeypress/up event to detect key presses and call a function which works out which key was pressed and deals with it accordingly. Which key was pressed?
Oct 20 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tim Simmons | last post by:
I am stumped. I encoded the action = of my form using GET and I can't seem to get the property/value stuff from it using a JavaScript script I got from the web. I want to create a trivia game...
21
by: Nik Coughlin | last post by:
Are there methods for manipulating images in JavaScript that would allow me to write functions to rotate, skew, mask and resize images (bitmaps)? The functions need to be fast enough for use in a...
5
by: Mike P | last post by:
This should be fairly straightforward, but I can't figure out how to approach this.... I'd appreciate any ideas you have. Goal: Make recommendations on what a Monopoly player should do with...
6
by: jar13861 | last post by:
Create a game in a 3x3 HTML table that works as follows: * The goal of the game is to beat the computer by scoring more points than the computer. * The game starts when a hidden random number...
0
by: Bree | last post by:
The game requires it not to accept negative numbers. At the moment it isnt, and it is urgent I find the solution asap. So if anyone can help I would much appreciate it. Thanks Bree This is the...
20
by: NeedJavaHelp | last post by:
Hello everyone, first time poster here, bear with me. RuneScape is an online multiplayer game run by Java www.runescape.com. The game itself is run on the website and when you play the game for...
11
tpgames
by: tpgames | last post by:
I've struck zero in finding a Link to a sudoku game that actually uses images for numbers, even images OF numbers. Yes, I'd prefer the game in JavaScript. Every site I've been too, (hundreds) uses...
2
by: Hoss | last post by:
I created this cool game. It was made entirely with JavaScript (and a tiny bit of PHP) <a href="http://beem.awardspace.com">http://beem.awardspace.com</a> Snippet from the explanation ...
2
by: Hoss | last post by:
I created a cool game almost entirely with Javascript and wanted to see what people thought. See a screenshot at http://lh6.google.com/todd.freed/R4Q_gtqjtcI/AAAAAAAAAeI/eeNq2xOBxGw/beem.jpg ...
2
by: tesa | last post by:
I am not able to figure out how to make this work. I am trying to create a hangman game. I am in a basic javascripting class. I am to only use very basic code as you can see. I am able to use any...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.