-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameWindow.java
More file actions
99 lines (89 loc) · 2.71 KB
/
GameWindow.java
File metadata and controls
99 lines (89 loc) · 2.71 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
package JavaRacer;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class GameWindow extends JPanel implements Runnable{
public final int tileSize = 32;
public final int collumn = 100;
public final int row = 100;
public final int WIDTH = tileSize*30;
public final int HEIGHT = tileSize*20;
private final int FPS = 60;
private final boolean CAMERA = false;
Thread gameThread;
KeyHandler keyHandle = new KeyHandler();
TileManager tileManager = new TileManager(this);
Agent[] agents = new Agent[100];
CollisionControl collisionControl = new CollisionControl(this);
Camera camera;
GenerationAlgorithm genAlg;
public GameWindow(){
Color backgroundColor = new Color(34, 139, 34);
for(int i = 0; i<agents.length;i++){
agents[i] = new Agent(this);
}
this.genAlg = new GenerationAlgorithm(agents);
this.camera = new Camera(this,keyHandle, agents[0].agentX, agents[0].agentY);
this.setBackground(backgroundColor);
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
this.setDoubleBuffered(true);
this.addKeyListener(keyHandle);
this.setFocusable(true);
}
public void startGameLoop(){
gameThread = new Thread(this);
gameThread.start();
}
@Override
public void run() {
while(gameThread != null){
//Update
update();
//Draw
repaint();
//set FPS
try {
Thread.sleep(1000/FPS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void update(){
for(int i = 0; i<agents.length;i++){
agents[i].update();
}
if(genAlg.generationNumber<genAlg.NUMBER_OF_GENERATIONS){
genAlg.checkGeneration();
}
camera.update();
}
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphic = (Graphics2D)graphics;
if(CAMERA){
tileManager.draw(graphic,camera);
}
else if(genAlg.eliteIndexes.size()>0){
tileManager.draw(graphic, camera, agents[genAlg.eliteIndexes.get(0)]);
}
else{
tileManager.draw(graphic, camera, agents[0]);
}
for (Agent agent : agents) {
agent.draw(graphic);
}
graphic.dispose();
}
public int getTileSize(){
return this.tileSize;
}
public int getRow(){
return this.row;
}
public int getCollumn(){
return this.collumn;
}
}