Skip to content

Commit

Permalink
JavaFx Game
Browse files Browse the repository at this point in the history
  • Loading branch information
khalidt committed Feb 22, 2016
1 parent 6ff7ee2 commit b4aa8a7
Show file tree
Hide file tree
Showing 9 changed files with 403 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Final_SA/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre1.8.0_31"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions Final_SA/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions Final_SA/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Final_SA</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions Final_SA/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
62 changes: 62 additions & 0 deletions Final_SA/src/Chick.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//package eggHunt;
/**
* Snakes in the Grass.
* Chick.java
*
* @author Alice Fischer
* @version March 18, 2012, from version of 4/30/07.
*/
import javafx.application.Platform;

public class Chick extends Thread {
private EggHunt hunt;
private int speed;
//--------------------------------------------------------------------------
public Chick( EggHunt hunt, int speed ) {
this.hunt = hunt;
this.speed = speed;
}

//--------------------------------------------------------------------------
/** The hen alternates between laying eggs and taking naps.
* The sleep command puts a thread into a suspended state. When the nap is
* over the interrupt system will throw an exception, which we receive in
* the catch block.
*/
public void run() {
System.out.println(" Chick is running. ");
try {
while ( ! hunt.isDone() ){
Egg e = new Egg(10+Math.random()*380, 10+Math.random()*380 );
synchronized (hunt) {
hunt.oEggList.add( e );
hunt.laid++;
}
layOnGrass( e );
// Lay eggs at slightly random times and take naps between eggs.
sleep (speed + (int)(Math.random() * 1000 )); // milliseconds
}
}
// Come here when the nap is over.
catch (InterruptedException e) { return; }
}


//--------------------------------------------------------------------------
public void layOnGrass( final Egg eg ){
Platform.runLater( new Runnable() {
public void run() {
hunt.grass.getChildren().add( eg );
}
});
}

//--------------------------------------------------------------------------
public void tracker( final String s ){
Platform.runLater( new Runnable() {
public void run() {
hunt.message.setText( s );
}
});
}
}
32 changes: 32 additions & 0 deletions Final_SA/src/Egg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//package eggHunt;
/**
* Egg.java
* The Data class for EggCarton
*
* @author Alice Fischer
* @version March, 2015, from earlier versions of March 2013, 3/18/08, 11/28/04.
*/
import javafx.scene.shape.Ellipse;
import javafx.scene.paint.*;
import javafx.geometry.Point2D; // for the click-point

public class Egg extends Ellipse {
private static double EggLong = 18;
private static double EggHigh = 14;

//--------------------------------------------------------------------------
public Egg( double x, double y ) {
setCenterX( x );
setCenterY( y );
setRadiusX( EggLong );
setRadiusY( EggHigh );

double r = Math.random()* .5 + .4; // Choose a color.
double g = Math.random()* .5 + .4;
double b = Math.random()* .5 + .4;
setFill( new Color( r, g, b, 1) );
System.out.printf("Egg at %f.2, %f.2\n", x, y);
}

Point2D getCenter() { return new Point2D(getCenterX(), getCenterY()); }
}
165 changes: 165 additions & 0 deletions Final_SA/src/EggHunt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
//package eggHunt;
/**
* Snakes in the eggList.
* EggHunt.java
*
* @author Alice Fischer
* @version March 18, 2012, from version of 4/30/07.
*/
import javafx.application.Application;
import javafx.application.Application.Parameters;
import javafx.application.Platform;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Line;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.event.EventHandler;
import javafx.scene.text.*; // Text
import javafx.geometry.*; // for Insets
import javafx.animation.*;
import javafx.util.*;

import java.util.List;
import java.util.ArrayList;

import javafx.collections.ObservableList;
import javafx.collections.ListChangeListener;
import javafx.collections.FXCollections;

