Reference no: EM13346889
Build the GUI layout of the game
Create a class called PipeGameApp.java which will be the main game user interface. The interface should have the title "The Frantic Pipe Layer" and contain an 11x11 grid of 40pixel x 40pixel buttons as shown below. The buttons should be arranged on a BoardPanel (which is a subclass of JPanel that you can download along with this assignment in the BoardPanel.java file). The BoardPanel ensures that the size of the buttons do not change. Make sure that your window opens with all buttons enabled and not selected.
工作按钮
PipeGame类代表实际的游戏out the graphical user interface. The idea is that the game stores a grid of pipe shapes that are placed onto it. The user will be able to click on any unoccupied grid locations to place a pipe piece down. The very first pipe piece that must be placed is the valve pipe shown here to the right. All other pipe pieces must be non-valve pipes and may be one of the following:
Also note that the valve pipe is a special one and is called pipesStart.GIF.
当游戏运行时,光标应始终显示用户可能放置的下一个(即当前的)管道,以便用户在将管道放下之前先查看管道。PipeGame类中的GetNextPipe()方法返回要放置的下一个管道。然后,您可以为该管道创建一个图像图标,如下所示:
ImageIcon i;
if (game.getNextPipe().isValve())
else
i = new ImageIcon("pipesStart.GIF");
i = new ImageIcon("pipes"+ game.getNextPipe().toString() +".GIF");
Then you can use the following code to change the cursor into any given pipe picture (assuming that tiles is the BoardPanel object that contains the buttons):
tiles.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(i.getImage(),
new Point(0,0), "pipe"));
Then to return the cursor to normal, you can just use:
tiles.setCursor(Cursor.getDefaultCursor());
将适当的事件处理程序添加到按钮中,以便用户单击按钮时,将管道放在该位置。为此,您只需要存储该按钮的图像即可。在Java中,您可以通过设置按钮的“选定图标”来执行此操作:如下:
String currentPipeCodes = game.getNextPipe().toString();
Abutton.setSelectedicon(新Imageicon(“ Pipes” + CurrentPipeCodes +“ .gif”));
aButton.setSelected(true);
请记住,第一个管道(即阀管)是使用“ pipesstart.gif”文件的特殊情况。放置任何管道后,将使用新的随机管道用作下一个要放置的管道。调整管道类中的静态随机()方法,以返回随机管道零件(即,在4个方向上具有随机开口的新管道),而不是每次同一件。确保您的代码有效。
查看管道类别,因为它已经保持了跟踪管道对象的2维网格的属性,到目前为止,已放置在网格上的管道数量以及将放置下一个管道。一种称为resetpipes()的实例方法将网格重置,因此网格中的所有管道都开始为无效,下一个要放置的管道是阀管。
在继续之前,请确保您可以在整个网格上放置大量管道。
(3) Smart Pipe Placement
Now you will adjust your code so that pipes can only be placed on a grid location if the pipe actually fits with the (up to) 4 pipes around it. Also, once a pipe has been placed at a location, no other pipe can be placed there.
每个管道都以1,2,3或4个方向开放。末端碎片显示为红色圆圈。
管道存储4个布尔值,指示管道在顶部,右,底部还是左的顶部,右,底部或左开放,表明它是阀门是否是阀门(设置为上述所有15个零件的false)。
Instance methods fitsBelow(Pipe p), fitsAbove(Pipe p), fitsToLeftOf(Pipe p), and fitsToRightOf(Pipe p) return a boolean indicating whether or not the receiver pipe fits below, above, to the left of or to the right of the given pipe p, respectively. Note that two pipes "fit" together if they both have openings that meet when placed beside each other or both do NOT have openings.
Here are examples of pipes that "fit" beside one another:
Here are examples of Pipes that DO NOT "fit" beside one another:
修改称为plandpipe(int Row,int col)的实例方法,该方法试图将当前管道放置在指定的网格位置。目前,无论是否正确安装管道,管道始终都放置。调整代码,以便如果管道不适合那里,则返回false,否则将返回true。同样,如果管道拟合在此位置,则将放置管道的数量增加,并选择一个随机选择的新管道作为下一个要放置的管道(而不是阀门,因为只有一个阀门)。下一页显示了一段时间后的有效板。确保还要输入所有管道图片文件,因为它们需要运行代码。