Results 1 to 4 of 4
  1. #1
    jeol is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default Simple game problem

    So I have the variable, " int turtles " which i use to try to track the number of times the user clicks on the randomly placed turtles, but for some reason no matter where I click it automatically adds 1 the the variable " turtle"?? any help??

    import java.awt.*;
    import javax.swing.JFrame;
    import java.io.File;
    import javax.imageio.ImageIO;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    import java.util.Random;

    public class CatchTheTurtles extends Canvas implements MouseListener
    {

    private int lastX, lastY;
    private String message;
    Image park;
    Random r = new Random();
    int turtle = 0;
    int tsX = 1 + r.nextInt(500);
    int tsY = 1 + r.nextInt(800);


    public CatchTheTurtles() throws Exception
    {


    park = ImageIO.read( new File("park.jpg") );

    addMouseListener(this);
    message = "Catch The Turtles!! " + turtle;

    lastX = 0;
    lastY = 0;





    }



    public void paint( Graphics g )

    {

    g.drawImage(park,100,100,this);

    Color bro = new Color(74,48,13);
    Color gre = new Color(15,59,15);


    tsX = 1 + r.nextInt(800);
    tsY = 1 + r.nextInt(300);

    int tHX = (tsX + 35);
    int tHY = ( tsY );

    int tLX = (tsX + 35);
    int tLY = (tsY + 15);

    int tL2X = (tsX + 5);
    int tL2Y = (tsY + 15);


    g.setColor(Color.black);
    g.drawString(message, 50, 100);

    //turtle shell
    g.setColor(bro);
    g.fillOval(tsX,tsY,40,20);

    g.setColor(gre);


    //turtle head
    g.fillOval(tHX,tHY,20,10);
    //turtle leg
    g.fillRect(tLX,tLY,5,10);
    //turtle leg 2
    g.fillRect(tL2X,tL2Y,5,10);




    }

    public void mouseClicked( MouseEvent evt )
    {


    lastX = evt.getX();
    lastY = evt.getY();


    if ( lastX == tsX );
    {

    turtle = turtle + 1;

    }

    message = "You Caught " + turtle + " " + " turtles!" + lastX + " " + tsX;



    repaint();


    }

    public void mousePressed( MouseEvent evt )
    {
    }

    public void mouseReleased( MouseEvent evt )
    {
    }

    public void mouseEntered( MouseEvent evt )
    {
    }

    public void mouseExited( MouseEvent evt )
    {
    }

    public static void main(String[] args) throws Exception
    {
    // You can change the title or size here if you want.
    JFrame win = new JFrame("CatchTheTurtles");
    win.setSize(1224,768);
    win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    win.add( new CatchTheTurtles() );
    win.setVisible(true);
    }


    }

  2. #2
    jeol is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default Re: Simple game problem

    "int tsX" is a variable I use as the turtle shell, ie the bulk of the turtle. so my Idea was as long as the click(lastX), + or - 30, == tsX then it adds one to int turtle.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple game problem

    Quote Originally Posted by jeol View Post
    Java Code:
    			if ( lastX == tsX );
    What's that semi colon doing there?

    kind regards,

    Jos

    ps. Merry Christmas!
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jeol is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Simple Game - Need Help
    By mitchbutter1210 in forum Advanced Java
    Replies: 3
    Last Post: 06-18-2012, 11:29 AM
  2. Class in simple game problem
    By thebiggestfish26 in forum Java Applets
    Replies: 1
    Last Post: 04-19-2011, 02:30 AM
  3. Need help with simple game! *collision*
    By CNew in forum New To Java
    Replies: 1
    Last Post: 12-05-2010, 12:09 AM
  4. simple game programming help
    By b.m in forum New To Java
    Replies: 0
    Last Post: 11-25-2010, 05:16 PM
  5. Simple Game example
    By mayuresh34 in forum CLDC and MIDP
    Replies: 1
    Last Post: 10-08-2009, 01:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •