8 Replies - 7906 Views - Last Post: 26 August 2011 - 04:19 PM Rate Topic: -----

#1 arjun.ak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 12-August 11

Help with making a simple game in Java

Posted 20 August 2011 - 10:49 PM

Hi,
Can someone please help me with making a game in java. I am to Create a class called Creature to represent a graphical creature. The method move moves the creature to a random location within area. The method pointInMe returns true if point is in the creature and increments the catch count, else returns false. The method getCatchCount returns the number of catches. The method draw draws the creature in a component.

Below is what I have done so far, for this. I don't know what to add to the DRAW and MOVE method and also I'm having some trouble with the BOOLEAN pointInMe. Any help will be greatly appreciated.
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.util.Random;
import javax.swing.ImageIcon;

public class Creature
{
	private ImageIcon creature;
	private int creatureX, creatureY;
	private int catchCount;
	private Random generator;

public Creature(ImageIcon creature)
{
	creature = new ImageIcon();
}

public void move(Dimension area)
{
	
}

public boolean pointInMe(int x, int y)
{
	return(creatureX == x && creatureY == y);
        catchCount++;
}

public int getCatchCount()
{
	return catchCount;
}

public void draw(Graphics g, Component c)
{
	
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with making a simple game in Java

#2 omfgkittenz  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 18-May 11

Re: Help with making a simple game in Java

Posted 20 August 2011 - 11:23 PM

well, firstly if its a desktop app you need a JFrame. it is possible to do all this within a jframe but my suggestion is to let you creature class extend JPanel, add that panel to the JFrame. with that panel you can override the paint component like so
public void paint(Graphics g){
  super.paint(g);
  g.drawImage([imageicon.getImage],[x], [y], null);
}


Was This Post Helpful? 0
  • +
  • -

#3 omfgkittenz  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 18-May 11

Re: Help with making a simple game in Java

Posted 20 August 2011 - 11:33 PM

sorry for the segmented response, as for your other method pointInMe you are on the right track but if you plan to do this with your mouse it would be necessary to implement the MouseListener interface on you JPanel.
class Creature extends JPanel implements MouseListener{
//also implement the methods, there are three or so of them, but if you are using eclipse you will havethe option of the ide adding the stubs for you.

public void pointInMe(int x, int y){
if(creatureX == x && creatureY == y){
 catchCount++;
return true;
}

return false;
}

//for example
public void mouseClicked(MouseEvent e){
 pointInMe(e.getX(), e.getY());
}
}



and for the move you just need to use the generator.nextInt() method for both the y and the x using the restrictive arguments.

you will need to continuously call these methods using a timer. the way i go is

//this timer is from the javax.swing library!!!!1

ActionListener updater = new ActionListener(){
public void actionPerformed(ActionEvent e){
 move([whatever dimension]);
 repaint();
}
}

Timer t = new Timer([how often IN MS], updater);[



put that in the constructor!

hope this helps!
-kittenz
Was This Post Helpful? 2
  • +
  • -

#4 arjun.ak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 12-August 11

Re: Help with making a simple game in Java

Posted 20 August 2011 - 11:40 PM

Thanks very much, i will give it a try :)
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1567
  • View blog
  • Posts: 3,520
  • Joined: 05-April 11

Re: Help with making a simple game in Java

Posted 21 August 2011 - 03:02 AM

I have changed your fields a bit (added width and height). It is not much fun to have a creature with a width 0 and height 0. Make sure that they are the same as the image's size.

private Image creature;
private int x, y;
private int width, height; //I guess you would also like to have width and height?
private int catchCount;
private static Random generator; //Use the same Random over and over for better results



Also I have a little comment about your pointInMe.
public boolean pointInMe(int x, int y) {
	Rectangle playerRect = new Rectangle( x, y, width, height );
    return(playerRect.contains(x, y));
        catchCount++; //You will never get to here since you return in the line above
}



Just to follow up on what omfgkittenz said, you will have to call the draw method of the creature from your JPanel class everytime it repaints.
public void draw(Graphics g) {
     g.drawImage( creature, x, y, null );
}


Was This Post Helpful? 1
  • +
  • -

#6 arjun.ak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 12-August 11

Re: Help with making a simple game in Java

Posted 21 August 2011 - 03:34 AM

I changed the boolean pointInMe to the below code, so that it returns true if point is in the creature and increments
the catch count, else returns false.
public boolean pointInMe(int x, int y)
{
	if(creatureX == x && creatureY == y)
	{
		catchCount ++;
		return true;
	}
	return false;
}



would the above work? also why did you use the new Rectangle() in your code?
Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1567
  • View blog
  • Posts: 3,520
  • Joined: 05-April 11

Re: Help with making a simple game in Java

Posted 21 August 2011 - 04:41 AM

View Postarjun.ak, on 21 August 2011 - 03:34 AM, said:

I changed the boolean pointInMe to the below code, so that it returns true if point is in the creature and increments
the catch count, else returns false.
public boolean pointInMe(int x, int y)
{
	if(creatureX == x && creatureY == y)
	{
		catchCount ++;
		return true;
	}
	return false;
}



would the above work? also why did you use the new Rectangle() in your code?


Yes it will work if the creature doesn't have a width and height. I use Rectangle class because if the creature has a width and height, it will have a bounding box (rectangle is a shape that is easy to work with.. for keeping it simple). The Rectangle class then have a contains method you can use to check, or you could just do the math yourself as it is fairly simple :D
Was This Post Helpful? 1
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12134
  • View blog
  • Posts: 45,117
  • Joined: 27-December 08

Re: Help with making a simple game in Java

Posted 22 August 2011 - 05:43 AM

Why does your draw() method accept a Component? Regardless of whether or not Creature extends JComponent, JPanel, etc., there is no reason for Creature.draw() to accept a Component.
Was This Post Helpful? 1
  • +
  • -

#9 arjun.ak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 12-August 11

Re: Help with making a simple game in Java

Posted 26 August 2011 - 04:19 PM

this is part of an assignment, i was just following the UML diagram, that was provided;

Creature
-creature : ImageIcon
-creatureX : int
-creatureY : int
-catchCount : int
-generator : Random
+Creature(image : Image)
+move(area : Dimention) : void
+pointInMe(x : int, y : int) : boolean
+getCatchCount() : int
+draw(c : Component, g : Graphics) : void

i also have to create an GUI panel afterwards.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1