回 帖 发 新 帖 刷新版面

主题:[求助]急~~关于NetBean的Bluej的一个作业~

This project contains the basic framework for a "Multi-room Doty" game. Notice that Doty is now able to move from room to room. The template starts with only 2 rooms.

Step 1

Add food into the game. Add an Arraylist<Food> foods to the Room class. Also, create a method void addFood(Food f) in the room class. The purpose of this method is give the caller the ability to add foods to each room. 

In the constructor of the game class, you can setup your game by adding foods to each room:

public Game()
{


roomLeft = new Room(10, 15, 10, "The left room");
roomRight = new Room(10, 15, 10, "The right room");

// Setup the left room 
roomLeft.setExits(null, roomRight, null, null);
roomLeft.setTextureEast("textures/door.gif");
roomLeft.addFood(new Food(1, 1, 4));
roomLeft.addFood(new Food(4, 1, 8));
roomLeft.addFood(new Food(7, 1, 3));

// Setup the right room
roomRight.setExits(null, null, null, roomLeft);
roomRight.setTextureWest("textures/door.gif"); 
roomRight.addFood(new Food(2, 1, 4));
roomRight.addFood(new Food(5, 1, 3));
roomRight.addFood(new Food(6, 1, 3)); 

doty1 = new Doty(5, 1, 1);

}

Every time Doty moves to a different room, make sure that all the foods are added back to the environment.

Step 2

We want Doty to be able to eat up the foods. Add a private method void checkFood(). This method will be called every frame. The purpose of this method is to check if Doty is close to any of the foods in the current room. Make the food disappear if Doty is close to any of the foods.

Step 3

Right now, each room can only have 2 exits. Doty can also move out of the wall on the north and south directions. Modify the Room class as well as the checkWall() method so that each room can now have 4 exits (north, east, south, west).

Create a map of 5 rooms as show below:


[ North Room ] 

[ West Room ] [ Middle Room ] [ East Room ] 

[ South Room ] 


Start Doty in the middle room. Put foods in all of the rooms. End the game when all foods are eaten.

其中每个class初始给的code 如下:
1) Game class的:

import java.util.ArrayList;
import env3d.Env;

/**
 * This is the main Game class.  It is the main entry point of our game and contains the control 
 * loop and all the various support methods.  
 * 
 * Only the play method is meant to be called by the caller to start the game.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Game
{
    private boolean finished;
    private Doty doty;
    private Env env;
    private Room roomLeft, roomRight;
    private Room currentRoom;
    
    /**
     * The constructor of the game.  It sets up all the room information 
     * and the doty's position.
     */
    public Game()
    {
        roomLeft = new Room(10, 15, 10);
        roomRight = new Room(10, 15, 10);
        
        roomLeft.setExits(null, roomRight);
        roomLeft.setTextureEast("textures/door.gif");
        
        roomRight.setExits(roomLeft, null);
        roomRight.setTextureWest("textures/door.gif");               
        
        doty = new Doty(5, 1, 1);
    }
    
    /**
     * This is the main game loop.  Try to keep it as short
     * as possible.
     */
    public void play()
    {
        finished = false;
        
        // Create a new environment
        env = new Env();
        
        // Starts with roomLeft
        currentRoom = roomLeft;
        env.setRoom(currentRoom);
        
        // Adding doty to the environment
        env.addObject(doty);
                
        // Position the camera
        env.setCameraXYZ(5, 14, 9);
        env.setCameraPitch(-75);
        
        // Disable mouse and camera control
        env.setDefaultControl(false);
        
        while (finished == false)
        {            
            processInput();

            checkWall();
            
            env.advanceOneFrame();
        }
        
        System.exit(0);
    }    
    
    /**
     * Checks keyboard input
     */
    private void processInput() 
    {
        int currentKey = env.getKey();
            
        if (currentKey == 1) {
            finished = true;
        } else if (currentKey == 200) {
            // Up arrow
            doty.moveZ(-1);
        } else if (currentKey == 208) {
            // Down arrow
            doty.moveZ(1);   
        } else if (currentKey == 203) {
            // Left arrow
            doty.moveX(-1);            
        } else if (currentKey == 205) {
            // Right arrow
            doty.moveX(1);         
        } 
    }
    
    /**
     * Check to see if doty has hit a wall.  It then tries to determine
     * if doty should be moved to the next room.
     */
    private void checkWall()
    {
        if (doty.getX() > currentRoom.getWidth()-doty.getScale()) {
            // Going east
            if (currentRoom.getEastExit() != null) {                
                currentRoom = currentRoom.getEastExit();
                env.setRoom(currentRoom);
                doty.setX(doty.getScale());                    
                env.addObject(doty);
            } else {
                // Doty has hit a wall
                doty.setX(currentRoom.getWidth()-doty.getScale());
            }
        } else if (doty.getX() < doty.getScale()) {
            // Going west
            if (currentRoom.getWestExit() != null) {
                currentRoom = currentRoom.getWestExit();
                env.setRoom(currentRoom);
                doty.setX(currentRoom.getWidth()-doty.getScale());                    
                env.addObject(doty);                    
            } else {
                // Doty has hit a wall
                doty.setX(doty.getScale());
            }
        }        
    }
    
    /**
     * Basic distance method.  It calculates the distance between 2 objects given
     * 2 sets of x, y, and z coordinates.
     */
    private float distance(float x1, float x2, float y1, float y2, float z1, float z2) {
        float xdiff, ydiff, zdiff;
        xdiff = x2 - x1;
        ydiff = y2 - y1;
        zdiff = z2 - z1;
        return (float) Math.sqrt(xdiff*xdiff + ydiff*ydiff + zdiff*zdiff);
    }
}

