Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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 / Re: New to Java Networking in Gaming on: 2011-01-23 15:50:56
since there isn't really a lot going on in my code yet, ill post all of it

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  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311  
312  
313  
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

import java.io.*;

import javax.imageio.*;
import javax.swing.*;

import java.net.*;

public class NetGades
{
   static int awid = 256, ahei = 256;
   static Server serv = null;
   static Client cli = null;

   static Rect p1, p2;
   
   static boolean clef = false, crig = false;
   
   static boolean isserv = true;
   
   public static void main (String[]args)
   {
      JFrame aa = new JFrame("Game");
       aa.setSize(awid,ahei);
       aa.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       aa.setContentPane(new GPanel());
       KeyListener k = new KeyListener()
       {
          public void keyPressed(KeyEvent e)
          {
             int ckey = e.getKeyCode();
             if (ckey == KeyEvent.VK_LEFT) {clef = true;}
             if (ckey == KeyEvent.VK_RIGHT) {crig = true;}
             if (ckey == KeyEvent.VK_UP) {}
             if (ckey == KeyEvent.VK_DOWN) {}
             if (ckey == KeyEvent.VK_SPACE) {}
          }
          public void keyReleased(KeyEvent e)
          {
             int ckey = e.getKeyCode();
             if (ckey == KeyEvent.VK_LEFT) {clef = false;}
             if (ckey == KeyEvent.VK_RIGHT) {crig = false;}
             if (ckey == KeyEvent.VK_UP) {}
             if (ckey == KeyEvent.VK_DOWN) {}
             if (ckey == KeyEvent.VK_SPACE) {}
          }
          public void keyTyped(KeyEvent e) {}
       };
       aa.addKeyListener(k);
       
         MouseListener m = new MouseListener()
       {
          public void mouseClicked(MouseEvent e){}
          public void mouseEntered(MouseEvent e){}
          public void mouseExited(MouseEvent e){}
          public void mousePressed(MouseEvent e)
          {
          }
          public void mouseReleased(MouseEvent e)
          {
          }
       };
       aa.addMouseListener(m);
       
       MouseMotionListener mm = new MouseMotionListener()
       {      
          public void mouseMoved(MouseEvent e)
          {
          }
          public void mouseDragged(MouseEvent e)
          {
          }
       };
       aa.addMouseMotionListener(mm);
       
       aa.pack();
       aa.setVisible(true);
   }
   
   public static class GPanel extends JPanel
   {
      Timer tr;
     
      GPanel()
      {
         setPreferredSize(new Dimension(awid, ahei));
           setBorder(BorderFactory.createLineBorder(Color.BLACK));

           p1 = new Rect(80,80,16,16);
           p2 = new Rect(160,80,16,16);
           
           if (isserv)
           {
              serv = new Server(6999);
           }
           else
           {
              cli = new Client("127.0.0.1",6999);
           }
           
           tr = new Timer(10, new TimerAction());
           tr.start();
      }
     
       public void paintComponent(Graphics g)
       {
          g.setColor(Color.LIGHT_GRAY);
          g.fillRect(0,0,awid,ahei);
         
          p1.draw(g);
          p2.draw(g);
       }
       
       public class TimerAction implements ActionListener
       {
           public void actionPerformed(ActionEvent e)
          {
              if (clef) p2.x--;
              if (crig) p2.x++;
              if (isserv)
              {
                 serv.tick();
              }
              else
              {
                 cli.tick();
              }
               repaint();
           }
       }
   }
   
   public static class Rect
   {
      double x, y;
      double wid, hei;
     
      Rect(double a, double b, double c, double d)
      {
         x = a; y = b;
         wid = c; hei = d;
      }
     
      boolean checkCL(Rect a, double l1, double l2)
      {
         return (l1-1 <= a.x+a.wid-1 && l1-1 >= a.x && (l2+hei-1) >= a.y && l2 <= a.y+a.hei-1);
      }
      boolean checkCR(Rect a, double l1, double l2)
      {
         return (l1+wid <= a.x+a.wid-1 && l1+wid >= a.x && (l2+hei-1) >= a.y && l2 <= a.y+a.hei-1);
      }
      boolean checkCT(Rect a, double l1, double l2)
      {
         return (l2-1 <= a.y+a.hei-1 && l2-1 >= a.y && (l1+wid-1) >= a.x && l1 <= a.x+a.wid-1);
      }
      boolean checkCB(Rect a, double l1, double l2)
      {
         return (l2+hei <= a.y+a.hei-1 && l2+hei >= a.y && (l1+wid-1) >= a.x && l1 <= a.x+a.wid-1);
      }
      boolean checkCBox(Rect a, double l1, double l2)
      {
         return (l2+hei-1 >= a.y && l2 <= a.y+a.hei-1 && l1+wid-1 >= a.x && l1 <= a.x+a.wid-1);
      }
     
      void tick(int i) {}
      void draw(Graphics g)
      {
         g.setColor(Color.BLACK);
         g.fillRect((int)x,(int)y,(int)wid,(int)hei);
      }
   }
   
   public static class Server
   {
      private Socket          socket   = null;
      private ServerSocket    server   = null;
      private DataInputStream streamIn =  null;
      boolean foundClient = false;
      boolean done = false;
     
      public Server(int port)
      {  
         try
          {  
            server = new ServerSocket(port);  
          }
         catch(IOException ioe) {}
      }
     
      void tick()
      {
         if (!done)
         {
            try
             {  
               if (!foundClient)
               {
                  socket = server.accept();
                  foundClient = true;
                  open();
               }
               else
                 {  
                    try
                     {  
                       String temp = streamIn.readUTF();
                       p2.x = Double.parseDouble(temp);
                       //done = true;
                    }
                     catch(IOException ioe)
                     {  
                        //done = true;
                    }
                 }
                 
            } catch(IOException ioe) {}
         }
         else
         {
            try
            {
               close();
            }
            catch (IOException e) {}
         }
      }
     
      void open() throws IOException
      {  
         streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
      }
     
      void close() throws IOException
      {  
         if (socket != null)    socket.close();
          if (streamIn != null)  streamIn.close();
      }
   }

   public static class Client
   {  
      private Socket socket              = null;
      private DataInputStream  console   = null;
      private DataOutputStream streamOut = null;

      boolean hasServer = false;
     
      String name = "";
      int port = 6999;
     
      public Client(String serverName, int serverPort)
      {  
         name = serverName;
         port = serverPort;
         connect();
      }
     
      public void connect()
      {
         try
          {  
            socket = new Socket(name, port);
            hasServer = true;
             start();
          }
          catch(Exception e) {}
      }
     
      public void tick()
      {
         if (hasServer)
         {
             send(p2);
         }
         else
         {
            connect();
         }
      }
     
      public void send(Rect r)
      {
          try
          {  
            String temp = "0.0";
             temp = ""+r.x;
              streamOut.writeUTF(temp);
              streamOut.flush();
          }
          catch(Exception e) {}
      }
     
      public void start() throws IOException
      {  
         console   = new DataInputStream(System.in);
          streamOut = new DataOutputStream(socket.getOutputStream());
      }
      public void stop()
      {  
         try
          {  
            if (console   != null)  console.close();
              if (streamOut != null)  streamOut.close();
              if (socket    != null)  socket.close();
          }
          catch(Exception e) {}
      }
   }
   
}


if you change isserv to true at the beginning it'll run as the server program, if it's changed to false it'll be the client. what happens atm is the client program can use the arrow keys to move one of the boxes left and right, and the changes are supposed to be noticeable on the other's screen
2  Game Development / Networking & Multiplayer / Re: New to Java Networking in Gaming on: 2011-01-23 05:24:53
I see, thank you a lot guys.


What ended up happening so far is that I decided to take a stab at writing up some of my own code using the default java.net libraries since attempting to add the kryonet library was a little confusing for me and it's going okay so far, however I have a new problem.

The TCP connection I have works fine however at the beginning of my program, however as the program continues to run, the input from the client takes longer and longer to transfer to the server. I am thinking that this is probably because TCP processes every packet that comes through and eventually at some point it can no longer catch up, so how would I fix this? Would simply sending the packets less often work? Is there some way I could have my socket decide not to receive redundant packets?

thanks in advance guys.
3  Game Development / Networking & Multiplayer / Re: New to Java Networking in Gaming on: 2011-01-21 16:29:48
Oh, I see, okay then thanks for the help I guess I'll just use KyroNet because I really don't think I could set up a dedicated server... Thanks.
4  Game Development / Networking & Multiplayer / Re: New to Java Networking in Gaming on: 2011-01-21 16:21:59
With Kyronet, would one need to know the IP address of the server they wish to play on beforehand?
yes.
hmmm... do you know if there exist any libraries or methods in which the program could look up all available servers? if not i can do it like this, but being able to have some sort of lobby would be preferable
5  Game Development / Networking & Multiplayer / Re: New to Java Networking in Gaming on: 2011-01-21 16:14:14
Thanks. With Kyronet, would one need to know the IP address of the server they wish to play on beforehand? Also, if I use the KyroNet library, people who play the game won't need to download it, right?
6  Game Development / Networking & Multiplayer / New to Java Networking in Gaming on: 2011-01-21 15:55:38
So once again I come to ask a question about making a game in Java, this time about networking. I'm trying to make a game with a lobby that allows you to either create a new server, or lets you join from a list of currently available servers similar to the system from Haxball, Stick Arena, etc. However, I really have no idea how to start or what key words I should be using to look this up so any help would be appreciated.


(also i wanted to see what this smiley looks like) persecutioncomplex
7  Game Development / Game Mechanics / Re: Collision detection efficiency on: 2010-11-10 05:29:16
If profiling doesn't work for you, you can always turn off feature by feature and see the impact on the FPS. For example turn off collision detection and see FPS difference.
The main thing is that my frame rate fluctuates a LOT (one moment it'll be at 1-10ms per frame, and then suddenly itll jump to 200ms impreceptibly) and it actually changes so quickly that i cant read it.

Yeah, I guess I'll try netbeans profiler to see if profiling helps, but right now Im having problems with getting it to run in the Netbeans IDE. It seems as if it doesnt taking to running applets very well for some reason and converting my applet to an application is harder than it seems.
fakeedit: (okay so now I can get it to build correctly on Netbeans (I just added a main method, but the applet isnt showing up?)

If push comes to shove though, I will try turning things off and maybe slowing down the speed at which i read the frame rate or decreasing my sensitivity.

Thank you everyone for replying to my topic guys.
8  Game Development / Game Mechanics / Re: Collision detection efficiency on: 2010-11-09 15:09:02
Well actually, I use Eclipse not Netbeans, but I will try it out just to see the pofiler  Wink and converting the applet into an application doesn't look too difficult so I'll try that too, thanks a lot guys, I'll edit this post with results
9  Game Development / Game Mechanics / Re: Collision detection efficiency on: 2010-11-09 14:20:47
All right, thanks for your recommendation to use lists, I was using strings just because for some reason when I started coding, I had this thing against using non primitive data types and I guess I didn't bother to look up to see if there were any more suitable data types other than a string, so yeah, i'll try to use lists instead then.

I tried to set up a profiler, but unfortunately, the ones I've found seem to require a working build and I still don't know how to make one for an applet.
10  Game Development / Game Mechanics / Collision detection efficiency on: 2010-11-08 23:27:06
Hello, this is my first post here.

So I've been trying to program a 2d side scroller platformer on java by myself and it's going okay so far (though programming slopes and moving platforms were horrific) but I've decided that perhaps attempting something like this without any sort of feedback from others may not be the best idea, so I decided tojoin this place to ask questions.

On to my problem. What's happening right now is that my application occasionally gets sort of choppy. It runs okay, but I'd like the frame rate to be about as consistent as Cave Story, because even though it's playable as is, I'm a bit of a perfectionist. The choppiness is NOT what's affecting my collision, I have a special timer system built so that the game will either catch up or not move at all if too much lag builds up, so it's not like im having problems with characters clipping through platforms or anything.

My main problem is that, in my hunt to remove the occasionally choppiness/random game freezes, i've been going through (attempting to) optimize my code (i don't know much about code optimization unfortunately), and one of the things i got to is the way my collision system for objects works.

What I've done is I've created two arrays about the size of the room in pixels divided by 16 (so one average screen is about 40x40), one of them being an int array, the other one being a string array. what i'm doing is at the beginning of my collision loop I index the number of objects overlapping with a certain room tile in the integer array, and in the string array, i concatenate on the object id by converting it to two characters (0-65535, which should cover any number of objects that should get on screen). Then, during collision checking, each object reads off the list of objects from within every tile it overlaps and does collision checks with them. In this way, if I have some 200+ objects somewhere, they won't ALL be doing collision checks with ALL of them (since that many checks a loop seemed like just a little too much)

But I'm wondering if their is another, more efficient way of being able to only do collision checks with relevant objects that might be faster.

Of course, I'm worried that this might actually only marginally be affecting my frame rate, and that the occasional freezing and choppiness might be caused by something else, so any general advice on optimizing is helpful too, as well as possibly advice on

- Loop Structure/Threading (particularly, should I make another thread for key inputs or something similar to that?)
- Graphics Rendering (Right now I'm mostly just using h.drawImage(), but is there some graphics library that would improve my performance? (Portability is important))
- Swing? (I'm using a JApplet right now, but would Swing or something else be faster? Or would it just be overkill)
- This one's a little embarassing, but Building (I'm using Eclipse and so far I haven't been able to turn even a Hello World project into a .jar file)
- Anything else that might possibly be the source of my strange skipping
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.

Nickropheliac (16 views)
2014-08-31 22:59:12

TehJavaDev (24 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (42 views)
2014-08-19 09:29:53

Tekkerue (41 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (26 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (73 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
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!