1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | import greenfoot.*; import java.awt.Color; import java.util.List; import java.util.ArrayList; public class levelBackground extends World { platformMap map = new platformMap(); GreenfootImage mapImg = map.getImage(); final int MAPIMGWIDTH = mapImg.getWidth(); final int MAPIMGHEIGHT = mapImg.getHeight(); Block platformTemplate = new Block( 0 , 0 ); GreenfootImage pfImg = platformTemplate.getImage(); final int PLATFORMHEIGHT = pfImg.getHeight(); final int PLATFORMWIDTH = pfImg.getWidth(); final int MAPWIDTH = MAPIMGWIDTH * PLATFORMWIDTH; final int MAPHEIGHT = MAPIMGHEIGHT * PLATFORMHEIGHT; private List<Block> thePlatforms = new ArrayList<Block>(); int leftBound = 0 ; int bottomBound = MAPHEIGHT; int topBound = MAPHEIGHT - getHeight(); int rightBound = getWidth(); public levelBackground() { super ( 1000 , 600 , 1 ); makeMap(); update(); } public void makeMap() { for ( int y= 0 ; y<MAPIMGHEIGHT; y++) { for ( int x= 0 ; x<MAPIMGWIDTH; x++) { int colorRGB = mapImg.getColorAt(x, y).getRGB(); if (colorRGB==Color.BLACK.getRGB()) { int mapX = x * PLATFORMWIDTH + PLATFORMWIDTH/ 2 ; int mapY = y * PLATFORMHEIGHT + PLATFORMHEIGHT/ 2 ; thePlatforms.add( new Block(mapX,mapY)); } } } } public void update() { Block thisPlatform; int thisPlatformX; int thisPlatformY; int screenX; int screenY; for ( int i= 0 ; i<thePlatforms.size(); i++) { thisPlatform = thePlatforms.get(i); thisPlatformX = thisPlatform.mapX; thisPlatformY = thisPlatform.mapY; if (thisPlatformX>=leftBound && thisPlatformX<=rightBound && thisPlatformY>=topBound && thisPlatformY<=bottomBound) { screenX = thisPlatformX - leftBound; screenY = thisPlatformY - topBound; if (thisPlatform.getWorld()== null ) { addObject(thisPlatform, screenX, screenY); } } } } } |

