Android Open Source - SpaceGame Stars






From Project

Back to project page SpaceGame.

License

The source code is released under:

Copyright (c) 2012 ??ukasz Patalas Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the So...

If you think the Android project SpaceGame listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.lpatalas.spacegame;
/*from  ww  w  . j  a  v  a  2  s .  c  o  m*/
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;

class Stars {
  private static final int STAR_COUNT = 20;
  private static final int STAR_LENGTH = 10;

  private final Vector2[] starPositions = new Vector2[STAR_COUNT];
  private final Texture texture;

  public Stars(Texture texture) {
    this.texture = texture;
  }

  public void initialize() {
    for (int i = 0; i < starPositions.length; i++) {
      starPositions[i] = new Vector2();
      randomizeStarPosition(starPositions[i], 0);
    }
  }

  public void render(SpriteBatch spriteBatch) {
    for (Vector2 starPosition : starPositions) {
      spriteBatch.draw(texture, starPosition.x, starPosition.y, STAR_LENGTH, 1);
    }
  }

  public void update(float deltaTime, float moveSpeed) {
    for (Vector2 starPosition : starPositions) {
      starPosition.x -= deltaTime * moveSpeed;
      if (isOutOfScreen(starPosition)) {
        randomizeStarPosition(starPosition, Gdx.graphics.getWidth());
      }
    }
  }

  private boolean isOutOfScreen(Vector2 starPosition) {
    return starPosition.x < 0;
  }

  private void randomizeStarPosition(Vector2 position, float xOffset) {
    position.x = MathUtils.random(xOffset, xOffset + Gdx.graphics.getWidth());
    position.y = MathUtils.random(Gdx.graphics.getHeight());
  }
}




Java Source Code List

com.lpatalas.spacegame.Assets.java
com.lpatalas.spacegame.Asteroid.java
com.lpatalas.spacegame.Asteroids.java
com.lpatalas.spacegame.CloudLayer.java
com.lpatalas.spacegame.Clouds.java
com.lpatalas.spacegame.DesktopStarter.java
com.lpatalas.spacegame.GameplayScreen.java
com.lpatalas.spacegame.MainMenuScreen.java
com.lpatalas.spacegame.Particles.java
com.lpatalas.spacegame.PlayerInputProcessor.java
com.lpatalas.spacegame.Player.java
com.lpatalas.spacegame.SpaceGameActivity.java
com.lpatalas.spacegame.SpaceGame.java
com.lpatalas.spacegame.Stars.java