Skip to content

Instantly share code, notes, and snippets.

@josejuan
Created November 22, 2012 08:27
Show Gist options
  • Select an option

  • Save josejuan/4129979 to your computer and use it in GitHub Desktop.

Select an option

Save josejuan/4129979 to your computer and use it in GitHub Desktop.
Star field
var NSTARS = 50;
var MAXX = 800;
var MAXY = 600;
var MAXVELOCIDAD = 10;
var MINVELOCIDAD = 1;
// Inicializamos todas las estrellas
var S = [];
for(var n = 0; n < NSTARS; n++)
S[n] = { x: 0
, y: 0
, dx: 0
, init: function() {
this.x = MAXX - 1;
this.y = ~~((MAXY - 1) * Math.random());
var f = Math.random();
this.dx = ~~(MINVELOCIDAD + (MAXVELOCIDAD - MINVELOCIDAD) * f);
this.s.fadeTo(0, (f + 1) * 0.5);
}
, step: function() {
this.x -= this.dx;
if(this.x < 0)
this.init();
this.s.offset({top: this.y, left: this.x});
}
, s: $('<img src="http://www.marketingplus.com.ar/images/star.png" style="position: absolute; z-index: 9999999" />')
};
// Inicializamos y añadimos al documento actual
for(var n = 0; n < NSTARS; n++) {
S[n].init();
S[n].x = ~~((MAXX - 1) * Math.random());
$(document.body).append(S[n].s);
}
// Creamos un timer para el ciclo del juego
window.setInterval(function() {for(var n = 0; n < NSTARS; n++) S[n].step()}, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment