Game programming questions

Hello there. I have made a 2d maze game recently like pacman. Im going to develop a 2d fighting game now. In my maze game i used the ImageIcon to present an image, now i wanna try the Image object instead.

I have some questions before i start to to this. I have read the great tutorial at http://www.cokeandcode.com/spaceinvaderstutorial
but still i don’t understand everything. Here are my questions.

  1. How do I paint some part of a big picture like a image file containing all the sprites I wanna use?

  2. How does the paint() method work? I mean does the image disappear that i painted when I paint another one or can i clear the previous one and restore the background there?

  3. Is the rectangle intersects technique good for collision detection between object,? or is there a better technique cause im going to do a fighting game and the pictures wont be rectangular.

Thanks // svartberg, new started game programmer.

okay the first question i found the answer for, cause there is a graphics.drawImage method that paint some part of a picture to the screen.

my next question i haven’t find any answer for. I know there is a repaint() method that paint the component, but does it have to paint all images like the background also, or just a specific image?

and about the third question i found out that maybe the collision detecting will work out fine, with points but im not sure how it work, I’ll give it a try i guess.

Please give me suggestions and tell me how the painting works. // svartberg

You can try pixel perfect collision or you can use a bounding polygon .

yes, it exist the draw method in java… and you also can use repaint to update your graphics… that you want update for example for every 1000 ms = 1 s.I’m also new to java game developpement… but if you understand how to use timers, thing would become more easy for you. For example, the method paint, you place all the elements that you wish to draw and you create a global timer which calls the method repaint() every 1000/FPS (FPS = 80). This will redraw all the elements in paint()… and to control the other objects you can create another timer for example to move a block to the right every 2 s by 10 pixels… you just add the current position in your new timer.So there are two timers, one that calls repaint() every 1000/FPS and the other one that adds the current x position.Also you can use the buffering, but as for beginner like me, i just use this style of coding in my first java game, tetris… hope this msg is useful to you

About your second question:

The method repaint() asks for the scren to repaint, that is done by awt calling to the update(Graphics) method

The default update(Graphics) method clears the screen and calls to paint(Graphics), that’s why in the paint(Graphics) method you draw all of your image again.

You can override the method to use some kind of double buffering and avoid flicker.

Next about painting, each time you draw a image in the graphics object you change the pixels so you cannot delete a image to restore the backgorund. If you need to repaint the backgorund you ned to draw the image again. (But this is covered by the update() -> paint() cycle)

Hope this helps,

 Rafael.-

I do recommend getting a book like. There are 2 or more Java Game Programming Books on Amazon. If you can afford it, they definetely cover some questions you have now and will have later.
Try Java 2 Game Programming by Thomas Petchel
Its more basic.

-JAW

Try Killer Java Game Programming or Developing Games in Java. You can sometimes pick up programming books for only a couple bucks plus shipping on Amazon.com. I haven’t read the book JAW mentioned, so I can’t comment upon it.

Don’t try Advanced Java Game Programming or Black Art of Java Game Programming. In general, don’t try any programming book more than 2 years old unless there’s no newer one available or it’s a mature technology that hasn’t changed much (e.g. sorting algorithms). Developing Games in Java is an exception, but Killer Java Game Programming is better anyways. I have both, and they each have some (not alot) of material that the other doesn’t cover.