<script src="http://spelprogrammering.nu/simple.js">


function Zombie()
{
  this.distanceToBase = totalWidth;
  this.speed = 1 + random(5);
  this.angle = random(360);
  this.dead  = false;

  this.update = function()
  {
    save();
    
    translate(totalWidth / 2, totalHeight / 2);
    
    this.distanceToBase -= this.speed;
    rotate(this.angle);
    translate(this.distanceToBase, 0);
    
    if (this.angle > 90 && this.angle < 270) 
    {
      scale(1, -1); 
    }
    
    scale(0.5, 0.5);
    
    rectangle(  3, 30, 23, 65, "lightgreen");
    rectangle(  3, 15, 23, 65, "gray");
    rectangle(-35, 20, 30, 14, "lightgreen");
    rectangle(-20, 20, 30, 14, "gray");
    circle(15, 0, 20, "lightgreen");
    circle( 0, 0,  3, "red");
    
    restore();
  };
}

function start()
{
  zombies = [];
  zombiesKilled = 0;
  
  base = { x: totalWidth  / 2 - 15, 
           y: totalHeight / 2 - 25,
           size: 30 };
}

function update()
{
  fill("black");
  
  rectangle(base.x, base.y, 50, base.size, "blue");
  
  for (var i = 0; i < zombies.length; i++)
  {
    var zombie = zombies[i];
    
    if (! zombie.dead) { zombie.update(); }
    
    if (mouse.left && getPixel(mouse.x, mouse.y).green > 0)
    {
      zombiesKilled++;
      circle(mouse.x, mouse.y, 30, "red");
      zombie.dead = true;
    }
    
    if (zombie.distanceToBase < base.size)
    {
      text(100, 200, 30, "BASE SELF DESTRUCT", "yellow");
      stopUpdate();
    }
  }
  
  text(10, 45, 15, "Zombies killed: "+ zombiesKilled, "white");
  
  if (random(10) == 0) { zombies.push(new Zombie()); }
}

</script>
        
Gå tillbaka