So you have all given me some help before getting started using Java for games, but I feel like Im hitting a huge wall. All of the resources Ive used seem like they are not writing for a beginner. I have Killer Game Programming and it just has been a bunch of code with little explanation. I am not a beginner programmer, but I am a total newbie to graphics and games. I feel like I am not learning the basic game framework from the resources I have used. I went through Kev's Space Invaders tutorial over at his website cokeandcode, but I found myself getting lost in the complex parts. I am not an idiot, but is it impossible to find some sort of step by step intro that explains how to build a simple game from nothing? Ive created quite advanced applications for my CS classes, so I know Im not just a terrible programmer, I am just having a lot of trouble understanding the basics here.. such as loading images, painting to the screen, getting input, updating, etc. I thought I would stick with the basic JAva2d stuff because it would be better for me then diving into something like LWJGL, but I cant even seem to grasp this! It seems like every tutorial I read is a completely different approach. Just something as simple as loading a saved image to the screen has been done in so many ways... one book uses Image, another uses ImageIcon, another uses VolatileImage, another uses Sprite, etc, etc.. and thats just to load an image! Not to mention the method for painting and updating is so different and hard to grasp from one tutorial to the next, some are using Jpanels, others use Canvases, etc.

All I want to do is make something very basic like pong or whatever. If I can make that and TOTALLY understand whats happening throughout the code, and I can write it on my own without the guide, Im sure I can build off of that to make things more complex. I just feel like my brain is mush when I read these books and tutorials, nothing is clicking and Im becoming unbelievably frustrated. Ive always been a pretty good programmer but I feel totally useless at this. Can anyone lend a word of advice or whatever?

It sounds like you're looking for "the right way" to do 2d in java, and that's a source of frustration. But as usual in the real world, there isn't one - there are many different ways, which are good or bad depending on what your goals are.

For something basic like pong, you'll have to:1) draw stuff on the screen (note I didn't say load images - you can get away with simple shapes for this)2) process input in some way3) use some kind of timer to base your game logic off of, and update the game world

As long as you find and understand *a* way to do all three of the above, you'll be off to a good start. Don't worry about whether it's a particularly good way - that'll become more clear as you get more experience. Usually you'll just run into some limitation of the way you're doing it and then switch to a different way that fits your needs better. No need to try to understand it all at once.

- project 1. display a jframe.- project 2. draw something in a frame (a box?)- project 3. draw the box in a frame and make it move over a period of time (animate)- project 4. draw multiples boxes in the same frame and move them around over time.- project 5. draw a box in a frame & control it's movement via user input (key/mouse?)- project 6. stitch the above projects together to get a crude game working.

thanks to the both of you. So I managed to put this bit together tonight. if you keep the part where I paint paddle 1 commented out, it works fine, but if I un-comment it, the rectangle never gets painted. I don't understand why. If the ball paints and repaints with no problem, I don't see why the rectangle shouldn't. I also get some strange errors when I try to paint the rectangle, but not always.. any ideas? Also, am I doing anything here that can be done better or different? Thanks

I can see your paddle. The only problem is that it's so small (0, 0, 5, 25) that you can barely see it in the top left corner.

Another thing I would read up on is passive vs active rendering. Passive rendering uses the Event Dispatch Thread closely associated with Java Swing etc. It kind of sucks having to learn about this stuff right away but I noticed you spawned a new thread. Combining passive rendering with with threading without fully understanding what's going on will likely have you running into many hard to detect concurrency errors.

loom weaver - thanks! Its so weird, my rectangle was being drawn completely off screen. I thought drawing it at 0, 0 would make it visible but I had to shift it quite a bit. Anyway it works so thanks a lot. And I am going to change the Thread and use a timer instead to repaint every 20ms.

bigwavejake - thanks so much, that seems like a solid tutorial

So reacting to key presses is confusing me, I added an inner keyadapter class, but Im sure Im doing it wrong because nothing is working:

Ahh thanks.. it works! Ok next problem is that it doesnt seem to be able to detect simultaneous key presses. If Im holding the up arrow and moving the right paddle up, I am unable to detect a "W" press to move the left paddle up at the same time. If I hold them both, one will start moving only when I release the other. I tried making two separate inner class KeyListeners... one for the first paddle and one for the second, and just adding them both to my constructor but that isnt working either. Any ideas?

Ok so Im still working out getting the keys to work simultaneously, but heres what I have so far. Would you guys mind running it and letting me know if the collisions are looking ok? Since the window is decorated, I seem to be losing pixels on the top of the frame, so I had to use some weird numbers in my collisions to make it seem like the ball was hitting the walls and the paddles stopped at the edges. If I assumed that the edge that I could see was the actual border of the Frame, the objects would just go off of the screen. any critiques?? By the way I added a ball velocity as a constant, so you can just change the value of BALL_VELOCITY at the beginning to make it easier to change the ball speed. Sometimes its easier to see the collisions at a low speed, so you can just speed it up if its too slow.

If the key has been pressed (in this case the up arrowkey), the boolean at that index will be true which means the code inside the block will run.However if the key has been released the boolean in that index will be false.

Doing it without an array is:- (maybe) slightly more memory efficient- much slower accessing and adding elements- significantly slower checking whether many individual buttons are down- faster at checking against all buttons that are down- easier to see exactly how many buttons are being pressed

Honestly in most situations I think it's easier just to use arrays like Captain Awesome suggested, but this option can work better depending on what you want to do. BTW, I used a TreeSet because it enforces uniqueness (you can't add the same button multiple times), and because it's automatically sorted doing a contains() check is much faster than in a list. Still, a typical user will only press like 4 buttons at once maximum, so none of these micro-optimizations really matter in the end.

With the multiple keys being pressed, something goofy goes on. Like posted, you need to use a boolean to keep track of what keys are being pressed. Here's a simple example. Each loop, you need to check if the key is pressed (true) or not.

I see what you guys are saying. Can anyone explain why exactly it works when having the keylistener set a boolean instead of directly changing the paddles position? Like I said it totally doesnt recognize two keys being held down when I do it my way, so why does it work using booleans? Just trying to understand whats happening

I'm not sure what's happening. I have noticed that when you press a second key down, the first key pressed no longer fires a key event. It appears to be more part of the OS then JAVA. When you bring up a text editor and press and hold one key then type another, the stops printing out the first key.

Just wanted to update... I changed my code to use booleans like youve suggested. The performance increased by at least 500% lol. The paddles are now super responsive its ridiculous. I might actually have to turn down their speed. Thank you so much! Why the hell does that make such a huge difference??

Heres what Ive done so far. Added scores and all that. I NEED to use double buffering because the flickering is killing me! Any critiques??

I'm guessing your speed difference is because you used to be adjusting the paddle's position from within keyPressed, and now you're doing it within your game loop. Your game loop appears to fire every 20ms - the EDT (Event Dispatch Thread, which is what calls keyPressed etc.) must be firing less often than that. To get something similar, you can increase the length of your Thread.sleep(), or you can just move your paddle by less distance every time, which is what I would recommend because a 20ms delay is already only 50fps.

The difference between the booleans and the way you were doing it before is that the OS will only fire an event for the most recent key. Because you're storing every key you've ever seen, you get to jump over this hurdle by remembering what was pressed independently of the OS.

Really? I don't really see how this can be true. It's best to avoid as much as possible Swing/AWT but Java2D is good enough.

Given that Java2D has improved in the last few releases but its still factors slower then what the OpenGL bindings can do and pretty much impossible to pull of a modern looking game with it. Also don't forget about input and sound which are also pretty important for games.

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