2)room class的

import java.util.ArrayList;

/**
 * The Room class represents a generic room.  
 * The dimensions of the room is flexible
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Room
{
    // instance variables - replace the example below with your own
    private float width, height, depth;
    private String textureNorth, textureSouth, textureEast, textureWest;
    private Room exitEast, exitWest;
        
    
    /**
     * Constructor for objects of class Room.  Allows the 
     * caller to create rooms of different dimensions.
     */
    public Room(float w, float h, float d)
    {
        width = w;
        height = h;
        depth = d;
        
        textureNorth = "textures/fence0.jpg"; 
        textureEast = "textures/fence0.jpg"; 
        textureSouth = "textures/fence0.jpg"; 
        textureWest =  "textures/fence0.jpg"; 
        
    }
    
    /**
     * Sets the north texture of the room
     * 
     * @param fileName name of the texture file
     */
    public void setTextureNorth(String fileName)
    {
        textureNorth = fileName;
    }

    /**
     * Sets the east texture of the room
     * 
     * @param fileName name of the texture file
     */
    public void setTextureEast(String fileName)
    {
        textureEast = fileName;
    }

    /**
     * Sets the south texture of the room
     * @param fileName name of the texture file
     */
    public void setTextureSouth(String fileName)
    {
        textureSouth = fileName;
    }

    /**
     * Sets the west texture of the room
     * @param fileName name of the texture file
     */
    public void setTextureWest(String fileName)
    {
        textureWest = fileName;
    }
    
    /**
     * Every object of the room class can have 2 exits.
     * Each exit is actually a pointer to another room.
     * We can essentially create a long corridor.
     * 
     * @param west The west exit 
     * @param east The east exit
     */
    public void setExits(Room west, Room east)
    {
        exitEast = east;
        exitWest = west;
    }
    
    /**
     * Returns the east exit room to the caller
     */
    public Room getEastExit()
    {
        return exitEast;
    }
    
    /**
     * Returns the west exit room to the caller
     */
    public Room getWestExit()
    {
        return exitWest;        
    }

    /**
     * Returns the width of the room
     */
    public float getWidth()
    {
        return width;
    }
    
    /**
     * Returns the depth of the room
     */
    public float getDepth()
    {
        return depth;
    }
    
}


回复列表 (共4个回复)

沙发

3)Doty Class的:


/**
 * Doty is the main character of our game.  For
 * now, it can only move around.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Doty
{

    private float x;
    private float y;
    private float z;
    private String texture;
    private float scale;    
    private float rotateY;
    
    
    /**
     * Constructor for objects of class Doty.  The
     * caller can create a doty in a flexible location
     * 
     * @param x the x coordinate
     * @param y the y coordinate
     * @param z the z coordinate
     */
    public Doty(float x, float y, float z)
    {
        // initialise instance variables
        this.x = x;
        this.y = y;
        this.z = z;
        texture = "textures/doty.gif";
        scale = 1f;
        rotateY = 0;
    }
    
    /**
     * Moves doty in the x direction
     * 
     * @param dir the direction. 1 means positive (to the right), -1 means negative (to the left), 0 means don't move
     */
    public void moveX(int dir)
    {
        // Make Doty face the right direction
        if (dir > 0) {
            rotateY = 90;
        } else {
            rotateY = -90;
        }            
            
        x = x + scale*dir;
    }
    
    /**
     * Moves doty in the z direction
     * 
     * @param dir the direction. 1 means positive (towards the screen), -1 means negative (away from the screen), 0 means don't move
     */
    public void moveZ(int dir)
    {
        if (dir > 0) {
            rotateY = 0;
        } else {
            rotateY = 180;
        }
        z = z + scale*dir;
    }
    
    /**
     * Returns the x coordinate
     */
    public float getX()
    {
        return x;
    }

    /**
     * Returns the y coordinate
     */
    public float getY()
    {
        return y;
    }

    /**
     * Returns the z coordinate
     */
    public float getZ()
    {
        return z;
    }
    
    /**
     * Returns the current scale of doty
     */
    public float getScale()
    {
        return scale;
    }
    
    /**
     * Sets the x coordinate
     * 
     * @param x the x coordinate
     */
    public void setX(float x)
    {
        this.x = x;
    }

    /**
     * Sets the y coordinate
     * 
     * @param y the y coordinate
     */
    public void setY(float y)
    {
        this.y = y;
    }
    
    /**
     * Sets the z coordinate
     * 
     * @param z the z coordinate
     */
    public void setZ(float y)
    {
        this.z = z;
    }
    
    /**
     * Grow doty by delta amount
     * 
     * @param delta the amount by which doty will grow
     */
    public void grow(float delta)
    {
        scale = scale + delta;
    }
    
    /**
     * Switch doty's texture to a simily face
     */
    public void smile()
    {
        texture = "textures/doty_happy.gif";
    }

}

板凳

4)Food Class的:


/**
 * Represents a food in the game
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Food
{

    private float x, y, z;
    private float scale;
    
    /**
     * Constructor for objects of class Food.  This
     * is flexible and the caller can position food anywhere
     */
    public Food(float x, float y, float z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
        scale = 0.3f;
    }

    /**
     * Get the x coordinate of this food
     */
    public float getX()
    {
        return x;
    }

    /**
     * Get the y coordinate of this food
     */
    public float getY()
    {
        return y;
    }

    /**
     * Get the z coordinate of this food
     */
    public float getZ()
    {
        return z;
    }

    /**
     * Get the scale of this food
     */
    public float getScale()
    {
        return scale;
    }
    
    /**
     * Make the food disappear.  It simply sets the food to be out of sight.
     */
    public void disappear()
    {
        x = 100;
    }
    
    

}

3 楼

还有个class貌似没用,不过初始给出的class里也有,我就给出来

叫BasicMp3Engine的class:


import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.File;
import entagged.audioformats.AudioFileIO;
import entagged.audioformats.AudioFile;
import java.io.InputStream;
import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.BitstreamException;
import javazoom.jl.decoder.Decoder;
import javazoom.jl.decoder.Header;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.decoder.SampleBuffer;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;

/**
 *
 * A very basic engine for playing one single mp3 file.  It contains 3 basic methods:
 * play() - plays the song
 * stop() - pause the song
 * skipFrames() - skip to a specific frame
 * @author Jason Madar
 * @version June 16, 2007
 */
public class BasicMp3Engine {
    private MP3File player;
    private String artist;
    private String genre;
    private String title;
    private String album;
    private String track;
    private String year;
    private String mp3filename;
    
    private int stopFrame;
    
