请告诉我是否有if()的命令

时间:2015-09-11 22:26:41

标签: java

我正在做一个项目,我需要点击图片,播放声音,然后点击屏幕上的任意位置并将贴纸复制并放置在那里。每次我发布左键单击时,贴纸将一直放置。点击新贴纸,然后重新开始。

我听到了声音,但我不知道如何做一个当时的命令。例如,如果(贴纸上发布)sound.play();然后下一次点击将贴上贴纸。

如何写“然后下一次点击会贴上贴纸”?

这是我到目前为止的代码。我知道第二个添加图像不起作用,但我不知道该怎么做,所以它就是实验。

import java.awt.Color;

public class pls {

public static void main(String[] args) {

        EZ.initialize(1644,1022); //pixel size of run screen

        EZImage picPicture = EZ.addImage("background.png", EZ.getWindowWidth() / 2, EZ.getWindowHeight() / 2); //set position of background

        EZImage rectanglePicture = EZ.addImage("rectangle.png", EZ.getWindowWidth() / 2, EZ.getWindowHeight () / 7); //set position of palette

        EZImage hatPicture = EZ.addImage("hat.png",1* EZ.getWindowWidth() / 10, EZ.getWindowHeight () / 6); //set position of stickers

        EZImage bluntPicture = EZ.addImage("blunt.png",1* EZ.getWindowWidth() / 3, EZ.getWindowHeight () / 6); //set position of stickers

        EZImage dealwithitPicture = EZ.addImage("dealwithit.png",3* EZ.getWindowWidth() / 5, EZ.getWindowHeight () / 6); //set position of stickers

        EZImage weedPicture = EZ.addImage("weed.png",10* EZ.getWindowWidth() / 11, EZ.getWindowHeight () / 6); //set position of stickers

        EZSound hatsound = EZ.addSound("airhorn.wav");
        EZSound bluntsound = EZ.addSound("yungdog.wav");
        EZSound dealwithitsound = EZ.addSound("sandstorm.wav");
        EZSound weedsound = EZ.addSound("weed.wav");

        while(true) { //while loop is always true
            int clickX = EZInteraction.getXMouse(); // declare an integer variable called clickX and the X mouse coordinate integer is assigned to it
            int clickY = EZInteraction.getYMouse(); // declare an integer variable called clickY and assign to it the Y mouse coordinate integer

            if (EZInteraction.wasMouseLeftButtonReleased()){  //if the left mouse button is released then
                if (hatPicture.isPointInElement(clickX, clickY)) //if the left mouse button is release on this picture
                    hatsound.play(); //then hatsound will play

                if (bluntPicture.isPointInElement(clickX, clickY)) //if the left mouse button is released on this picture
                    bluntsound.play(); //then bluntsound will play

                if (dealwithitPicture.isPointInElement(clickX,  clickY)) //if the left mouse button is released on this picture
                    dealwithitsound.play(); //then dealwithitsound will pay

                if (weedPicture.isPointInElement(clickX,  clickY)) //if the left mouse button is released on this picture
                    weedsound.play(); //then weedsound will play
        }

        while(true) {               
            if (EZInteraction.wasMouseLeftButtonReleased()){
                if (hatPicture.isPointInElement(clickX, clickY))
                    EZ.addImage("hat.png", clickX, clickY);

                if (bluntPicture.isPointInElement(clickX, clickY))
                    EZ.addImage("blunt.png", clickX, clickY);

                if (dealwithitPicture.isPointInElement(clickX, clickY))
                    EZ.addImage("dealwithit.png", clickX, clickY);

                if (weedPicture.isPointInElement(clickX, clickY))
                    EZ.addImage("weed.png", clickX, clickY);
            }
            EZ.refreshScreen();
        }
      }
    }
}

1 个答案:

答案 0 :(得分:2)

首先,如果你想在编程方面做得很清楚,那么你可以避免使用速记if语句。使用括号,很多。使用空格,很多。没有理由通过删除括号和空格进行优化,而且您不会像在实体纸上那样为额外的行付费。

我不会指出你的代码样式的所有明显问题,因为在那一点上我不妨推荐你阅读令人惊叹的Head First Java书,但你明白了。

其次,我非常建议存储当前选择的图像(或者,因为Java是通过引用传递的,对该图像的引用),然后在用户单击时,该点击不在图像和图像上选中后,将图像的副本放在屏幕上。

实施例

EZ.initialize(1644, 1022); //pixel size of run screen
EZImage selectedImage;

EZImage rectanglePicture = EZ.addImage("rectangle.png", EZ.getWindowWidth() / 2, EZ.getWindowHeight() / 7);
EZImage hatPicture = EZ.addImage("hat.png", 1 * EZ.getWindowWidth() / 10, EZ.getWindowHeight() / 6);

EZSound rectangleSound = EZ.addSound("sandstorm.wav");
EZSound hatSound = EZ.addSound("airhorn.wav");

while (true) { // Assuming while loop even necessary
    int clickX = EZInteraction.getXMouse();
    int clickY = EZInteraction.getYMouse();

    if(EZInteraction.wasMouseLeftButtonReleased()) {

        if(hatPicture.isPointInElement(clickX, ClickY)) {

            selectedImage = hatPicture;
            hatSound.play();

        } else if(rectanglePicture.isPointInElement(clickX, ClickY)) {

            selectedImage = rectanglePicture;
            rectangleSound.play();

        } else {

            EZ.addImage("hat.png", clickX, clickY);
            EZ.refreshScreen();

        }

    } // End if

} // End loop