2012-08-30 53 views
2

我正在尝试创建一个小程序,在该小程序中,用户通过将狗移动到羊上并使羊在随机方向上远离狗而将羊变成笔。羊和狗被定义为对象。在Java Applet中随机移动对象

小程序仍处于早期阶段。到目前为止,我可以在屏幕上拖动狗物体,当狗物体靠近绵羊物体时,它只会移动,但只能在一定范围内(在我设定的范围内)移动。我不在寻找我只是寻求一些帮助的解决方案。

我想要帮助的是当狗对象进入我设定的范围内而不是在设定范围内移动时,使羊对象从狗的随机方向移动。任何帮助或提示将不胜感激。这里是我的代码:

package mandAndDog; 

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


public class SheepDog extends Applet implements ActionListener, MouseListener, MouseMotionListener 
{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    /** 
    * 
    */ 

    Dog dog; 
    Sheep sheep; 
    int xposR; 
    int yposR; 
    int sheepx; 
    int sheepy; 
    int sheepBoundsx; 
    int sheepBoundsy; 

    public void init() 
    { 
     addMouseListener(this); 
     addMouseMotionListener(this); 
     dog = new Dog(10, 10); 
     sheep = new Sheep(200, 100); 
     sheepx = 175; 
     sheepy = 75; 
     sheepBoundsx = 50; 
     sheepBoundsy = 50; 


    } 
    public void paint(Graphics g) 
    { 
     dog.display(g); 
     sheep.display(g); 

    } 
    public void actionPerformed(ActionEvent ev) 
    {} 
    public void mousePressed(MouseEvent e) 
    {} 
    public void mouseReleased(MouseEvent e) 
    {} 
    public void mouseEntered(MouseEvent e) 
    {} 
    public void mouseExited(MouseEvent e) 
    {} 
    public void mouseMoved(MouseEvent e) 
    { 
    } 
    public void mouseClicked(MouseEvent e) 
    {} 
    public void mouseDragged(MouseEvent e) 
    { 
     dog.setLocation(xposR, yposR); 
     if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy 
       && yposR < sheepy+sheepBoundsy){ 
      sheep.setLocation(xposR + 50, yposR + 50); 
     } 

     xposR = e.getX(); 
     yposR = e.getY(); 
     repaint(); 

    } 
} 

class Dog 
{ 
    int xpos; 
    int ypos; 
    int circleWidth = 30; 
    int circleHeight = 30; 

    public Dog(int x, int y) 
    { 
     xpos = x; 
     ypos = y; 

    } 

    public void setLocation(int lx, int ly) 
    { 
     xpos = lx; 
     ypos = ly; 
    } 

    public void display(Graphics g) 
    { 
     g.setColor(Color.blue); 
     g.fillOval(xpos, ypos, circleWidth, circleHeight); 
    }  
} 
class Sheep 
{ 
    int xpos; 
    int ypos; 
    int circleWidth = 10; 
    int circleHeight = 10; 

    public Sheep(int x, int y) 
    { 
     xpos = x; 
     ypos = y; 

    } 

    public void setLocation(int lx, int ly) 
    { 
     xpos = lx; 
     ypos = ly; 
    } 

    public void display(Graphics g) 
    { 
     g.setColor(Color.green); 
     g.fillOval(xpos , ypos, circleWidth, circleHeight); 
    } 


} 
+2

决定要移动绵羊的矢量长度,得到两个随机数,然后按照随机数的比例缩放移动矢量的x和y坐标。将矢量添加到羊的当前位置。 –

回答

1

你不是在寻找代码,所以这里是我会做什么。这是非常基本的,并且由于JVM的随机性可以产生可预测的结果,但是,我喜欢使用内置的Math.random()函数来生成一个随机数,并使用模运算符将随机数绑定到某个整数介于0和上限之间的值。

我会推荐计算旅行的方向和距离作为一个向量。

int MAX_DISTANCE = 50; 
    int direction = (int) (Math.random() * 360) % 360; 
    int distance = (int) (Math.random() * MAX_DISTANCE) % MAX_DISTANCE; 

这里距离是像素的度量,方向是度数的度量。由于我们在向量中工作,我们可以使用角度和距离以及一些非常基本的trig来计算新的像素位置。

我会建议把这个功能计算出新的目标位置,这样,函数就可以计算出新的位置,如果那个位置太靠近狗了,你可以决定一条新的路径。

如果我可以提出建议,我建议将羊类放入一个Timer类中,以重新计算羊的下一个运动。这样,羊可以四处游荡。在徘徊时,绵羊可能每次运动的MAX_DISTANCE都要小得多。从而模拟一定程度的威胁。

如果你真的想进入模拟,你可以测量狗到羊的距离,并根据距离,缩放羊运动的速度,以便狗越接近,快速羊移动(不超过最大距离)。如果你这样做,我建议你考虑使用Math.log()来缩放运动,这样当狗离开200个像素时,距离狗到绵羊10个像素的距离对绵羊运动的影响更小。狗距离15个像素。

希望这有助于。

+0

+1为一个很好的答案,但是,只是要意识到,这可能会导致羊朝着狗;) – MadProgrammer

0

看一看在,因为它可能会有所帮助Java标准库的Random类。您可以轻松生成伪随机整数并调整角色的(x,y)。添加一些错误检查,以确保他们相互离开,你应该很好去!

0

我会划分你想将羊移动到一个数字的方向。如果羊可以上下左右移动,并且你减去狼来自的方向,那么说你离开了三个。向上,向右。

使用数学随机函数从中随机选择一个,你可以在API中查找,但基本上乘以你想要随机选择的最大数字,并将其转换为Int来摆脱小数。

int intDirection = (int)(Math.random()*3); 

这将给予应该给你0,1和2.然后做一个case语句来移动基于数字的方向。也许好的做法是考虑三个方向的统计员。

如果你想有更多的方向,你可以看向量,但这是你如何做随机。