javaarrays2d-gamespacman

Java-Tile Map Image Flickering-What can i do to stop this?


I am making a pacman game and i have a MAP in an array, i assign the different values in the map to draw different images, in order to create a pacman map... but It keeps flickering all over the place

Any help with identifying the problem would be wonderful

I realize that this is a lot of reading, but i have looked at it for hours and not figured it out.... I am new to programming so I think that more experienced eyes could really benefit me

Draw Map

public void drawMap() {
    BufferStrategy bs = this.getBufferStrategy();
    do {
        do {
            Graphics g = null;
            g = getGraphics();
            try {
                for (int x = 0; x < graphics.map.width; x++) {
                    for (int y = 0; y < graphics.map.height; y++) {
                        tile = graphics.map.level1[y][x];
                        //System.out.println(y + ", " + x);
                        int tileX = x * 21; //(int) (x * 21.42857142857143);
                        int tileY = y * 26; //(int) (y * 25.80645161290323);
                        if (tile == 0) {
                            g.drawImage(wall, tileX, tileY, 21, 26, null);
                            //System.out.println(x + ", " + y);
                        }
                        if (tile == 1) {
                            //g.drawImage(space,tileX, tileY, (int)21.42857142857143, (int) 25.80645161290323,null);
                        }

                    }
                }
            } finally {
                g.dispose();
            }
        } while (bs.contentsRestored());
        bs.show();
    } while (bs.contentsLost());

}
    

Render

public void render() {
    BufferStrategy bs = this.getBufferStrategy();
    do {
        do {
            drawn = false;

            Graphics g = null;


            try {
                g = bs.getDrawGraphics();

                if (MAIN_MENU == false && GAME == true) {
                    if (level1test) {
                        if (!drawn) {
                            drawMap();
                            drawn = true;
                        }

                        //g.drawImage(level1,0,0,getWidth(),getHeight(),null);
                    }

                    if (level2test == true) {
                        g.drawImage(level2, 0, 0, getWidth(), getHeight(), null);
                    }

                    g.drawImage(point, playerX, playerY, playerHeight, playerWidth, null);
                } else if (MAIN_MENU == false && GAME == false) {
                    g.drawImage(settingsBackground, 0, 0, getWidth(), getHeight(), null);
                } else {
                    g.drawImage(background, 0, 0, getWidth(), getHeight(), null);
                }
            } finally {
                g.dispose();
            }
        } while (bs.contentsRestored());
        bs.show();
    } while (bs.contentsLost());
}
            

Run and Timer

public void run() 
{
    long lastTime = System.nanoTime();
    double nsPerTick = 1000000000 / 60D;
    long lastTimer = System.currentTimeMillis();
    double delta = 0;
    
    int frames = 0;
    int ticks = 0;
    
    while (running == true) {
        long now = System.nanoTime();
        delta += (now - lastTime) / nsPerTick;
        lastTime = now;
        boolean render = false;
        
        while (delta >= 1) {
            ticks++;
            tick();
            delta -= 1;
            render = true;
            
        }
        
            try {
                Thread.sleep(3);        //keep the Frames from going to high
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
        if(render == true){
        frames++;
        render();
        }
        
        if (System.currentTimeMillis() - lastTimer >= 1000) {
            lastTimer +=1000;
            //System.out.println("Frames: " + frames + "   Ticks: " + ticks); 
            
            frames = 0;
            ticks = 0;
        }
    }
}

Map Array

public static int width = 28;
public static int height = 31;

public static int[][] level1 = {
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
    {0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,},
    {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,},
    {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,},
    {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,},
    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,},
    {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,},
    {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,},
    {0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0,},
    {0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,},
    {1,1,1,1,1,0,1,0,0,0,0,0,1,0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,0,0,0,0,0,0,0,0,0,0,1,0,},
    {0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,},
    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,},
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}
};

Solution

  • You need double buffer, the problem is that java paints the canvas with the background color and then put the grahpics instructions, so, if you need to change the image you can do it in a "hidden image" and when it's ready print it in the canvas area replacing the image with the new rendered image. More info here Double Buffer in java. EDIT: Almost forgot, is really easy to do it, here is an example double buffer example