2017-08-31 4 views
0

Actuellement, je suis capable de déplacer la boîte vers la gauche et la droite à travers la limite d'affichage, mais je suis incapable de comprendre comment déplacer la boîte de haut en bas. Je voudrais que la boîte navigue dans un cercle autour de l'affichage, par exemple, déplacez-la vers la droite, puis vers le bas, puis vers la gauche, puis vers le haut, puis continuez tout au long de l'affichage. Voici mon code pour ma classe d'entité:Déplacer la boîte 2d le long des limites d'affichage

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;    
    } 

} 
} 

Voici mon code pour 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); 
} 
} 

Toute aide serait appréciée. J'ai aussi besoin que la boîte change de couleur chaque fois qu'elle change de direction.

Répondre

0

Corrigez-moi si je me trompe mais je pense que vous avez juste besoin d'utiliser box.translate(0,value) pour déplacer votre Rectangle vers le haut ou vers le bas. Il suffit d'adapter votre mouvement gauche-droite à la valeur y de la boîte. Vous pouvez également utiliser l'État pour déterminer la couleur à laquelle il doit être tracé.