Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

hangman, hang me while your at it

Options
  • 10-01-2002 3:48pm
    #1
    Closed Accounts Posts: 8,478 ✭✭✭


    im sure most graduates or ppl that have done programming b4 has done their own hangman version at one stage. Im at that level just now. having troubles in trying to get the body parts to show up after each try :

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.awt.Graphics.*;

    public class guessgame extends Applet implements ActionListener
    {
    Label prompt;
    TextField input;
    int number;
    double randomnumber;
    int displaynum;
    Font myfont;
    Font f;
    int tries=0;
    Color mycolour;

    public void init()
    {
    prompt = new Label("Enter a Number and Press Enter ");
    input = new TextField(10);
    myfont = new Font("Courier",Font.BOLD, 16);
    f = new Font("Courier",Font.BOLD, 16);
    Color mycolour = new Color(3,255,201);
    add(prompt);
    add(input);
    input.addActionListener(this);
    }

    public void paint(Graphics g)
    {

    g.setFont(myfont);

    g.drawString("Number you Inputed " +number, 30,85);
    g.drawString("Computer number " + displaynum, 30, 105);

    g.drawLine(80,135,80,235);//vertical thrust
    g.drawLine(80,135,125,135);//horizontal thrust
    g.drawLine(125,135,125,145);//rope

    if (tries >=1 )
    g.drawRoundRect(110,145,30,30,30,30);//head
    if (tries >=2 )
    g.drawLine(125,200,125,175);//body
    if (tries >=3 )
    g.drawLine(105,195,125,180);//left arm
    if (tries >=4 )
    g.drawLine(145,195,125,180);//right arm
    if (tries >=5 )
    g.drawLine(105,225,125,200);//left leg
    if (tries >=6 )
    g.drawLine(145,225,125,200);//right leg

    if( tries>=6)
    g.drawString("You Lose", 40, 135);

    if( number == randomnumber)
    {
    g.setColor(Color.red);
    g.setFont(f);
    g.drawString("You Win", 40, 120);
    }

    if(number > randomnumber)
    g.drawString("Too High", 40, 120);


    if(number < randomnumber)
    g.drawString("Too Low", 40, 120);

    }

    public void actionPerformed(ActionEvent e)
    {
    number = Integer.parseInt(e.getActionCommand());
    randomnumber = Math.random() * 50;
    displaynum = (int)randomnumber;
    input.setText("");
    repaint();
    }
    }

    i understand the ways of the forum, no coding done

    all i want is guidance or even a hint as to where im going wrong

    let the flames begin


Comments

  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Try puttiny tries++ into the start of your paint method, there are a few other things you'll need to do, like stopping it from saying "You Win" and having the head drawn before you even start (Hint: initialize int tries to certain value).


  • Closed Accounts Posts: 8,478 ✭✭✭GoneShootin


    thanks alot enygma, ill give it a go


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    I hate to be a critic but....
    Originally posted by Enygma
    Try puttiny tries++ into the start of your paint method

    I have just noticed that it's an applet and not a frame, but repaint events can occur quite frequently (user drags something over applet canvas, causing a repaint). This would incur a tries++ which would decrease the number of tries remaining, even though the user didn't take his/her turn yet.

    Another thing, I didn't see the user requirements for this project, but why don't you use images instead of shapes and lines. TBH I think it would be easier, and it would look more impressive to a user, who wouldn't appreciate you hard coding in Cartesian coords for every visible object on the canvas. For images you could get an image of a guy (clipart or similar graphic), and divide the image horizontally in to [tries] equal segments.

    You could create an Image array, of tries length, containing all the broken image segments. All you have to do then is draw element 0 to [tries] on top of eachother. Just remember you are bound to start off with tries set to 0, which would result in the top image being drawn first, even though the user didn't take his/her turn. So you should try and draw from element 0 to (tries-1), making sure to put an if in there, to make sure that (tries-1) is a positive integer.

    Hope that helps :)

    ;-phobos-)


Advertisement