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 (576)
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] 2 3
1  Game Development / Newbie & Debugging Questions / Re: building new engine from scratch, networking/GUI questions on: 2012-04-14 20:18:39
Basic networking is pretty easy. The challenge is in designing your client/server model to suit your needs and not have lag.
In my junior year of college, our class was split into two groups of four and each group had to design and build its own mini mmorpg. We had one person do the web site, one do databases, and me and another guy coded the client and server. The trick is to keep the number of objects on your server as low as possible. Like don't concatenate string literals. That's a lot of overhead and it can crash the server. We made that mistake and our server couldnt support more than 15 people. Granted we still got a goodgrade because the other teams wouldn't even run.

It came out pretty cool though. You could walk around, cha, fight monsters, and pick up gold andweapons.
2  Game Development / Newbie & Debugging Questions / Re: Can I add an abstract class as a KeyListener? on: 2012-04-13 23:07:04
Just a simple luxury so I don't need to import or specify java.awt.event.KeyEvent.
3  Game Development / Newbie & Debugging Questions / Re: Can I add an abstract class as a KeyListener? on: 2012-04-13 19:25:03
Sure. This is what my keyPressed method looks like.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public void keyPresssd(KeyEvent e)
{
         switch (e.getKeyCode())
         {
               case KeyEvent.VK_ESCAPE:
                      keyboard[KEY_ESC] = true;
               case KeyEvent.VK_ENTER:
                      keyboard[KEY_ENTER] = true;
               ...
          }
}


That's the basic idea. The KEY variables are my own constants.

By the way, what is dependency injection?
4  Game Development / Newbie & Debugging Questions / Re: Can I add an abstract class as a KeyListener? on: 2012-04-13 18:35:16
Ok so basically you guys think that this is a bad idea. Can you guys maybe give me some code samples as to what you are suggesting?
5  Game Development / Newbie & Debugging Questions / Re: Can I add an abstract class as a KeyListener? on: 2012-04-12 20:47:22
@sproingie
Well the goal was to have a system where no matter where I am in the code I can poll the keyboard without passing objects around or having 8 keylisteners. The most obvious solution to me was an abstract class with static methods.

@Cruiser
I'm on my phone so I can't copy paste my code but I'll try to explain it.

I have an abstract class called Input. It doesn't extend anything and it doesn't use any interfaces. In it, I have an instance of a Keyboard class as well as an init and getKeyboard method both of which are public static methods.

Keyboard is a keylistener and has a ton of static int constants for key codes as well as an array of booleans that is the same size as the number of key code constants. Basically each array element represents an individual keys being pressed (true) or normal (false). There is a reset method which just sets all the key states to false, an isKeyDown method which returns true if the element of the provided key code is true, and an isKeyUp method which returns true if the element of the provided key code is false.

In the keyPressed method I have a switch statement for the key event's key code and for each case I will set the array element to true with my key constant. As an example, I might have case KeyEvent.VK_ESCAPE: keyboard[KEY_ESC] = true: where KEY_ESC is my constant for the escape key. I do the same thing for keyReleased but I'll set it to false instead because that means they let go of the key.

So say they press Q. KeyPressed will be called and it will prse the switch statement to find case KeyEvent.VK_Q. In that case, I will use my int constant for Q which might have set to 15 or something. I'll use that as my index for my boolean array and set element 15 (in this case KEY_Q I would have set to 15) to true because VK_Q was pressed. That all happens automatically in the EventDispatchThread. Now in my update method, after adding Input.getKeyboard() as a KeyListener, I can say if (Input.getKeyboard().isKeyDown(Keyboard.KEY_Q)) { ... } to detect if Key Q was pressed.

I hope I explained that well enough. Basically the goal was to be able to poll the keyboard from anywhere without needing object passing or registering more than one key listener.
6  Game Development / Newbie & Debugging Questions / Re: Can I add an abstract class as a KeyListener? on: 2012-04-12 17:45:37
Well I went to go try the singleton idea when I had an epiphany. I made a Keyboard class that implemented KeyListener that held all the key constants and the isKeyDown and isKeyUp methods. Then I can supply Input.keyboard as a listener and when I check inputs.

Unfortunately I think my idea of a static input won't work. I have an array of booleans to represent key states and a list of int constants for each key's spot in the array. Inside keylisteners methods I have a switch statement to check against KeyEvent's constants. Then I can set a certain key in the array to false or true. Finally isKeyDown checks the array for true or false on th key you query it about.

However, it seems there is some lag between the pressing and th query...ill keep at it, i don't think its doable.
7  Game Development / Newbie & Debugging Questions / Re: Can I add an abstract class as a KeyListener? on: 2012-04-12 17:01:45
A singleton eh? I'm not sure if that would work or not. I'll go try right now.

I want to have just a class where I can check the input from anywhere with static methods. So in any one game state I can say Input.isKeyDown and it will work. Not even sure if it is doable but I'm gonna try.

Thanks for the idea. 
8  Game Development / Newbie & Debugging Questions / Can I add an abstract class as a KeyListener? on: 2012-04-12 16:26:30
Hi guys.

Quick question. I want to make an abstract input class so I can poll the mouse and keyboard from a static context wherever, but I am not 100% sure if I can actually add it as a KeyListener since I can't instantiate it. Would Input.class work or is it not possible?
9  Games Center / 4K Game Competition - 2012 / Re: Community voting results on: 2012-03-17 16:25:55
Great job zeroone. Maybe I'll enter next year.
10  Game Development / Newbie & Debugging Questions / Re: <List> add question on: 2012-03-16 12:27:35
Where do you set removed to false? That could be the problem.
11  Game Development / Game Mechanics / Re: 2D Map Formats on: 2012-03-15 13:06:29
Just a simple txt file is a good 1st step. Just write your array into your file element with a loop, and separate them with a comma (,). When you read in your map data, just read in the entire line and use String's split to get your map in one go. Then you can loop through and for each one, use Integer.parseInt(splitArray);

Of course, ideally it would be better to make a map editor that saves it in the right format.
12  Discussions / Miscellaneous Topics / Re: Statistics project: Please take a few seconds to answer! :) on: 2012-03-13 21:57:49
22, Male, Caucasian80's Rock
13  Game Development / Newbie & Debugging Questions / Re: Could someone explain this line of code? on: 2012-03-13 21:10:06
Thanks ra4king, that was really helpful.

Still fiddling with the collision. Maybe it's just how my move code works is why it seems like it isn't colliding.
14  Game Development / Newbie & Debugging Questions / Re: Could someone explain this line of code? on: 2012-03-13 18:55:36
Maybe you guys could help me with this.

I'm just screwing around with this code trying to work it, and at most angles it is to-the-pixel collision. At some angles though it is closer than bounding box, but there's still a gap when it registers a collision. I'll post my 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  
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  
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashSet;


public class PixelPerfect extends JFrame implements KeyListener, Runnable {

   int x = 0, y = 0, xVel = 0, yVel = 0;
   HashSet<String> maskPlayer1, maskPlayer2;
   JPanel panel = new JPanel();
   BufferedImage b1, b2;
   
   @Override
   public void keyPressed(KeyEvent arg0) {
     
      if(arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
         xVel = 3;
      }
      if(arg0.getKeyCode() == KeyEvent.VK_LEFT) {
         xVel = -3;
      }
      if(arg0.getKeyCode() == KeyEvent.VK_DOWN) {
         yVel = 3;
      }
      if(arg0.getKeyCode() == KeyEvent.VK_UP) {
         yVel = -3;
      }
   }

   @Override
   public void keyReleased(KeyEvent arg0) {
     
      xVel = 0;
      yVel = 0;
   }

   @Override
   public void keyTyped(KeyEvent arg0) {
     
     
   }
   
   public void paint(Graphics g) {
     
      Graphics2D g2d = (Graphics2D)g;
      g2d.setColor(Color.white);
      g2d.fillRect(0,0,300,300);
     
      g2d.drawImage(b1.getSubimage(0, 0, 79, 77), x, y, 79, 77, null);
      g2d.drawImage(b2.getSubimage(0, 0, 67, 60), 200, 200, 67, 60, null);
   }


   public static void main(String[] args) {
     
      new Thread(new PixelPerfect()).start();
   }
   
