简体   繁体   中英

How to use Thread.sleep in a basic RPG game

I've been working on this really basic Java RPG game, and when I tried using Thread.sleep for my monster to move slower, it seems to affect the player as well, in other words, the movement of the player had slow down when the monster appears, but turns back to normal when I move to another part of the map where the monster is not there

Here's the code for the monster's movement

int wolfRandNum;

    if (isKilled() == false){

        wolfRandNum = (int) (Math.random()*4);

        if (wolfRandNum == 1){ // up
            System.out.println("up");

            if (playerRow -1 <= 0){
                setCurrentEnemySprite(3);

            } else {
                moveUp();
                playerRow--;
            }
        } else if (wolfRandNum == 2){ // down
            System.out.println("down");

            if (playerRow +1 >= 8){
                setCurrentEnemySprite(0);

            } else {
                moveDown();
                playerRow++;

            }
        } else if (wolfRandNum == 3){ // left
            System.out.println("left");

            if (playerColumn -1 <= 0){
                setCurrentEnemySprite(1);

            } else {
                moveLeft();
                playerColumn--;
            }
        } else{ // right
            System.out.println("right");
            if (playerColumn + 1 >= 9){
                setCurrentEnemySprite(2);
            } else {
                moveRight();
                playerColumn++;

            }
        }

    }

The code above is in a method called update, which is from an enemy class.

Here's the where I call the method:

public void paintComponent(Graphics g) { 
    super.paintComponent(g); //required to ensure the panel is correctly redrawn

    map.draw(g);
    player.draw(g);

    for (Character character : characterList) {
        character.draw(g);
    }

    for (Enemy enemy : enemyList){
        enemy.draw(g);

        try {
            enemy.update();
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    for (Item item : bagList) {
        item.draw(g);
    }

    repaint();
} 

Is there anyway to use thread.sleep without affecting the player's movement? If not, what other methods can I use to make the monster move slower but not the player?

I'm not really sure how to fix this problem, any advise would help a lot! Thank you :)

This is a event-driven Swing GUI, and so the answer to the question:

How to use Thread.sleep(...)

is that you don't .

You're coding this as if it were a linear console program, and this design won't work for event-driven non-linear programs. Calling Thread.sleep on the Swing event thread and especially in any painting method is a guarantee to put your entire GUI to sleep so that it becomes totally unresponsive. The proper solution is to create a non-GUI Model class to go with your GUI (your "View"), and to change how the view responds based on the state of the Model, ie, the state of its key fields.

Also, if your game is being run in "real time", then you will need a game loop to drive your game. This can be done easily via a Swing Timer, although it does not provide absolute precision. If greater precision is required, then use the Swing Timer, but don't assume that each time slice is accurate, and instead measure each delta time and base your physics on the measured slice, not the assumed slice. Other ways to create a game loop include use of a background thread with a while loop and Thread.sleep within this thread.

All you are slowing down is the rendering (drawing) of your monster. As all drawing is done on the UI thread, this will, as you have rightly noticed, have a huge performance impact on your entire application. As a general rule, you should never call a blocking operation such as sleep in the UI thread.

Given you only have two characters, what I would consider trying is creating two threads, one for your character and one for your monster. You will do what you need to do in each of these threads, and then call repaint() when you need to redraw. If you want to slow down the monster, for example, you can call sleep in the monster thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM