0

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

子弹.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;
            }
        }

    }
}

游戏.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();
    }
}
4

1 回答 1

0

glEnd();画完每个子弹后可能会有所帮助。

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

于 2013-05-03T03:37:40.827 回答