   public PixelPerfect() {
     
            b1 = new BufferedImage(79, 77, BufferedImage.TYPE_INT_ARGB);
      b2 = new BufferedImage(67, 60, BufferedImage.TYPE_INT_ARGB);
         try {
            b1.createGraphics().drawImage(ImageIO.read(this.getClass().getResource("groudon.gif")),
                  0,0,79,77,null);
            b2.createGraphics().drawImage(ImageIO.read(this.getClass().getResource("squirrel.png")),
               0,0,67,60,null);
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
         
         
         setSize(300, 300);
      add(panel);
      setVisible(true);
     
      addKeyListener(this);
      setFocusable(true);
   }

   @Override
   public void run() {
     
      try {
         while(true) {
           
            update();
            repaint();
           
            Thread.sleep(17);
           
         }
      } catch(Exception ex) {
         
      }
   }

   private void update() {
     
      x += xVel;
      y += yVel;
     
      if(checkCollision()) {

         x -= xVel;
         y -= yVel;
      }
   }

   private boolean checkCollision() {
     
      // This method detects to see if the images overlap at all. If they do, collision is possible
            int ax1 = x;
            int ay1 = y;
            int ax2 = x + 79;
            int ay2 = y + 77;
            int bx1 = 200;
            int by1 = 200;
            int bx2 = 267;
            int by2 = 260;
           
            /*if(by2 < ay1 || ay2 < by1 || bx2 < ax1 || ax2 < bx1)
            {
               return false; // Collision is impossible.
            }
            else // Collision is possible.
            {*/

               // get the masks for both images
               maskPlayer1 = getMask(b1);
               maskPlayer2 = getMask(b2);
           
               maskPlayer1.retainAll(maskPlayer2);  // Check to see if any pixels in maskPlayer2 are the same as those in maskPlayer1
               
               if(maskPlayer1.size() > 0){  // if so, than there exists at least one pixel that is the same in both images, thus
                  return true;
               }
               return false;  
            //}
   }

//returns a HashSet of strings that list all the pixels in an image that aren't transparent
   // the pixels contained in the HashSet follow the guideline of:
   // x,y where x is the absolute x position of the pixel and y is the absolute y position of the pixel
   public HashSet<String> getMask(BufferedImage img){
     
      String s = "";
      HashSet<String> mask = new HashSet<String>();
      BufferedImage image = img;

      int pixel, a;
      for(int i = 0; i < image.getWidth(); i++){ // for every (x,y) component in the given box,
         for( int j = 0; j < image.getHeight(); j++){
     
            pixel = image.getRGB(i, j); // get the RGB value of the pixel
            a = (pixel >> 24) & 0xff;
     
            if(a != 0){  // if the alpha is not 0, it must be something other than transparent
               if(img == b1) {
                  s = (x + i) + "," + (y + j);
                  mask.add(s); // add the absolute x and absolute y coordinates
               }
               else {
                  s = (200 + i) + "," + (200 + j);
                  mask.add(s);
               }
            }
         }
      }
     
      return mask;  //return our set
   }

}


It's sloppy, but I don't care as I'm just messin' around.

Here's a screen of the near-collision.



These are fully-transparent pics.

Any ideas?
15  Game Development / Newbie & Debugging Questions / Re: Could someone explain this line of code? on: 2012-03-13 18:10:53
Interesting...

So, getRGB() must return an 0xAARRGGBB notation which you'd have to parse (for lack of a better word) to get the exact color data you want.

What is the 0xff for then? Is that to say I only want 2 bytes out of my 0xAARRGGBB? If I didn't specify, would I get everything up to >> 24 as well?
16  Game Development / Newbie & Debugging Questions / Re: Could someone explain this line of code? on: 2012-03-13 17:30:30
Ah! That's a little more clear.

How does 8 bits get you to green and 16 get you to red? I'll guess that it's no coincidence that there 8 bits in a byte.
17  Game Development / Newbie & Debugging Questions / Re: Could someone explain this line of code? on: 2012-03-13 17:14:34
Yeah, that's what I figured out. It's a shorthand for alpha as 0xAARRGGBB with the base 16 0-F I am assuming.

What exactly is it setting a equal to? I guess I'm asking what does & do with operands, why is the pixel being moved 24 bits, and why is it and 0xff operands to '&'?
18  Game Development / Newbie & Debugging Questions / Could someone explain this line of code? on: 2012-03-13 16:35:16
Hi guys!

I found this code for pixel-perfect collision, but there's 1 line I can't grasp...

Here's the 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  
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  
// returns a HashSet of strings that list all the pixels in an image that aren't transparent
   // the pixels contained in the HashSet follow the guideline of:
   // x,y where x is the absolute x position of the pixel and y is the absolute y position of the pixel
   public HashSet<String> getMask(GameObject go){
     
      HashSet<String> mask = new HashSet<String>();
      BufferedImage image = null;
      try {
         image = ImageIO.read(new File (go.getDefaultImageLocation()));
      } catch (IOException e) {
         System.out.println("error");
      }
      int pixel, a;
      for(int i = 0; i < image.getWidth(); i++){ // for every (x,y) component in the given box,
         for( int j = 0; j < image.getHeight(); j++){
     
      pixel = image.getRGB(i, j); // get the RGB value of the pixel
      a= (pixel >> 24) & 0xff;
     
      if(a != 0){  // if the alpha is not 0, it must be something other than transparent
         mask.add((go.xPos+i)+","+(go.yPos- j)); // add the absolute x and absolute y coordinates to our set
      }
         }
      }
     
      return mask;  //return our set
     
   }

// Returns true if there is a collision between object a and object b  
   public boolean checkCollision(GameObject a, GameObject b){
     
      // This method detects to see if the images overlap at all. If they do, collision is possible
      int ax1 = a.getxPos();
      int ay1 = a.getyPos();
      int ax2 = ax1 + a.getWidth();
      int ay2 = ay1 + a.getHeight();
      int bx1 = b.getxPos();
      int by1 = b.getyPos();
      int bx2 = bx1 + b.getWidth();
      int by2 = by1 + b.getHeight();
     
      if(by2 < ay1 || ay2 < by1 || bx2 < ax1 || ax2 < bx1)
      {
         return false; // Collision is impossible.
      }
      else // Collision is possible.
      {
         // get the masks for both images
         HashSet<String> maskPlayer1 = getMask(player1);
         HashSet<String> maskPlayer2 = getMask(player2);
     
         maskPlayer1.retainAll(maskPlayer2);  // Check to see if any pixels in maskPlayer2 are the same as those in maskPlayer1
         
         if(maskPlayer1.size() > 0){  // if so, than there exists at least one pixel that is the same in both images, thus
            System.out.println("Collision" + count);//  collision has occurred.
            count++;
            return true;
           
            }
         }
         return false;  
      }


This is the line I don't get:
1  
a= (pixel >> 24) & 0xff;


I have no experience with bitwise operators, but Google revealed that the 0xff notation is alpha-hex shorthand for fully transluscent. And the pixel >> 24 apparently moves the color data to the right 24 bits, but I don't know what that means. No clue what & means either and what it does to the operands.

Could someone please explain what this line does?
19  Game Development / Newbie & Debugging Questions / Re: When to reset input on: 2012-03-12 18:36:41
Oh, I see. You're having concurrency issues.

It shouldn't really make a difference if it was pressed this loop or not. The game loops upwards of 60x/second. I don't use variables, I just have the dispatch thread do my dirty work. =p

What exactly is it doing? Is there lag between a press and an action?

20  Game Development / Newbie & Debugging Questions / Re: When to reset input on: 2012-03-12 16:54:24
You don't need variables ^_^

Controls is a key listener, a mouse listener, and a mouse motion listener. Once you set it as the default listener with your component's addKeyListener, addMouseListener, and addMouseMotionListener methods, anytime any of those events happen, the respective method in Controls will be called 1 time.

So, after setting it as a listener, let's say you click the left mouse button. The Dispatch thread will be like "Oh! He pressed something on the mouse. Let's see who he told us was the class that should be notified. Controls? Ok, I'll call Controls' mousePressed method and pack the event details with it (MouseEvent)."

From there you can get the details on which exact mouse button it was with MouseEvent and make the player jump.

Does that make sense?
21  Game Development / Newbie & Debugging Questions / Re: When to reset input on: 2012-03-12 16:25:48
Well, for simple games, I have a Controls class that handles all mouse/keyboard input, but for a more complex game with menus and stuff you will probably want game states.

Basically though, Controls.java implements KeyListener, MouseListener, and MouseMotionListener and overrides all their respective methods. It also gets passed a reference to your game in the constructor so it can access things.

So, I can check in keyPressed method of Controls.java if the spacebar was pressed, if it was I can simply say game.getPlayer().jump(). Easy as pie!

The listeners run on a separate thread in the background. So while you do your normal update, render, sleep it waits in the background for an event to happen. When something does get pressed, Controls.java will catch it and handle it.
22  Game Development / Newbie & Debugging Questions / Re: Two entities in 1 image? on: 2012-03-12 13:11:43
Yeah, I was afraid that would be the solution...

So, maybe a base sprite sheet for each character w/o weapons. I can't really make the weapon sheet the same size (to have enough room to animate slashes and swings) because then I still don't have a bounding box. But that would be probably be best anyway because some of the weapons might ginormous, so that will keep all player sprites compact for their own collisions...

So do you guys agree with this? Having a base sprite for the character maybe with no arms or weapons (nvm that's weird), then have an overlay sheet for weapons and just do pixel-perfect on weapons?
23  Game Development / Newbie & Debugging Questions / Two entities in 1 image? on: 2012-03-11 16:28:28
Hi guys.

I'm thinking on making a fighting game soon, but there's a problem...

The characters will all have weapons. Now how will that work for collisions? I want to detect character collision against the weapons (not quite character vs character), which may be in different locations (in the sprite animation frame) depending on the move/animation being performed. How could a check a collision vs a certain portion of the other player entity if that portion is changing each frame?

Any ideas?
24  Game Development / Newbie & Debugging Questions / Re: GUI with Java2d - Need help on: 2012-03-11 15:36:41
Great post ra4king.

Yeah, it's best to do all your rendering/painting with 1 Graphics2D object. If you use 2 BufferedImage objects, every render you need to get the graphics context from both images, which could cause unneeded slowdown. Not to mention that one is alpha-enabled.

I've yet to try BufferStrategy out, but it sounds good.
25  Game Development / Newbie & Debugging Questions / Re: Paying to outsource artwork, anyone had success with this? on: 2012-03-11 15:24:31
This doesn't exactly answer your question, but you can learn it yourself:

http://www.petesqbsite.com/sections/tutorials/tuts/tsugumo/

They're great tuts. Kevglass recommended it.
26  Discussions / General Discussions / Re: This ever happen to you? on: 2012-03-09 23:29:24
I just have terrible Resident Evil + Fever Hallucination induced nightmares. xD

Oh dear god...

I've never dreamt about code, but I fall asleep thinking about it all the time. You guys take it to a whole 'nother level though, haha.
27  Games Center / WIP games, tools & toy projects / Re: Mr. Boots on: 2012-03-09 22:19:44
Very nice for a 1st game. It was quite fun.

The most obvious suggestion to me is to make it pretty. Put a nice paralax-scrolling background in maybe. You could make it a jungle theme and have totem poles or something.

Another thing you could do is add things you'd want to jump into like an hourglass that slows it a little bit. Or maybe really tall pillars with tiny platforms you need to land on to be high enough to make it.

Great job!
28  Game Development / Newbie & Debugging Questions / Re: Bullets? on: 2012-03-09 14:13:41
Since the only reference to each of your bullets is the array list, zngga is right. If you set an object to null, if there are no other variables tied to it, then it qualifies for garbage collection. It's nice 'cause it removes it from memory making room for more bullets.
29  Discussions / General Discussions / Re: FOUND! - Old ncSoft game that was built on jMonkeyEngine on: 2012-03-08 22:48:45
That looks very pretty.
30  Game Development / Networking & Multiplayer / Re: Starting point for multiplayer development needed! on: 2012-03-08 00:26:45
I did a small MMO demo with a few classmates in college using just the standard java stuff.

The number one thing I learned is to keep the number of server-side objects to a min. Strings are the number 1 culprit...Bastards. (Can I say that?)
Pages: [1] 2 3
 

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 (43 views)
2014-10-17 03:59:02

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

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

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

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

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

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

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

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

BurntPizza (80 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!