Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //inspired by Pixel Zombies Live Wallpaper (https://play.google.com/store/apps/details?id=haydenTheAndroid.liveWallpaper.pixelZombies)
- //(c)2012 brian poole
- //to-do:
- //add gameover message
- //add settings interface
- //make hunters work
- //add nuke option/functionality
- //optimize!
- /*html
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- <head><title>Zombieland</title>
- <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
- <meta name="description" content="Zombieland" />
- <meta name="keywords" content="" />
- <meta name="robots" content="all" />
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
- <script type="text/javascript" src="zombie.js"></script>
- </head>
- <body style="text-align: center;">
- Zombieland
- <div id="zombieland" style="margin: 10px auto; width: 300px; height: 200px; border: 3px solid #cdcdcd; position: relative;">
- </div>
- </body>
- </html>
- end html*/
- var container_width;
- var container_height;
- $(document).ready(function( ){
- container_width = $('#zombieland').width( ) * .95;
- container_height = $('#zombieland').height( ) * .95;
- init_zombies( );
- });
- var game_over = false;
- var living = new Array( );
- var base_speed = 5;
- var zombie_speed = 1;
- var hunter_speed = 5;
- var civ_speed = 3;
- var zombie_start = 5;
- var hunter_start = 5;
- var civ_start = 50;
- function init_zombies( ){
- i = 0;
- for(z = 0; z < zombie_start; z++){
- x_pos = Math.floor(Math.random( ) * container_width);
- y_pos = Math.floor(Math.random( ) * container_height);
- t_zombie = $('<div class="zombie mover_' + i + '" />').appendTo('#zombieland');
- $(t_zombie).css({width: '5px', height: '5px', 'background-color': '#f00', position: 'absolute', top: y_pos + 'px', left: x_pos + 'px'});
- }
- for(h = 0; h < hunter_start; h++){
- x_pos = Math.floor(Math.random( ) * container_width);
- y_pos = Math.floor(Math.random( ) * container_height);
- t_hunter = $('<div class="hunter mover_' + i + '" />').appendTo('#zombieland');
- $(t_hunter).css({width: '6px', height: '6px', 'background-color': '#00f', position: 'absolute', top: y_pos + 'px', left: x_pos + 'px'});
- living[i] = t_hunter;
- i++;
- }
- for(c = 0; c < civ_start; c++){
- x_pos = Math.floor(Math.random( ) * container_width);
- y_pos = Math.floor(Math.random( ) * container_height);
- t_civ = $('<div class="civ mover_' + i + '" />').appendTo('#zombieland');
- $(t_civ).css({width: '4px', height: '4px', 'background-color': '#0f0', position: 'absolute', top: y_pos + 'px', left: x_pos + 'px'});
- living[i] = t_civ;
- i++;
- }
- run_zombies( );
- run_hunters( );
- run_civs( );
- }
- function run_zombies( ){
- $('.zombie').each(function( ){
- move_div(this, 4, 900);
- });
- if(!game_over){
- setTimeout("run_zombies( )", 500);
- }
- }
- function run_hunters( ){
- $('.hunter').each(function( ){
- move_div(this, 10, 500);
- });
- if(!game_over){
- setTimeout("run_hunters( )", 150);
- }
- }
- function run_civs( ){
- $('.civ').each(function( ){
- move_div(this, 8, 750);
- });
- if(!game_over){
- setTimeout("run_civs( )", 200);
- }
- }
- var check_countdown = 3;
- function move_div(t_obj, t_distance, t_duration){
- y_pos_orig = parseInt($(t_obj).css('top').replace('px'));
- x_pos_orig = parseInt($(t_obj).css('left').replace('px'));
- if($(t_obj).hasClass('zombie') && check_countdown <= 0){
- check_countdown = 3;
- victim_obj = get_closest_living(t_obj);
- if(!victim_obj){ game_over = true; }
- victim_y = parseInt($(victim_obj).css('top').replace('px'));
- victim_x = parseInt($(victim_obj).css('left').replace('px'));
- dir_y_mod = 0;
- if(y_pos_orig > victim_y){
- dir_y_mod = 1;
- }
- else if(y_pos_orig < victim_y){
- dir_y_mod = -1;
- }
- dir_x_mod = 0;
- if(x_pos_orig > victim_x){
- dir_x_mod = 1;
- }
- else if(x_pos_orig < victim_x){
- dir_x_mod = -1;
- }
- //1.5 modifier because they smell brains!
- y_pos = y_pos_orig - t_distance * dir_y_mod * 2;
- if(y_pos > container_height || y_pos < 0){
- y_pos = y_pos_orig;
- }
- x_pos = x_pos_orig - t_distance * dir_x_mod * 2;
- if(x_pos > container_width || x_pos < 0){
- x_pos = x_pos_orig;
- }
- }
- else{
- check_countdown--;
- dir_y_rand = Math.floor(Math.random( ) * 10) + 1;
- dir_y_mod = 0;
- if($(t_obj).hasClass('y_down') && ($(t_obj).hasClass('y_up') || dir_y_rand > 6)){
- dir_y_mod = -1;
- $(t_obj).toggleClass('y_up');
- }
- else if($(t_obj).hasClass('y_down') || dir_y_rand > 3){
- dir_y_mod = 1;
- $(t_obj).toggleClass('y_down');
- }
- dir_x_rand = Math.floor(Math.random( ) * 10) + 1;
- dir_x_mod = 0;
- if(!$(t_obj).hasClass('x_right') && ($(t_obj).hasClass('x_left') || dir_x_rand > 6)){
- dir_x_mod = -1;
- $(t_obj).toggleClass('x_left');
- }
- else if($(t_obj).hasClass('x_right') || dir_x_rand > 3){
- dir_x_mod = 1;
- $(t_obj).toggleClass('x_right');
- }
- y_pos = y_pos_orig - t_distance * dir_y_mod;
- if(y_pos > container_height || y_pos < 0){
- y_pos = y_pos_orig;
- }
- x_pos = x_pos_orig - t_distance * dir_x_mod;
- if(x_pos > container_width || x_pos < 0){
- x_pos = x_pos_orig;
- }
- living[parseInt($(t_obj).prop('id').replace(/^\w+_/, ''))] = t_obj;
- }
- $(t_obj).animate({top: y_pos + 'px', left: x_pos + 'px'}, {queue: false, duration: t_duration});
- }
- function get_closest_living(t_zombie){
- zombie_y = parseInt($(t_zombie).css('top').replace('px'))
- zombie_x = parseInt($(t_zombie).css('left').replace('px'))
- living_length = living.length;
- closest_living = false;
- closest_living_distance = 0;
- for(j = 0; j < living_length; j++){
- t_living = living[j];
- if(!t_living || t_living == undefined){ continue; }
- 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));
- if(t_distance < $(t_zombie).width( )){
- $(t_living).removeClass('civ');
- $(t_living).removeClass('hunter');
- $(t_living).addClass('zombie').css({width: '5px', height: '5px', 'background-color': '#f00'});
- living[j] = false;
- }
- else if(t_distance < closest_living_distance || closest_living_distance == 0){
- closest_living = t_living;
- closest_living_distance = t_distance;
- }
- }
- return closest_living;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement