Slick2D即使它存在,也不会加载图像

时间:2014-09-04 00:46:39

标签: java artificial-intelligence lwjgl freeze slick2d

出于某种原因,我的游戏不会在关卡开始时冻结,而菜单加载就好了! 当老板音乐开始时,游戏将冻结。 编辑:对不起,我发现屏幕冻结,因为canMove变量被卡住了。但是,游戏仍会随机崩溃并说无法找到图像。

这是我的代码:

这是菜单(工作正常):

//Sprite sheeting
private SpriteSheet sleepSpriteSheet;
private Animation sleepAnimation;
private SpriteSheet walkSpriteSheet;
private Animation walkAnimation;

//System info
private String operatingSystem = Client.getOS();

//Game texuring
private static String IntroBackground = "res/images/level/IntroBackground.png";
private static String KnightQuestLogo = "res/images/icons/KnightQuestLogo.png";

//Menu Info
private static boolean readyForIntro = false;
private static boolean readyForPlay = false;
private static boolean showArrow = false;
private static String playButton = Button.PlayButton;
private static boolean playerSleeping = true;
private static boolean knightWalking = false;
private static float knightX = 145;
private static float knightY = 456;
private static float knightQuestLogoX = -750;
private static float knightQuestLogoY = 75;
private static boolean soundStarted = false;

public GameMenu(int State) {
}

// Render the game
@Override
public void render(GameContainer GameContainer, StateBasedGame SBG,
        Graphics G) throws SlickException {
    //Draw Background Image
    G.drawImage(new Image(IntroBackground), 0, 0);

    //Choose what animation to use
    if(playerSleeping) {
        sleepAnimation.draw(145, 406, 250, 250);
    }
    else {
        walkAnimation.draw(knightX, knightY, 175, 200);
    }

    //Draw Floor
    G.drawImage(new Image(Block.GroundTile), 0, 656);

    //Draw The Logo
    G.drawImage(new Image(KnightQuestLogo), knightQuestLogoX, knightQuestLogoY);

    //Draw the Play Button
    if(readyForPlay) {
        G.drawImage(new Image(playButton), 750, 315);
        if(showArrow) {
            G.drawImage(new Image(Icon.Arrow), 730, 335);
        }
    }

    //System Info
    G.drawString("Game State: " + this.getID(), 10, 30);
    G.drawString("X:" + Mouse.getX() + " Y:" + Mouse.getY(), 10, 50);
    G.drawString("Your OS: " + operatingSystem, 10, 70);
}

// Update the game
@Override
public void update(GameContainer GameContainer, StateBasedGame SBG,
        int Delta) throws SlickException {
    //Start Sound (Would be started in init(), But the StopSound function will not work if done so.
    if(!soundStarted) {
        PlaySound.playSound("res/sounds/startup/MainMenu.wav");
        soundStarted = true;
    }

    //When ready, slide the logo to the right
    if (readyForIntro) {
        if (knightQuestLogoX < 750.0) {
            knightQuestLogoX += Delta * .5f;
        }
    }

    //When ready, allow the player the press the play button
    if(readyForPlay) {
        if(Mouse.getX() > 755 && Mouse.getX() < 875 && Mouse.getY() > 345 && Mouse.getY() < 400) {
            showArrow = true;
            if(Mouse.isButtonDown(0)) {
                playerSleeping = false;
                knightWalking = true;
            }
        }
        else {
            showArrow = false;
        }
    }

    //If the knight is walking in the intro, slide him off the screen
    if(knightWalking) {
        if(knightX < 1290) {
            knightX += Delta* .5f;
        }
        else {
            SBG.enterState(1, new FadeOutTransition(new Color(Color.black)), new FadeInTransition(new Color(Color.black)));
            PlaySound.stopSound();
            PlaySound.playSound("res/sounds/events/LeaveState.wav");
        }
    }
}

// Initialize the GameState
@Override
public void init(GameContainer GameContainer, StateBasedGame SBG)
        throws SlickException {
    //Start the thread for the Intro
    new Thread(new MenuIntro()).start();

    //Create the Sprite sheets
    sleepSpriteSheet = new SpriteSheet("res/images/characters/knight/spritesheets/SleepAnimation.png", 52, 50);
    sleepAnimation = new Animation(sleepSpriteSheet, 250);
    walkSpriteSheet = new SpriteSheet("res/images/characters/knight/spritesheets/WalkAnimation.png", 35, 48);
    walkAnimation = new Animation(walkSpriteSheet, 75);

}

// Get the ID of the GameState
@Override
public int getID() {
    return 0;
}

// MenuIntro thread
static class MenuIntro implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(3000);
            readyForIntro = true;
            Thread.sleep(4000);
            readyForPlay = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是游戏等级(已破坏):

//Colors RGB
private static String Grey = "rgb/Grey.png";
private static String Blue = "rgb/Blue.png";
private static String Red = "rgb/Red.png";

// Level Variables
private static boolean stateStarted = false;
private static boolean startBossBattle = false;
private static boolean startedBossBattle = false;
private static boolean startedBossMusic = false;
private static boolean BothTouching = false;

// Player Variables
/* Coordinates, And Direction */
private static float PlayerX = 25;
private static float PlayerY = 470;
private static String Knight = "res/images/characters/knight/Knight.png";
private static String PlayerDirection = "right";
private static int PlayerHealth = 100;
private static boolean PlayerWalking = false;
private static boolean PlayerJumping = false;
private static boolean PlayerJumped = false;
private static float AmountPlayerJumped;
private static boolean PlayerAttacking = false;
private static boolean PlayerCanAttack = true;
private static int PlayerRegenTime = 100;
/* Sprite Sheets */
private static String PlayerLeftSword = "res/images/icons/sword/SwordLeft.png";
private static String PlayerLeftAttackSword = "res/images/icons/sword/SwordLeftAttacking.png";
private static String PlayerRightSword = "res/images/icons/sword/SwordRight.png";
private static String PlayerRightAttackSword = "res/images/icons/sword/SwordRightAttacking.png";
private static String PlayerCurrentSword = PlayerRightSword;
private static String RightKnight = "res/images/characters/knight/ResizedKnightRight.png";
private static String LeftKnight = "res/images/characters/knight/ResizedKnightLeft.png";
/* Everything Else */
private static boolean PlayerCanMove = false;

// AI Variables
/* Coordiantes and Direction */
private static float EnemyX = 1000;
private static float EnemyY = 470;
private static String EnemyKnight = "res/images/characters/knight/Knight.png";
private static String EnemyDirection = "right";
private static int EnemyHealth = 100;
private static boolean EnemyWalking = false;
private static boolean DecidedJump = false;
private static int JumpPossibility = 1;
private static boolean EnemyJumping = false;
private static boolean EnemyJumped = false;
private static float AmountEnemyJumped;
private static boolean EnemyAttacking = false;
private static boolean EnemyCanAttack = true;
private static int EnemyRegenTime = 100;
/* Sprite Sheets */
private static String EnemyLeftSword = "res/images/icons/sword/SwordLeft.png";
private static String EnemyLeftAttackSword = "res/images/icons/sword/SwordLeftAttacking.png";
private static String EnemyRightSword = "res/images/icons/sword/SwordRight.png";
private static String EnemyRightAttackSword = "/res/images/icons/sword/SwordRightAttacking.png";
private static String EnemyCurrentSword = EnemyRightSword;
private static String RightEnemy = "res/images/characters/knight/ResizedKnightRight.png";
private static String LeftEnemy = "res/images/characters/knight/ResizedKnightLeft.png";

/* Everything Else */
private static boolean EnemyTalking = false;
private static String EnemyMessage = "";
private static boolean EnemyCanMove = false;

// Mixed Variables
/* Sprite Sheets */
private static SpriteSheet WalkRightSpriteSheet;
private static Animation WalkRightAnimation;
private static SpriteSheet WalkLeftSprite;
private static Animation WalkLeftAnimation;
private static int SpriteSheetWidth = 135;
private static int SpriteSheetHeight = 188;



// System Variables
private String operatingSystem = Client.getOS();

// Constructor
public FinalBattle(int State) {
}

@Override
public void render(GameContainer GameContainer, StateBasedGame SBG,
        Graphics G) throws SlickException {
    //GameContainer Things
    GameContainer.setShowFPS(false);

    //Level
    /* Draw Background */
    G.drawImage(new Image("res/images/level/FinalBattle.png"), 0, 0);

    /* Draw Floor */
    G.drawImage(new Image(Block.GroundTile), 0, 656);

    //Player
    /* Draw Player */
    if(PlayerWalking) {
        if(PlayerDirection.equalsIgnoreCase("right")) {
            if(!PlayerJumping) {
                WalkRightAnimation.draw(PlayerX, PlayerY, SpriteSheetWidth, SpriteSheetHeight);
            }
            else {
                G.drawImage(new Image(RightKnight), PlayerX, PlayerY);
            }
            G.drawImage(new Image(PlayerCurrentSword), PlayerX + 60, PlayerY + 90);
        }
        if(PlayerDirection.equalsIgnoreCase("left")) {
            if(!PlayerJumping) {
                WalkLeftAnimation.draw(PlayerX, PlayerY, SpriteSheetWidth, SpriteSheetHeight);
            }
            else {
                G.drawImage(new Image(LeftKnight), PlayerX, PlayerY);
            }
            G.drawImage(new Image(PlayerCurrentSword), PlayerX + 6, PlayerY + 90);
        }
    }

    /* Draw his/her sword */
    else {
        if(PlayerDirection.equalsIgnoreCase("right")) {
            G.drawImage(new Image(RightKnight), PlayerX, PlayerY);
            G.drawImage(new Image(PlayerCurrentSword), PlayerX + 60, PlayerY + 90);
        }
        if(PlayerDirection.equalsIgnoreCase("left")) {
            G.drawImage(new Image(LeftKnight), PlayerX, PlayerY);
            G.drawImage(new Image(PlayerCurrentSword), PlayerX + 6, PlayerY + 90);
        }
    }

    /* Draw his/her power level */
    G.setColor(new Color(Color.blue.darker()));
    G.drawString("Knight Power Level", 10, 20);
    G.setColor(new Color(Color.white));
    G.drawImage(new Image(Grey).getScaledCopy(100, 10), 10, 40);
    G.drawImage(new Image(Blue).getScaledCopy(PlayerRegenTime, 10), 10, 40);

    /* Draw his/her health level */
    G.setColor(new Color(Color.red.darker()));
    G.drawString("Knight Health", 10, 60);
    G.setColor(new Color(Color.white));
    G.drawImage(new Image(Grey).getScaledCopy(100, 10), 10, 80);
    G.drawImage(new Image(Red).getScaledCopy(PlayerHealth, 10), 10, 80);

    /* Draw his/her name */
    G.setColor(new Color(Color.red.darker()));
    G.drawString("You\n |\n\\ /", PlayerX + 50, PlayerY - 70);
    G.setColor(new Color(Color.white));


    //Enemy 
    if(EnemyWalking) {
        if(EnemyDirection.equalsIgnoreCase("right")) {
            if(!EnemyJumping) {
                WalkRightAnimation.draw(EnemyX, EnemyY, SpriteSheetWidth, SpriteSheetHeight);
            }
            else {
                G.drawImage(new Image(RightKnight), EnemyX, EnemyY);
            }
            G.drawImage(new Image(EnemyCurrentSword), EnemyX + 60, EnemyY + 90);
        }
        if(EnemyDirection.equalsIgnoreCase("left")) {
            if(!EnemyJumping) {
                WalkLeftAnimation.draw(EnemyX, EnemyY, SpriteSheetWidth, SpriteSheetHeight);
            }
            else {
                G.drawImage(new Image(LeftKnight), EnemyX, EnemyY);
            }
            G.drawImage(new Image(EnemyCurrentSword), EnemyX + 6, EnemyY + 90);
        }
    }

    /* Draw enemy sword */
    else {
        if(EnemyDirection.equalsIgnoreCase("right")) {
            if(!EnemyAttacking) {
                EnemyCurrentSword = EnemyRightSword;
            }
            G.drawImage(new Image(RightKnight), EnemyX, EnemyY);
            G.drawImage(new Image(EnemyCurrentSword), EnemyX + 60, EnemyY + 90);
        }
        if(EnemyDirection.equalsIgnoreCase("left")) {
            if(!EnemyAttacking) {
                EnemyCurrentSword = EnemyLeftSword;
            }
            G.drawImage(new Image(LeftKnight), EnemyX, EnemyY);
            G.drawImage(new Image(EnemyCurrentSword), EnemyX + 6, EnemyY + 90);
        }
    }

    /* Draw enemy power level */
    G.setColor(new Color(Color.blue.darker()));
    G.drawString("Enemy Power Level", 1115, 20);
    G.setColor(new Color(Color.white));
    G.drawImage(new Image(Grey).getScaledCopy(100, 10), 1165, 40);
    G.drawImage(new Image(Blue).getScaledCopy(EnemyRegenTime, 10), 1165, 40);

    /* Draw enemy health level */
    G.setColor(new Color(Color.red.darker()));
    G.drawString("Enemy Health", 1115, 60);
    G.setColor(new Color(Color.white));
    G.drawImage(new Image(Grey).getScaledCopy(100, 10), 1165, 80);
    G.drawImage(new Image(Red).getScaledCopy(EnemyHealth, 10), 1165, 80);

    // System Info (Not used currently)
    /*G.drawString("Game State: " + this.getID(), 10, 30);
    G.drawString("X:" + Mouse.getX() + " Y:" + Mouse.getY(), 10, 50);
    G.drawString("Your OS: " + operatingSystem, 10, 70);
    G.drawString("Player X: " + PlayerY + " Enemy X:" + EnemyX, 10, 170);*/
}

@Override
public void update(GameContainer GameContainer, StateBasedGame SBG,
        int Delta) throws SlickException {

    // Fixes for some stuff that might happen
    if(PlayerY > 470) {
        PlayerY = 470;
    }
    if(EnemyY > 470) {
        EnemyY = 470;
    }

    // Enemy
    if(!EnemyTalking) {
        new Thread(new startFight()).start();
        EnemyTalking = true;
    }
    /* Boss Battle Music */
    if(startBossBattle && !startedBossBattle) {
        if(!startedBossMusic) {
            PlaySound.playSound("res/sounds/events/BossBattleLoop.wav");
            PlaySound.startLoop(1000);
            startedBossMusic = true;
        }
        startedBossBattle = true;
    }

    // Player
    /* Player Movement */
    if(PlayerCanMove) {
        Input playerInput = GameContainer.getInput();
        if(playerInput.isKeyDown(Input.KEY_A)) {
            PlayerX -= Delta*.3f;
            PlayerWalking = true;
            PlayerDirection = "left";
            if(!PlayerAttacking) {
                PlayerCurrentSword = PlayerLeftSword;
            }
        }
        else if(playerInput.isKeyDown(Input.KEY_D)) {
            PlayerX += Delta*.3f;
            PlayerWalking = true;
            PlayerDirection = "right";
            if(!PlayerAttacking) {
                PlayerCurrentSword = PlayerRightSword;
            }
        }
        else {
            PlayerWalking = false;
        }

        /* Player Jumping */
        if(playerInput.isKeyPressed(Input.KEY_SPACE)) {
            if(!PlayerJumped) {
                new Thread(new playerJump()).start();
            }
        }
        if(PlayerJumping) {
            PlayerY -= Delta*.5f;
            AmountPlayerJumped = PlayerY;
        }
        else {
            if(PlayerY < 470) {
                PlayerY += Delta*.5f;
            }
            if(Math.round(PlayerY) == 470 || Math.round(PlayerY) == 471) {
                PlayerJumped = false;
            }
        }

        /* Player Attack */
        if(playerInput.isKeyPressed(Input.KEY_ENTER)) {
            if(PlayerCanAttack) {
                new Thread(new playerAttack()).start();
            }
            else {
            }
        }
    }

    /* Player Collision Detection */
    if(PlayerX < 0) {
        PlayerX = 0;
    }
    if (PlayerX > 1155) {
        PlayerX = 1155;
    }

    //Enemy
    /* Enemy Movement and Jumping */
    if(EnemyCanMove) {
            if(PlayerX < EnemyX) {
                if(PlayerDirection.equalsIgnoreCase("right")) {
                    if(!(EnemyX - 125 < PlayerX)) {
                        EnemyX -= Delta*.2f;
                        EnemyDirection = "right";
                        EnemyWalking = true;
                        BothTouching = false;
                        if(!(EnemyX - 50 < PlayerX)) {
                            if(!DecidedJump) {
                                int Jump = GenerateNumber.generateNumber(JumpPossibility);
                                if(Jump == JumpPossibility) {
                                    new Thread(new enemyJump()).start();
                                }
                                DecidedJump = true;
                            }
                        }
                        else {
                            DecidedJump = false;
                        }
                    }
                    else {
                        EnemyWalking = false;
                        BothTouching = true;
                        if(EnemyRegenTime == 100) {
                            if(EnemyCanAttack) {
                                new Thread(new enemyAttack()).start();
                            }
                        }
                    }
                }
                if(PlayerDirection.equalsIgnoreCase("left")) {
                    if(!(EnemyX - 110 < PlayerX)) {
                        EnemyX -= Delta*.2f;
                        EnemyDirection = "left";
                        EnemyWalking = true;
                        BothTouching = false;
                        if(!(EnemyX + 50 < PlayerX)) {
                            if(!DecidedJump) {
                                int Jump = GenerateNumber.generateNumber(JumpPossibility);
                                if(Jump == JumpPossibility) {
                                    new Thread(new enemyJump()).start();
                                }
                                DecidedJump = true;
                            }
                        }
                        else {
                            DecidedJump = false;
                        }
                    }
                    else {
                        EnemyWalking = false;
                        BothTouching = true;
                        if(EnemyRegenTime == 100) {
                            if(EnemyRegenTime == 100) {
                                if(EnemyCanAttack) {
                                    new Thread(new enemyAttack()).start();
                                }
                            }
                        }
                    }
                }
                EnemyDirection = "left";
            }
            if(PlayerX > EnemyX) {
                if(PlayerDirection.equalsIgnoreCase("right")) {
                    if(!(EnemyX + 119 > PlayerX)) {
                        EnemyX += Delta*.2f;
                        EnemyDirection = "right";
                        EnemyWalking = true;
                        BothTouching = false;
                        if(!(EnemyX - 50 < PlayerX)) {
                            if(!DecidedJump) {
                                int Jump = GenerateNumber.generateNumber(JumpPossibility);
                                if(Jump == JumpPossibility) {
                                    new Thread(new enemyJump()).start();
                                }
                                DecidedJump = true;
                            }
                        }
                        else {
                            DecidedJump = false;
                        }
                    }
                    else {
                        EnemyWalking = false;
                        BothTouching = true;
                        if(EnemyRegenTime == 100) {
                            if(EnemyRegenTime == 100) {
                                if(EnemyCanAttack) {
                                    new Thread(new enemyAttack()).start();
                                }
                            }
                        }
                    }
                }
                if(PlayerDirection.equalsIgnoreCase("left")) {
                    if(!(EnemyX + 135 > PlayerX)) {
                        EnemyX += Delta*.2f;
                        EnemyDirection = "left";
                        EnemyWalking = true;
                        BothTouching = false;
                        if(!(EnemyX + 50 < PlayerX)) {
                            if(!DecidedJump) {
                                int Jump = GenerateNumber.generateNumber(JumpPossibility);
                                if(Jump == JumpPossibility) {
                                    new Thread(new enemyJump()).start();
                                }
                                DecidedJump = true;
                            }
                        }
                        else {
                            DecidedJump = false;
                        }
                    }
                    else {
                        EnemyWalking = false;
                        BothTouching = true;
                        if(EnemyRegenTime == 100) {
                            if(EnemyRegenTime == 100) {
                                if(EnemyCanAttack) {
                                    new Thread(new enemyAttack()).start();
                                }
                            }
                        }
                    }
                }
                EnemyDirection = "right";
            }
            /* Enemy Jumping */
            if(EnemyJumping) {
                EnemyY -= Delta*.5f;
                AmountEnemyJumped = EnemyY;
            }
            else {
                if(EnemyY < 470) {
                    EnemyY += Delta*.5f;
                }
                if(Math.round(EnemyY) == 470 || Math.round(EnemyY) == 471) {
                    EnemyJumped = false;
                }
            }
    }

}

// Initializing Constructor
@Override
public void init(GameContainer GameContainer, StateBasedGame SBG)
        throws SlickException {
    WalkRightSpriteSheet = new SpriteSheet("res/images/characters/knight/spritesheets/WalkAnimationRight.png", 35, 48);
    WalkRightAnimation = new Animation(WalkRightSpriteSheet, 100);
    WalkLeftSprite = new SpriteSheet("res/images/characters/knight/spritesheets/WalkAnimationLeft.png", 35, 48);
    WalkLeftAnimation = new Animation(WalkLeftSprite, 100);
}

// Gets the ID of the current State
@Override
public int getID() {
    return 1;
}

// Start Fight
static class startFight implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
            startBossBattle = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

// Player Threads
/* Player Attack */
static class playerAttack implements Runnable {

    @Override
    public void run() {
        try {
            PlayerAttacking = true;
            if(PlayerDirection.equalsIgnoreCase("right")) {
                PlayerCurrentSword = PlayerRightAttackSword;
            }
            if(PlayerDirection.equalsIgnoreCase("left")) {
                PlayerCurrentSword = PlayerLeftAttackSword;
            }
            if(BothTouching) {
                EnemyHealth -= 10;
            }
            Thread.sleep(100);
            if(PlayerDirection.equalsIgnoreCase("right")) {
                PlayerCurrentSword = PlayerRightSword;
            }
            if(PlayerDirection.equalsIgnoreCase("left")) {
                PlayerCurrentSword = PlayerLeftSword;
            }
            PlayerAttacking = false;
            PlayerCanAttack = false;
            PlayerRegenTime = 0;
            while(PlayerRegenTime != 100) {
                Thread.sleep(100);
                PlayerRegenTime++;
            }
            PlayerCanAttack = true;
        }
        catch (Exception e) {

        }
    }

}

