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 22: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.
|
|
|
|
|
3
|
Game Development / Newbie & Debugging Questions / Re: Can I add an abstract class as a KeyListener?
|
on: 2012-04-13 21: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?
|
|
|
|
|
5
|
Game Development / Newbie & Debugging Questions / Re: Can I add an abstract class as a KeyListener?
|
on: 2012-04-12 22: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 19: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.
|
|
|
|
|
11
|
Game Development / Game Mechanics / Re: 2D Map Formats
|
on: 2012-03-15 14: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.
|
|
|
|
|
14
|
Game Development / Newbie & Debugging Questions / Re: Could someone explain this line of code?
|
on: 2012-03-13 19: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) { 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() { 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; maskPlayer1 = getMask(b1); maskPlayer2 = getMask(b2); maskPlayer1.retainAll(maskPlayer2); if(maskPlayer1.size() > 0){ return true; } return false; }
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( int j = 0; j < image.getHeight(); j++){ pixel = image.getRGB(i, j); a = (pixel >> 24) & 0xff; if(a != 0){ if(img == b1) { s = (x + i) + "," + (y + j); mask.add(s); } else { s = (200 + i) + "," + (200 + j); mask.add(s); } } } } return mask; }
} |
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?
|
|
|
|
|
18
|
Game Development / Newbie & Debugging Questions / Could someone explain this line of code?
|
on: 2012-03-13 17: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
| 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( int j = 0; j < image.getHeight(); j++){ pixel = image.getRGB(i, j); a= (pixel >> 24) & 0xff; if(a != 0){ mask.add((go.xPos+i)+","+(go.yPos- j)); } } } return mask; }
public boolean checkCollision(GameObject a, GameObject b){ 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; } else { HashSet<String> maskPlayer1 = getMask(player1); HashSet<String> maskPlayer2 = getMask(player2); maskPlayer1.retainAll(maskPlayer2); if(maskPlayer1.size() > 0){ System.out.println("Collision" + count); 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 19: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 17: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 17: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 14: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 17: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 16: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.
|
|
|
|
|
26
|
Discussions / General Discussions / Re: This ever happen to you?
|
on: 2012-03-10 00: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 23: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 15: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.
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|