从按钮按下的LWJGL四边形消失

时间:2015-01-01 01:24:28

标签: java opengl mouse lwjgl

我想点击一下四边形。每次我点击屏幕,屏幕上的四边形闪烁然后消失。如果我一直点击,它会一直闪烁。是因为我开始我的方法的顺序?谢谢!

使用更新代码编辑...我在点击时显示正方形,但它不会停留。它跟在屏幕上的鼠标

package main;
import static org.lwjgl.opengl.GL11.*;

import java.awt.event.MouseEvent;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.openal.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.tiled.TiledMap;

import static helper.Artist.*;
import helper.Artist.*;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.*;

public class GameWindow {
    public TiledMap map;
    public TiledMap menu;
    private int sx,sy;
    public int x,y;
    public static int fx;
    public static int fy;
    public int fxy;
    public boolean place = true;
public static enum states{

        MAIN, GAME; }

    public states state = states.MAIN;




    public void render(){
        switch(state){
        case MAIN:


            try {
                init();
                update();
                menu.render(0, 0);
            } catch (SlickException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            break;
        case GAME:
            try {
                map = new TiledMap("res/map.tmx");
                map.render(0,0);
                Draw();
                if(place == false){
                    Quad();

                }

                update();
                //Draw();
                System.out.println(place);

                glBegin(GL_QUADS);
                    glVertex2i(x*64,y*64);
                    glVertex2i(x * 64 + 64, y*64);
                    glVertex2i(x*64 + 64,y*64 + 64);
                    glVertex2i(x*64,y*64 + 64);
                glEnd();

            } catch (SlickException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



        }


    }


    public void Draw(){
        fx = this.fx;
        fy = this.fy;

        while(Mouse.next()){
            if(Mouse.getEventButtonState()){
                if(Mouse.getEventButton() == 0){
                System.out.println("Click");
                int iy = Mouse.getY();
                 fy = (iy+0x20)>>0x06;

                int ix = Mouse.getX();
                 fx = (ix+0x20)>>0x06;


                place = false;


            }else{

                place = true;
            }

        }} 



    }


    public void Quad(){

    glBegin(GL_QUADS);
        glVertex2i(fx*64,fy*64);
        glVertex2i(fx * 64 + 64, fy*64);
        glVertex2i(fx*64 + 64,fy*64 + 64);
        glVertex2i(fx*64,fy*64 + 64);
    glEnd();
    }

    public void update(){




        int object = map.getLayerIndex("Tile Layer 2");

        //System.out.println(x);
        map.getTileId(0, 0, object);

        while(Keyboard.next()){

        if(Keyboard.getEventKeyState()){
            if(Keyboard.getEventKey() == Keyboard.KEY_DOWN){
            //System.out.println(y);
            if(map.getTileId(x , y+1, object) ==0){

                y++;
                //System.out.println(y);
            }

        }}
        if(Keyboard.isKeyDown(Keyboard.KEY_UP)){
            System.out.println(y);
            if(y>=1){
                if(map.getTileId(x, y-1, object) ==0){

                    y--;
                System.out.println(y);

                }
            }
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
            System.out.println(x);
            if(map.getTileId(x -1, y, object) ==0){

                x--;
                System.out.println(x);
            }

        }


        if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
            System.out.println(x);
            if(map.getTileId(x + 1,y, object) ==0){

                x++;
                System.out.println(x);
            }
        }}
    }

    public void init() throws SlickException{
        menu = new TiledMap("res/menu.tmx");
        map = new TiledMap("res/map.tmx");
        x = 1;
        y = 0;


    }
    public void Mouse(){
        int iy = Mouse.getY();
         fy = (iy+0x20)>>0x06;

        int ix = Mouse.getX();
         fx = (ix+0x20)>>0x06;


        //System.out.println(fy);
        //System.out.println(fx);
    }
    public void checkInput(){
        switch(state){
        case MAIN:
            if(Keyboard.isKeyDown(Keyboard.KEY_RETURN)){
                state = states.GAME;
            }

            break;
        case GAME:
            if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
                state = states.MAIN;
            }
        }

    }


    public GameWindow(){


        try {
            Display.setDisplayMode(new DisplayMode(1280,960));
            Display.create();
        } catch (LWJGLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Display.setTitle("Hello");

        //initialization
        grass = LoadTexture("dirt64");
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 1280, 960, 0, 1, -1 );
        glMatrixMode(GL_MODELVIEW);
        glEnable(GL_TEXTURE_2D);

        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        while(!Display.isCloseRequested()){
            //render code
            checkInput();
            Mouse();
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            render();

            Display.update();
            Display.sync(60);
        }

        Display.destroy();
    }


    public static void main(String[] args){
    new GameWindow();

}}

0 个答案:

没有答案