如何使我的主菜单按钮在我的游戏中工作

时间:2015-06-27 12:14:53

标签: java lwjgl

我正在尝试为我的游戏制作主菜单,我不知道如何使我的按钮工作 我正在使用Slic2DLWJGL个库

我的GameMenu课程:

package lt.CobaltPlanet.nelixus.state;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;



public class GameMenu extends BasicGameState{

    Image Backgruond,Game_Title;
    Image play,play_hover,play_pressed;
    Image option,option_hover,option_pressed;
    Image quit,quit_hover,quit_pressed;

    public GameMenu(int state){
    }

    public void init(GameContainer gamecontainer1, StateBasedGame statebasedgame1) throws SlickException{
    Game_Title = new Image("res/Game_Title.png");
    Backgruond = new Image("res/BG.png");
    play = new Image("res/Main_menu_buttons/play.png");
    play_hover = new Image("res/Main_menu_buttons/play_hover.png");
    play_pressed = new Image("res/Main_menu_buttons/play_pressed.png");
    quit = new Image("res/Main_menu_buttons/quit.png");
    quit_hover = new Image("res/Main_menu_buttons/quit_hover.png");
    quit_pressed = new Image("res/Main_menu_buttons/quit_pressed.png");
    option = new Image("res/Main_menu_buttons/options.png");
    option_hover = new Image("res/Main_menu_buttons/options_hover.png");
    option_pressed = new Image("res/Main_menu_buttons/options_pressed.png");
    }

    public void render(GameContainer gamecontainer1, StateBasedGame statebasedgame1, Graphics g1) throws SlickException{
            Backgruond.draw(0,0);
            play.draw(205,206);
            option.draw(205,286);
            quit.draw(205, 366);
            Game_Title.draw(0,-100);
            //Mouse Position
            g1.drawString(mousePos, 10, 25);
        }


    public void update(GameContainer gamecontainer1, StateBasedGame statebasedgame1, int delta1) throws SlickException{ 
    }
    public int getID(){
        return 1;
    }

}

1 个答案:

答案 0 :(得分:1)

查看thisthis。你想要鼠标位置:

int mousePosX = Mouse.getX();
int mousePosY = Mouse.getY();

检查鼠标是否被点击:

Mouse.isButtonDown(0);

使用Slick的Rectangle.contains(float x, float y)查看该点是否位于按钮的hitbox中。假设您有一个设置了Slick矩形的按钮:

if(Mouse.isButtonDown(0) && buttonHitbox.contains(mousePosX, mousePosY)) {
    System.out.println("The button was clicked!");
}

<强>更新

if(quitRect.contains(Mouse.getX(), Mouse.getY()) {
    //draw hover image
    quit_hover.draw();
    //check if button is pressed
    if(Mouse.isButtonDown(0)) {
        System.out.println("Button pressed");
    }
}

这也会绘制悬停图像。

更新II:

package lt.CobaltPlanet.nelixus.state;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.geom.*;


public class GameMenu extends BasicGameState{

    boolean playHover = false;
    Rectangle playRect;
    Image Backgruond,Game_Title;
    Image play,play_hover,play_pressed;
    Image option,option_hover,option_pressed;
    Image quit,quit_hover,quit_pressed;

    public GameMenu(int state){
    }

    public void init(GameContainer gamecontainer1, StateBasedGame statebasedgame1) throws SlickException{
    Game_Title = new Image("res/Game_Title.png");
    Backgruond = new Image("res/BG.png");
    play = new Image("res/Main_menu_buttons/play.png");
    playRect = new Rectangle(205, 206, play.getWidth(), play.getHeight());
    play_hover = new Image("res/Main_menu_buttons/play_hover.png");
    play_pressed = new Image("res/Main_menu_buttons/play_pressed.png");
    quit = new Image("res/Main_menu_buttons/quit.png");
    quit_hover = new Image("res/Main_menu_buttons/quit_hover.png");
    quit_pressed = new Image("res/Main_menu_buttons/quit_pressed.png");
    option = new Image("res/Main_menu_buttons/options.png");
    option_hover = new Image("res/Main_menu_buttons/options_hover.png");
    option_pressed = new Image("res/Main_menu_buttons/options_pressed.png");
    }

    public void render(GameContainer gamecontainer1, StateBasedGame statebasedgame1, Graphics g1) throws SlickException{
            Backgruond.draw(0,0);
            play.draw(205,206);
            option.draw(205,286);
            quit.draw(205, 366);
            if(playHover) play_hover.draw(205, 206);
            Game_Title.draw(0,-100);
            //Mouse Position
            g1.drawString(mousePos, 10, 25);
        }


    public void update(GameContainer gamecontainer1, StateBasedGame statebasedgame1, int delta1) throws SlickException{ 
        playHover = false;
        if(playRect.contains(Mouse.getX(), Mouse.getY()) {
            playHover = true;
            if(Mouse.isbuttonDown(0)) {
                System.out.println("The button was pressed!");
            }
        }
    }
    public int getID(){
        return 1;
    }

}

这是你的代码,我添加了播放按钮的功能。现在你应该能够自己完成剩下的工作了。