Hi there i'm not to sure on how to go about removing or clearing the screen but then redraw the next level while keeping the score counter. I've currently been trying to figure out (with some help from the users of this website) how to go about it here is my current code so far, on a side note my levels are created from text files.
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.io.*; import java.util.List; // (for array) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { // varables Counter counter = new Counter(); private final int WORLD_WIDTH = getWidth(); // Width of the world private final int WORLD_HEIGHT = getHeight(); // Height of the world Wall wallTemplate = new Wall(); //creates a wall (Not used) Player playerTemplate = new Player(); //creates a Player (Not used) Pearl pickupTemplate = new Pearl(); //wall GreenfootImage wtImg = wallTemplate.getImage(); // get the image of the wall private final int WALL_HEIGHT = wtImg.getHeight(); //finds the height of the wall private final int WALL_WIDTH = wtImg.getWidth(); //finds the width of the wall //player GreenfootImage plImg = playerTemplate.getImage(); // get the image of the player private final int PLAYER_HEIGHT = plImg.getHeight(); //finds the height of the Player private final int PLAYER_WIDTH = plImg.getWidth(); //finds the width of the player //Item GreenfootImage ppImg = pickupTemplate.getImage(); // get the image of the Players pickup private final int PLAYER_PU_HEIGHT = ppImg.getHeight(); //finds the height of the Player Pickup private final int PLAYER_PU_WIDTH = ppImg.getWidth(); //finds the width of the player Pickup private final int MAP_WIDTH = ( int )(WORLD_WIDTH/WALL_WIDTH); // changes the MAP_WIDTH depending on wall width private final int MAP_HEIGHT = ( int ) (WORLD_HEIGHT/WALL_HEIGHT); // changes the MAP_HEIGHT depending on wall height private final String FILENAME = "Map1.txt" ; private final String FILENAME2 = "Map2.txt" ; private final String WALL_MARKER = "*" ; private final String PLAYER_SPAWN = "P" ; private final String PICK_UP = "I" ; String[][]mapArray = new String [MAP_HEIGHT][MAP_WIDTH]; /// /// /** * Constructor for objects of class MyWorld. * */ public MyWorld( int lv) throws IOException { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 600 , 400 , 1 ); perpare(); loadLevel( 1 ); } public void loadLevel( int lv) throws IOException { if (lv== 1 ) Level1(); else if (lv== 2 ) { Level2(); } } public Counter getCounter() { return counter; } private void perpare() { addObject(counter, 40 , 15 ); } public void Level1() throws IOException { mapArray = readMapFromTextFile(MAP_WIDTH, MAP_HEIGHT, FILENAME); drawWallMapFromTextArray(WALL_WIDTH,WALL_HEIGHT,WALL_MARKER,PLAYER_WIDTH, PLAYER_HEIGHT, PLAYER_SPAWN,PLAYER_PU_WIDTH, PLAYER_PU_HEIGHT, PICK_UP, mapArray); perpare(); } public void Level2() throws IOException { mapArray = readMapFromTextFile(MAP_WIDTH, MAP_HEIGHT, FILENAME2); drawWallMapFromTextArray(WALL_WIDTH,WALL_HEIGHT,WALL_MARKER,PLAYER_WIDTH, PLAYER_HEIGHT, PLAYER_SPAWN,PLAYER_PU_WIDTH, PLAYER_PU_HEIGHT, PICK_UP, mapArray); ///perpare(); } public void drawWallMapFromTextArray( int WALL_WIDTH, int WALL_HEIGHT, String WALL_MARKER, int playerHeight, int playerWidth, String playerMarker, int pearlWidth, int pearlHeight, String ItemMarker,String [][] mapArray) { int x = 0 ; int y = 0 ; for (y= 0 ;y<mapArray.length;y++) { for (x= 0 ;x<mapArray[y].length;x++) { if (mapArray[y][x].equals(WALL_MARKER)) { int wallX = x*WALL_WIDTH + WALL_WIDTH/ 2 ; int wallY = y*WALL_HEIGHT + WALL_HEIGHT/ 2 ; addObject ( new Wall(), wallX, wallY); //addObject(counter,40,15); perpare(); } if (mapArray[y][x].equals(playerMarker)) { int playerX = x*playerWidth + playerWidth/ 2 ; int playerY = y*playerHeight + playerHeight/ 2 ; addObject( new Player(), playerX, playerY); } if (mapArray[y][x].equals(ItemMarker)) { int pearlX = x*pearlWidth + pearlWidth/ 2 ; int pearlY = y*pearlHeight + pearlHeight/ 2 ; addObject( new Pearl(), pearlX,pearlY); } } } } public String [][] readMapFromTextFile( int MAP_WIDTH, int MAP_HEIGHT, String fileName) throws IOException { BufferedReader br = null ; String[][] mArray = new String[MAP_HEIGHT][MAP_WIDTH]; try { br = new BufferedReader( new FileReader(fileName)); String l; int mapRow= 0 ; //tells us what row we're on while ((l=br.readLine())!= null ) { mArray[mapRow] = l.split( "" ); mapRow++; } } finally { if (br!= null ) br.close(); } return mArray; } } |