Can someone help me with my button game code?

338 views Asked by At

I think my problem is in my delay method. My game is supposed to have a green button first and randomly will turn red. If the user presses this RED button, they lose. My code doesn't detect when the red button is clicked I think because of my delay method. But eventually it does detect it, just after multiple times of the red button showing. PLEASE help!! Run my code and see what is wrong, and please tell me how to fix it! (The images for buttons are pretty self explanatory in the titles in what they should be)

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.Applet;
import java.awt.image.*;
import java.util.*;
import java.net.*;

public class pages extends Applet
{
  int numClicks;
  boolean isGreen;
  boolean isClicked;
  boolean ClickedDuringDelay;
  int count;
  int rand;
  int buttonCode;        
  public void init()
  {  
     numClicks = 0; 
     buttonCode = 0;
     count = 0;
     ClickedDuringDelay = false;


  }   

  public void paint(Graphics g)
  {  

           //screens
           switch(numClicks)
           {
                 case 0: StartPage(g); break;
                 case 1: InstrucPage(g); break;
                 case 2: GamePage(g); break;
           }

           //pushing buttons
           if(numClicks >1)
           {
                 if(rand >= 80)
                    isGreen = false;
                 else
                    isGreen = true;

                 Image button;
                      if (isClicked && isGreen)                                      
                          {button = getImage(getDocumentBase(), "greenpressed.png"); //green pressed
                       g.drawImage(button,200,150,this);}
                    else if(!isGreen)
                       {button = getImage(getDocumentBase(), "red.png");          //red unpressed
                       g.drawImage(button,200,150,this);

                       ClickedDuringDelay = delay();
                       if(ClickedDuringDelay)
                          LosePage(g);
                       else{
                          button = getImage(getDocumentBase(), "green.png");
                          g.drawImage(button,200,150,this);
                          rand = (int)(Math.random() * 100);
                          repaint(); }
                       }
            }




  }

  public boolean mouseDown(Event e, int x, int y)
   {

     if(!ClickedDuringDelay){
        if(numClicks <2)
           {numClicks++; count--;}
          isClicked = true;
          repaint();
        count++;}

       return true;

   }

   public boolean mouseUp(Event e, int x, int y)
   {
     rand = (int)(Math.random() * 100);
     //System.out.println(rand);

     if(!ClickedDuringDelay){
          isClicked = false;
        repaint();}
       return true;
   }

  public void StartPage(Graphics g)
  {     //background
        g.setColor(Color.black);
        g.fillRect(0,0,800,600);

        //Title
        g.setColor(Color.white);
        g.setFont(new Font("Desdemona",Font.BOLD,100));
        g.drawString("DO NOT PRESS THE",20,100);

        g.setColor(Color.red);
        g.setFont(new Font("Braggadocio",Font.BOLD,100));
        g.drawString("RED",250,250);
        g.drawString("BUTTON",120,350);

        g.setColor(Color.white);
        //g.drawRect(200,400,400,100);
        g.setFont(new Font("American Typewriter",Font.PLAIN,50));
        g.drawString("Click anywhere to continue..",60,490);
  }

  public void InstrucPage(Graphics g)
  {
        //background
        g.setColor(Color.black);
        g.fillRect(0,0,800,600);

        //title
        g.setColor(Color.white);
        g.setFont(new Font("Desdemona",Font.BOLD,100));
        g.drawString("Instructions",100,100);

        g.setColor(Color.red);

        g.setFont(new Font("American Typewriter",Font.PLAIN,40));
        g.drawString("A green button will first appear.",70,200);
        g.drawString("You MAY click this button.",130,250);
        g.drawString("A red button will randomly appear.",70,300);
        g.drawString("Do NOT press this button.",130,350);

        g.setColor(Color.white);
        g.setFont(new Font("American Typewriter",Font.PLAIN,50));
        g.drawString("Click anywhere to start!", 95, 520);
  }

  public void GamePage(Graphics g)
  {
        //background
        g.setColor(Color.black);
        g.fillRect(0,0,800,600);

        //score
        g.setColor(Color.white);
        g.setFont(new Font("Arial",Font.BOLD,100));
        g.drawString("SCORE: " + count,150,100);

        //button

        Image buttons = getImage(getDocumentBase(), "green.png");
        g.drawImage(buttons,200,150,this);



   }


  public void LosePage(Graphics g)
  {
        //background
        g.setColor(Color.black);
        g.fillRect(0,0,800,600);

        g.setColor(Color.red);
        g.setFont(new Font("Desdemona",Font.BOLD,250));
        g.drawString("YOU",200,220);
        g.drawString("LOST",150,470); 

        g.setColor(Color.white);
        g.setFont(new Font("American Typewriter",Font.PLAIN,50));
        g.drawString("Your final score was " + count, 140, 540);

  }

  public boolean delay() 
  {
     boolean delayClick = false;

     for (int i = 0; i<200;i++) 
     {
        try 
        {
           Thread.sleep(5);
           if (isClicked)
             {delayClick = true;
             System.out.println("pressed");}

        }
        catch(InterruptedException ex) 
        {
           Thread.currentThread().interrupt();
        }

     }
     return delayClick;


  }

}

1

There are 1 answers

3
Kevin Workman On

You're doing a few things wrong here.

First of all, you should never call Thread.sleep() from the EDT. This will cause exactly the behavior you're describing: your UI will become laggy and unresponsive. You can google "Java EDT" for a ton more information, but this is a reasonable starting point.

Instead of (mis)using Thread.sleep() to detect a delay, you probably want to use a Swing Timer. More info on that can be found here.

Secondly, you shouldn't be loading images from the EDT. Instead, load them once at the beginning.

Other than that: you'd probably be better off using a JPanel (Swing instead of AWT), and you should really fix your formatting since your code is very hard to read as-is!