Look at the following programs, add them to your project, play with them and understand them. You can use them to create your Dodge Game. However, you must use ArrayLists.
MouseFollower.java
OneSimpleAttractor.java
SimpleAttractors
Springs.java
Classwork:
The Dodge Game. Implement the game dodge using StdDraw: move a blue disc within the unit square to touch a randomly placed green disc, while avoiding the moving red discs. After each touch, add a new moving red disc.
/******************************************************************************
* Compilation: javac MouseFollower.java
* Execution: java MouseFollower
* Dependencies: StdDraw.java
*
* Draw a blue filled circle wherever the mouse is, in cyan if the
* mouse is pressed.
*
*
* % java MouseFollower
*
* Credits: Jeff Traer-Bernstein
*
******************************************************************************/
public class MouseFollower {
public static void main(String[] args) {
StdDraw.enableDoubleBuffering();
while (true) {
// mouse click
if (StdDraw.isMousePressed()) StdDraw.setPenColor(StdDraw.CYAN);
else StdDraw.setPenColor(StdDraw.BLUE);
// mouse location
StdDraw.clear();
double x = StdDraw.mouseX();
double y = StdDraw.mouseY();
StdDraw.filledCircle(x, y, 0.05);
StdDraw.show();
StdDraw.pause(10);
}
}
}
//Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
//Last updated: Fri Oct 20 14:12:12 EDT 2017.
/******************************************************************************
* Compilation: javac OneSimpleAttractor.java
* Execution: java OneSimpleAttractor
* Dependencies: StdDraw.java
*
* A particle is attracted to the current location of the mouse.
* Incorporates drag.
*
*
* % java OneSimpleAttractor
*
*
* Credits: Jeff Traer-Bernstein
*
******************************************************************************/
public class OneSimpleAttractor {
public static void main(String[] args) {
double rx = 0.0, ry = 0.0; // position
double vx = 0.0, vy = 0.0; // velocity
double mass = 1.0; // mass
double dt = 0.5; // time quantum
double drag = 0.1; // mess around with this a bit
double attractionStrength = 0.01;
// do the animation
StdDraw.enableDoubleBuffering();
while (true) {
// compute the attractive force to the mouse, accounting for drag
double dx = StdDraw.mouseX() - rx;
double dy = StdDraw.mouseY() - ry;
double fx = (dx * attractionStrength) - (drag * vx);
double fy = (dy * attractionStrength) - (drag * vy);
// Euler step: update velocity, then position
vx += fx * dt / mass;
vy += fy * dt / mass;
rx += vx * dt;
ry += vy * dt;
// draw particle
StdDraw.clear();
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.filledCircle(rx, ry, 0.02);
StdDraw.show();
StdDraw.pause(10);
}
}
}
Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.
/******************************************************************************
* Compilation: javac SimpleAttractors.java
* Execution: java SimpleAttractors n
* Dependencies: StdDraw.java
*
* n particles are attracted to the mouse; randomly rearrange when
* user clicks.
*
* % java SimpleAttractors 20
*
* Credits: Jeff Traer-Bernstein
*
******************************************************************************/
public class SimpleAttractors {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
double[] rx = new double[n];
double[] ry = new double[n];
double[] vx = new double[n];
double[] vy = new double[n];
double dt = 0.5;
double mass = 1.0;
double drag = 0.05; // try changing this to 0.1 or 0.01 or even 0...
double attractionStrength = 0.01;
// initialize the drawing area
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.enableDoubleBuffering();
StdDraw.setScale(-1, +1);
// do the animation
while (true) {
// if the mouse is pressed add some random velocity to all the particles
if (StdDraw.isMousePressed()) {
for (int i = 0; i < n; i++) {
vx[i] = 0.2 * Math.random() - 0.1;
vy[i] = 0.2 * Math.random() - 0.1;
}
}
// clear all the forces
double[] fx = new double[n];
double[] fy = new double[n];
// add attraction forces for attraction to the mouse
for (int i = 0; i < n; i++) {
double dx = StdDraw.mouseX() - rx[i];
double dy = StdDraw.mouseY() - ry[i];
fx[i] += attractionStrength * dx;
fy[i] += attractionStrength * dy;
}
// add drag forces to all particles
// drag is proportional to velocity in the opposite direction
for (int i = 0; i < n; i++) {
fx[i] += -drag * vx[i];
fy[i] += -drag * vy[i];
}
// update positions
// euler step
for (int i = 0; i < n; i++) {
vx[i] += fx[i] * dt / mass;
vy[i] += fy[i] * dt / mass;
rx[i] += vx[i] * dt;
ry[i] += vy[i] * dt;
}
StdDraw.clear();
// draw a filled circle for each particle
for (int i = 0; i < n; i++) {
StdDraw.filledCircle(rx[i], ry[i], 0.01);
}
StdDraw.show();
StdDraw.pause(10);
}
}
}
/******************************************************************************
* Compilation: javac Springs.java
* Execution: java Springs N
* Dependencies: StdDraw.java
*
* Simulates a system of springs.
*
* % java Springs 15
*
* Credits: Jeff Traer-Bernstein
*
******************************************************************************/
public class Springs {
public static void main(String[] args) {
// mess around with this, try 7, 8, 9, 10, 11, 12, 15
// probably have to turn down the spring force to keep it stable after that...
int n = Integer.parseInt(args[0]);
double[] rx = new double[n];
double[] ry = new double[n];
double[] vy = new double[n];
double[] vx = new double[n];
double particleMass = 1.0;
double drag = 0.1;
double springStrength = 0.1;
double springLength = 30;
double gravity = 1.0;
double timeStep = 0.5;
// set up the drawing area
StdDraw.setXscale(0, 100);
StdDraw.setYscale(0, 100);
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.setPenRadius(0.0025);
StdDraw.enableDoubleBuffering();
// initialize the particle positions randomly
for (int i = 0; i < n; i++) {
rx[i] = 100 * Math.random();
ry[i] = 100 * Math.random();
}
// do the animation
while (true) {
// clear all the forces
double[] fx = new double[n];
double[] fy = new double[n];
// spring forces act between every pairing of particles
// spring force is proportional to the difference between the rest length of the spring
// and the distance between the 2 particles it's acting on
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) continue;
// calculate distance between particles i and j
double dx = rx[j] - rx[i];
double dy = ry[j] - ry[i];
double length = Math.sqrt(dx*dx + dy*dy);
// figure out the force
double force = springStrength * (length - springLength);
double springForceX = force * dx / length;
double springForceY = force * dy / length;
// update the force
fx[i] += springForceX;
fy[i] += springForceY;
}
}
// add drag force
// drag is proportional to velocity but in the opposite direction
for (int i = 0; i < n; i++) {
fx[i] += -drag * vx[i];
fy[i] += -drag * vy[i];
}
// add gravity forces
// just add some force pointing down to all of them
for (int i = 0; i < n; i++) {
fy[i] += -gravity;
}
// fix particle 1 at the mouse position
rx[0] = StdDraw.mouseX();
ry[0] = StdDraw.mouseY();
vx[0] = 0.0;
vy[0] = 0.0;
fx[0] = 0.0;
fy[0] = 0.0;
// update positions using approximation
for (int i = 0; i < n; i++) {
vx[i] += fx[i] * timeStep/particleMass;
vy[i] += fy[i] * timeStep/particleMass;
rx[i] += vx[i] * timeStep;
ry[i] += vy[i] * timeStep;
}
// clear
StdDraw.clear();
// draw everything
for (int i = 0; i < n; i++) {
// draw a circle for each node
StdDraw.filledCircle(rx[i], ry[i], 1.0);
// draw the connections between every 2 nodes
for (int j = 0; j < i; j++) {
StdDraw.line(rx[i], ry[i], rx[j], ry[j]);
}
}
// show and wait
StdDraw.show();
StdDraw.pause(10);
}
}
}
Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.
If you are missing isMousePressed in any of your programs, you should copy the StdDraw below to your original. StdDraw Code


