Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (564)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Kryonet] Online Snake Game  (Read 3087 times)
0 Members and 1 Guest are viewing this topic.
Offline Kiddo

Senior Newbie





« Posted 2011-12-19 21:54:39 »

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  Cool).

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



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  Tongue - on both ends).
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
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).
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
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..
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Grunnt (21 views)
2014-09-23 14:38:19

radar3301 (14 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (108 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!