I have a system in place for a game yet I don’t know what I should use to display it. I am making a vertical shooter game and I have written the methods for all the classes controlling enemies and players, but I have not idea how to efficiently display the game. I was thinking a Canvas, that would repaint every frame, but is that really the most efficient method?
Important details:
- ideal framerate: 25fps
- It is a 2d game
- There are anywhere between 25-100 objects on the screen at any one time, all of which are moving
- All objects being displayed are images, all in PNG format
- The window is 640px by 480px
- Right now all the images are loaded as BufferedImage, although I could easily change this
7. I need a coordinate plane. This is the only fundamental part that cannot be changed without completely restructuring my code.
Most importantly the way I have everything set up, every frame all of the objects move and interact in a coordinate plane I devised (deals with collision detection and movement, no graphical component), then everything should get painted to the screen, simply by going through the ArrayLists which keep track of all moving objects and painting them one by one.
If Swing is acceptable,
JPanelis double-buffered by default, and ajavax.swing.Timerwith a period of 40 ms will give you ~25 Hz updates. This example shows the basic approach, while this example shows a number of images in motion.It’s not unusual to have the the model and view use different coordinates; all that’s needed are functions to map one system to the other. In this game, the view relies on four methods that map tiles to pixels and vice-versa. The same approach is outlined here for map tiles.