/* Player Jump */
static class playerJump implements Runnable {

    @Override
    public void run() {
        try {
            PlayerJumped = true;
            PlayerJumping = true;
            Thread.sleep(750);
            PlayerJumping = false;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

// Enemy Threads
/* Enemy Attack */
static class enemyAttack implements Runnable {

    @Override
    public void run() {
        try {
                EnemyAttacking = true;
                if(EnemyDirection.equalsIgnoreCase("right")) {
                    EnemyCurrentSword = EnemyRightAttackSword;
                }
                if(EnemyDirection.equalsIgnoreCase("left")) {
                    EnemyCurrentSword = EnemyLeftAttackSword;
                }
                PlayerHealth -= 10;
                Thread.sleep(100);
                if(EnemyDirection.equalsIgnoreCase("right")) {
                    EnemyCurrentSword = EnemyRightSword;
                }
                if(EnemyDirection.equalsIgnoreCase("left")) {
                    EnemyCurrentSword = EnemyLeftSword;
                }
                EnemyAttacking = false;
                EnemyCanAttack = false;
                EnemyRegenTime = 0;
                while(EnemyRegenTime != 100) {
                    Thread.sleep(100);
                    EnemyRegenTime++;
                }
                EnemyCanAttack = true;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}

/* Decide Jump */
static class decideJump implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(1000 * 10);
            int number = GenerateNumber.generateNumber(20);
            if(number == 20) {
                if(!EnemyJumping) {
                    new Thread(new enemyJump()).start();
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}

/* Enemy Jump */
static class enemyJump implements Runnable {

    @Override
    public void run() {
        try {
            EnemyJumped = true;
            EnemyJumping = true;
            Thread.sleep(750);
            EnemyJumping = false;
        }
        catch (Exception e) {
        }
    }

}

1 个答案:

答案 0 :(得分:0)

事实证明,我没有将它添加到构建路径中。如果您正在查看此问题,请确保将其添加到Eclipse(或netbeans)中的构建路径中。