-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStarfieldSimulation.java
More file actions
97 lines (73 loc) · 2.68 KB
/
StarfieldSimulation.java
File metadata and controls
97 lines (73 loc) · 2.68 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
import java.awt.*;
import java.util.ArrayList;
public class StarfieldSimulation implements Runnable {
private Window window;
private Panel panel;
private Thread thread;
private static final int FPS = 60;
private static final int UPS = 1;
public static final int HEIGHT = 800;
public static final int WIDTH = 1200;
public static final float SPEED = 8f;
private ArrayList<Star> stars;
public StarfieldSimulation(int numStars) {
panel = new Panel(this);
window = new Window(panel);
stars = new ArrayList<Star>();
for (int i = 0; i < numStars; i++) {
stars.add(new Star());
}
start();
}
public void start() {
thread = new Thread(this);
thread.start();
}
public void update() {
for (Star star : stars) {
star.update();
}
}
public void render(Graphics g) {
// Draw the stars
g.setColor(Color.BLACK);
g.fillRect(0, 0, StarfieldSimulation.WIDTH, StarfieldSimulation.HEIGHT);
g.setColor(Color.WHITE);
for (Star star : stars) {
star.render(g);
}
}
@Override
public void run() {
double timePerFrame = 1000000000.0 / FPS; // DFS: 1 second divided by the FPS
double timePerUpdate = 1000000000.0 / UPS; // DFS: 1 second divided by the UPS
long previousTime = System.nanoTime(); // DFS: Get the current time in nanoseconds
int frames = 0;
int updates = 0;
long lastCheck = System.currentTimeMillis();
double deltaFrame = 0;
double deltaUpdate = 0;
while (true) { // DFS: Main loop of the game
long currentTime = System.nanoTime();
deltaFrame += (currentTime - previousTime) / timePerFrame; // DFS: Calculate the deltaFrame
deltaUpdate += (currentTime - previousTime) / timePerUpdate; // DFS: Calculate the deltaUpdate
previousTime = currentTime;
if (deltaUpdate >= 1) { // DFS: If the deltaUpdate is greater than or equal to 1, update the game
update();
updates++;
deltaUpdate--;
}
if (deltaFrame >= 1) { // DFS: If the deltaFrame is greater than or equal to 1, render the game
panel.repaint();
frames++;
deltaFrame--;
}
if (System.currentTimeMillis() - lastCheck >= 1000) {
System.out.println("FPS: " + frames + " UPS: " + updates);
frames = 0;
updates = 0;
lastCheck = System.currentTimeMillis();
}
}
}
}