沿显示边界移动2d框

时间:2017-08-31 04:51:38

标签: java awt state lwjgl

目前,我可以通过显示边界向左和向右移动框,但我无法弄清楚如何上下移动框。我希望盒子在显示器周围的圆圈中导航,例如向右移动,然后向下移动,然后向左移动,然后向上移动,然后在整个显示器中连续移动。这是我的实体类的代码:

import java.awt.Rectangle;
import org.lwjgl.opengl.GL11;

class Entity
{
private static enum State  { START, LEFT, RIGHT, UP, DOWN};

private Rectangle box;
private State state;
private float speed;        // pixels / ms

public Entity(float speed)
{
    box = new Rectangle(10, 10, 10, 10);
    state = State.START;
    this.speed = speed;
}

public void draw()
{
    float x = (float)box.getX();
    float y = (float)box.getY();
    float w = (float)box.getWidth();
    float h = (float)box.getWidth();


    // draw the square

    GL11.glColor3f(0,1,0);
    GL11.glBegin(GL11.GL_QUADS);

    GL11.glVertex2f(x, y);
    GL11.glVertex2f(x+w, y);
    GL11.glVertex2f(x+w, y+w);
    GL11.glVertex2f(x, y+w);

    GL11.glEnd();

}

public void update(int delta)
{
    switch (state)
    {
     case START:
         state = State.RIGHT;


     case RIGHT:

         box.translate((int)(speed*delta), 0);

         if (box.getX() >= 800)
         {
             state = State.LEFT;
         }

         break;


     case LEFT:

         box.translate((int)(-speed*delta), 0);

         if (box.getX() <= 0)
         {
             state = State.RIGHT;
         }

         break;             
    }

}
}

这是我的GameLoop代码:

 import org.lwjgl.Sys;
 import org.lwjgl.opengl.Display;
 import org.lwjgl.opengl.DisplayMode;
 import org.lwjgl.opengl.GL11;

 import org.lwjgl.input.Keyboard;
 import org.lwjgl.LWJGLException;


public class GameLoop
{
public static final int TARGET_FPS=100;
public static final int SCR_WIDTH=800;
public static final int SCR_HEIGHT=600;

public static void main(String[] args) throws LWJGLException
{
    initGL(SCR_WIDTH, SCR_HEIGHT);

    Entity e = new Entity(.1f);


    long time = (Sys.getTime()*4000)/Sys.getTimerResolution(); // ms
    while (! Display.isCloseRequested())
    {
        long time2 = (Sys.getTime()*4000)/
            Sys.getTimerResolution(); // ms
        int delta = (int)(time2-time);
        System.out.println(delta);

        e.update(delta);


        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

        e.draw();


        // UPDATE DISPLAY
        Display.update();
        Display.sync(TARGET_FPS);
        time = time2;
    }

    Display.destroy();
}


public static void initGL(int width, int height) throws LWJGLException
{
    // open window of appropriate size
    Display.setDisplayMode(new DisplayMode(width, height));
    Display.create();
    Display.setVSyncEnabled(true);

    // enable 2D textures
    GL11.glEnable(GL11.GL_TEXTURE_2D);              

    // set "clear" color to black
    GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);         

    // enable alpha blending
    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

    // set viewport to entire window
    GL11.glViewport(0,0,width,height);

    // set up orthographic projectionr
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, width, height, 0, 1, -1);
    // GLU.gluPerspective(90f, 1.333f, 2f, -2f);
    // GL11.glTranslated(0, 0, -500);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
}

任何帮助将不胜感激。每次改变方向时,我都需要让盒子改变颜色。

1 个答案:

答案 0 :(得分:0)

如果我错了,请纠正我,但我认为您只需要使用box.translate(0,value)向上或向下移动矩形。只需将左右移动调整为框的y值即可。您还可以使用State来确定应该绘制哪种颜色。