Last modified: March 08, 2023
Long long ago, in a mystery land far away lived two little mosters, Bluey Blue and Pinky Pink. Pinky Pink is madly in love with Bluey Blue, but... alas, Bluey Blue doesn't want anything to do with Pinky Pink... on the surface. The truth is, Bluey Blue is madly in love with Pinky Pink too; its just that Bluey Blue is too shy to say it out loud. But wherever Bluey Blue goes, he leaves behind a trace so that Pinky Pink can catch up with him sooner or later. Eventually, Bluey Blue stopped to wait for Pinky Pink. The couple finally unites, and a little heart appears 💕
In this lab, you will recreate the legend of Bluey Blue and Pinky Pink.
You can download a demo of this lab here.
You may use the sprites from the demo's /images folder, or any images of your preference.
According to the legend, Blue "leaves behind a trace" wherever he goes so that Pinky Pink can follow the trace.
In Java, you can represent this idea of "trace" using the Point class together with the ArrayList class:
To keep track of where Blue has been, create an instance variable in Blue to hold an ArrayList of Point objects.
When it has been 100 frames since Pink was at the same position as Blue, Pink starts following the route Blue took by repeating the following steps:
Importing the ArrayList:
import java.util.ArrayList;
Creating an empty ArrayList:
ArrayList<Actor> actors = new ArrayList<Actor>(); // stores a list of actors
ArrayList<Point> points = new ArrayList<Point>(); // stores a list of points
More ArrayList methods:
method name | return type | description |
---|---|---|
boolean | add(E element) | Appends the specified element to the end of this list |
E | get(int index) | Returns the element at the specified index in this list |
E | remove(int index) | Remove and returns the element at the specified index |
int | size() | Returns the number of elements in this list |
Note: "E" is the class name of the object the ArrayList contains. If an ArrayList contains many points, "E" means "Point".
To learn more about ArrayList, you can read Java's official documentation here, or read CHS AP Computer Science's lesson on ArrayList
Importing the Point:
import java.awt.Point;
Creating a new Point:
Point newPoint = new Point(100, 200); // x is 100, y is 200
More Point methods:
method name | return type | description |
---|---|---|
distance(double px, double py) | double | Returns the distance from this Point to another x y coordinate |
distance(Point2D pt) | double | Returns the distance from this Point to a another Point |
static distance(double x1, double y1, double x2, double y2) | double | Returns the distance between two points whose coordinates are provided in the parameter |
To learn more about Point, you can read Java's official documentation here.
You must Sign In to submit to this assignment