使用lwjgl和带有for循环的openGL从列表中添加项目符号的可视错误

时间:2013-05-02 22:33:38

标签: java opengl lwjgl

所以我用lwjgl学习openGL,一切都很好,直到我添加了一种拍摄方式。按空格键后,将创建一个项目符号并将其添加到项目符号列表中。然后,当我去渲染它时,我使用for循环调用每个子弹的x和y,使用coords渲染,然后更新所有子弹。除非我这样做,否则玩家(一个绿色的盒子)会变得浮华并且不会移动(视觉上)并且子弹永远不会从屏幕上消失而是向下移动而留下“鬼道”。但是,我可以在不移动绿色框的情况下移动玩家,因为当我将他向上移动并向右移动一点时,我可以从第二个向上和向右创建一个新的子弹鬼道。

Bullet.java:

package game;
import java.util.ArrayList;
import java.util.List;


public class Bullet {
  public int speed;
  public int x;
    public int y;
    public char direction;
    int origin;
    public static final int PLAYER_ORIGIN = 0;
    public static final int MONSTER_ORIGIN = 1;
    static List<Bullet> bullets = new ArrayList<Bullet>();
    public static void doTick(){
        for(int i = 0; i < bullets.size(); i++){
            //moves bullets based on speed and direction
            if(bullets.get(i).direction == 'w'){
                bullets.get(i).y -= bullets.get(i).speed;
            } else if(bullets.get(i).direction == 'a'){
                bullets.get(i).x -= bullets.get(i).speed;
            } else if(bullets.get(i).direction == 's'){
                bullets.get(i).y += bullets.get(i).speed;
            } else if(bullets.get(i).direction == 'd'){
                bullets.get(i).x += bullets.get(i).speed;
            }
        }
    }
    public Bullet(int X, int Y, char newDirection, int newOrigin){
        direction = newDirection;
        speed = 6;
        if(newOrigin == PLAYER_ORIGIN){
            if(direction == 'w' ){
                x= X + 16;
                y= Y;
            } else if (direction == 'd'){
                x= X + 32;
                y= Y + 16;
            } else if (direction == 'a'){
                x= X;
                y= Y + 16;
            } else if(direction == 's'){
                x= X + 16;
                y= Y + 32;
            }
        }

    }
}

Game.java:

package game;
import javax.swing.*;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.Timer;
import java.awt.event.*;
import java.util.List;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
@SuppressWarnings({ "serial" })
public class Game{
  static Player player = new Player(10,10);
    Timer timer;
    boolean wP;
    boolean aP;
    boolean sP;
    boolean dP;
    boolean spP;
    int xP;
    int yP;

    public static void main(String[] args){ 
        Game game = new Game();
        game.start();
    }
    private void start(){
        //Initialize display
        try {
            Display.setDisplayMode(new DisplayMode(800,600));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
        }
        //Initialize opengl
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0,Display.getWidth(),0,Display.getHeight(), -1, 1);
        glMatrixMode(GL_MODELVIEW);
        glClearColor(0,0,0,1);
        glDisable(GL_DEPTH_TEST); //No 3D
        //Game loop
        while(!Display.isCloseRequested()){
            glClear(GL_COLOR_BUFFER_BIT);

            glColor3f(0.25f,0.75f,0.5f);

            glBegin(GL_QUADS);
            {
                glVertex2f(xP, yP);
                glVertex2f(xP, 64 + yP);
                glVertex2f(64 + xP, 64 + yP);
                glVertex2f(64 + xP, yP);
            }
            glEnd();
            for(int i = 0; i < Bullet.bullets.size(); i++){
                glBegin(GL_QUADS);
                glVertex2f(Bullet.bullets.get(i).x, Bullet.bullets.get(i).y);
                glVertex2f(Bullet.bullets.get(i).x, 4 + Bullet.bullets.get(i).y);
                glVertex2f(4 + Bullet.bullets.get(i).x, Bullet.bullets.get(i).y);
                glVertex2f(4 + Bullet.bullets.get(i).x, 4 + Bullet.bullets.get(i).y);
            }
            Display.update();
            if(Keyboard.isKeyDown(Keyboard.KEY_W)){
                yP += 10;
            }
            if(Keyboard.isKeyDown(Keyboard.KEY_A)){
                xP -= 10;
            }
            if(Keyboard.isKeyDown(Keyboard.KEY_S)){
                yP -= 10;
            }
            if(Keyboard.isKeyDown(Keyboard.KEY_D)){
                xP += 10;
            }
            if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
                Bullet tempBullet = new Bullet(xP+32, yP+32, 'w', Bullet.PLAYER_ORIGIN); //Creates a temporary bullet based on player
                Bullet.bullets.add(tempBullet); //adds bullet to the static list of bullets in Bullet.java
            }
            Bullet.doTick();

        }
        Display.destroy();
    }
}

1 个答案:

答案 0 :(得分:0)

绘制每个项目符号后glEnd();可能会有所帮助。

此外,不相关,但您可以将glBegin / glEnd放在for循环的外部。