Redrawing the Screen

In my game, i have a class called ‘Inputs’ which contains keylisteners. If a movement key is pressed, this class adjusts the players x,y accordingly.
The actual drawing is done in a class called ‘MapRenderer’ which is responsible for drawing the map and all the sprites (including the player) in their initial positions at the start.
Then the main method of my ‘World’ class is what setups the screen to fullscreen, loads the map, and calls the draw method of the MapRenderer class to get it all going.

My question is, how/where should i redraw the screen after every movement (i.e. everytime a keylistener updates a sprites position)?
Thanks.

well… this may work if the screen really only changes upon a key event, but if there’s anything else on the map moving (like non-static objects, enemies or whatever), you should put the draw()s and show() at the end of your gamelogic loop.

the keyevents usually only set variables which tell the game logic loop what to do next tick

Im trying to get the structure of it all right, and thats where im getting stuck.
I put a gameLoop() method into my ‘World’ class and i was planning on using that to keep redrawing the screen everytime something on the screen changes. Im just not sure how to do it.
Ive tried using a paint() method in my main ‘World’ class which draws the player (which is a sprite object of my Sprite class). Then my gameLoop method is calling repaint(), but this doesnt work. The player sprite doesnt move from his initial position, and it doesnt look like the screen is being redrawn.

Can someone clear this up for me? Its more a program structure problem than anything else. (Im using a tileMap by the way)
Thanks.

make a method run() in your main class (I guess it’s world) like this (assuming player is the player object and b the bufferstrategy. if you’re not yet using one, read about it :P):


public void run(){
  while(true){

     // game logic, this should be extended :P
     if(moveright)
       player.x += 10;
     if(movelegt)
       player.x -= 10;
     if(moveup)
       player.y += 10;
     if(movedown)
       player.y -= 10;

     // fetch the graphics object from the buffer strategy
     g = (Graphics2D) b.getDrawGraphics();

    // draw map
    drawmap(g);

    // call the player's draw method, which drawImage()s itself onto the graphics object
    player.draw(g);

    // release graphics object
    g.dispose();

    // you may want to add a Thread.wait(); loop here for a constant framerate

    // show graphics buffer
    b.show();
}

the keyinput functions should look something like this:


public void keyPressed(KeyEvent ke) {
        switch(ke.getKeyCode()){
            case KeyEvent.VK_LEFT:
                 game.moveleft = true;
                 break;
            case KeyEvent.VK_RIGHT:
                 game.moveright = true;
                 break;
            case KeyEvent.VK_UP:
                 game.moveup = true;
                 break;
            case KeyEvent.VK_DOWN:
                 game.movedown = true;
                 break;
        }
}


and the keyReleased method accordingly with blah = false. that way, the char moves at a constant speed, and not as fast as the player can hit the keys :stuck_out_tongue:

now call new Thread(worldobject).start(); to start the game thread

Thanks for the code, ill see what i can do with it later and let u know how i got on.

Ive tried that code and i get a NullPointerException at

// show graphics buffer
strategy.show();

what exactly is this line supposed to do? At the moment im only drawing the map and sprites once, at the start. I want to redraw the screen everytime he moves, BUT not by redrawing the entire tilemap and everything again in some kind of constant loop.

In openGL (JOGL) theres a display method that does the drawing and then and animator automatically redraws it all everytime it need to. So how can i do this withough OpenGL? I dont want to use JOGL cause it means i have to redo all my code for drawing the tilemap, and for creating a fullscreen window. I dont know how to do this is JOGL so im trying to do it using basic java2d code.
Anyone able to help me do this?

not drawing in a constant loop is generally a bad idea, but possible :stuck_out_tongue:

strategy.show() tells the BufferStrategy to flush the buffer to screen (read about BufferStrategy :wink: )

so, will the only non-static object in your game be the player?? what kind of game is that supposed to be? o_O

Its a top-down view, multiplayer (LAN) war game (Axis Vs Allies). So the only non-static objects will be players and bullets. At the moment, my keylisteners are calling the draw method so that everytime a player moves, it redraws the screen. Is there a better way to do this or will this be ok? Could u show me a sample game loop that is redrawing, if thats a better way to do it.
Thanks.

check the code 3 posts above :stuck_out_tongue: