GAMES LESSON Twenty: Tile Games: Basic Layout


torn.jpg

dot.jpg

tee.jpg

x.jpg

wall.jpg

hall.jpg

Here are the images used in a simple applet which does nothing more than arrange the images on the screen.

The key to arranging the tiles on the screen is a two-dimensional array called map. Each image has an integer assigned to it as an identifier as shown in the comments below.

//wall=0, dot=1, hall=2, tee=3, x=4, torn=5 int[][] map = { { 0,0,0,0,0,0,0,0,0,0,0,0}, { 0,1,1,4,1,1,1,1,1,1,1,0}, { 0,1,0,0,0,1,0,0,0,1,0,0}, { 0,1,0,0,0,1,0,0,0,3,1,0}, { 0,1,0,0,0,1,1,1,1,1,1,0}, { 1,1,1,0,0,0,0,0,0,0,1,1}, { 0,0,1,1,1,5,0,0,0,0,1,0}, { 0,1,1,0,0,1,1,1,1,1,1,0}, { 0,3,0,0,0,1,0,0,0,0,1,0}, { 0,1,0,0,0,1,0,0,0,0,1,0}, { 0,1,1,1,1,1,1,1,4,1,1,0}, { 0,0,0,0,0,0,0,0,0,0,0,0} }; Image[] sps = new Image[6];
Embedded for-loops are used in the paint method to draw the tiles.

import java.awt.*; import java.applet.*; public class TMAN1 extends Applet{ Dimension d; //wall=0, dot=1, hall=2, tee=3, x=4, torn=5 int[][] map = { { 0,0,0,0,0,0,0,0,0,0,0,0}, { 0,1,1,4,1,1,1,1,1,1,1,0}, { 0,1,0,0,0,1,0,0,0,1,0,0}, { 0,1,0,0,0,1,0,0,0,3,1,0}, { 0,1,0,0,0,1,1,1,1,1,1,0}, { 1,1,1,0,0,0,0,0,0,0,1,1}, { 0,0,1,1,1,5,0,0,0,0,1,0}, { 0,1,1,0,0,1,1,1,1,1,1,0}, { 0,3,0,0,0,1,0,0,0,0,1,0}, { 0,1,0,0,0,1,0,0,0,0,1,0}, { 0,1,1,1,1,1,1,1,4,1,1,0}, { 0,0,0,0,0,0,0,0,0,0,0,0} }; Image[] sps = new Image[6]; public void init(){ d = getSize(); sps[0] = getImage(getDocumentBase(), "wall.jpg"); sps[1] = getImage(getDocumentBase(), "dot.jpg"); sps[2] = getImage(getDocumentBase(), "hall.jpg"); sps[3] = getImage(getDocumentBase(), "x.jpg"); sps[4] = getImage(getDocumentBase(), "tee.jpg"); sps[5] = getImage(getDocumentBase(), "torn.jpg"); } public void paint(Graphics g){ g.setColor(Color.black); g.fillRect(0,0,d.width,d.height); for(int y=0; y<map.length; y++) for(int x=0; x<map[y].length; x++) g.drawImage(sps[map[y][x]],10+30*x,10+30*y,this); } }


ASSIGNMENT: