我正在执行一项任务,但我被困在一个要求上。我已尽我所能尽善尽美,别无选择,只能谦虚地请求您的专业知识。我的任务要求我用 Java 制作一个简单的游戏,其中一个图像将在随机持续时间内随机出现和重新出现。用户点击图片,如果用户点击图片,则将点击次数输出到屏幕。我的主要问题是如何让所述图像在随机位置出现/重新出现随机持续时间?为了使持续时间随机,我会以某种方式在计时器中设置它吗?我试过了,但没有用。至于随机位置,我什至不知道如何开始编码,有人能指出我正确的方向吗?我会在 actionPerformed 方法中这样做吗?
无论如何,这是我到目前为止的代码。它编译,但运动和速度是平稳和恒定的。我需要图像随机出现/重新出现,而不是以平稳的恒定速率“滑动”。
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.Random;
public class CreatureClass extends JPanel
{
private final int WIDTH = 400, HEIGHT = 300;
private final int DELAY=20, IMAGE_SIZE = 60;
Random r = new Random();
private ImageIcon image;
private Timer timer;
private int x, y, moveX, moveY;
private int catchCount=0;
//-----------------------------------------------------------------
// Sets up the panel, including the timer for the animation.
//-----------------------------------------------------------------
public CreatureClass()
{
timer = new Timer(DELAY, new CreatureListener());//how to make duration random here?
addMouseListener (new MouseClickedListener());
image = new ImageIcon ("UMadBro.gif");
x = 0; //starting coordinates of image
y = 40;
moveX = moveY = 3;//image is shifted 3 pixels every time image is updated. I
// tried setting these to a random number, but it makes the
// image "stuck" in one position.
setPreferredSize (new Dimension(WIDTH, HEIGHT));
setBackground (Color.yellow);
timer.start();
}
//-----------------------------------------------------------------
// Draws the image in the current location.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent (page);
image.paintIcon (this, page, x, y);
page.drawString("Number of clicks: " + catchCount, 10, 15);
}
//*****************************************************************
// Detects when mouse clicked=image postition
//*****************************************************************
public boolean pointInMe(int posX, int posY)
{
if(x == posX && y == posY)
{
catchCount++;
return true;
}
return false;
}
public int getCatchCount()
{
return catchCount;
}
private class MouseClickedListener extends MouseAdapter
{
public void mouseClicked (MouseEvent event)
{
pointInMe(event.getX(), event.getY());
}
}
//*****************************************************************
// Represents the action listener for the timer.
//*****************************************************************
private class CreatureListener implements ActionListener
{
//--------------------------------------------------------------
// Updates the position of the image and possibly the direction
// of movement whenever the timer fires an action event.
// (I don't know how to make the image appear and reappear
// randomly for random durations)
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
x += moveX;
y += moveY;
if (x <= 0 || x >= WIDTH-IMAGE_SIZE)
moveX = moveX * -1;
if (y <= 0 || y >= HEIGHT-IMAGE_SIZE)
moveY = moveY * -1;
repaint();
}
}
}