You Are Here:

Community: Wiki

This page was last modified on 3 September 2009, at 10:51.

How to develop a game - Part 4

From Forum Nokia Wiki

Reviewer Approved   

In the previous article, the GameCanvas class was constructed with all the main interactions between elements. Now that all gameplay elements have been built, it is time to improve the visual appearence of the game by using image files instead of Graphic's draw/fill methods to represent the game entities. I have created some images based on the free SpriteLib resource pack to be used in the game.

Image:Arkanoid-Graphics.png

Image

To access and display these images in the Midlet, another low-level user interface element, the Image class, needs to be used. This class stores graphical image data independently of the display device in an off-screen memory buffer. Images are either mutable or immutable depending on how they are created. Immutable images are generally created by loading image data from resource bundles, files, or the network. Immutable Images can be created using the static createImage() methods available in the Image class. However, immutable Images cannot be modified once they have been created. Mutable images are created with Image constructors as blank images containing only white pixels. The application can render a mutable image by calling getGraphics() on the Image to obtain a Graphics object quickly for this purpose.

In this game, immutable images represent the game entities. Start by changing the Pad class. First create an Image object in the constructor:

Image image;
public Pad() {
try {
image = Image.createImage("/pad.png");
width = image.getWidth();
height = image.getHeight();
} catch (IOException e) {
e.printStackTrace();
}
}

Then rewrite the paint method of the Pad class to draw the image:

public void paint(Graphics g) {    
g.drawImage(image, x,y, Graphics.TOP | Graphics.LEFT);
}

As you may have noticed, the used format is PNG. It is the only format that is mandatory in all MIDP implementations. Some devices support also other formats, such as JPEG or BMP, but PNG is the only safe option for all devices.

Run the Midlet and you will see the new pad. Next, do the same for the ball:

Image image;
public Ball() {
try {
image = Image.createImage("/ball.png");
width = image.getWidth();
height = image.getHeight();
} catch (IOException e) {
e.printStackTrace();
}
}
 
public void paint(Graphics g) {
g.drawImage(image, x,y, Graphics.TOP | Graphics.LEFT);
}

If you run the Midlet now, you will have a nice ball. The brick images, however, contain several brick elements, so each brick image must be cropped from the main Image. This can be done with the setClip methods in the Graphics class, but in Midp 2.0 there is an easier way, the Sprite class.

Sprite

A sprite is a common term in games. It refers to a visual element made of an image, usually animated, which can be moved around the game independently of other elements. The Sprite class is the MIDP 2.0 representation of this concept. It allows creating sprites based on Images with multiple frames. It can change the frames, control animations, and check collisions with other elements.

All these capabilities can be used in the game entities. Start with the Brick class.

public static int BRICK_FRAMES = 20;
Image image = null;
Sprite sprite = null;
public Brick(){
// load image
image = Image.createImage("/bricks.png");
// create the sprite with 20 frames, one for each brick
sprite = new Sprite(image,image.getWidth()/BRICK_FRAMES, image.getHeight());
width = sprite.getWidth();
height = sprite.getHeight();
}

This code creates a sprite with 20 frames, one for each brick available on the image. Before painting the brick you need to change the frame that will be used.

public void paint(Graphics g) {
if (active){
sprite.nextFrame();
sprite.setPosition(x, y);
sprite.paint(g);
}
}

If you run the Midlet now, you will have nice disco bricks that change the color all the time. Another problem is that the bricks.png image is loaded for each Brick that is created, which not very optimized. Modify the code to address these problems:

public Brick(Image image, int numFrames, int frameSelected){    
sprite = new Sprite(image,image.getWidth()/numFrames,image.getHeight());
// set frame
sprite.setFrame(frameSelected);
// get size for collision detection
width = sprite.getWidth();
height = sprite.getHeight();
}
 
public void paint(Graphics g) {
if (active){
sprite.setPosition(x, y);
sprite.paint(g);
}
}

Now just load the Image once in the init() method and select one frame to be used during the lifetime of a brick.

// create bricks
Image bricksFrames = null;
try {
bricksFrames = Image.createImage("/bricks.png");
} catch (IOException e) {
e.printStackTrace();
}
Brick brick = new Brick(bricksFrames, 20, 0);
bricks = new Vector();
for (int i=0; (i*(brick.width+2)) < getWidth(); i++){
brick = new Brick(bricksFrames, 20, i);
brick.sprite.setFrame(i);
brick.x = (i*(brick.width+2));
brick.y = 20;
bricks.addElement(brick);
}

Now you have a nice row of bricks, each or which is in its own color. The next article discusses saving high scores.

Downloads:


Go To Part 5

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fWhyE5fChooseE5fJavaE5fForE5fMobileE5fGameE5fE44evelopmentE253FX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxWhyE20ChooseE20JavaE20ForE20MobileE20GameE20E44evelopmentE3fE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtopicQUqfnTopicZentertainmentQRqmarsZrelevanceQNx100X qfnZtopicQUqfnTopicZgamesQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtopicQUqfnTopicZjavaQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d02X qfnZuserE5ftagQSxentertainmentX qfnZuserE5ftagQSxgamesX qfnZuserE5ftagQSxjavaX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