Java 为破砖游戏创建砖阵列
我正在做一项作业,其中包括用java编写一个破砖游戏。提供了一个gui和2个枚举类。我们要写4节课,水平,划桨,球和砖头。目前,我被困在水平班。我试图输出一个砖块数组,但在第120行:Java 为破砖游戏创建砖阵列,java,arrays,Java,Arrays,我正在做一项作业,其中包括用java编写一个破砖游戏。提供了一个gui和2个枚举类。我们要写4节课,水平,划桨,球和砖头。目前,我被困在水平班。我试图输出一个砖块数组,但在第120行:if(brickConfig[x].子字符串(I,I+1).等于(“*”)上得到了一个字符串索引超出范围:2。将brickConfig合并到我唯一坚持的东西中的构造函数。我不相信我已经计算出了砖块之间的像素间距,但我假设它将在这个类的数组创建中的某个地方-1在brick类中转换为不易碎的砖块 public clas
if(brickConfig[x].子字符串(I,I+1).等于(“*”)
上得到了一个字符串索引超出范围:2
。将brickConfig
合并到我唯一坚持的东西中的构造函数。我不相信我已经计算出了砖块之间的像素间距,但我假设它将在这个类的数组创建中的某个地方<代码>-1在brick类中转换为不易碎的砖块
public class Level
{
private int startLeft = 10;
private int startTop = 10 * 4;
private Ball levelBall;
private Paddle levelPaddle;
private int width;
private int height;
private Brick[][] levelStructure = new Brick[5][7];
private GameState gState;
public Level(int widthIn, int heightIn)
{
width = widthIn;
height = heightIn;
levelBall = new Ball(width / 2, height / 2);
levelPaddle = new Paddle(0, 420);
}
/**Creates a level with a given dimension and specified brick
* configuration. The Brick configuration is specified through an
* array of String values where each character corresponds to a
* single Brick.This array of Strings is guaranteed to contain 5
* valid Strings, each of which will have 7 characters.Characters
* in the string will correspond to one value in
* {'*', '0', '1', '2', '3'} where:
* '*' signifies a brick that cannot be broken
* '0' signifies a "ghost brick" that is already broken
* '1' signifies a brick that requires one hit to break
* '2' signifies a brick that requires two hits to break
* '3' signifies a brick that requires three hits to break
*
* Input strings are assumed valid and no error checking is
* provided.
*
* Bricks are arranged starting 40 pixels from the top edge of
* the screen and 10 pixels from the left edge of the screen.
* Bricks should be spaced with 5 pixels between each Brick in
* both dimensions.
*
* The Ball will start in the center of the screen, and the
* Paddle's top-left edge should be positioned at the left edge
* and 20 pixels up from the bottom of the screen.
*
* @param widthIn The logical width of the new level in pixels.
* @param heightIn The logical height of the new level in pixels.
* @param brickConfig The configuration array specifying the grid of Bricks to use in this new level.
*/
public Level(int widthIn, int heightIn, java.lang.String[] brickConfig)
{
width = widthIn;
height = heightIn;
gState = GameState.PLAYING;
for (int x = 0; x < levelStructure.length; x++)
{
for (int i = 0; i < levelStructure[x].length; i++)
{
if (brickConfig[x].substring(i, i + 1).equals("*"))
{
levelStructure[x][i] = new Brick(startTop, startLeft, -1);
}
else
{
levelStructure[x][i] = new Brick(startTop, startLeft,
Integer.parseInt(brickConfig[x].substring(i, i + 1)));
}
}
}
}
公共类级别
{
私人int STARTEFT=10;
私有int STARTOP=10*4;
私人水平球;
私人划桨;
私有整数宽度;
私人内部高度;
私用砖[][]水平结构=新砖[5][7];
私人配子状态;
公共级别(整数宽度英寸、整数高度英寸)
{
宽度=宽度英寸;
高度=高度英寸;
水平球=新球(宽度/2,高度/2);
水平桨叶=新桨叶(0420);
}
/**创建具有给定尺寸和指定砖的标高
*配置。通过
*字符串值数组,其中每个字符对应一个
*单块。此字符串数组保证包含5个
*有效字符串,每个字符串将有7个字符。个字符
*中的值将对应于中的一个值
*{'*',0',1',2',3'}其中:
*“*”表示无法打碎的砖块
*“0”表示已损坏的“鬼砖”
*“1”表示需要一次敲击才能打破的砖块
*“2”表示需要两次敲击才能打破的砖块
*“3”表示需要三次敲击才能打破的砖块
*
*假设输入字符串有效,不进行错误检查
*提供。
*
*砖块从顶部边缘开始排列40个像素
*屏幕和屏幕左边缘的10个像素。
*砖块之间的间距应为5个像素
*两个维度。
*
*球将从屏幕中央开始,然后
*桨叶的左上边缘应位于左边缘
*从屏幕底部向上20像素。
*
*@param width新级别的逻辑宽度(以像素为单位)。
*@param heightIn新级别的逻辑高度(以像素为单位)。
*@param brickConfig配置数组,指定要在此新级别中使用的砖块网格。
*/
公共级别(int-widthIn、int-heightIn、java.lang.String[]brickConfig)
{
宽度=宽度英寸;
高度=高度英寸;
gState=GameState.PLAYING;
对于(int x=0;x
问题在于对字符串进行迭代的方式。i+1可能是使其超出范围的最后一个字符+1。请尝试使用string.length您收到了什么错误消息?@resueman我在if(brickConfig[x]上得到了字符串索引超出范围:2
。子字符串(i,i+1)。等于(“*”)
。我可能应该提到,这表明数组包含类似“*”
的字符串,但您试图访问字符串中的第二个字符(显然不存在)。按照您的设置方式,列表中的第一个字符串
需要1+个字符长,第二个需要2+个字符长,第三个需要3+个字符长,等等。如果这不是您想要的,那么您可能根本不想使用子字符串
,只需直接对原始字符串
进行比较即可。