Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: beginner w/ text based RPG

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Cool beginner w/ text based RPG

    I am just finishing my entry level Java class, but think i am going to take the next semester off from school. To keep studying, I want to work on a text based RPG. Do you think this is too large to take on so early? My questions off the bat are how can I emplement a container, like a backpack where the user can check the enventory, or add/drop items. I would like to give the bag a maximum value and then assign each item a value, giving the bag a limit to what it can hold. Also, I would like to incorporate graphics, really .jpgs, but am not sure how to implement them. I would like one at title, and then maybe some at certain points of the game.
    Thanks,
    Brandon


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: beginner w/ text based RPG

    Arrays or ArrayLists might work.

    Arrays if there is a maximum size.

    Adding/removing items needs an ArrayList.

    You just need to make sure the user can't add beyond a certain range.

    ArrayList has a get() method.

    What do you mean by text based? You could always have a JFrame and have them hit buttons to add/remove stuff.

    You could have them add the stuff by typing in a JOptionPane dialog box.

    Let's see what I can do:
    public class Backpack
    {
    private GraphicsWindow gw;
    private ArrayList<DataType> aList;
     
    private class GraphicsWindow
    {
     
    // variables
     
    public GraphicsWindow()
    {
     
    }
     
    }
     
    public Backpack(int size)
    {
    gw = new GraphicsWindow();
     
    aList = new ArrayList<DataType>();
     
     
     
    }
    }

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: beginner w/ text based RPG

    for check inventory

     
    public boolean checkInventory(DataType item)
    if (aList.contains(item))
     
    return true;
     
    else return false;
     
    }

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: beginner w/ text based RPG

    Assuming that each item has a slot, or location. Well, the user would need to know the location, but they can.

    public void addAtSlot(int index, DataType item)
    {
     
    if (index < 0 || index >= aList.size())
    {System.out.println("Cannot add item!");
    return;
    }
    if (aList.get(index) == null)
    {System.out.println("There is an item already here!");
    return;
    }
    if (aList.size() == MAXIMUX_SIZE)
    {
    System.out.println("Your pack is full!");
    return;
    }
    aList.add(index, item);
     
    }
     
    public void add(DataType item)
    {
    if (aList.size() == MAXIMUM_SIZE)
    {
    System.out.println("Your pack is full!");
    return;
    }
    aList.add(item);
    }
     
    public void remove(int index)
    {
    if (index < 0 || index >= aList.size())
    {
    System.out.println("Impossible!");
    return;
    }
     
    if (aList.get(index) == null)
    {
    System.out.println("Cannot remove an item that isn't there!");
    return;
    }
    aList.remove(index);
     
    }

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: beginner w/ text based RPG

    Value as in how many slots it takes up,or value as in price?

  6. The Following User Says Thank You to javapenguin For This Useful Post:

    rbread80 (December 7th, 2010)

  7. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: beginner w/ text based RPG

    sorry, by value i was thinking by weight as in the amount you can carry.Thanks for all the work you've put forth into my project! By text based i meant there's no graphics really. its an old design from when graphics took too much memory. the commands were like "go west" or "take sword" "use sword" "attack skeleton". something along those lines. I figure its mainly going to be a lot of outputted text and if/else and maybe switch statements. I kind of am thinking of making it more along the lines of those choose your own adventures books and let the user have a list of commands to choose from.

    Thanks,
    Brandon

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: beginner w/ text based RPG

    Well, you could always assign each one a weight and if the total was more than the maximum weight, it wouldn't let you add it.

    This might be trickier, but I think it's not that much harder.

    First of all, before they pick up the item, it'd be nice to have all the weights of everything, other than stuff like packs which will have a change in weight.

  9. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,699
    Thanks
    25
    Thanked 334 Times in 310 Posts

    Default Re: beginner w/ text based RPG

    Just for fun, I played around with creating a text choosing thingy, with more or less success.

    Here is what I made, it extends JScrollPane and accepts a Vector<String> as the text choices. I tried to use a JTextArea as the Component to allow line wrapping, but I haven't gotten that to work. If you figure it out, let me know. The code will have to be worked over considerably to be useful for your implementation, but the framework is there.
    import javax.swing.JScrollPane;
    import javax.swing.JList;
    import java.util.Vector;
    import java.awt.Dimension;
    import javax.swing.JTextArea;
    import javax.swing.DefaultListCellRenderer;
    import java.awt.Component;
    import java.awt.Color;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
     
    public class TextChooser extends JScrollPane
    {
    	//Create JList to Hold List of Text
    	JList textList;
     
    	//Vector for JList
    	Vector<JTextArea> text;
     
        public TextChooser(Vector<String> textChoices)
        {
        	//Call to Super Constructor
        	super(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    		//Initalize Vector
        	text = new Vector<JTextArea>();
        	//Add Each Text sent to constructor as a JTextArea
        	for(int i=0;i<textChoices.size();i++)
        	{
        		//Create temporary JTextArea
        		JTextArea temp = new JTextArea(textChoices.get(i));
        		//Wrap Lines
        		temp.setLineWrap(true);
        		//Wrap Words
        		temp.setWrapStyleWord(true);
        		//Add temporary JTextArea to JList's data vector
        		text.add(temp);
        	}
    		//Initalize JList
        	textList = new JList(text);
        	//Add JList's Cell renderer
        	textList.setCellRenderer(new DefaultListCellRenderer(){
        		public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus)
        		{
        			setText(((JTextArea)value).getText());
        			if(isSelected)
        			{
        				setBackground(Color.BLUE);
        				setForeground(Color.WHITE);
        			}
        			else
        			{
        				setBackground(Color.white);
        				setForeground(Color.black);
        			}
        			//return (JTextArea)value;
    				return this;
        		}
        	});
        	//Add JList's Selection Listener
        	textList.addListSelectionListener(new ListSelectionListener(){
        		public void valueChanged(ListSelectionEvent e)
        		{
        			if(!e.getValueIsAdjusting())
        				System.out.println(textList.getSelectedValue());
        		}
        	});
    		//Set Size of JScrollPane
        	super.setPreferredSize(new Dimension(210,60));
    		//Add textList to JScrollPane
        	setViewportView(textList);
        }
     
    	//Method to send the TextChooser a new Vector of Text Options
        public void setText(Vector<String> in)
        {
        	//Reinitalize text Vector
        	text = new Vector<JTextArea>();
        	//Add each String as JTextArea
        	for(int i=0;i<in.size();i++)
        	{
        		JTextArea temp = new JTextArea(in.get(i));
        		temp.setLineWrap(true);
        		temp.setWrapStyleWord(true);
        		text.add(temp);
        	}
        	//Set JList's new Data Vector
        	textList.setListData(text);
        }
    }
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,699
    Thanks
    25
    Thanked 334 Times in 310 Posts

    Default Re: beginner w/ text based RPG

    As far as having a Backpack, you can create a Backpack Object that extends a DataStructure (such as Vector) and has a Max Weight and Current Weight and create an Item Object that holds a Name and Weight. Then you just change the DataStructure's add method to restrict based on weight.

    Here is the code I threw together in the past few minutes that does as I said:
    Backpack:
    import java.util.Vector;
     
    public class Backpack extends Vector<Item>
    {
    	int maxWeight;
    	int currentWeight;
     
        public Backpack(int mw)
        {
    		super();
    		maxWeight = mw;
    		currentWeight = 0;
        }
     
    	public boolean add(Item item)
    	{
    		if(item.weight+currentWeight>maxWeight)
    			return false;
    		super.add(item);
    		currentWeight+=item.weight;
    		return true;
    	}
    }
    Item:
    public class Item
    {
    	String name;
    	int weight;
     
        public Item(String n,int w)
        {
        	name = n;
        	weight = w;
        }
     
        public String toString()
        {
        	return name;
        }
    }


    You can figure out a way to show the Backpack Graphically and you will be good.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  11. #10
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: beginner w/ text based RPG

    Thanks for the help! The beauty of the concept is that there really is no graphics. I just want the player to be able to call for an inventory of what they are carrying.

    Maybe I should have included this earlier, but I asked my instructor today what I might consider and she said possibly if I put them in the same directory i could learn to make my own package, and then access the inventory of that package through import.

    She also said I might look at the File class to import a few jpegs into the game (menu screen & inbetween key events, and I may consider one as a background to the text). I don't know if ya'll feel these may be used successfully, or if there is better options for my use. She just called those out off the top of her head (and as i am unfamiliar with them, I have have no idea of their value for my intent).
    THanks,
    Brandon

  12. #11
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,699
    Thanks
    25
    Thanked 334 Times in 310 Posts

    Default Re: beginner w/ text based RPG

    Ok, well the question that needs to be ask is: where will the jpg files be when the project is finished? The reason I ask is because if you are executing the program from a jar file and the jpg files are packaged in the jar file with the executing class, you must use Buffered Streams instead of the standard File IO in order to read the jpg files. This is a problem that you will most likely not run into while building your project, but you will run into after it has been packaged.

    The other question that needs to be asked is how you want to present the jpgs. You can put images on a ton of components, but some require you to read the images differently than others.

    Here are some examples of a few standard swing elements with images. Have a look at them and decide what the direction you want to go in is. Once you have a plan, tell us what it is and I will try to help you out better than those tutorials will.
    JLabels
    JButtons
    Text Panels
    Paint
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  13. #12
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: beginner w/ text based RPG

    The images aren't really that important I guess if they are going to cause the project to fail, but would be nice. I liked the paint example. What I was thinking was just like when you are playing a video game and they may have an image on the screen while it loads the next level. I know I won't have to load a level, but think the images could be nice. If I want the images in, do I need to plan for that at the start, or could I always add them in as polish to the game?
    Thanks,
    Brandon

  14. #13
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,699
    Thanks
    25
    Thanked 334 Times in 310 Posts

    Default Re: beginner w/ text based RPG

    It is a good idea to plan from the start. The reason is because, depending on how you make your design, it may be very difficult to add a few pictures at the end.

    There are a few things to consider. Mainly, if done correctly, you can deal with any image related issues at runtime. This will require carefully handling possible errors that may occur from failing to read the images and preventing those errors from crashing your program.

    Basically, let's imagine you have your program. While developing your program, you access the images using standard File IO and your images are inside the project's root folder (thus no compile or runtime errors will occur while developing). When you package your program, those images will probably be inside the .jar your program is executed from. The problem is that those images are effectively in a .zip folder and cannot be accessed with standard File IO. If this executable jar is located in the same folder as your images, the program will not have an issue when you attempt to run it. If, however, the program is located somewhere other than where your images are or if your images are moved somewhere else, suddenly you will be getting IOExceptions thrown since the images cannot be found. On the flip side, if you intend on having the images inside your executing jar from the beginning, you can preempt the problems and use a Buffered Stream to the location of the executing .class. That means that when you are developing your program, no errors will occur, and when you package your program with the images, no errors will occur (unless something happens that I cannot predict). If that is the route you want to go, I can provide you with some sample code to help you out. It is actually very simple to do.

    The same goes for any file reading. ALL files, whether images or text files, that will be packaged with the project must be read using Buffered Streams or the program will have a laundry list of potential issues when packaged. It is an unassuming issue that can cause unimaginable amounts of frustration since you can only debug it after packaging.
    Last edited by aussiemcgr; December 4th, 2010 at 02:42 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  15. The Following User Says Thank You to aussiemcgr For This Useful Post:

    rbread80 (December 7th, 2010)

Similar Threads

  1. token-based processing
    By amandat in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2010, 01:05 AM
  2. Some Theory based questions
    By Bacon n' Logic in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: October 1st, 2010, 07:23 PM
  3. Content Based Image Retrieval
    By sanjay_jbp in forum AWT / Java Swing
    Replies: 9
    Last Post: March 1st, 2010, 08:55 PM
  4. Exception-Based Problem
    By Hax007 in forum Exceptions
    Replies: 1
    Last Post: December 10th, 2009, 03:53 AM
  5. Com based component project using Java
    By jazz2k8 in forum Java Native Interface
    Replies: 1
    Last Post: October 7th, 2008, 11:54 PM