Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当使用For循环在屏幕上绘制ImageArray时,保持在屏幕上绘制/make.jar时,它不会执行_Java_Slick2d - Fatal编程技术网

Java 当使用For循环在屏幕上绘制ImageArray时,保持在屏幕上绘制/make.jar时,它不会执行

Java 当使用For循环在屏幕上绘制ImageArray时,保持在屏幕上绘制/make.jar时,它不会执行,java,slick2d,Java,Slick2d,我用Java和Slick制作了这个2D游戏。问题似乎出现在以下代码中: public void initRedAndBlueBall() throws SlickException { // Makes sure the red and the blue ball aren't too close for(int i = 0; i <= amountOfEatenBalls; i++) {

我用Java和Slick制作了这个2D游戏。问题似乎出现在以下代码中:

public void initRedAndBlueBall() throws SlickException
    {           
       //   Makes sure the red and the blue ball aren't too close
        for(int i = 0; i <= amountOfEatenBalls; i++)
        {
            // Get random cordinates for the red and blue ball.
            randomRedX[i] = random.nextInt(550) + 24;
            randomRedY[i] = random.nextInt(350) + 24;
            randomBlueX = random.nextInt(550) + 24;
            randomBlueY = random.nextInt(350) + 24;

    // Initialize the new Redball with a picture.
    redball[i] = new Image("res/RedBall.png");
    // Draws the red ball on the screen. 
    drawRedBall(randomRedX[i], randomRedY[i], redball[i]);

    if(isInTheRangeOf(randomRedX[i], randomBlueX, 30) == false)
    {
        System.out.println("The red and the blue ball have good positions");
            break;
            }
            else if(isInTheRangeOf(randomRedX[i], randomBlueX, 30) == true)
            {
                System.out.println("The red and the blue ball are to close!");
                System.out.println("Repositioning the red and blue ball");
            }  
        }
    }
Menu.java

 package javagame;

    import org.lwjgl.input.Mouse;
    import org.newdawn.slick.*;
    import org.newdawn.slick.state.*;
    import org.newdawn.slick.MouseListener;

    public class Menu extends BasicGameState
    {
        Image PlayNow, PlayNowHover, ExitGame, ExitGameHover, PlayButton, ExitGameButton;
        int MousePosX;
        int MousePosY;

       public Menu(int state)  {}

       public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
       {
           // Billederne er 125x26
           PlayNow = new Image("res/PlayNow.png");
           PlayNowHover = new Image("res/PlayNowHover.png");
           ExitGame = new Image("res/ExitGame.png");
           ExitGameHover = new Image("res/ExitGameHover.png");

           PlayButton = PlayNow;
           ExitGameButton = ExitGame;
       }

       public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
       {
           // Framet er 600x400
           g.drawString(MousePosX + ", " + MousePosY, 500, 10);
           g.drawLine(0, 26, 600, 26);

           g.drawImage(PlayButton, 200, 100);
           g.drawImage(ExitGameButton, 200, 150);

           g.drawString("You move with the arrowkeys.", 200, 200);
           g.drawString("There's 1 rule:", 200, 215);
           g.drawString("Don't eat the red balls!", 200, 230);
           g.drawString("But you can enjoy the blue ones :- P", 200, 245);
       }

       public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
       {
              Input input = gc.getInput();
              MousePosX = input.getMouseX();
              MousePosY = input.getMouseY();

              // PlayNow Hover or Normal
              if(MousePosX > 200 && MousePosX < 325     &&      MousePosY > 100 && MousePosY < 126)
              {
                  PlayButton = PlayNowHover;
              }
              else
              {
                  PlayButton = PlayNow;
              }
              //ExitGame Hover or Normal
              if(MousePosX > 200 && MousePosX < 325     &&      MousePosY > 150 && MousePosY < 176)
              {
                  ExitGameButton = ExitGameHover;
              }
              else
              {
                  ExitGameButton = ExitGame;
              }

              // Now the buttons have a function
              if(MousePosX > 200 && MousePosX < 325     &&      MousePosY > 100 && MousePosY < 126)
              {
                  if(Mouse.isButtonDown(0))
                  {
                      sbg.enterState(1);
                  }
              }
              if(MousePosX > 200 && MousePosX < 325     &&      MousePosY > 150 && MousePosY < 176)
              {
                  if(Mouse.isButtonDown(0))
                  {
                      System.exit(0);
                  }
              }


       } 

       public int getID()
       {
          return 0;
       }
    }
包javagame;
导入org.lwjgl.input.Mouse;
导入org.newdawn.slick.*;
导入org.newdawn.slick.state.*;
导入org.newdawn.slick.MouseListener;
公共类菜单扩展了BasicGameState
{
图像PlayNow、PlayNowHover、ExitName、ExitGameHover、PlayButton、ExitGameButton;
int MousePosX;
鼠标定位;
公共菜单(int状态){}
public void init(GameContainer gc,StateBasedGame sbg)引发异常
{
//Billederne er 125x26
PlayNow=新图像(“res/PlayNow.png”);
PlayNowHover=新图像(“res/PlayNowHover.png”);
ExitGame=新图像(“res/ExitGame.png”);
ExitGameHover=新图像(“res/ExitGameHover.png”);
PlayButton=PlayNow;
ExitGame按钮=ExitGame;
}
公共void呈现(GameContainer gc、StateBasedGame sbg、Graphics g)引发异常
{
//框架er 600x400
g、 拉丝(MousePosX+”,“+MousePosY,500,10);
g、 抽绳(0,26,600,26);
g、 drawImage(播放按钮,200100);
g、 drawImage(ExitGameButton,200150);
g、 抽绳(“用箭头键移动。”,200,200);
g、 抽绳(“有一条规则:”,200,215);
g、 抽绳(“不要吃红色的球!”,200230);
g、 抽绳(“但你可以欣赏蓝色的:-P”,200245);
}
公共无效更新(GameContainer gc、StateBasedGame sbg、int delta)引发异常
{
Input=gc.getInput();
MousePosX=input.getMouseX();
MousePosY=input.getMouseY();
//现在播放悬停或正常
if(MousePosX>200&&MousePosX<325&&MousePosY>100&&MousePosY<126)
{
PlayButton=PlayNowHover;
}
其他的
{
PlayButton=PlayNow;
}
//退出暂停或正常
if(MousePosX>200&&MousePosX<325&&MousePosY>150&&MousePosY<176)
{
ExitGameButton=ExitGameHover;
}
其他的
{
ExitGame按钮=ExitGame;
}
//现在按钮有了功能
if(MousePosX>200&&MousePosX<325&&MousePosY>100&&MousePosY<126)
{
if(鼠标.isButtonDown(0))
{
sbg.企业(1);
}
}
if(MousePosX>200&&MousePosX<325&&MousePosY>150&&MousePosY<176)
{
if(鼠标.isButtonDown(0))
{
系统出口(0);
}
}
} 
公共int getID()
{
返回0;
}
}
Play.java

    package javagame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

import java.util.Random;

public class Play extends BasicGameState
{
    Image ball, blueball;
    Image[] redball;
    Animation roll;   

    int[] duration = {200, 200};
    float defBallPosX = 300; // DEFAULT
    float defBallPosY = 200; // DEFAULT
    float ballPosX = 300; 
    float ballPosY = 200; 
    int MousePosX;
    int MousePosY;
    Random random;
    float[] randomRedX; // X for the RedBall.png
    float[] randomRedY; // Y for the RedBall.png
    float randomBlueX; // X for the BlueBall.png
    float randomBlueY; // Y for the BlueBall.png

    static int amountOfEatenBalls;

    boolean blueAndRedAreNotToClose;

    public Play(int state)
    {

    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
    {
        ball = new Image("res/PurpBallNorm.png"); // 25x25 px
        Image[] images = {ball, ball};

        redball = new Image[50];
        blueball = new Image("res/BlueBall.png");

        roll = new Animation(images, duration, false);

        random = new Random();

        randomRedX = new float[50];
        randomRedY = new float[50];

        amountOfEatenBalls = 0;
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
    {
       //   The Window is 600x400
        g.drawString(MousePosX + ", " + MousePosY, 500, 10);

        g.drawLine(0, 26, Game.windowX, 26); // Ceiling
        g.drawLine(1, 26, 1, Game.windowY); // Left Wall
        g.drawLine(Game.windowX - 2, 26, Game.windowX - 2, Game.windowY - 2); // Right Wall
        g.drawLine(1, Game.windowY - 2, Game.windowX - 2, Game.windowY - 2); // Floor

        roll.draw(ballPosX, ballPosY);

        initRedAndBlueBall(); 
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
    {
            Input input = gc.getInput();
            MousePosX = input.getMouseX();
            MousePosY = input.getMouseY();

            if(input.isKeyDown(Input.KEY_UP))
                ballPosY -= delta * .1f;
            else if(input.isKeyDown(Input.KEY_DOWN))
                ballPosY += delta * .1f;
            else if(input.isKeyDown(Input.KEY_LEFT))
                ballPosX -= delta * .1f;
            else if(input.isKeyDown(Input.KEY_RIGHT))
                ballPosX += delta * .1f;

            blueBallTouch(); 

            for(int i = 0; i <= amountOfEatenBalls; i++)
            {
                if(isInTheRangeOf(ballPosX, randomRedX[i], 25) == true && isInTheRangeOf(ballPosY, randomRedY[i], 25) == true)
                {
                    System.out.println("Player has eaten a red ball!");
                    sbg.enterState(2);
                }
            }

//          Makes sure the ball can't run out of the window
            if(ballPosX <= 2)
            {
                ballPosX += delta * .1f;
            }
            else if(ballPosX >= Game.windowX - 27)
            {
                ballPosX -= delta * .1f;
            }
            else if(ballPosY <= 26)
            {
                ballPosY += delta * .1f;
            }
            else if(ballPosY >= Game.windowY - 27)
            {
                ballPosY -= delta * .1f;
            }
    } 

    public int getID() 
    {
        return 1;
    }

    public boolean isInTheRangeOf(float x, float y, float range)
    {
            float a = 0;
            float b = 0;

            if(x > y)
            {
                a = x;
                b = y;
            }
            else if(y > x)
            {
                b = x;
                a = y;
            }   

            if((a - b) <= range)
            {
                return true;
            }
            else if((a - b) >= range)
            {
                return false;
            }
            return true;
    }

    public void initRedAndBlueBall() throws SlickException
    {           
       //   Makes sure the red and the blue ball aren't too close
        for(int i = 0; i <= amountOfEatenBalls; i++)
        {
            // Get random cordinates for the red and blue ball.
            randomRedX[i] = random.nextInt(550) + 24;
            randomRedY[i] = random.nextInt(350) + 24;
            randomBlueX = random.nextInt(550) + 24;
            randomBlueY = random.nextInt(350) + 24;

            // Initialize the new Redball with a picture.
            redball[i] = new Image("res/RedBall.png");
            // Draws the red ball on the screen. 
            drawRedBall(randomRedX[i], randomRedY[i], redball[i]);

            if(isInTheRangeOf(randomRedX[i], randomBlueX, 30) == false)
            {
                System.out.println("The red and the blue ball have good positions");
                break;
            }
            else if(isInTheRangeOf(randomRedX[i], randomBlueX, 30) == true)
            {
                System.out.println("The red and the blue ball are to close!");
                System.out.println("Repositioning the red and blue ball");
            }  
        }
    }

    public void drawRedBall(float x, float y, Image image)
    {
        Graphics g = new Graphics();
        g.drawImage(image, x, y);
    }

    public void blueBallTouch() throws SlickException
    {
        if(isInTheRangeOf(ballPosX, randomBlueX, 25) == true && isInTheRangeOf(ballPosY, randomBlueY, 25) == true)
        {
            amountOfEatenBalls++;

            initRedAndBlueBall();
        }
        else
        {

        }
   }

   public static int getAmountOfEatenBalls()
   {
       return amountOfEatenBalls;
   }

}
包javagame;
导入org.newdawn.slick.*;
导入org.newdawn.slick.state.*;
导入java.util.Random;
公共类播放扩展了BasicGameState
{
形象球、蓝球;
图像[]红球;
动画卷;
int[]持续时间={200200};
float defBallPosX=300;//默认值
float defBallPosY=200;//默认值
浮球X=300;
浮球=200;
int MousePosX;
鼠标定位;
随机;
float[]randomRedX;//X表示RedBall.png
float[]randomRedY;//Y表示RedBall.png
float randomBlueX;//X表示BlueBall.png
float random bluey;//Y表示BlueBall.png
静态整数和羽毛球;
布尔蓝,不敢掉头;
公共游戏(国际国家)
{
}
public void init(GameContainer gc,StateBasedGame sbg)引发异常
{
ball=新图像(“res/PurpBallNorm.png”);//25x25像素
Image[]Image={ball,ball};
红球=新图像[50];
blueball=新图像(“res/blueball.png”);
滚动=新动画(图像、持续时间、假);
随机=新随机();
randomRedX=新浮点[50];
randomRedY=新浮点[50];
amountOfEatenBalls=0;
}
公共void呈现(GameContainer gc、StateBasedGame sbg、Graphics g)引发异常
{
//窗户是600x400
g、 拉丝(MousePosX+”,“+MousePosY,500,10);
g、 抽绳(0,26,Game.windowX,26);//天花板
g、 抽绳(1,26,1,Game.windowY);//左墙
g、 抽绳(Game.windowX-2,26,Game.windowX-2,Game.windowY-2);//右墙
g、 抽绳(1,Game.windowY-2,Game.windowX-2,Game.windowY-2);//地板
滚画(ballPosX,ballPosY);
initRedAndBlueBall();
}
公共无效更新(GameContainer gc、StateBasedGame sbg、int delta)引发异常
{
Input=gc.getInput();
MousePosX=input.getMouseX();
MousePosY=input.getMouseY();
if(input.isKeyDown(input.KEY_UP))
球形-=δ*.1f;
else if(input.isKeyDown(input.KEY_DOWN))
球型+=δ*.1f;
else if(input.isKeyDown(input.KEY_左))
ballPosX-=delta*.1f;
else if(input.isKeyDown(input.KEY_RIGHT))
ballPosX+=δ*.1f;
蓝球触摸();
对于(int i=0;i 100&&MousePosY<126)
{
ExitGameButton=ExitGameHover;
}
其他的
{
ExitGame按钮=ExitGame;
}
if(MousePosX>200&&MousePosX<325&&MousePosY>100&&MousePosY<126)
{
if(鼠标.isButtonDown(0))
{
系统出口(0);
    package javagame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

import java.util.Random;

public class Play extends BasicGameState
{
    Image ball, blueball;
    Image[] redball;
    Animation roll;   

    int[] duration = {200, 200};
    float defBallPosX = 300; // DEFAULT
    float defBallPosY = 200; // DEFAULT
    float ballPosX = 300; 
    float ballPosY = 200; 
    int MousePosX;
    int MousePosY;
    Random random;
    float[] randomRedX; // X for the RedBall.png
    float[] randomRedY; // Y for the RedBall.png
    float randomBlueX; // X for the BlueBall.png
    float randomBlueY; // Y for the BlueBall.png

    static int amountOfEatenBalls;

    boolean blueAndRedAreNotToClose;

    public Play(int state)
    {

    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
    {
        ball = new Image("res/PurpBallNorm.png"); // 25x25 px
        Image[] images = {ball, ball};

        redball = new Image[50];
        blueball = new Image("res/BlueBall.png");

        roll = new Animation(images, duration, false);

        random = new Random();

        randomRedX = new float[50];
        randomRedY = new float[50];

        amountOfEatenBalls = 0;
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
    {
       //   The Window is 600x400
        g.drawString(MousePosX + ", " + MousePosY, 500, 10);

        g.drawLine(0, 26, Game.windowX, 26); // Ceiling
        g.drawLine(1, 26, 1, Game.windowY); // Left Wall
        g.drawLine(Game.windowX - 2, 26, Game.windowX - 2, Game.windowY - 2); // Right Wall
        g.drawLine(1, Game.windowY - 2, Game.windowX - 2, Game.windowY - 2); // Floor

        roll.draw(ballPosX, ballPosY);

        initRedAndBlueBall(); 
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
    {
            Input input = gc.getInput();
            MousePosX = input.getMouseX();
            MousePosY = input.getMouseY();

            if(input.isKeyDown(Input.KEY_UP))
                ballPosY -= delta * .1f;
            else if(input.isKeyDown(Input.KEY_DOWN))
                ballPosY += delta * .1f;
            else if(input.isKeyDown(Input.KEY_LEFT))
                ballPosX -= delta * .1f;
            else if(input.isKeyDown(Input.KEY_RIGHT))
                ballPosX += delta * .1f;

            blueBallTouch(); 

            for(int i = 0; i <= amountOfEatenBalls; i++)
            {
                if(isInTheRangeOf(ballPosX, randomRedX[i], 25) == true && isInTheRangeOf(ballPosY, randomRedY[i], 25) == true)
                {
                    System.out.println("Player has eaten a red ball!");
                    sbg.enterState(2);
                }
            }

//          Makes sure the ball can't run out of the window
            if(ballPosX <= 2)
            {
                ballPosX += delta * .1f;
            }
            else if(ballPosX >= Game.windowX - 27)
            {
                ballPosX -= delta * .1f;
            }
            else if(ballPosY <= 26)
            {
                ballPosY += delta * .1f;
            }
            else if(ballPosY >= Game.windowY - 27)
            {
                ballPosY -= delta * .1f;
            }
    } 

    public int getID() 
    {
        return 1;
    }

    public boolean isInTheRangeOf(float x, float y, float range)
    {
            float a = 0;
            float b = 0;

            if(x > y)
            {
                a = x;
                b = y;
            }
            else if(y > x)
            {
                b = x;
                a = y;
            }   

            if((a - b) <= range)
            {
                return true;
            }
            else if((a - b) >= range)
            {
                return false;
            }
            return true;
    }

    public void initRedAndBlueBall() throws SlickException
    {           
       //   Makes sure the red and the blue ball aren't too close
        for(int i = 0; i <= amountOfEatenBalls; i++)
        {
            // Get random cordinates for the red and blue ball.
            randomRedX[i] = random.nextInt(550) + 24;
            randomRedY[i] = random.nextInt(350) + 24;
            randomBlueX = random.nextInt(550) + 24;
            randomBlueY = random.nextInt(350) + 24;

            // Initialize the new Redball with a picture.
            redball[i] = new Image("res/RedBall.png");
            // Draws the red ball on the screen. 
            drawRedBall(randomRedX[i], randomRedY[i], redball[i]);

            if(isInTheRangeOf(randomRedX[i], randomBlueX, 30) == false)
            {
                System.out.println("The red and the blue ball have good positions");
                break;
            }
            else if(isInTheRangeOf(randomRedX[i], randomBlueX, 30) == true)
            {
                System.out.println("The red and the blue ball are to close!");
                System.out.println("Repositioning the red and blue ball");
            }  
        }
    }

    public void drawRedBall(float x, float y, Image image)
    {
        Graphics g = new Graphics();
        g.drawImage(image, x, y);
    }

    public void blueBallTouch() throws SlickException
    {
        if(isInTheRangeOf(ballPosX, randomBlueX, 25) == true && isInTheRangeOf(ballPosY, randomBlueY, 25) == true)
        {
            amountOfEatenBalls++;

            initRedAndBlueBall();
        }
        else
        {

        }
   }

   public static int getAmountOfEatenBalls()
   {
       return amountOfEatenBalls;
   }

}
package javagame;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class GameOver extends BasicGameState
{
    Image ExitGame, ExitGameHover, ExitGameButton;
    int MousePosX;
    int MousePosY;


    public GameOver(int state)
    {

    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
    {
        ExitGame = new Image("res/ExitGame.png");
        ExitGameHover = new Image("res/ExitGameHover.png");

        ExitGameButton = ExitGame;
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
    {
        g.drawImage(ExitGameButton, 200, 100);
        g.drawString("You've eaten: " + Play.getAmountOfEatenBalls() + " balls!", 200, 190);
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
    {
        Input input = gc.getInput();
        MousePosX = input.getMouseX();
        MousePosY = input.getMouseY();

         if(MousePosX > 200 && MousePosX < 325  &&      MousePosY > 100 && MousePosY < 126)
          {
              ExitGameButton = ExitGameHover;
          }
          else
          {
              ExitGameButton = ExitGame;
          }

         if(MousePosX > 200 && MousePosX < 325  &&      MousePosY > 100 && MousePosY < 126)
          {
              if(Mouse.isButtonDown(0))
              {
                  System.exit(0);
              }
          }
    }

    public int getID()
    {
        return 2;
    }

}