1

我有一个正在开发的 Pong 游戏。现在,我正在努力让玩家桨正常工作。这就是现在发生的事情。游戏开始,桨位于屏幕上。向上按桨可将正方形向上延伸。按下则相反。我需要的是让正方形实际作为一个单独的对象移动,并在每次移动时自行删除,以便它始终保持正方形。而不是桨变大,我需要它保持相同的形状,但只是移动。我将如何在 openGL 中执行此操作?这是我的两节课。

启动(主类):

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

import com.evanklein.pong.entitity.Player;

public class Startup {

    // set up display
    public void start() {
        try {
            Display.setDisplayMode(new DisplayMode(600, 400)); // these numbers
                                                                // pending
            Display.setTitle("Evan's Pong!");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 600, 400, 0, 1, -1);

        while (!Display.isCloseRequested()) {

            // render OpenGL here
            GL11.glBegin(GL11.GL_QUADS);
            GL11.glColor3f(3.0f, 7.2f, 6.7f);
            GL11.glVertex2d(player.width, player.length);
            GL11.glVertex2d(player.width + 100, player.length);
            GL11.glVertex2d(player.width + 100, player.length + 100);
            GL11.glVertex2d(player.width, player.length + 100);
            GL11.glEnd();

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

            if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
                player.moveUp();
            } 
            if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
                player.moveDown();
            }
        }

        Display.destroy();
    }

    // Let's start this beyotch up!
    Player player = new Player();

    public static void main(String[] args) {
        new Startup().start();
    }
}

球员等级:

public class Player {

    // size variables
    public int width = 50;
    public int length = 120;

    private int moveSpeed = 10; // mph

    public Player() {

    }

    public void moveUp() {
        length -= moveSpeed;
    }

    public void moveDown() {
        length += moveSpeed;
    }
}

如果您有任何其他问题或需要其他任何其他详细信息,请告诉我。

4

2 回答 2

5

glClear()isCloseRequested()循环开始时的帧缓冲区。

于 2013-07-15T20:40:07.030 回答
0

我还没有运行代码,但它应该让你继续

在您的主要课程中:

...
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity(); 
GL11.glOrtho(0, 600, 400, 0, 1, -1);

GL11.glMatrixMode(GL11.GL_MODELVIEW); // make active the ModelView matrix
GL11.glClearColor(0, 0, 0, 1);        // setup black as the clear color

while (!Display.isCloseRequested()) {

  GL11.glClear(GL.GL_COLOR_BUFFER_BIT); // clear the screen using the color above

  GL11.glLoadIdentity();   // reset any transformations in the ModelView matrix

  // move the player square to its current position
  GL11.glTranslatef(player.x, player.y, 0); 

  // render OpenGL here
  ....

在您的播放器类中:

  ...
  // add new fields for player's position 
  // change those values according to the desired initial position
  public int x = 100; 
  public int y = 100;  
  ... 

  // change the position of the square, not its dimensions
  public void moveUp() {
      y -= moveSpeed;
  }

  public void moveDown() {
      y += moveSpeed;
  }
于 2013-07-15T21:29:09.147 回答