-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameScene.pde
More file actions
169 lines (149 loc) · 4.81 KB
/
GameScene.pde
File metadata and controls
169 lines (149 loc) · 4.81 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.util.concurrent.ThreadLocalRandom;
public class GameScene{
boolean online;
Locals locals;
Simple_NEAT n;
Grid gameGrid;
public GameScene(Locals l){
locals = l;
locals.level = 3;
resetAstroids(locals.level);
gameGrid = new Grid(50, l);
locals.player = new Ship(locals);
n = new Simple_NEAT(gameGrid.getWidth() * gameGrid.getWidth() + 3, 4);
Network temp = Network.loadFromFile("/Users/ryanwalt/Downloads/CODE/Java/Processing/Asteroids_Train/best.net");
n.addAgent(temp);
n.setCurrentAgent(0);
}
/**
* Show the text of the scene
*/
private void showText(){
textAlign(CENTER);
textSize(30);
String levelString = "Level " + locals.level + "\n" + locals.player.getScore();
fill(255);
textSize(30);
text(levelString, width/2, 50);
String liveString = "";
for (int i = 0; i < locals.player.getLives(); i++){
liveString += " | ";
}
text(liveString, width - 50, 50);
}
private void resetAstroids(int level){
locals.asteroids.clear();
int num = 2 + (level * 2);
for (int i = 0; i < num; i++){
float tempX = ThreadLocalRandom.current().nextInt(50, locals.width - 50);
float tempY = ThreadLocalRandom.current().nextInt(50, locals.height/2 - 150) + ((locals.height/2 + 150) * ((ThreadLocalRandom.current().nextInt(0, 2))));
locals.asteroids.add(new Asteroid(tempX, tempY, 3, locals));
locals.player.resetPos();
locals.player.clearBullets();
}
}
/**
* Display all of the asteroids to the screen.
*/
private void showAsteroids(){
try{
for (Asteroid a : locals.asteroids){
a.show();
}
}
catch(Exception e){
System.out.println("Exception in showAsteroids (gameScene): " + e);
}
}
private boolean checkLevel(){
if (locals.asteroids.size() < 1){
locals.level++;
resetAstroids(locals.level);
return true;
}
return false;
}
public void show(){
background(0);
showText();
// locals.player.showBullets();
locals.player.show();
runNetwork2();
showAsteroids();
checkLevel();
gameGrid.show();
}
public void runNetwork(){
float[] inputs = new float[33];
int c = 0;
for (Sensor s : locals.player.getSensors()){
inputs[c] = (float) s.getWeightValue();
c++;
}
for (Sensor s : locals.player.getSensors()){
inputs[c] = (float) s.getLevelValue();
c++;
}
inputs[32] = (float) locals.player.bullets.size() / 4;
n.runCurrent(inputs);
float[] outputs = n.getCurOutput();
if (outputs[0] > 0.5){
locals.player.shoot();
}
if (outputs[1] >=0.5){
locals.player.turnLeft();
}
if (outputs[2] >= 0.5){
locals.player.turnRight();
}
if (outputs[3] <= 0.5){
locals.player.accelerate = true;
locals.player.accelerate();
}
else{
locals.player.accelerate = false;
}
String outs = "";
for(float f : outputs){
outs +=(f + ", ");
}
textSize(20);
text(outs, width/2, height-50);
}
public void runNetwork2(){
float[] inputs = new float[gameGrid.getWidth() * gameGrid.getWidth() + 3];
inputs[0] = (float) locals.player.getX() / 900.0;
inputs[1] = (float) locals.player.getY() / 900.0;
inputs[2] = (float) locals.player.getAngle() / (2*3.14159265359);
int c = 3;
for (int x = 0; x < gameGrid.getGrid().length; x++){
for (int y = 0; y < gameGrid.getGrid().length; y++){
inputs[c] = (float) gameGrid.getGrid()[x][y];
}
}
n.runCurrent(inputs);
float[] outputs = n.getCurOutput();
if (outputs[0] > 0.5){
locals.player.shoot();
}
if (outputs[1] >=0.5){
locals.player.turnLeft();
}
if (outputs[2] >= 0.5){
locals.player.turnRight();
}
if (outputs[3] <= 0.5){
locals.player.accelerate = true;
locals.player.accelerate();
}
else{
locals.player.accelerate = false;
}
String outs = "";
for(float f : outputs){
outs +=(f + ", ");
}
textSize(20);
text(outs, width/2, height-50);
}
}