Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Networking & Multiplayer / [Kryonet] Online Snake Game on: 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..
2  Game Development / Game Play & Game Design / Algorithm for Pattern game on: 2011-03-01 03:11:38
Hello everyone,

Question:
    Can anyone direct me to good resources for pattern generation code? (i.e. code that will make patterns for me.)

Details:
    I want to make a pattern recognition game where the player must decide what the next element in the sequence should be. There are two ways, in my mind, to go about this:

    1) construct the patterns myself and implement them. OR
    2) think up an algorithm that I can use to generate the patterns.

    Naturally I prefer the latter but the resources I've found (from google searching and wiki) don't really help me much. If anyone can help me out I would greatly appreciate it
3  Game Development / Newbie & Debugging Questions / Re: Quick Question on: 2011-01-18 04:16:57
Haha, I've found a real Getto way to solve this problem.

1  
2  
3  
4  
5  
6  
try {
   do {
      Thread.sleep(100);
   } while(filename == null);
}
catch(Exception e) {}


Does anyone know of a method that will do this for me?
4  Game Development / Newbie & Debugging Questions / Re: Quick Question on: 2011-01-18 04:06:09
Ok, I did a little research online and I now believe the code does almost everything I need it to. The only problem is that when I run the program it doesn't wait for the user to enter values and the resulting fields end null when they shouldn't be. How can I make the program wait on the user?

Thanks All,

Updated Code:
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  
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class GUITest extends JFrame {
   
   private JTextField item1;
   
   public GUITest() {
      super("LaTeX Template");
      setLayout(new FlowLayout());
     
      item1 = new JTextField(10);
      add(item1);
     
      thehandler handler = new thehandler();
      item1.addActionListener(handler);
     
   }
   
   private class thehandler implements ActionListener {
      public void actionPerformed(ActionEvent event) {
     
         if(event.getSource() == item1) {
            TemplateCreator.filename = String.format("%s", event.getActionCommand());
            System.out.println(TemplateCreator.filename);
         }
      }
   }
   
}

5  Game Development / Newbie & Debugging Questions / Quick Question on: 2011-01-18 02:23:20
Hello All,

I'm in the process of making a GUI for a program that I wrote. What I want to do is take the character string that the user types into the text field and use it in main() like a regular string. Excuse me if this problem has been addressed in the past.

Here is my GUI thus far: Thanks to all those who respond.
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  
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;


public class GUITest extends JFrame {
   
   private String text;
   private static String filename;

   public GUITest() {
     
      super("LaTeX Template.exe");
      setSize(300,300);
      FlowLayout flo = new FlowLayout();
      setLayout(flo);
      JLabel Title = new JLabel("Doc Title:", JLabel.RIGHT);
      add(Title);
      JTextField text = new JTextField(20);
      add(text);
      JButton Create = new JButton("Create!");
      add(Create);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
     
   }
   
   public void actionPerformed(ActionEvent event) {
        String command = event.getActionCommand();
        if (command.equals("Create!")) {
           filename = "Hello";
        }
   }
   
   public static void main(String args[]) {
     
      GUITest Temp = new GUITest();
      if (filename != null) {
         System.out.print(filename);
      }
   }
   
}

6  Discussions / General Discussions / Re: Vote now in the official JGO logo contest thread with this topic on: 2011-01-14 04:41:23
B++
7  Games Center / Featured Games / Re: Age of Conquest III on: 2011-01-06 15:39:05
nice work, very enjoyable  Grin
8  Discussions / General Discussions / Re: A successful attack on SMF on: 2011-01-06 15:29:30
Wow, that's horrible  Undecided
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (26 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (50 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (75 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!