Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So 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.

EDIT: 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 your beloved graphics that you so 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.

share|improve this question
up vote 4 down vote accepted

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 :)

share|improve this answer
Actually this makes a lot more sense. Would I add the objects in my main class via Public Player player; and then obj.add()? – A13X Jul 4 '12 at 18:25
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! – 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)

share|improve this answer
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 – A13X Jun 29 '12 at 20:32
If you give me detail on the issue you're having, I can provide a more informative response. – Vaughan Hilts Jun 29 '12 at 20:51
P.S. I gave some more details above. I basically just need to know where and how to render the Player sprite. – A13X Jul 3 '12 at 17:44
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? – Vaughan Hilts Jul 3 '12 at 22:38
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. – A13X Jul 4 '12 at 2:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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