8 Replies - 2591 Views - Last Post: 28 July 2009 - 09:40 PM Rate Topic: -----

#1 fakuatlong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-July 09

Need help in my final project - java 2d game

Posted 23 July 2009 - 08:14 PM

hello,

i found alot game tutorial about the game tutorial..
but i cant totally understand wat they post.. lolz...
so may i ask got some source code for me to do some revision?
if can better that game is using mouse to play..

Thank you very much
Is This A Good Question/Topic? 0
  • +

Replies To: Need help in my final project - java 2d game

#2 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: Need help in my final project - java 2d game

Posted 23 July 2009 - 11:12 PM

im no pyschic but ur gonna fail this one
Was This Post Helpful? 0
  • +
  • -

#3 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,197
  • Joined: 14-February 08

Re: Need help in my final project - java 2d game

Posted 24 July 2009 - 02:13 AM

Hi fakuatlong

Giving you source code would not help you at all, it is highly unlikely that it would be what you are looking for, and if you decided to change it you wouldn't know where to start. Especially because you couldn't understand some beginners tutorials.

Your best bet if you are at school is to talk to your teacher and explain that your having problems, maybe he can point you in the right direction, or explain in more detail what it is you are trying to achieve so that we can be of more help to you.

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#4 fakuatlong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-July 09

Re: Need help in my final project - java 2d game

Posted 27 July 2009 - 06:09 AM

I'm so sorry... please forgive my stupid question...

i should ask my problem 1 by 1... lolz...

1st question... how to i change my mouse cursor with image?
here is my code..

 Image cursorImage = Toolkit.getDefaultToolkit().getImage("number1.gif");
		  Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point( 0, 0), "" );
		  setCursor( blankCursor ); 


when i run the program it doesn't work... it didnt show the image on the frame... izzit i do something mistake??


2nd question... how to i delete or remove the image from the game?
izzit using the dispose() function?

Thank you very much
Was This Post Helpful? 0
  • +
  • -

#5 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Need help in my final project - java 2d game

Posted 27 July 2009 - 07:52 AM

View PostKanvus, on 24 Jul, 2009 - 05:12 AM, said:

im no pyschic but ur gonna fail this one

hi*5
Was This Post Helpful? 0
  • +
  • -

#6 bobjob  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 163
  • Joined: 29-March 08

Re: Need help in my final project - java 2d game

Posted 27 July 2009 - 11:11 AM

there are some complete examples for basic java games

here is one I found using google:
Java Game Source Code
Was This Post Helpful? 0
  • +
  • -

#7 fakuatlong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-July 09

Re: Need help in my final project - java 2d game

Posted 28 July 2009 - 04:59 AM

View Postbobjob, on 27 Jul, 2009 - 10:11 AM, said:

there are some complete examples for basic java games

here is one I found using google:
Java Game Source Code


thanks you coz ur reply...
that tutorial i already read before... but it too hard to understand...
can i have another tutorial which similar with this??

thank you very much
Was This Post Helpful? 0
  • +
  • -

#8 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: Need help in my final project - java 2d game

Posted 28 July 2009 - 06:50 AM

Lesson learned here is bobjob used this site:

www.google.com

So getting your entire project done isnt going to be easy as ctrl+C, ctrl+V. If java confuses you there are hundreds of very simple tutorials both on this site and the aforementioned. As you get confused we will help you with each part, but no one is going to bend over backwards to find you all of your work.
Was This Post Helpful? 0
  • +
  • -

#9 fakuatlong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-July 09

Re: Need help in my final project - java 2d game

Posted 28 July 2009 - 09:40 PM

OK here is mine full coding...

 
 import java.awt.*;
 import java.awt.event.*;
 import java.net.URL;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.FileInputStream;

 import javax.sound.sampled.*;
 import javax.swing.ImageIcon;
 import javax.swing.JFrame;

 public class Level3 extends Frame implements MouseMotionListener, MouseListener
 {


	 Image Background;
	 Image User;
	 Image Police;

	 int posX=0;
	 int posY=0;
	 public BufferedImage buffer;
	 Cursor c;

	 public Level3 ()
 	{
		Image cursorImage = Toolkit.getDefaultToolkit().getImage("number1.gif");
		Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point( 0, 0), "" );
		setCursor( blankCursor );

		c = new Cursor (Cursor.CROSSHAIR_CURSOR);
		this.setCursor (c);


		int WIDTH=500;
		int HEIGHT=600;

		setTitle("Thief");
		setSize(WIDTH, HEIGHT);
		buffer = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);

	 	setVisible(true);
		loadImage();

		addMouseListener( this );
		game();


	}

	public void game()
	{
		while(true)
		{
			updateWorld();
			paintWorld();
			repaint();

			try{
				Thread.sleep(1000);
			} catch (InterruptedException e) {}
		}
	}



	public BufferedImage loadImage()
	{
	   	Background=loadImage("city.jpg");
	   	Police = loadImage("icon-police.gif");
	   	User = loadImage("number1.png");

	   	return null;

	 }

	 private Image loadImage(String fileName)
	 {
		 return new ImageIcon(fileName).getImage();
	 }

	 public void paintWorld()
	 {
		 Graphics g=buffer.getGraphics();
		 g.fillRect(0,0,getWidth(),getHeight());
		 drawImage(g,Background,0,0,null);
		 drawImage(g,Police,posX, posY,null);
	 }



	 public void paint(Graphics g)				//function which paint the image
	 {
		g.drawImage(buffer,0,0,this);

	 }

	 public void drawImage(Graphics g, Image image, int x, int y, String caption)
	 {
		 g.drawImage(image, x,y,null);
	 }

	 public void updateWorld()
	 {
		 posX=(int)(Math.random()*450);
		 posY=(int)(Math.random()*500);
	 }

	 public void mousePressed(MouseEvent e)
	 {
	 }

	 public void mouseReleased(MouseEvent e)
	 {
	 }

	 public void playSound()
	 {
		 try{
		 File tadaSound = new File(System.getenv("windir") + "/" + "gun_fire.wav");
		 AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new FileInputStream(tadaSound));
		 AudioFormat audioFormat = audioInputStream.getFormat();
		 DataLine.Info dataLineInfo = new DataLine.Info(Clip.class, audioFormat);
		 Clip clip = (Clip) AudioSystem.getLine(dataLineInfo);
		 clip.open(audioInputStream);
		 clip.start();
	 } catch(Exception e)
	 {}
	 }

	 public void mouseClicked(MouseEvent e)
	 {
		 int ex = e.getX();
		 int ey = e.getY();

		 playSound();
		 
		 if(check_hit(ex, ey))
		 {
			 dispose();
		 }
	 }

	 public boolean check_hit(int manus_x, int manus_y)
	 {
		 double diffX = manus_x - posX;
		 double diffY = manus_y - posY;

		 double distance = Math.sqrt ((diffX*diffX) + (diffY*diffY));

		 if(distance<80)
		 {
			 return true;
		 }
		 else return false;

	 }

	 public void mouseEntered(MouseEvent e)
	 {
	 }

	 public void mouseExited(MouseEvent e)
	 {
	 }

	 public void mouseDragged(MouseEvent e)
	 {
	 }

	 public void mouseMoved(MouseEvent e)
	 {

	 }

}





i got few question need to ask...

1st question: why my mouse cursor didnt change to the image?

2nd question: why i cant play the sound? the program was run but without the sound...

3nd question: how to i delete the picture in check_hit ? using dispose() will remove whole frame... i just need to delete the current picture which hit by player...


Thank you very much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1