El codigo esta bastante facil de entender ya que si se dan cuenta, es bastate similar a python con pygame. Lo comente para mayor comprension (lineas en mayuscula depues de un #). Esta clase lleva mas codigo como el crear el personaje, los enemigos, los eventos de teclado, etc., pero para no enredarse extraje nada mas el codigo referente al viewport y al parallax.
Mi objetivo con esto aportar al menos una idea para seguir mejorando pilas

- Código: Seleccionar todo
class Level1 < GameState
#ALGUNAS FUNCIONALIDADES COMO EL VIEWPORT SE DEBEN
#IMPORTARANTES DE USAR.
traits :viewport
#METODO INICIALIZADOR (CONSTRUCTOR):
def initialize(options = {})
super
#ASIGNANDO LOS PARAMETROS AL AREA O VIEWPORT: [x, y, ancho, alto]
self.viewport.game_area = [0, 0, 1280, 800]
#CREANDO UN PARALLAX DONDE SE TOMA COMO REFERENCIA LA
#ESQUINA SUPERIOR IZQUIERDA Y SE DIBUJA DESDE ALLI MISMO (x=0, y=0).
@parallax = Parallax.new(:x => 0, :y => 0, :rotation_center => :top_left)
#ADAPTANTE UNA CAPA AL PARALLAX, EN ESTE CASO UN FONDO DEL ESPACIO.
#NOTAR QUE SE REPITE CONSTANTEMENTE DE
#FORMA HORIZONTAL (REPEAT_X = TRUE)
#Y NO SE REPETIRA VERTICALMENTE (REPEAT_Y = FALSE)
@parallax.add_layer(
:image => 'space.jpg',
:repeat_x => true,
:repeat_y => false,
:damping => 1
)
end
#METODO ACTUALIZAR:
def update
super
#LA CAMARA SEGUIRA AL PERSONAJE
self.viewport.center_around(@player)
#CONFIGURADO DE ACUERDO A UN PLATAFORMAS ESTILO MARIO BROS.
@parallax.camera_y = self.viewport.y
@parallax.camera_x += 20
@parallax.update
#CUALQUIER OBJETO (EXCEPTO EL JUGADOR) SE DESTRUIRA SI SE
#SALE DEL VIEWPORT
game_objects.destroy_if{|game_object|
self.viewport.outside_game_area?(game_object)
}
end
#METODO DIBUJAR:
def draw
@parallax.draw
super
end
end