[英]Can someone help me with my button game code?
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;
}
} }
You're doing a few things wrong here.你在这里做错了一些事情。
First of all, you should never call Thread.sleep()
from the EDT.首先,您永远不应该从 EDT 调用
Thread.sleep()
。 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.
你可以谷歌“的Java EDT”为一吨多的信息,但是这是一个合理的起点。
Instead of (mis)using Thread.sleep()
to detect a delay, you probably want to use a Swing Timer.您可能希望使用 Swing Timer,而不是(错误地)使用
Thread.sleep()
来检测延迟。 More info on that can be foundhere .更多信息可以在这里找到。
Secondly, you shouldn't be loading images from the EDT.其次,您不应该从 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!除此之外:您可能最好使用 JPanel(Swing 而不是 AWT),并且您应该真正修复您的格式,因为您的代码很难按原样阅读!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.