Important alert: (current site time 7/15/2013 3:16:56 PM EDT)
 

article

A Basic Game - Part 3

Email
Submitted on: 4/20/2003 5:25:55 AM
By: John Croucher  
Level: Beginner
User Rating: By 9 Users
Compatibility: Java (JDK 1.2)
Views: 30731
author picture
 
     This is the 3rd section of my Basic Game Tutorial. This part shows how to put graphics into the game.


 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

Java Game Tutorial - Part 3


This tutorial is a small add on to part 2 of my Java Game Tutorial. I have had many requests by people for this so I finally decided to put this part 3 of my Java Game Tutorial together.
In this tutorial you will see how to change from using those boring shapes to using images.

The first thing you will need is two 20x20 pixel gif or jpeg image. If you are using gif you must make sure you save it as 8bit Paletted. I have sometimes had problems if this is not done.
One image will be the alien type characters. I have smiley faces in my game.
Also you will need some sort of ship which does the shooting down the bottom.
These can be animated gif images if you would like to add some extra effect to them.

Now to the coding
This is very simple and will not take very long at all.

In the global variable section place the following:

Image alien;
Image ship;

This will set up two image objects to store out images.

Next is in the “public void init()” function. Place in the following code.

alien = getImage(getDocumentBase(), "img1.gif");
ship = getImage(getDocumentBase(), "img2.gif");

This will load the image files into the Image objects.

The final part is to then draw the images to our buffer.

The first one to draw are the aliens
We are currently drawing them with this line:

bufferdImgSurface.fillOval(currentX[i], currentY[i], 20,20);

Replace that line with this line

bufferdImgSurface.drawImage(alien, currentX[i], currentY[i], this);

This draws the image ‘alien’ to the current position in the buffer.
The same goes for the ship.

Replace this line:

bufferdImgSurface.fillRect(shipX, shipY, 20, 20);

With this line:

bufferdImgSurface.drawImage(ship, shipX, shipY, this);

You should now be able to compile this code and run it. If you have any errors such as getting a white or grey java frame then check your paths for your images. Make sure they are in the same directory with your class file. You can put paths in if you want by doing the following.

alien = getImage(getDocumentBase(), "folder1/folder2/img1.gif");

That is the end of my third tutorial for making games in Java.
I hope you like my tutorial. If you find any errors please email me, also if you have any comments please email me.

Also if you have something you think I could put in a future tutorial like multi player or networking email me.
If you would like to see a working version go here http://www.jcroucher.com/javatutg3.html
*Note: This tutorial has been compiled and tested with J2sdk 1.4.0_01 attempting to run or use this tutorial without this version JSDK or JRE can have bad effects. Use these at your own risk*

Also check out my website for more tutorials and code samples www.jcroucher.com

This code and tutorial is copyright 2003 John Croucher.


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
4/25/2003 4:37:43 PM

Cool...
(If this comment was disrespectful, please report it.)

 
4/26/2003 11:30:51 AMĐonny Nadolny

Hey. There's a bug/glitch in your game... if there's only one or two enemies left, they shoot the same number of bullets as if they were all there.
(If this comment was disrespectful, please report it.)

 
4/28/2003 10:26:17 AMJohn Croucher

This is not really a bug. I left this like this so the game can get harder as the enemies are shot.
(If this comment was disrespectful, please report it.)

 
6/3/2005 7:38:52 AM

I want to you provide some of games on Java Source code.
Because I am a student. Now, I need them to do project for subject
thanks,

(If this comment was disrespectful, please report it.)

 
4/5/2006 10:02:56 AMSchindler

I can't get the health to work right. I've looked over the code and so have some other people and nobody can find anyhting worng.
(If this comment was disrespectful, please report it.)

 
6/16/2006 2:01:38 PMricky

cooll....
(If this comment was disrespectful, please report it.)

 
11/7/2006 10:32:26 PMkiembinh

request source code

(If this comment was disrespectful, please report it.)

 
1/24/2007 4:56:11 AMdaniel

your coding is good,can you help me to explore more about java
(If this comment was disrespectful, please report it.)

 
3/21/2007 1:45:34 PMdoddy

kalau bisa menampilkan contoh program dengan tampilan dari awal

(If this comment was disrespectful, please report it.)

 
10/29/2007 5:41:19 AMkevin

am a beginner in java, and i did find your tutorial intresting and easy to understand. i wounder if one day i will be able to come up with such wonderful codes.. cheers men!!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.