random—如何使用java通过鼠标单击使对象随机移动?

carvr3hs  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(180)

我对java很陌生,但我正在尝试制作一个游戏,当你点击一个对象时,它会在屏幕上随机移动。对于这个物体,我选择了一个圆。它还可以在每次单击时将大小减少25。
问题是我不知道如何使圆圈随机移动。
这是鼠标点击的方法

public void mouseClicked(MouseEvent event)
    {
        circleClicked++;
        xClick = event.getX();      //adds the mouse x click position to variable x Click
        yClick = event.getY();      //adds the mouse y click position to variable y click
        repaint();                  //forces applet paint method to be called again

        if(CIRCLE_LENGTH < 2)       //resets the game only if the CIRCLE_LENGTH is equal or less than 0 and if the left mouse button is clicked
        {
            xClick = 0;
            yClick = 0;
            misses = 0;
            CIRCLE_LENGTH = 100;
        }
    }//end mouseClicked method

下面是单击圆时的if语句

//program only executes IF statement if the user clicks on the circle       
        if((xCircle == xClick) && (yCircle == yClick))
        {   
            hitNum++;
            //Paint x and y to a random integer value between MIN_RANDOM and MAX_RANDOM
            xCircle = X_MIN_RANDOM + (int)(Math.random()*(X_MAX_RANDOM - X_MIN_RANDOM));
            yCircle = Y_MIN_RANDOM + (int)(Math.arandom()*(Y_MAX_RANDOM - Y_MIN_RANDOM));
            CIRCLE_LENGTH = CIRCLE_LENGTH - 25;   //the circle length is subsctracted by 25 everytime the user click on the circle
        }//end if

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题