//==============================================================================
public class EggHunt extends Application {
// These are for the command-line arguments.
private int speed;
double radiusSq;
private long lifetime;

// This is the model.
private boolean done = false;
int eggs = 1; // The number of eggs currently available.
int laid = 0; // The number of eggs the chick has laid.
final int goal = 15; // the number of eggs for chick to win.

private ArrayList< Egg > eggList = new ArrayList<Egg>();
ObservableList< Egg > oEggList = FXCollections.observableArrayList(eggList);

// This class is used to produce a concurrent threads that shares the model.
private Chick C;
//private Referee R;

// These variables are part of the GUI view.
Text mouse = new Text(" "); // Click coordinate display
Text message = new Text(" "); // Win or lose message

BorderPane mainPane = new BorderPane();
Pane grass = new Pane(); // Display of the eggList.
Scene sn;

//--------------------------------------------------------------------------
// Chick will stop laying when done becomes true.
public boolean isDone() { return done; }
public long getLifetime() { return lifetime; }

//==========================================================================
private void buildGui() {
Color bColor = new Color(.95, 1, .8, 1.);
BackgroundFill bFill = new BackgroundFill( bColor, null, new Insets(2));
Background bkGrass = new Background( bFill );
grass.setBackground( bkGrass );

VBox p2 = new VBox(); // Scoreboard.
p2.getChildren().addAll(message, mouse);

mainPane.setCenter(grass);
mainPane.setBottom(p2);
sn = new Scene( mainPane, 400, 430 );
System.out.println("Gui is built.");
}

/*=========================================================================
* @version March 2015, from versions of March 18, 2012, 4/30/07.
*
* @param arg[0] Base frequency at which the hen lays eggs, in milliseconds.
* @param arg[1] Distance from snake's birthplace at which he can eat an egg.
* @param arg[2] Lifetime of a snake, in milliseconds.
*/
public void start( Stage st ) throws InterruptedException {

// Get and process the command line arguments --------------------------
Application.Parameters params = getParameters();
final java.util.List<String> argv = params.getRaw();

// If user supplied command-line arguments, use them.
if (argv.size() == 3) {
speed = Integer.parseInt(argv.get(0));
double radius = Double.parseDouble( argv.get(1) );
radiusSq = radius * radius;
lifetime = Long.parseLong( argv.get(2) );
System.out.println("Arg 0 = " + speed + ", Arg 1 = " + radius +
", Arg 2 = " + lifetime);
}
else { // Set the parameters to relatively small values.
speed = 0;
radiusSq = 900;
lifetime = 2000;
}
//----------------------------------------------------------------------
grass.setOnMouseClicked( e -> { doClick( e ); } );

oEggList.addListener( new ListChangeListener(){
public void onChanged( ListChangeListener.Change change) {
int eggs = oEggList.size(); // Current size of arraylist.
System.out.println (" List change event caught");

if (eggs >= goal) {
done = true;
tracker(" I won! There are " + eggs + " eggs left!");
}
else if (eggs == 0) { // Announce the score.
done = true;
tracker(" Super! You won!" );
}
else {
tracker(" There are " + eggs + " eggs.");
}
}
} );

//----------------------------------------------------------------------
buildGui();
st.setTitle("Snakes in the Grass");
st.setScene( sn );
st.show();

// Get a new Chicken and tell it to start laying eggs.
C = new Chick( this, speed );
C.start();
}

//--------------------------------------------------------------------------
public void tracker( final String s ){
Platform.runLater( new Runnable() {
public void run() {
message.setText( s );
}
});
}

//==========================================================================
// Handle a mouse click by spawning a Snake thread for that board position.
public void doClick( MouseEvent ev ){
double x = ev.getX();
double y = ev.getY();
mouse.setText(" New snake at ("+ x +", " +y +")" );
System.out.print(" New snake at ("+ x +", " +y +") " );
Snake hiss = new Snake( this, ev, lifetime );
hiss.start();
}
public static void main(String[] args) {
Application.launch(args);
}
}
28 changes: 28 additions & 0 deletions Final_SA/src/MyPoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//package eggHunt;
/**
* Egg.java
* The Data class for EggCarton
*
* @author Alice Fischer
* @version March, 2015, from earlier versions of March 2013, 3/18/08, 11/28/04.
*/
import javafx.scene.shape.Ellipse;
import javafx.scene.paint.*;
import javafx.geometry.Point2D; // for the click-point

public class MyPoint {
double x;
double y;

//--------------------------------------------------------------------------
public MyPoint( double x, double y ) {
this.x = x;
this.y = y;
}

double distanceSq( Point2D pt2 ){
double deltaX = x - pt2.getX();
double deltaY = y - pt2.getY();
return deltaX * deltaX + deltaY * deltaY;
}
}
Loading

0 comments on commit b4aa8a7

Please sign in to comment.