I've been following a JavaHub tutorial that basically uses a pixel engine similar to MiniCraft. I've attempted to make a Player Class as such, and I'm basically making a mock Pokemon game for learning's sake:

    package pokemon.entity;

    import java.awt.Rectangle;

    import pokemon.gfx.Screen;
    import pokemon.levelgen.Tile;
    import pokemon.entity.SpritesManage;;

    public class Player 
      int x, y;
      int vx, vy;
      public Rectangle AshRec;
      public Sprite AshSprite;
      Screen screen;
      Sprite[][] AshSheet;

      public Player()
        AshSprite = SpritesManage.AshSheet[1][0];

        AshRec = new Rectangle(0, 0, 16, 16);

        x = 0;
        y = 0;

        vx = 1;
        vy = 1;

        screen.renderSprite(0, 0, AshSprite);

      public void update()

      private void checkCollision() 


      private void move() 
        AshRec.x += vx;
        AshRec.y += vy;

      public void render(Screen screen, int x, int y)
        screen.renderSprite(x, y, AshSprite);


I guess what I really want to do is have the Player centered in the screen and have the sprite drawn based on an Input Handler.

I have a render method in the main Game Class that I'm using to render the tiles for the map. I'd imagine I'd just need to add some parameters or methods in the Player Class? Do I create and render the sprite for the Player in the Main Class or Player Class?

Main render method:

    private void render() 
      BufferStrategy bs = getBufferStrategy();
      if(bs == null)
        //Technically 3 dimensions;

      level.renderBkgrd(xScroll, yScroll, screen);
      level.renderSprite(0, 0, Ash.AshSprite);

      for(int i = 0; i < screen.pixels.length; i++)
        pixels[i] = screen.pixels[i];

      //Here are the graphics that you meticulously use to put in a separate paint method.
      Graphics g = bs.getDrawGraphics();
      g.drawImage(image, 0, 0, getWidth(), getHeight(), null);

      //If they are already drawn, don't show them again.

...And level.renderSprite() is what I thought would work...But without it the code gives me a NullPointerException.


Normally, a Game/Board/World class can aggregate all of the object updates and renderings. Knowing what I do now, in a World update() method, I typically throw the player.update() method in there and the drawing method in the World draw() method. The answer concerning through objects into an object manager is also a good suggestion, especially considering you could have multiple different enemies that will probably be deleted or updated synchronously in the near future as the game runs.

It appears I had a specific problem with an NPE (as general as it can be). It looks like I was over-complicating things way too much: I could just abstract the render/draw method out and have a player sprite (determined by the Player Class, if need be) rendered at the player's coordinates (again, determined by the Player Class). As you can see in the Player Class above I have a render method which calls the screen render method while I'm fiddling around with the Level instances rendering something in the main render() method and it's just a hot mess.

Moral of the story: A good game dev tutorial will offer you the reason for their methodology based on design patterns like separation of concerns that make the code much more maintainable.

  • \$\begingroup\$ I'm voting to close as unclear. It's not clear what the question or problem is. Apparently going by the accepted answer it is/was specified in comments somewhere, but it's not specified in the question. \$\endgroup\$ – doppelgreener May 22 '17 at 17:12
  • \$\begingroup\$ I tried to clean it up a little, but it is half a "debug my code" question. I won't feel bad if you close it though, there are probably better questions about player movement in tile-based games (gamedev.stackexchange.com/questions/26817/…). \$\endgroup\$ – A13X May 23 '17 at 20:15
  • \$\begingroup\$ Wow, I didn't actually expect you to come back to revise it! That's a step above. :) Thanks a lot, and I'm glad you figured it out. \$\endgroup\$ – doppelgreener May 23 '17 at 21:20

I noticed in the comments you want to make an Entity Class.

This basically indicates you want all of your objects in the game for example your player, a building etc to share similarities. To do this you simply decide what are those similarities and put them in your entity class (for my example i will assume everything will have a position so X & Y and update and render method this would then become.

public class Entity {
  private int x;
  private int y;

 public int getX() {
   return x;

 public void setX(int x) {
  this.x = x;

 public int getY( {
   return Y;

 public void setY(int y) {
  this.y = y;

 public void update() {

 public void render(Screen screen) {

(Note my render method does not pass x, y as my entity will know its position.)

Now I my entity class I can create sub types of that class like player by doing

public class Player extends Entity
  public void update() {
    //update logic for my player here

  public void render(Screen screen) {
     //Players Drawing logic here.

No that i have a Base Class (Entity) I can put my objects in a list and then use that list to call the methods of each like so.

Add to list

List<Entity> objInMyGame = new ArrayList<Entity>();
obj.add(new Player());

iterate through them all to update them.

for(Entity e : objInGame) {

Hope this helps.

On a side note please take a look at Java coding conventions, it helps make your code more readable :)

  • \$\begingroup\$ Actually this makes a lot more sense. Would I add the objects in my main class via Public Player player; and then obj.add()? \$\endgroup\$ – A13X Jul 4 '12 at 18:25
  • \$\begingroup\$ Oh wait I do have an update class in my main method. I'd assume I would make the array of objects in a declaration at the top and then use the for loop to update them. Thanks again! \$\endgroup\$ – A13X Jul 5 '12 at 3:12

Draw your player at:

playerX - cameraX playerY - cameraY

Then, set your camera to:

cameraX = (screenWidth / 2) - (playerX / 2) cameraY = (screenHeight / 2) - (playerY / 2)

  • \$\begingroup\$ Thanks, I guess I'll need to figure out how to render it on my own or ask the tutorial maker how to render the player itself but I will be able to place it correctly with this formula \$\endgroup\$ – A13X Jun 29 '12 at 20:32
  • \$\begingroup\$ If you give me detail on the issue you're having, I can provide a more informative response. \$\endgroup\$ – Vaughan Hilts Jun 29 '12 at 20:51
  • \$\begingroup\$ P.S. I gave some more details above. I basically just need to know where and how to render the Player sprite. \$\endgroup\$ – A13X Jul 3 '12 at 17:44
  • \$\begingroup\$ You're better off creating an ObjectManager and creating an ArrayList of all your 'objects'. Then, you can iterate over them all and draw them all at once. Does that make sense? \$\endgroup\$ – Vaughan Hilts Jul 3 '12 at 22:38
  • \$\begingroup\$ I will look into it. I am actually novice at classes and objects and sometimes the tutorial even goes over my head. I see what you mean and I think I would just add some tiles to my level class and then make the actual traits/physics of the object in its own class? I was really hoping for a way to make an Entity Class. \$\endgroup\$ – A13X Jul 4 '12 at 2:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.