    /**
     * Create a new mp3 engine.  Must provide a filename.
     * @param filename The name of the mp3 file, including full path.
     */
    public BasicMp3Engine(String filename) {
        try {
            // Get the ID3 tags
            AudioFile audioFile = AudioFileIO.read(new File(filename)); //Reads the given file.
            artist = audioFile.getTag().getFirstArtist(); //Retreive the artist name.
            genre = audioFile.getTag().getFirstGenre();
            title = audioFile.getTag().getFirstTitle();
            album = audioFile.getTag().getFirstAlbum();
            track = audioFile.getTag().getFirstTrack();
            year = audioFile.getTag().getFirstYear();
            
            mp3filename = filename;
            stopFrame = 0;
            player = null;
        } catch (Exception e) {
            System.out.println("Error: "+e);
        }
    }
    
    /**
     * Play the mp3 file.  This method returns right away and the music is
     * played in the background
     */
    synchronized public void play() {
        if (player == null || player.isComplete()) 
            createPlayer();
        if (stopFrame > 0) skipFrames(stopFrame);
        Thread playerThread = new Thread() {
            public void run() {
                try {
                    // Just make sure that the player has not been destroyed
                    if (player != null) player.play();   
                } catch (Exception e) {
                    throw new RuntimeException(e.getMessage());
                }
            }
        };
        playerThread.start();
    }
    
    /**
     * Skip from the beginning of the song to the specific frame number.
     * @param numFrame The number of frames to skip
     */
    public void skipFrames(int numFrame) {
        if (player == null) createPlayer();
        try {
            if (player.skipFrames(numFrame)) {
                stopFrame = numFrame;
            } else {
                // Skipping beyond the last frame
                stopFrame = 0;
                player = null;
            }
        } catch (Exception e) {
            System.out.println("Error: "+e);
        }
    }
    

4 楼

/**
     * Stops the player and retain the stopping position.  If play() is called again,
     * it will start from the previous stopped position.
     * @return The stopped frame number, or 0 if the song has finished playing
     */
    synchronized public int stop() {
        if (player != null) {
            player.close();
            if (player.isComplete()) {
                stopFrame = 0;
            } else {
                stopFrame += player.getCurrentFrame();
            }
            player = null;
        } else {
            stopFrame = 0;
        }
        return stopFrame;
    }
    
    /**
     * Checks if the song has completed playing.
     * @return true if the song is finished, false otherwise.
     */
    synchronized public boolean isComplete() {
        if (player != null) {
            return player.isComplete();
        } else {
            return true;
        }
    }
    
    
    /**
     * Create a player for internal use
     *
     */
    private void createPlayer() {
        // Create a player
        if (player == null || player.isComplete()) {
            try {
                FileInputStream fin = new FileInputStream(mp3filename);
                BufferedInputStream bin = new BufferedInputStream(fin);
                player = new MP3File(bin);
            } catch (Exception e) {
                System.out.println("Cannot initialize player "+e);
            }
        }
    }
    
    /**
     * Gets the artist from the ID3 tag
     * @return the name of the artist
     */
    public String getArtist() {
        return artist;
    }
    
    /**
     * Get the genre of the song from the ID3 tag
     * @return The genre
     */
    public String getGenre() {
        return genre;
    }
    
    /**
     * Gets the title of the song from the ID3 tag
     * @return the title
     */
    public String getTitle() {
        return title;
    }
    
    /**
     * Gets the album of the song from the ID3 tag
     * @return the album
     */
    public String getAlbum() {
        return album;
    }
    
    /**
     * Gets the track of the song from the ID3 tag
     * @return the track
     */
    public String getTrack() {
        return track;
    }
    
    /**
     * Gets the year of the song from the ID3 tag
     * @return the year
     */
    public String getYear() {
        return year;
    }
    
    private class MP3File {
        /**
         * The current frame number.
         */
        private int frame = 0;
        
        /**
         * The MPEG audio bitstream.
         */
        // javac blank final bug.
        /*final*/ private Bitstream     bitstream;
        
        /**
         * The MPEG audio decoder.
         */
        /*final*/ private Decoder       decoder;
        
        /**
         * The AudioDevice the audio samples are written to.
         */
        private AudioDevice audio;
        
        /**
         * Has the player been closed?
         */
        private boolean     closed = false;
        
        /**
         * Has the player played back all frames from the stream?
         */
        private boolean     complete = false;
        
        private int         lastPosition = 0;
        

我来回复

您尚未登录,请登录后再回复。点此登录或注册