Advertisement
bpoole

Zombieland

Nov 20th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //inspired by Pixel Zombies Live Wallpaper (https://play.google.com/store/apps/details?id=haydenTheAndroid.liveWallpaper.pixelZombies)
  2. //(c)2012 brian poole
  3. //to-do:
  4. //add gameover message
  5. //add settings interface
  6. //make hunters work
  7. //add nuke option/functionality
  8. //optimize!
  9.  
  10. /*html
  11. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  12. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  13. <head><title>Zombieland</title>
  14. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  15. <meta name="description" content="Zombieland" />
  16. <meta name="keywords" content="" />
  17. <meta name="robots" content="all" />
  18. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  19. <script type="text/javascript" src="zombie.js"></script>
  20. </head>
  21. <body style="text-align: center;">
  22.  Zombieland
  23.  <div id="zombieland" style="margin: 10px auto; width: 300px; height: 200px; border: 3px solid #cdcdcd; position: relative;">
  24.  </div>
  25. </body>
  26. </html>
  27. end html*/
  28.  
  29. var container_width;
  30. var container_height;
  31. $(document).ready(function( ){
  32.     container_width = $('#zombieland').width( ) * .95;
  33.     container_height = $('#zombieland').height( ) * .95;
  34.     init_zombies( );
  35. });
  36.  
  37. var game_over = false;
  38.  
  39. var living = new Array( );
  40.  
  41. var base_speed = 5;
  42. var zombie_speed = 1;
  43. var hunter_speed = 5;
  44. var civ_speed = 3;
  45.  
  46. var zombie_start = 5;
  47. var hunter_start = 5;
  48. var civ_start = 50;
  49. function init_zombies( ){
  50.     i = 0;
  51.     for(z = 0; z < zombie_start; z++){
  52.         x_pos = Math.floor(Math.random( ) * container_width);
  53.         y_pos = Math.floor(Math.random( ) * container_height);
  54.         t_zombie = $('<div class="zombie mover_' + i + '" />').appendTo('#zombieland');
  55.         $(t_zombie).css({width: '5px', height: '5px', 'background-color': '#f00', position: 'absolute', top: y_pos + 'px', left: x_pos + 'px'});
  56.     }
  57.  
  58.     for(h = 0; h < hunter_start; h++){
  59.         x_pos = Math.floor(Math.random( ) * container_width);
  60.         y_pos = Math.floor(Math.random( ) * container_height);
  61.         t_hunter = $('<div class="hunter mover_' + i + '" />').appendTo('#zombieland');
  62.         $(t_hunter).css({width: '6px', height: '6px', 'background-color': '#00f', position: 'absolute', top: y_pos + 'px', left: x_pos + 'px'});
  63.         living[i] = t_hunter;
  64.         i++;
  65.     }
  66.  
  67.     for(c = 0; c < civ_start; c++){
  68.         x_pos = Math.floor(Math.random( ) * container_width);
  69.         y_pos = Math.floor(Math.random( ) * container_height);
  70.         t_civ = $('<div class="civ mover_' + i + '" />').appendTo('#zombieland');
  71.         $(t_civ).css({width: '4px', height: '4px', 'background-color': '#0f0', position: 'absolute', top: y_pos + 'px', left: x_pos + 'px'});
  72.         living[i] = t_civ;
  73.         i++;
  74.     }
  75.  
  76.     run_zombies( );
  77.     run_hunters( );
  78.     run_civs( );
  79. }
  80.  
  81. function run_zombies( ){
  82.     $('.zombie').each(function( ){
  83.         move_div(this, 4, 900);
  84.     });
  85.  
  86.     if(!game_over){
  87.         setTimeout("run_zombies( )", 500);
  88.     }
  89. }
  90.  
  91. function run_hunters( ){
  92.     $('.hunter').each(function( ){
  93.         move_div(this, 10, 500);
  94.     });
  95.  
  96.     if(!game_over){
  97.         setTimeout("run_hunters( )", 150);
  98.     }
  99. }
  100.  
  101. function run_civs( ){
  102.     $('.civ').each(function( ){
  103.         move_div(this, 8, 750);
  104.     });
  105.  
  106.     if(!game_over){
  107.         setTimeout("run_civs( )", 200);
  108.     }
  109. }
  110.  
  111. var check_countdown = 3;
  112. function move_div(t_obj, t_distance, t_duration){
  113.     y_pos_orig = parseInt($(t_obj).css('top').replace('px'));
  114.     x_pos_orig = parseInt($(t_obj).css('left').replace('px'));
  115.     if($(t_obj).hasClass('zombie') && check_countdown <= 0){
  116.         check_countdown = 3;
  117.         victim_obj = get_closest_living(t_obj);
  118.         if(!victim_obj){ game_over = true; }
  119.         victim_y = parseInt($(victim_obj).css('top').replace('px'));
  120.         victim_x = parseInt($(victim_obj).css('left').replace('px'));
  121.  
  122.         dir_y_mod = 0;
  123.         if(y_pos_orig > victim_y){
  124.             dir_y_mod = 1;
  125.         }
  126.         else if(y_pos_orig < victim_y){
  127.             dir_y_mod = -1;
  128.         }
  129.  
  130.         dir_x_mod = 0;
  131.         if(x_pos_orig > victim_x){
  132.             dir_x_mod = 1;
  133.         }
  134.         else if(x_pos_orig < victim_x){
  135.             dir_x_mod = -1;
  136.         }
  137.  
  138.         //1.5 modifier because they smell brains!
  139.         y_pos = y_pos_orig - t_distance * dir_y_mod * 2;
  140.         if(y_pos > container_height || y_pos < 0){
  141.             y_pos = y_pos_orig;
  142.         }
  143.  
  144.         x_pos = x_pos_orig - t_distance * dir_x_mod * 2;
  145.         if(x_pos > container_width || x_pos < 0){
  146.             x_pos = x_pos_orig;
  147.         }
  148.     }
  149.     else{
  150.         check_countdown--;
  151.         dir_y_rand = Math.floor(Math.random( ) * 10) + 1;
  152.         dir_y_mod = 0;
  153.         if($(t_obj).hasClass('y_down') && ($(t_obj).hasClass('y_up') || dir_y_rand > 6)){
  154.             dir_y_mod = -1;
  155.             $(t_obj).toggleClass('y_up');
  156.         }
  157.         else if($(t_obj).hasClass('y_down') || dir_y_rand > 3){
  158.             dir_y_mod = 1;
  159.             $(t_obj).toggleClass('y_down');
  160.         }
  161.  
  162.         dir_x_rand = Math.floor(Math.random( ) * 10) + 1;
  163.         dir_x_mod = 0;
  164.         if(!$(t_obj).hasClass('x_right') && ($(t_obj).hasClass('x_left') || dir_x_rand > 6)){
  165.             dir_x_mod = -1;
  166.             $(t_obj).toggleClass('x_left');
  167.         }
  168.         else if($(t_obj).hasClass('x_right') || dir_x_rand > 3){
  169.             dir_x_mod = 1;
  170.             $(t_obj).toggleClass('x_right');
  171.         }
  172.  
  173.         y_pos = y_pos_orig - t_distance * dir_y_mod;
  174.         if(y_pos > container_height || y_pos < 0){
  175.             y_pos = y_pos_orig;
  176.         }
  177.  
  178.         x_pos = x_pos_orig - t_distance * dir_x_mod;
  179.         if(x_pos > container_width || x_pos < 0){
  180.             x_pos = x_pos_orig;
  181.         }
  182.         living[parseInt($(t_obj).prop('id').replace(/^\w+_/, ''))] = t_obj;
  183.     }
  184.  
  185.     $(t_obj).animate({top: y_pos + 'px', left: x_pos + 'px'}, {queue: false, duration: t_duration});
  186. }
  187.  
  188. function get_closest_living(t_zombie){
  189.     zombie_y = parseInt($(t_zombie).css('top').replace('px'))
  190.     zombie_x = parseInt($(t_zombie).css('left').replace('px'))
  191.  
  192.     living_length = living.length;
  193.     closest_living = false;
  194.     closest_living_distance = 0;
  195.     for(j = 0; j < living_length; j++){
  196.         t_living = living[j];
  197.         if(!t_living || t_living == undefined){ continue; }
  198.  
  199.         t_distance = Math.sqrt(Math.pow(zombie_y - parseInt($(t_living).css('top').replace('px')), 2) + Math.pow(zombie_x - parseInt($(t_living).css('left').replace('px')), 2));
  200.         if(t_distance < $(t_zombie).width( )){
  201.             $(t_living).removeClass('civ');
  202.             $(t_living).removeClass('hunter');
  203.             $(t_living).addClass('zombie').css({width: '5px', height: '5px', 'background-color': '#f00'});
  204.             living[j] = false;
  205.         }
  206.         else if(t_distance < closest_living_distance || closest_living_distance == 0){
  207.             closest_living = t_living;
  208.             closest_living_distance = t_distance;
  209.         }
  210.     }
  211.  
  212.     return closest_living;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement