1

私が取り組んでいる Pong ゲームがあります。現在、プレーヤーのパドルを機能させる作業を行っています。これが今起こっていることです。ゲームが開始され、パドルが画面上に配置されます。パドルを上に押すと、正方形が上に伸びます。押し下げると逆になります。私が必要とするのは、正方形が実際に個々のオブジェクトとして移動し、移動するたびにそれ自体を削除して、ずっと正方形のままになるようにすることです。パドルが大きくなるのではなく、同じ形を保ったまま移動する必要があります。openGLでこれを行うにはどうすればよいですか? これが私の2つのクラスです。

スタートアップ (メイン クラス):

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 に答える