Pong , but how?

Hello,

iam sorry but iam really new at jME. I programmed before c++.



I found a interisting short tutorial how to program a small pong game http://developers.sun.com/learning/javaoneonline/2008/pdf/TS-5711.pdf?cid=925556 .



But this describes only the important functions, i want to have a running code which i want to understand.



I tryed to make the first steps but it will not work.


package FirstGame;


import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingSphere;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.math.Vector3f;
import com.jme.scene.Geometry;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;

public class FirstGame extends SimpleGame {

   @Override
   protected void simpleInitGame() {
      Geometry ball = new Sphere("Ball", 8, 8, 2);
      ball.setModelBound(new BoundingSphere());
      ball.updateModelBound();
      
      Box player1 = new Box("Player1", new Vector3f(), 2, 5, 10);
      player1.setModelBound(new BoundingBox());
      player1.updateModelBound();
      player1.getLocalTranslation().set(-100, 0, 0);
      
      // Tastatureingaben
      KeyBindingManager.getKeyBindingManager().set("MOVE_UP", KeyInput.KEY_W);
      
   }
   
   protected void simpleUpdate(Box player1) {
      if (KeyBindingManager.getKeyBindingManager()
      .isValidCommand("MOVE_UP", true)) {
         float player1Speed = 0;
         player1.getLocalTranslation().z -= player1Speed * timer.getTimePerFrame();
      }
      if (player1.hasCollision(ball, false)) {
         ballVelocity.x *= -1f;
         }
         if (sideWalls.hasCollision(ball, false)) {
         ballVelocity.z *= -1f;
         }
         if (player1GoalWall.hasCollision(ball, false)) {
         player2Score++;
         }
      
      }

}



Thanks :)

Crikey 45 page PDFs scare me so I didn't read it!



There is a lot of important stuff that your code is not doing. Take a look at some of the basic wiki entries and FlagRush tutorials for ideas.



That said, I heartily agree with the point that basic working code to play with is a good place to start. So I did the basics to get it working  :smiley: See below for code. I changed the keys to U,J as most keys already do something in a SimpleGame. There is no scoring and player2 is just a big wall at the moment to keep the game going.



Note that I'm not saying this is the best way to do it - just took what you had and tried to make it work.



EDIT - Removed nasty override tags :wink:


public class FirstGame extends SimpleGame {

   private final float PLAYERSPEED = 30.0f;
   private final float BALLSIZE = 2.0f;
   private final float BATSIZE = 10.0f;
   
   private Geometry ball = null;
   private Vector3f ballVelocity = null;
   
   private Box player1 = null; private Box player2 = null;
   private Box topWall = null, bottomWall = null;
   
   protected void simpleInitGame() {
      
      ball = new Sphere("Ball", 8, 8, BALLSIZE);
      ball.setModelBound(new BoundingSphere());
      ball.updateModelBound();
      
      ballVelocity = new Vector3f(-30.0f, 15, 0);
      
      topWall = new Box("topWall", new Vector3f(), 100f, 5f, 10f);
      topWall.setModelBound(new BoundingBox());
      topWall.updateModelBound();
      topWall.getLocalTranslation().set(0, 50, 0);
      
      bottomWall = new Box("topWall", new Vector3f(), 100f, 5f, 10f);
      bottomWall.setModelBound(new BoundingBox());
      bottomWall.updateModelBound();
      bottomWall.getLocalTranslation().set(0, -50, 0);
      
      player1 = new Box("Player1", new Vector3f(), 2f, BATSIZE, 5f);
      player1.setModelBound(new BoundingBox());
      player1.updateModelBound();
      player1.getLocalTranslation().set(-100, 0, 0);
      
      player2 = new Box("Player2", new Vector3f(), 2f, 50, 5f);
      player2.setModelBound(new BoundingBox());
      player2.updateModelBound();
      player2.getLocalTranslation().set(100, 0, 0);
      
      cam.getLocation().set(0, 0, 200f);
      cam.update();
      
      rootNode.attachChild(topWall); rootNode.attachChild(bottomWall);
      rootNode.attachChild(player1); rootNode.attachChild(player2);
      rootNode.attachChild(ball);
      
      // Tastatureingaben
      KeyBindingManager.getKeyBindingManager().set("MOVE_UP", KeyInput.KEY_U);
      KeyBindingManager.getKeyBindingManager().set("MOVE_DOWN", KeyInput.KEY_J);
      
   }
   
   protected void simpleUpdate() {
      ball.getLocalTranslation().addLocal(ballVelocity.mult(tpf));
      ball.updateWorldVectors();
      
      if (KeyBindingManager.getKeyBindingManager()
      .isValidCommand("MOVE_UP", true)) {player1.getLocalTranslation().y += PLAYERSPEED * tpf;}
      if (KeyBindingManager.getKeyBindingManager()
            .isValidCommand("MOVE_DOWN", true)) {player1.getLocalTranslation().y -= PLAYERSPEED * tpf;}
      
      if (player1.hasCollision(ball, false)) {
         ballVelocity.x *= -1f;
         ball.getLocalTranslation().x += BALLSIZE;   // Crude way to stop it sticking to the bat
         }
      if (player2.hasCollision(ball, false)) {
         ballVelocity.x *= -1f;
         ball.getLocalTranslation().x -= BALLSIZE;   // Crude way to stop it sticking to the bat
         }
      if (topWall.hasCollision(ball, false)) {
         ballVelocity.y *= -1f;
         ball.getLocalTranslation().y -= BALLSIZE;
         }
      if (bottomWall.hasCollision(ball, false)) {
         ballVelocity.y *= -1f;
         ball.getLocalTranslation().y += BALLSIZE;
         }
      
      if( (FastMath.abs(ball.getLocalTranslation().x) > 110f) ||
            (FastMath.abs(ball.getLocalTranslation().y) > 110f) ||
            (FastMath.abs(ball.getLocalTranslation().z) > 110f)) ball.setLocalTranslation(0, 0, 0);
      
      }

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

We added the pong examples (from which the snips were taken) to the jME tests, under TutorialGuide. TestPong and TestPongCool. If that helps.

Yeah, that helps me very much :). Thanks Alric and MrCoder !  :smiley: