Hello Everyone,
I’ve recently taken interest the Kryonet library and decided to share my simple results in hopes that people can provide feedback to help me better implement the library in future projects. I took the following two tutorials and combined them to make an online version of the classic Snake game. (please note this post isn’t intended to be a tutorial but you are welcome to the source code). (Also note, that I’m not taking credit for anything besides a crappy implementation of the Kryonet library 8)).
The tutorials (My sources):
- http://zetcode.com/tutorials/javagamestutorial/snake/
- http://code.google.com/p/kryonet/ (download library and find the Chat example)
The client / server Java projects (Sorry my code is so messy! - still learning):
- http://www.srmeier.info/development/OnlineSnake.zip
- http://www.srmeier.info/development/OnlineSnakeServer.zip
The .jar files which you should be able to run on localhost:
- http://www.srmeier.info/development/OnlineSnake.jar
- http://www.srmeier.info/development/OnlineSnakeServer.jar
http://srmeier.info/development/OnlineSnake.png
Naturally, just because it works for me doesn’t mean it will work for everyone so please let me know about you experiences. I have already found some bugs myself:
- When players disconnect the connection isn’t being removed correctly (I think).
- Running the server for more than 30mins with continuous connections causes all clients to crash.
- For this project I believe the UDP would work better than TCP but I don’t know enough to use UDP.
- Eating apples only works if more than one player is connected to server.
- Rendering thread and receiving-from-server thread are working together to cause some kinda weird rendering lag (see pic; lower worm).
- Etc… (I’m kinda just happy it works at all LoLz)
Also, I’ll add little bits of the code that might be of interest (so you don’t have to download everything).
Here I’m receiving the information I need to update worms from the server. I tried moving all the properties to a single object and send that object (which seems ideal) but I kept getting serialize/deserialize errors (I did register my object - on both ends).
public void received(Connection connection, Object object) {
if(object instanceof ArrayList) {
int[] addx = (int[]) ((ArrayList) object).get(0);
int[] addy = (int[]) ((ArrayList) object).get(1);
int addd = (Integer) ((ArrayList) object).get(2);
int refid = (Integer) ((ArrayList) object).get(3);
apple = (int[]) ((ArrayList) object).get(4);
for(int i=0; i<players.size(); i++) {
if(players.get(i).refid == refid) {
players.set(i, new Snake(addx, addy, addd, refid));
return;
}
}
players.add(new Snake(addx, addy, addd, refid));
return;
}
}
This is how it is rendering (I’m rendering the client worm separate from all the other worms).
if (inGame) {
g.drawImage(appleImg, apple_x, apple_y, this);
for (int z = 0; z < dots; z++) {
if (z == 0){
g.drawImage(headImg, x[z], y[z], this);
} else {
g.drawImage(ballImg, x[z], y[z], this);
}
}
for(int i=0; i<players.size(); i++) {
for (int z = 0; z < players.get(i).dots; z++) {
if (z == 0){
g.drawImage(headImg, players.get(i).x[z], players.get(i).y[z], this);
} else {
g.drawImage(ballImg, players.get(i).x[z], players.get(i).y[z], this);
}
}
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
Since this project has really pushed my limits as a programmer if I left anything out I apologize 1000X. Thank you for taking the time to read and possibly provide feedback! <3
P.S. - is it better to run the game loop in a thread or a timer? I’ve seen it done both ways…