⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 castle.java

📁 龙与地下城游戏(小型)的设计与实现。主要是随机产生地牢
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                Door d = new Door(rooms[row][col]);                d.setR2(rooms[row][col + 1]);                rooms[row][col].setEast(d);                rooms[row][col + 1].setWest(d);            }            row = rand.nextInt(GameFrame.SIZE);            col = rand.nextInt(GameFrame.SIZE - 1);            success = false;        }    }    /**     * Build the image representing the current castle (the current room layout)     * @throws java.io.IOException if something happens when building image     */    private void buildCastleImg() throws IOException {        if (castleImage == null) {            BufferedImage img = new BufferedImage(GameFrame.SIZE * GameFrame.ROOM_SIZE, GameFrame.SIZE * GameFrame.ROOM_SIZE, BufferedImage.TYPE_INT_RGB);            Graphics g = img.createGraphics();            for (int i = 0; i < rooms.length; i++) {                for (int j = 0; j < rooms.length; j++) {                    System.out.println("Room: " + rooms[i][j]);                    String imgName = "img/" + rooms[i][j].roomString() + ".jpg";                    System.out.println("Trying to read " + imgName);                    File f = new File(imgName);                    try {                        Image tmp = ImageIO.read(f);                        g.drawImage(tmp, j * GameFrame.ROOM_SIZE, i * GameFrame.ROOM_SIZE, null);                        System.out.println("Drawing in " + i * GameFrame.ROOM_SIZE + "/" + j * GameFrame.ROOM_SIZE);                    } catch (Exception exception) {                        System.out.println("Failing to read " + imgName);                        exception.printStackTrace();                    }                }            }            ImageIO.write(img, "jpg", imageFile);            System.out.println("castle written");        }    }    /**     * Get the image representing the rooms of the castle     * @return the image object     * @throws java.io.IOException if there is trouble reading the generated image from file     */    public Image getCastleImage() throws IOException {        if (castleImage == null) {            castleImage = ImageIO.read(imageFile);        }        return castleImage;    }    /**     * Randomize the monsters over the rooms of the castle     */    public void randomizeMonsters() {        //Create a new random object        Random rand = new Random();               //Randomize 3 pacman monsters        int counter = 0;        int shift = 1;        while (counter < 3) {            int row = rand.nextInt(GameFrame.SIZE-shift-1);             int col = rand.nextInt(GameFrame.SIZE-shift-1);             col = col + shift;            if(rooms[row][col].getItem() == null){                //OK to place a monster here                counter ++;                PacmanMonster pm = new PacmanMonster();                rooms[row][col].setItem(pm);                pm.setCurrentRoom(rooms[row][col]);                System.out.println("Placing pacman in " + row +"/"+col);            }        }                //Randomize 2 devils         counter = 0;         shift = 1;        while (counter < 2) {            int row = rand.nextInt(GameFrame.SIZE-shift-1);             int col = rand.nextInt(GameFrame.SIZE-shift-1);             col = col + shift;            if(rooms[row][col].getItem() == null){                //OK to place a monster here                counter ++;                DevilMonster dm = new DevilMonster();                rooms[row][col].setItem(dm);                dm.setCurrentRoom(rooms[row][col]);                System.out.println("Placing devil in " + row +"/"+col);            }        }                 //Randomize 2 dragons        counter = 0;        shift = 0;        while (counter < 2) {            int row = rand.nextInt(GameFrame.SIZE-shift-1);             int col = rand.nextInt(GameFrame.SIZE-shift-1);             col = col + shift;            if(rooms[row][col].getItem() == null){                //OK to place a monster here                counter ++;                DragonMonster dr = new DragonMonster();                rooms[row][col].setItem(dr);                dr.setCurrentRoom(rooms[row][col]);                System.out.println("Placing dragon in " + row +"/"+col);            }        }             counter = 0;        shift = 0;        while (counter < 2) {            int row = rand.nextInt(GameFrame.SIZE-shift-1);             int col = rand.nextInt(GameFrame.SIZE-shift-1);             col = col + shift;            if(rooms[row][col].getItem() == null){                //OK to place a monster here                counter ++;              Bb dr = new Bb();                rooms[row][col].setItem(dr);                dr.setCurrentRoom(rooms[row][col]);                System.out.println("Placing dragon in " + row +"/"+col);            }        }        }    /**     * Randomize the aid kits over the rooms     */    public void randomizeAidKit() {         //Create a new random object        Random rand = new Random();                //Randomize 10 treasures        int counter = 0;        while (counter < 3) {            int row = rand.nextInt(GameFrame.SIZE-1);             int col = rand.nextInt(GameFrame.SIZE-1);                         if(rooms[row][col].getItem() == null){                //OK to place a aid kit here                counter ++;                //Random value                int value = rand.nextInt(4);                value ++;                AidKit kit = new AidKit(value);                rooms[row][col].setItem(kit);                kit.setCurrentRoom(rooms[row][col]);            }        }        }    /**     * Randomize the treasures in the castle     */    public void randomizeTreasures() {        //Create a new random object        Random rand = new Random();                //Randomize 10 treasures        int counter = 0;        while (counter < 10) {            int row = rand.nextInt(GameFrame.SIZE-1);             int col = rand.nextInt(GameFrame.SIZE-1);                         if(rooms[row][col].getItem() == null){                //OK to place a treasure here                counter ++;                                int value = 50;                Treasure t = new Treasure(value);                rooms[row][col].setItem(t);                t.setCurrentRoom(rooms[row][col]);                System.out.println("Placing treasure in " + row +"/"+col);            }        }        }    public void randomizeWeapons() {         //Create a new random object        Random rand = new Random();              //Randomize 1 rayguns        int counter = 0;           while (counter < 1) {            int row = rand.nextInt(GameFrame.SIZE-1);             int col = rand.nextInt(GameFrame.SIZE-1);                       if(rooms[row][col].getItem() == null){                //OK to place a weapon here                counter ++;                Raygun weapon= new Raygun();                rooms[row][col].setItem(weapon);                weapon.setCurrentRoom(rooms[row][col]);                System.out.println("Placing raygun in " + row +"/"+col);            }        }                //Randomize 1 swords         counter = 0;                 while (counter < 1) {            int row = rand.nextInt(GameFrame.SIZE-1);             int col = rand.nextInt(GameFrame.SIZE-1);                         if(rooms[row][col].getItem() == null){                //OK to place a weapon here                counter ++;                Sword weapon = new Sword();                rooms[row][col].setItem(weapon);                weapon.setCurrentRoom(rooms[row][col]);                System.out.println("Placing sword in " + row +"/"+col);            }        }                 //Randomize 1 bows        counter = 0;                while (counter < 1) {            int row = rand.nextInt(GameFrame.SIZE-1);             int col = rand.nextInt(GameFrame.SIZE-1);                     if(rooms[row][col].getItem() == null){                //OK to place a monster here                counter ++;                Bow weapon = new Bow();                rooms[row][col].setItem(weapon);                weapon.setCurrentRoom(rooms[row][col]);                System.out.println("Placing bow in " + row +"/"+col);            }        }             counter = 0;                while (counter < 1) {            int row = rand.nextInt(GameFrame.SIZE-1);             int col = rand.nextInt(GameFrame.SIZE-1);                     if(rooms[row][col].getItem() == null){                //OK to place a monster here                counter ++;                D weapon = new D();                rooms[row][col].setItem(weapon);                weapon.setCurrentRoom(rooms[row][col]);                System.out.println("Placing bow in " + row +"/"+col);            }        }        }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -