-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathExample5.java
More file actions
139 lines (114 loc) · 4.34 KB
/
Copy pathExample5.java
File metadata and controls
139 lines (114 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.animation.AnimationTimer;
import javafx.event.EventHandler;
import javafx.scene.input.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
// Collect the Money Bags!
public class Example5 extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage theStage)
{
theStage.setTitle( "Collect the Money Bags!" );
Group root = new Group();
Scene theScene = new Scene( root );
theStage.setScene( theScene );
Canvas canvas = new Canvas( 512, 512 );
root.getChildren().add( canvas );
ArrayList<String> input = new ArrayList<String>();
theScene.setOnKeyPressed(
new EventHandler<KeyEvent>()
{
public void handle(KeyEvent e)
{
String code = e.getCode().toString();
if ( !input.contains(code) )
input.add( code );
}
});
theScene.setOnKeyReleased(
new EventHandler<KeyEvent>()
{
public void handle(KeyEvent e)
{
String code = e.getCode().toString();
input.remove( code );
}
});
GraphicsContext gc = canvas.getGraphicsContext2D();
Font theFont = Font.font( "Helvetica", FontWeight.BOLD, 24 );
gc.setFont( theFont );
gc.setFill( Color.GREEN );
gc.setStroke( Color.BLACK );
gc.setLineWidth(1);
Sprite briefcase = new Sprite();
briefcase.setImage("briefcase.png");
briefcase.setPosition(200, 0);
ArrayList<Sprite> moneybagList = new ArrayList<Sprite>();
for (int i = 0; i < 15; i++)
{
Sprite moneybag = new Sprite();
moneybag.setImage("moneybag.png");
double px = 350 * Math.random() + 50;
double py = 350 * Math.random() + 50;
moneybag.setPosition(px,py);
moneybagList.add( moneybag );
}
LongValue lastNanoTime = new LongValue( System.nanoTime() );
IntValue score = new IntValue(0);
new AnimationTimer()
{
public void handle(long currentNanoTime)
{
// calculate time since last update.
double elapsedTime = (currentNanoTime - lastNanoTime.value) / 1000000000.0;
lastNanoTime.value = currentNanoTime;
// game logic
briefcase.setVelocity(0,0);
if (input.contains("LEFT"))
briefcase.addVelocity(-50,0);
if (input.contains("RIGHT"))
briefcase.addVelocity(50,0);
if (input.contains("UP"))
briefcase.addVelocity(0,-50);
if (input.contains("DOWN"))
briefcase.addVelocity(0,50);
briefcase.update(elapsedTime);
// collision detection
Iterator<Sprite> moneybagIter = moneybagList.iterator();
while ( moneybagIter.hasNext() )
{
Sprite moneybag = moneybagIter.next();
if ( briefcase.intersects(moneybag) )
{
moneybagIter.remove();
score.value++;
}
}
// render
gc.clearRect(0, 0, 512,512);
briefcase.render( gc );
for (Sprite moneybag : moneybagList )
moneybag.render( gc );
String pointsText = "Cash: $" + (100 * score.value);
gc.fillText( pointsText, 360, 36 );
gc.strokeText( pointsText, 360, 36 );
}
}.start();
theStage.show();
}
}