From 5e5c710018a5d4dc608612fa5f817c4fe1cdb77d Mon Sep 17 00:00:00 2001 From: Joe Finney Date: Wed, 28 Feb 2024 17:22:28 +0000 Subject: [PATCH] Introduce starfield classes --- Star.java | 39 +++++++++++++++++++++++++++++++++++++++ StarField.java | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 Star.java create mode 100644 StarField.java diff --git a/Star.java b/Star.java new file mode 100644 index 0000000..d30d659 --- /dev/null +++ b/Star.java @@ -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); + } + } +} \ No newline at end of file diff --git a/StarField.java b/StarField.java new file mode 100644 index 0000000..9bbe7c1 --- /dev/null +++ b/StarField.java @@ -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