Skip to content

Commit

Permalink
Introduce starfield classes
Browse files Browse the repository at this point in the history
  • Loading branch information
finneyj committed Feb 28, 2024
1 parent b8f6e02 commit 5e5c710
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Star.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
public class Star
{
private Ball b;
private double speedX;
private double speedY;

private double startX;
private double startY;

private GameArena arena;

public Star(double x, double y, double size, String colour, double speedX, double speedY, GameArena a)
{
this.startX = x;
this.startY = y;
this.speedX = speedX;
this.speedY = speedY;
this.arena = a;

b = new Ball(x, y, size, colour);
}

public void addTo(GameArena a)
{
a.addBall(b);
}

public void move()
{
b.move(speedX, speedY);

// Check bounds within the given GameArena
if (b.getXPosition() > arena.getArenaWidth() || b.getXPosition() < 0 || b.getYPosition() > arena.getArenaHeight() || b.getYPosition() < 0)
{
b.setXPosition(startX);
b.setYPosition(startY);
}
}
}
45 changes: 45 additions & 0 deletions StarField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* A class to represent a simple starfield effect.
* A cnfigurable number of stars appear in the centre of the screen,
* and move outward to the edges in a randome direction.
*/

public class Starfield
{
private Star[] stars;
private Ball horizon;
private int numberOfStars;
private double effectDepth = 1.0;
private double effectSpeed = 1.0;
private double starSize = 3;
private String starColour = "#DDDDDD";

private GameArena arena;

/**
* Constructor. Creates a Ball with the given parameters.
* @param numberOfStars The number of stars that should be on screen at any point in time
* @param speed The spparent speed of the effect - higher numbers are faster.
*/
public Starfield(int numberOfStars, double speed, GameArena a)
{
stars = new Star[numberOfStars];
this.effectSpeed = speed;
this.arena = a;

for (int i=0; i<stars.length; i++)
{
stars[i] = new Star(arena.getWidth()/2, arena.getHeight()/2, starSize, starColour, speed * (0.5 - Math.random()), speed * (0.5 - Math.random()), arena);
stars[i].addTo(a);
}

horizon = new Ball(arena.getWidth()/2, arena.getHeight()/2, 20, "BLACK");
arena.addBall(horizon);
}

public void move()
{
for (int i=0; i<stars.length; i++)
stars[i].move();
}
}

0 comments on commit 5e5c710

Please sign in to comment.