Final project for school

Ok, hi. I'm new here and I would like some help on things I don't know how to do with java. I'm creating a java RPG game, respectfully named RPG, and I need some help on these things:

1.) A random number generator. This is needed to apply "level up" stats for attack and defence scores. I have looked online for the script for a random number generator and I couldn't understand it for the love of me. I would like it to be a 'int' varable (yes, i know the script for the RNG is a 'double' varable, but you can use Math.round to round the 'double' varable to a number a 'int' varable can hold) and seperated for attack and defence (I'm thinking that you can just call the RNG method again, but I'm not sure).

2.) A "map", so-to-say. I need a script for a map because whats an RPG without a map? I've tried writing the map code with no prevail. I really need it to be a number-type system with the varables x_move and y_move. I think the upper-left hand corner is 0,0 and it adds 1 to x_move if you move right and subtracts 1 from x_move if you move left. The problem that I ran into is that it would go into negitive numbers, thus crashing the game because it would look for, lets say -1,4, and then, when not found, will crash. I've tried setting the "north" button visibility to false when you couldn't move any farther up, but it still managed to to go negitive from time to time.

3.) Saving/loading the game. I already have a working code for this, but I would like whatever is saved, like numbers, in a number format, like int. I've written the code like this:

atk_str.writeInt()

// atk_str ISN'T a String, it's an Int varable. str means strength

and it would output insainly large numbers, like 192830249328. So currently I have everything saved as a UTF (String) and it seems that everything runs fine

Thanks

[1915 byte] By [Dark_Rulera] at [2007-10-2 16:40:46]
# 1

> 1.) A random number generator. This is needed to

> apply "level up" stats for attack and defence scores.

> I have looked online for the script for a random

> number generator and I couldn't understand it for the

> love of me. I would like it to be a 'int' varable

> (yes, i know the script for the RNG is a 'double'

> varable, but you can use Math.round to round the

> 'double' varable to a number a 'int' varable can

> hold) and seperated for attack and defence (I'm

> thinking that you can just call the RNG method again,

> but I'm not sure).

yes, the Math.random() is a static method or if you use Random use the nextInt or what ever. I found that finding a least common multiple (k) of all of the maximums that I will use and then loading an array with that many elements ranging from 1 to k and then suffling the array (collection), works well for what I have done.

> 2.) A "map", so-to-say. I need a script for a map

> because whats an RPG without a map? I've tried

> writing the map code with no prevail. I really need

> it to be a number-type system with the varables

> x_move and y_move. I think the upper-left hand corner

> is 0,0 and it adds 1 to x_move if you move right and

> subtracts 1 from x_move if you move left. The problem

> that I ran into is that it would go into negitive

> numbers, thus crashing the game because it would look

> for, lets say -1,4, and then, when not found, will

> crash. I've tried setting the "north" button

> visibility to false when you couldn't move any

> farther up, but it still managed to to go negitive

> from time to time.

you need an in-line check on your address values and any value less than your minimum either needs to be set to your minimum or do some wrapping logic where you move to your (maximum - magnitude of the left over after you reach your minimum).

I don't believe you'll get anyone here to do your project for you--especially since it's for school, but post your code, using code tags, and ask specific questions and i'm sure many will be glad to help.

morgalra at 2007-7-13 17:49:24 > top of Java-index,Other Topics,Java Game Development...
# 2

Well, I wasn't asking for anyone to do my code for me, I just need help with some programming for the final project.

Currently, this is my main code (I have other external classes that I programmed for this program):

/*

Jacob Litewski

Final Project

Start Date: 3/12/2006

Finnish Date:

Last Update: 3/20/2006

Version: 0.3.6a

CodeWarrior IDE Conversion: 4/5/06

*/

import java.io.*;

import java.awt.*;

import java.awt.event.*;

public class TrivialApplication extends Frame implements ActionListener //TrivialApplication HAS TO be the name for this project... I cant have any other name because CodeWarrior is a 'School' edition

{

static DataInputStream input; //File Input Code

static DataOutputStream output; //File Output Code

ConfirmBox exit_confirm = new ConfirmBox(this, "Exit", "Do you really want to exit?");

ReadWrite start = new ReadWrite(this, "Start Menu" , "Please select an Option:");

MessageBox read_error = new MessageBox(this, "Error", "The selected File was not found.");

MessageBox read_error_2 = new MessageBox(this, "Error", "The selected File could not be Read.");

MessageBox read_success = new MessageBox(this, "Success", "File Loaded Successfully.");

MessageBox write_error = new MessageBox(this, "Error", "The File could not be Written.");

MessageBox write_success = new MessageBox(this,"Success", "The Game was Saved Successfully.");

MessageBox name_error = new MessageBox(this, "Error", "No File selected.");

//Main Window variables

static Panel item_panel = new Panel();

static Panel button_panel = new Panel();

static Button north_btn = new Button("NORTH");

static Button south_btn = new Button("SOUTH");

static Button east_btn = new Button("EAST");

static Button west_btn = new Button("WEST");

static Panel stats_panel = new Panel();

static TextField stats_txf = new TextField(30);

static Button stats_btn = new Button("Save Game");

static Button stats_btn2 = new Button("Load Game");

static Panel load_panel = new Panel();

static TextField load_txf = new TextField(20);

static Button load_btn = new Button("Load");

static String version = "0.3.6a";

static String file_name;

static String title = "RPG " + version;

//Game File variables

static String ver_nbr; //Game File version

static String cur_HP;

static String max_HP;

static String atk_str;

static String def_str;

static String cur_exp;

static String exp_ned;

static String item1;

static String item1_amt;

static String item2;

static String item2_amt;

static String item3;

static String item3_amt;

static String item4;

static String item4_amt;

static String item5;

static String item5_amt;

public static void main(String[] args)

{

Setup(); //sets up the panels

TrivialApplication window = new TrivialApplication();

window.setTitle(title);

window.setSize(500,250);

window.setVisible(true);

}

public TrivialApplication()

{

setLayout(new BorderLayout());

setBackground(Color.green);

start.setVisible(true); //Works

add(button_panel, BorderLayout.SOUTH);

load_btn.addActionListener(this);

add(stats_panel, BorderLayout.NORTH);

stats_btn.addActionListener(this);//Save button

stats_btn2.addActionListener(this);//Load button

addWindowListener(

new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

exit_confirm.setVisible(true); //Works

}

}

);

}

public static void Setup() //Sets up the main window

{

load_panel.add(load_txf);

load_panel.add(load_btn);

button_panel.setLayout(new BorderLayout());

button_panel.add(north_btn, BorderLayout.NORTH);

button_panel.add(south_btn, BorderLayout.SOUTH);

button_panel.add(east_btn, BorderLayout.EAST);

button_panel.add(west_btn, BorderLayout.WEST);

button_panel.add(load_panel, BorderLayout.CENTER);

stats_panel.add(stats_btn2);

stats_panel.add(stats_txf);

stats_txf.setEditable(false);

stats_panel.add(stats_btn);

return;

}

public void Load() //Load Script

{

file_name = load_txf.getText();

try

{

input = new DataInputStream(new FileInputStream(file_name + ".dat"));

}

catch(IOException ex)

{

read_error.setVisible(true);

}

try

{

ver_nbr = input.readUTF();

cur_HP = input.readUTF();

max_HP = input.readUTF();

atk_str = input.readUTF();

def_str = input.readUTF();

cur_exp = input.readUTF();

exp_ned = input.readUTF();

item1 = input.readUTF();

item1_amt = input.readUTF();

item2 = input.readUTF();

item2_amt = input.readUTF();

item3 = input.readUTF();

item3_amt = input.readUTF();

item4 = input.readUTF();

item4_amt = input.readUTF();

item5 = input.readUTF();

item5_amt = input.readUTF();

}

catch(IOException c)

{

read_error_2.setVisible(true);

}

stats_txf.setText("Health: " + cur_HP + "/" + max_HP + "Exp: " + cur_exp + "/" + exp_ned);

load_panel.setVisible(false);

title = "RPG_" + version + "" + file_name + "_" + ver_nbr;

return;

}

public void Save() //Save Script

{

try

{

output = new DataOutputStream(new FileOutputStream(file_name + ".dat"));

}

catch(IOException ex)

{

write_error.setVisible(true); //Nope, no System.exit(0) here.

}

try

{

output.writeUTF(ver_nbr);

output.writeUTF(cur_HP);

output.writeUTF(max_HP);

output.writeUTF(atk_str);

output.writeUTF(def_str);

output.writeUTF(cur_exp);

output.writeUTF(exp_ned);

output.writeUTF(item1);

output.writeUTF(item1_amt);

output.writeUTF(item2);

output.writeUTF(item2_amt);

output.writeUTF(item3);

output.writeUTF(item3_amt);

output.writeUTF(item4);

output.writeUTF(item4_amt);

output.writeUTF(item5);

output.writeUTF(item5_amt);

}

catch(IOException c)

{

write_error.setVisible(true);

}

write_success.setVisible(true);

return;

}

public void actionPerformed(ActionEvent e)

{

String arg = e.getActionCommand();

if (load_txf.getText().compareTo("")<1)

{

name_error.setVisible(true);

}

else

{

if(arg == "Load")

{

Load();

}

if(arg == "Load Game")

{

load_panel.setVisible(true);

}

if(arg == "Save Game")

{

Save();

}

}

}

}

I hope this helps...

And, please talk in english. I taught myself Java, so I dont know the terminology.

Dark_Rulera at 2007-7-13 17:49:24 > top of Java-index,Other Topics,Java Game Development...
# 3
OK, you posted your code, but you didn't ask any specific questions about the code you posted.You actually have to code up the parts you want help on, or ask how to go about it. We'll not write it for you.
morgalra at 2007-7-13 17:49:24 > top of Java-index,Other Topics,Java Game Development...
# 4

Ok, I updated my code, now I need to know how to use the Math.random() statement. Is it used like this?

double test_dbl = Math.random();

If it is, then can I use it like this?

double atk_up_dbl = (Math.random())/(Math.random());

/*

I've tested this formula on my TI-83+. If you divide a decimal

With another decimal, it has a higher chance of showing a number

above 1 then if you multiply them.

*/

int atk_up_int = Math.round(atk_up_dbl);

atk_str = atk_str + atk_up_int;

\\Since atk_str is an Integer, and you can't add a double to an int

Dark_Rulera at 2007-7-13 17:49:24 > top of Java-index,Other Topics,Java Game Development...
# 5

Ok, now I have a problem. I've rewritten the movement code, which is this:

public void Map()

{

if(UP == true)

{

x = x - 1;

UP = false;

}

else

{

if(DOWN == true)

{

x = x + 1;

DOWN = false;

}

else

{

if(LEFT == true)

{

y = y - 1;

LEFT = false;

}

else

{

if(RIGHT == true)

{

y = y + 1;

RIGHT = false;

}

}

}

}

//Checks the X location

if(0<x && x><10)

{

N = true;

S = true;

}

else

{

if(0<x && x>=10)

{

x = 10;

N = true;

S = false;

}

else

{

if(0>=x)

{

x = 0;

N = false;

S = true;

}

}

}

//Checks the Y location

if(0<y && y><10)

{

E = true;

W = true;

}

else

{

if(0<y && y>=10)

{

y = 10;

E = false;

W = true;

}

else

{

if(0>=y)

{

y = 0;

E = true;

W = false;

}

}

}

ButtonPanel();

loc_x = ""+x;

loc_y = ""+y;

stats_txf.setText("X: " + x + " Y: "+y);

//Save Script

try

{

output = new DataOutputStream(new FileOutputStream(file_name + ".dat"));

}

catch(IOException ex)

{

write_error_1.setVisible(true);

}

try

{

output.writeUTF(ver_nbr);

output.writeUTF(level);

output.writeUTF(cur_HP);

output.writeUTF(max_HP);

output.writeUTF(atk_str);

output.writeUTF(def_str);

output.writeUTF(cur_exp);

output.writeUTF(exp_ned);

output.writeUTF(loc_x);

output.writeUTF(loc_y);

}

catch(IOException c)

{

write_error_2.setVisible(true);

}

Now, I've been working on this all day. The problem is that it doesn't add 1 to x or y! instead, it adds 2, then multiplies by three each time a directional button is pushed. I've tried everything that I could think of, so i need help just getting the code working right. I have it so that it cannot go above 10 or below 0, and the N, S, E, and W booleans controll the visibility of the buttons in a little peice of code i created:

north_btn.setVisible(N);

so I don't need to write the same thing fifty times, and so it cannot go above 10 or below 0 because it hides the buttons. the UP, DOWN, LEFT, and RIGHT booleans were my last ditch attempt to fix the problem, but it didn't work...

the loc_x and loc_y are the String versions of the integers x and y, fyi.

Dark_Rulera at 2007-7-13 17:49:24 > top of Java-index,Other Topics,Java Game Development...
# 6

>Ok, I updated my code, now I need to know how to use the Math.random() statement. Is it used like this?

>double test_dbl = Math.random();

I think from your earlier posts, you actually wanted an int random number anyway rather than a double, so rather than using Math.random(), you'd be better to use Random.nextInt(max) which will return you a random int from 0 .. max - 1.

So do this:Random rnd = new Random();

...

// Need a number from 0 .. 19

int myRandomNumber = rnd.nextInt(20);

// Need a number from 5 .. 95

int anotherRandomNumber = rnd.nextInt(90) + 5;

// Need a number from 1 .. 10

int randomNumberFrom1To10 = rnd.nextInt(10) + 1;

Remember you only need to instantiate one Randowm, and then you can use it lots of times.

Regards,

Tim

TimRyanNZa at 2007-7-13 17:49:24 > top of Java-index,Other Topics,Java Game Development...
# 7
Mutter mutter mutter - I made that same typo five times in the post and only caught it four times...sp /Randowm/Random/
TimRyanNZa at 2007-7-13 17:49:24 > top of Java-index,Other Topics,Java Game Development...
# 8

I suspect you are confusing yourself by using an unusual order for your operands in your comparisons.

This one probably does what you want...

> //Checks the X location

> if(0<x && x><10)

But I bet these ones don't...

> if(0<x && x>=10)

> if(0<y && y>=10)

I would strongly suggest you always put the operands in the order you would use when you say the equation out loud. For example this:

> if(0<x && x><10)

is probably meant to be "If x is greater than zero and x is less than ten", so why not write it that way...

if (x > 0 && x < 10)

This

> if(0<x && x>=10)

could be translated into English as "if X is greater than 0 and x is greater than or equal to 10"

I bet that wasn't what you really wanted! I can't actually guess what you meant here, but maybe it was

"if x is less that or equal to zero or x is greater than or equal to 10"

if (x <= 0 || x >= 10)

Also I saw:

//Checks the X location

if(0<x && x><10)

which shouldn't even compile! Although it doesn't look like that in my original, so maybe it's the forum software adding that rogue > sign.

I think it would help to say "out loud" to yourself what you want for each "if" statement in that whole method and then code what you said. Once you've done that, retry the program and post the updated code with any further questions.

Good luck,

Tim

TimRyanNZa at 2007-7-13 17:49:24 > top of Java-index,Other Topics,Java Game Development...
# 9

> //Checks the X location

> if(0<x && x><10)

> which shouldn't even compile! Although it doesn't

> look like that in my original, so maybe it's the

> forum software adding that rogue > sign.

Wow, thats weird... it should be:

if(0<x && [b]x><10[/b])

I didn't accually see that intill you pointed that out to me.

Well, that code does work, but it doesn't just add one. it doubles the value, which i don't want...

It may be something with the way i have it save. currently, I have it save everytime you press a directional button... could it be doubling itself because of that? I'll remove the save code from that part of the script and see if thats the problem.

On another note, I got the Random Number generator up. This is the code:

double def_gain = (Math.random()*5+1);

int def_str_temp = Integer.parseInt(def_str);

def_str = ""+ Math.round(def_str_temp + def_gain);

Now, since all of the charater stats are in UTF form, I have to convert the new stats to a string, which is done above. pretty ingenious, huh?

If you can help figure out what is wrong with the movement system, i'll be greatful.

Dark_Rulera at 2007-7-13 17:49:24 > top of Java-index,Other Topics,Java Game Development...
# 10

> > //Checks the X location

> > if(0<x && x><10)

//sorry, typo...

> > which shouldn't even compile! Although it doesn't

> > look like that in my original, so maybe it's the

> > forum software adding that rogue > sign.

>

> Wow, thats weird... it should be:

> if(0<x && [b]x><10[/b])

>

> I didn't accually see that intill you pointed that

> out to me.

>

> Well, that code does work, but it doesn't just

> add one. it doubles the value, which i don't want...

>

> It may be something with the way i have it save.

> currently, I have it save everytime you press a

> directional button... could it be doubling itself

> because of that? I'll remove the save code from that

> part of the script and see if thats the problem.

>

> On another note, I got the Random Number generator

> up. This is the code:

> > double def_gain = (Math.random()*5+1);

> int def_str_temp = Integer.parseInt(def_str);

> def_str = ""+ Math.round(def_str_temp + def_gain);

>

> Now, since all of the charater stats are in UTF form,

> I have to convert the new stats to a string, which is

> done above. pretty ingenious, huh?

>

> If you can help figure out what is wrong with the

> movement system, i'll be greatful.

Dark_Rulera at 2007-7-13 17:49:24 > top of Java-index,Other Topics,Java Game Development...
# 11
As I said previously, I really recommend you rework all of the "if" statements like I described, and then repost your code so we can have another look at it.Regards,Tim
TimRyanNZa at 2007-7-13 17:49:24 > top of Java-index,Other Topics,Java Game Development...
# 12

Oh... my... god...

I figured out why my buttons weren't working right... and it didn't have anything to do with my if statements...

import java.io.*;

import java.awt.*;

import java.awt.event.*;

public class TrivialApplication extends Frame implements ActionListener

{

static DataInputStream input; //File Input Code

static DataOutputStream output; //File Output Code

ConfirmBox exit_confirm = new ConfirmBox(this, "Exit", "Do you really want to exit?");

MessageBox read_error = new MessageBox(this, "Error", "The selected File was not found.");

MessageBox read_error_2 = new MessageBox(this, "Error", "The selected File could not be Read.");

MessageBox read_success = new MessageBox(this, "Success", "File Loaded Successfully.");

MessageBox write_error_1 = new MessageBox(this, "Error", "The File could not be Found.");

MessageBox write_error_2 = new MessageBox(this, "Error", "The File cound not be Written.");

MessageBox write_success_1 = new MessageBox(this,"Success", "The Game was Saved Successfully.");

MessageBox write_success_2 = new MessageBox(this,"Success", "The Game was Created Successfully.");

MessageBox name_error = new MessageBox(this, "Error", "No File selected.");

//Main Window variables

static Panel item_panel = new Panel();

static Panel button_panel = new Panel();

static Button north_btn = new Button("NORTH");

static Button south_btn = new Button("SOUTH");

static Button east_btn = new Button("EAST");

static Button west_btn = new Button("WEST");

static Panel stats_panel = new Panel();

static TextField stats_txf = new TextField(30);

static Button stats_btn = new Button("Save Game");

static Button stats_btn2 = new Button("Load Game");

static Panel load_panel = new Panel();

static TextField load_txf = new TextField(20);

static Button load_btn = new Button("Load");

static Button create_btn = new Button("Create");

static String version = "0.8.0a";

static String file_name;

static String title = "RPG "+version;

int x;

int y;

boolean N = true; //North Boolean

boolean S = true; //South Boolean

boolean E = true; //East Boolean

boolean W = true; //West Boolean

boolean UP = false; // North Boolean2

boolean DOWN = false; //South Boolean2

boolean LEFT = false; //West Boolean2

boolean RIGHT = false; //East Boolean2

//Game File variables

static String ver_nbr; //Game File version

static String level;//Character level

static String cur_HP;

static String max_HP;

static String atk_str;

static String def_str;

static String cur_exp;

static String exp_ned;

static String loc_x; //Map Location(x)

static String loc_y; //Map Location(y)

public static void main(String[] args)

{

TrivialApplication window = new TrivialApplication();

window.setTitle(title);

window.setSize(500,250);

window.setVisible(true);

}

public TrivialApplication()

{

Setup(); //sets up the panels

ButtonPanel();

setLayout(new BorderLayout());

setBackground(Color.green);

add(button_panel, BorderLayout.SOUTH);

load_txf.requestFocus();

load_btn.addActionListener(this);

create_btn.addActionListener(this);

add(stats_panel, BorderLayout.NORTH);

stats_btn.addActionListener(this);//Save button

stats_btn2.addActionListener(this); //Load button

addWindowListener(

new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

exit_confirm.setVisible(true); //Works

}

}

);

}

public void Setup() //Sets up the main window

{

load_panel.add(create_btn);

load_panel.add(load_txf); //Game Name TextField

load_panel.add(load_btn);

stats_panel.add(stats_btn2);

stats_panel.add(stats_txf);

stats_txf.setEditable(false);

stats_panel.add(stats_btn);

return;

}

public void ButtonPanel()

{

//This peice of code is what was messing up my directional buttons...

button_panel.setLayout(new BorderLayout());

button_panel.add(north_btn, BorderLayout.NORTH);

north_btn.addActionListener(this);

north_btn.setVisible(N);

button_panel.add(south_btn, BorderLayout.SOUTH);

south_btn.addActionListener(this);

south_btn.setVisible(S);

button_panel.add(east_btn, BorderLayout.EAST);

east_btn.addActionListener(this);

east_btn.setVisible(E);

button_panel.add(west_btn, BorderLayout.WEST);

west_btn.addActionListener(this);

west_btn.setVisible(W);

button_panel.add(load_panel, BorderLayout.CENTER);

return;

}

public void Load() //Load Script

{

file_name = load_txf.getText(); //Gets the File Name

try

{

input = new DataInputStream(new FileInputStream(file_name+".dat"));

}

catch(IOException ex)

{

read_error.setVisible(true);

}

try

{

ver_nbr = input.readUTF();

level = input.readUTF();

cur_HP = input.readUTF();

max_HP = input.readUTF();

atk_str = input.readUTF();

def_str = input.readUTF();

cur_exp = input.readUTF();

exp_ned = input.readUTF();

loc_x = input.readUTF();

loc_y = input.readUTF();

}

catch(IOException c)

{

read_error_2.setVisible(true);

}

Check(); //The Check script

Map(); //The Map Script

stats_txf.setText("X: " + x + " Y: "+y); //Debug

//stats_txf.setText("Lvl: "+level+" Health: "+ cur_HP +"/"+ max_HP+"Exp: "+cur_exp+"/"+exp_ned);

load_panel.setVisible(false);

return;

}

public void Save() //Save Script

{

try

{

output = new DataOutputStream(new FileOutputStream(file_name+".dat"));

}

catch(IOException ex)

{

write_error_1.setVisible(true);

}

try

{

output.writeUTF(ver_nbr);

output.writeUTF(level);

output.writeUTF(cur_HP);

output.writeUTF(max_HP);

output.writeUTF(atk_str);

output.writeUTF(def_str);

output.writeUTF(cur_exp);

output.writeUTF(exp_ned);

output.writeUTF(loc_x);

output.writeUTF(loc_y);

}

catch(IOException c)

{

write_error_2.setVisible(true);

}

write_success_1.setVisible(true);

return;

}

public void Check() //This should check if the current experence is over experence needed

{

//Seems to work now...

//May add a FileVersionCheck

int cur_exp_chk = Integer.parseInt(cur_exp);

int exp_ned_chk = Integer.parseInt(exp_ned);

if (cur_exp_chk >= exp_ned_chk)

{

LevelUp();

}

else

{

return;

}

}

public void Create() //coded 4/12/2006

{

file_name = load_txf.getText();

ver_nbr = "2.1.0";

level = "1";

cur_HP = "10";

max_HP = "10";

atk_str = "10";

def_str = "10";

cur_exp = "0";

exp_ned = "100";

loc_x = "0";

loc_y = "0";

try

{

output = new DataOutputStream(new FileOutputStream(file_name+".dat"));

}

catch(IOException ex)

{

write_error_1.setVisible(true);

}

try

{

output.writeUTF(ver_nbr);

output.writeUTF(level);

output.writeUTF(cur_HP);

output.writeUTF(max_HP);

output.writeUTF(atk_str);

output.writeUTF(def_str);

output.writeUTF(cur_exp);

output.writeUTF(exp_ned);

output.writeUTF(loc_x);

output.writeUTF(loc_y);

}

catch(IOException c)

{

write_error_2.setVisible(true);

}

write_success_2.setVisible(true);

return;

}

public void MapSetup()

{

x = Integer.parseInt(loc_x);

y = Integer.parseInt(loc_y);

return;

}

public void Map() //Coded 4/11/2006

{

//Works

//Going to remove soon... no need for it anymore

if(UP == true)

{

x = x - 1;

UP = false;

}

else

{

if(DOWN == true)

{

x = x + 1;

DOWN = false;

}

else

{

if(LEFT == true)

{

y = y - 1;

LEFT = false;

}

else

{

if(RIGHT == true)

{

y = y + 1;

RIGHT = false;

}

}

}

}

//Checks the X location

//Works

if(0 < x && x < 10)

{

N = true;

S = true;

}

else

{

if(0 < x && x >= 10)

{

x = 10;

N = true;

S = false;

}

else

{

if(0 >= x)

{

x = 0;

N = false;

S = true;

}

}

}

//Checks the Y location

//Works

if(0 < y && y < 10)

{

E = true;

W = true;

}

else

{

if(0 < y && y >= 10)

{

y = 10;

E = false;

W = true;

}

else

{

if(0 >= y)

{

y = 0;

E = true;

W = false;

}

}

}

//The *improved* button code

north_btn.setVisible(N);

south_btn.setVisible(S);

east_btn.setVisible(E);

west_btn.setVisible(W);

loc_x = ""+x;

loc_y = ""+y;

stats_txf.setText("X: " + x + " Y: "+y); //Debug

//Save Script

try

{

output = new DataOutputStream(new FileOutputStream(file_name + ".dat"));

}

catch(IOException ex)

{

write_error_1.setVisible(true);

}

try

{

output.writeUTF(ver_nbr);

output.writeUTF(level);

output.writeUTF(cur_HP);

output.writeUTF(max_HP);

output.writeUTF(atk_str);

output.writeUTF(def_str);

output.writeUTF(cur_exp);

output.writeUTF(exp_ned);

output.writeUTF(loc_x);

output.writeUTF(loc_y);

}

catch(IOException c)

{

write_error_2.setVisible(true);

}

return;

}

public void LevelUp() //coded 4/11/2006

{

//Level Script

int level_gain = Integer.parseInt(level);

level = ""+ (level_gain+1);

//Experence Script

double exp_gain = (Math.random()*2+1);

int exp_ned_temp = Integer.parseInt(exp_ned);

exp_ned = ""+ Math.round(exp_ned_temp*exp_gain);

//Attack Script

double atk_gain = (Math.random()*5+1);

int atk_str_temp = Integer.parseInt(atk_str);

atk_str = ""+ Math.round(atk_str_temp + atk_gain);

//Defence Script

double def_gain = (Math.random()*5+1);

int def_str_temp = Integer.parseInt(def_str);

def_str = ""+ Math.round(def_str_temp + def_gain);

//HP Script

double HP_gain = (Math.random()*8+1);

int HP_max_temp = Integer.parseInt(max_HP);

int HP_cur_temp = Integer.parseInt(cur_HP);

max_HP = ""+ Math.round(HP_max_temp + HP_gain);

cur_HP = ""+ Math.round(HP_cur_temp + HP_gain);

//Save Script

try

{

output = new DataOutputStreamnew FileOutputStream(file_name + ".dat"));

}

catch(IOException ex)

{

write_error_1.setVisible(true);

}

try

{

output.writeUTF(ver_nbr);

output.writeUTF(level);

output.writeUTF(cur_HP);

output.writeUTF(max_HP);

output.writeUTF(atk_str);

output.writeUTF(def_str);

output.writeUTF(cur_exp);

output.writeUTF(exp_ned);

output.writeUTF(loc_x);

output.writeUTF(loc_y);

}

catch(IOException c)

{

write_error_2.setVisible(true);

}

return;

}

public void actionPerformed(ActionEvent e) //Updated 4/19/2006

{

String arg = e.getActionCommand();

if(arg == "Load")

{

if (load_txf.getText().compareTo("")<1)

{

name_error.setVisible(true);

}

else

{

Load();

}

}

if(arg == "Create")

{

if (load_txf.getText().compareTo("")<1)

{

name_error.setVisible(true);

}

else

{

Create();

MapSetup();

}

}

if(arg == "Load Game")

{

load_panel.setVisible(true);

}

if(arg == "Save Game")

{

Save();

}

//The Direction Buttons

//Works

if(arg == "NORTH")

{

UP = true;

DOWN = false;

LEFT = false;

RIGHT = false;

arg = "";

Map();

}

else

{

if(arg == "SOUTH")

{

UP = false;

DOWN = true;

LEFT = false;

RIGHT = false;

arg = "";

Map();

}

else

{

if(arg == "EAST")

{

UP = false;

DOWN = false;

LEFT = false;

RIGHT = true;

arg = "";

Map();

}

else

{

if(arg == "WEST")

{

UP = false;

DOWN = false;

LEFT = true;

RIGHT = false;

arg = "";

Map();

}

else

{

return;

}

}

}

}

}

}

gha... thats a lot of code... But the directional buttons work the way they should. apparently, calling ButtonPanel() while inside the Map() script caused the computer to read the Map() script twice, adding 2 + x each time a button was pressed (note, if x = 2, then it would add 2 but take away 4 if another button was pressed)

version 0.8.1a will implement the new(er) button code, so it doesn't take up so much room. I don't know if I want to change the random Number Generators i have in my script... they have been good to me and take up a lot less space then the Random() script would have.

so, now, my next project is the monster script (yay). I don't know in god's name i would be able to do that, but i'll try... Im thinking that I may use mon_x = Math.round(Math.random(mon_x_temp)*10);

to figure out the Monsters X & Y cords, but the attacking script and the damage script will be uber hard to do...

Well, intill next time...

Dark_Rulera at 2007-7-13 17:49:24 > top of Java-index,Other Topics,Java Game Development...
# 13

Some really interesting code. Just to point out some things you might want to consider int he future. Camelcase it typically the standard nowadays...Class.methodName()it makes things a litle more easy to read. A great job for a first try at an RPG though. You might want to look at my project for interest sake. http://individual.utoronto.ca/Phibred/LoG.htm

My e-mail address is Phibred@hotmail.com if you want to discuss RPG engine theory, or if you want to contribute :p.

Phibreda at 2007-7-13 17:49:24 > top of Java-index,Other Topics,Java Game Development...
# 14

Well, thanks Phibred. Um, is there anything wrong with my save/load script that anyone can see? I need to create a new file each time i start, or it errors out saying that the file is not found... The file is where it should be, in the main root of my project file (ie c:/java/final/) but it can't find it... I can't see any callMethods conflicts, but I'm also new at java programming to, so I can't tell 100% of the time.

Well, yea, once all of my script works, I'm going to dump all of the internal methods (i.e. Save(), Load(), Create()) into another file, and since this is just the Alpha version, I want all the code in one place so it's eaiser to debug.

I would also contact you, Phibred, but Yahoo is blocked at my school and I have to get my grades up to go on the internet during the weekend.

Dark_Rulera at 2007-7-13 17:49:24 > top of Java-index,Other Topics,Java Game Development...
# 15

Ok, new question, how do you create Text Areas? My Programming 101 book doesn't show me how to create a Text Area, but shows one in the book (dumb, i know). Also, I want to use the Text Area as a GUI. Sounds weird, yes, but the purpose of the Text Area (intill I figure out how the JPanel SuperClass works) is to tell the user what he/she did, kinda like the Text Area in Runescape. And, just to let you know, for everyone that helped me so far and in the future, will be mentioned in the game, and DukeDollars will be rewarded for people that will(have) help(ed) me.

And, to ask in advance, is their anyway to code it so it could return (like hitting enter) to the next line? That would be helpful.

Dark_Ruler

Dark_Rulera at 2007-7-20 23:01:46 > top of Java-index,Other Topics,Java Game Development...
# 16

I see that you are relatively new to Java programming, and so please tell me if you don't understand. You are using AWT for you windowing toolkit, so you will use the TextArea class to create a text area. Try the following as you please:

TextArea ta = new TextArea(numRows, numColumns);

add(ta);

note that numRows is the number of text rows you would like to allow, and numColumns is the number of columns you would like to allow. I'm also going to suggest a very helpful resource, just in case you don't already have it available. You may not find it of much use now, but I believe that if you continue in Java programming, that it will become your friend. It is the Java api specification:

http://java.sun.com/j2se/1.5.0/docs/api/

good luck and cheers.

Nathan

nathanlanea at 2007-7-20 23:01:46 > top of Java-index,Other Topics,Java Game Development...
# 17

> I see that you are relatively new to Java

> programming, and so please tell me if you don't

> understand. You are using AWT for you windowing

> toolkit, so you will use the TextArea class to create

> a text area. Try the following as you please:

> > TextArea ta = new TextArea(numRows, numColumns);

> add(ta);

>

> note that numRows is the number of text rows you

> would like to allow, and numColumns is the number of

> columns you would like to allow. I'm also going to

> suggest a very helpful resource, just in case you

> don't already have it available. You may not find it

> of much use now, but I believe that if you continue

> in Java programming, that it will become your friend.

> It is the Java api specification:

>

> http://java.sun.com/j2se/1.5.0/docs/api/

>

> good luck and cheers.

> Nathan

Ok, I figured that out on a stroke of genius...

Well, maybe not genius, but since TextField makes a text field, then TextArea would make a text area.

Ok, can I have numRows and numColumns equal i, which starts out as 1, but increases by one each time something happens? For example:

TextArea ta = new TextArea(i, i);

if(ckb_1.getState == true)

{ i++

}

That would add one to the column and row, right?

Dark_Rulera at 2007-7-20 23:01:46 > top of Java-index,Other Topics,Java Game Development...
# 18

Here is some sample code that rolls a 6 sided die for you. If this is helpful, please assign me some Duke Dollars! (:

public class Class1

{

public static void main(String[] argv)

{

System.out.println(rollDice(6));

}

public static int rollDice(int sides)

{

return (int) (Math.random() * 6) + 1;

}

}

fryeta at 2007-7-20 23:01:46 > top of Java-index,Other Topics,Java Game Development...
# 19

I personally use JTextArea, so I know little about TextArea. It appears that you want the size of the window to expand as you have more data to be displayed, which I believe might be more painful than is necessary. For a JTextArea, I put it in a JScrollPane which then allows a user to scroll through what is said. Here is some sample code to give you an idea:

JTextArea tfResults = new JTextArea();

JScrollPane jScrollPane2 = new JScrollPane();

jScrollPane2.setBounds(new Rectangle(5, 265, 535, 265));

jScrollPane2.getViewport().add(tfResults, null);

this.getContentPane().add(jScrollPane2, null);

Not sure how much that code will help you, but it is a start.

One piece of advice. When I am stuck with how to do something new in Java, the first thing I do is go to Google and type "Java" and then the item that I need more info, like "Java TextArea". Usually the Sun JavaDocs is the first thing that comes up, and from there I can usually get some basic instruction and examples on how to use a new class.

fryeta at 2007-7-20 23:01:46 > top of Java-index,Other Topics,Java Game Development...
# 20

> Here is some sample code that rolls a 6 sided die for

> you. If this is helpful, please assign me some Duke

> Dollars! (:

>

> public class Class1

> {

>

> public static void main(String[] argv)

> {

>System.out.println(rollDice(6));

>

>

> public static int rollDice(int sides)

> {

>return (int) (Math.random() * 6) + 1;

>

> }

funny, but no. I don't need a six-sided dice for my RPG.

> I personally use JTextArea, so I know little about

> TextArea. It appears that you want the size of the

> window to expand as you have more data to be

> displayed, which I believe might be more painful than

> is necessary. For a JTextArea, I put it in a

> JScrollPane which then allows a user to scroll

> through what is said. Here is some sample code to

> give you an idea:

>

> JTextArea tfResults = new JTextArea();

> JScrollPane jScrollPane2 = new JScrollPane();

> jScrollPane2.setBounds(new Rectangle(5, 265, 535,

> 265));

> jScrollPane2.getViewport().add(tfResults, null);

> this.getContentPane().add(jScrollPane2, null);

>

> Not sure how much that code will help you, but it is

> a start.

>

> One piece of advice. When I am stuck with how to do

> something new in Java, the first thing I do is go to

> Google and type "Java" and then the item that I need

> more info, like "Java TextArea". Usually the Sun

> JavaDocs is the first thing that comes up, and from

> there I can usually get some basic instruction and

> examples on how to use a new class.

Yea, I'm staying away from anything with a J in front of it, basically because it's more work that I don't need to do.

Dark_Rulera at 2007-7-20 23:01:46 > top of Java-index,Other Topics,Java Game Development...
# 21
FYI - That code can provide more random numbers than just 1-6. Plug in a different number and it will provide you a random number between 1 and that number. If you want between 0 and that number minus 1, remove the + 1 to the operation in the method.
fryeta at 2007-7-20 23:01:46 > top of Java-index,Other Topics,Java Game Development...
# 22
Yes I know that, Thats how my Levelup code works.
Dark_Rulera at 2007-7-20 23:01:46 > top of Java-index,Other Topics,Java Game Development...
# 23

Ok, im in a snag... I cant get my load code to work. It only works when you create a new file via the create code, then load it. I was looking into the FileReader method for the DataInputStream method, but I have no clue how to code it, or if that will even work. Is there anything I can do to fix this, because I would really like for my application to load an all ready made gamesave, and I can't figure out how to fix it. It may be my load/save code, because that would be my first guess... If you need to see the code, just say so with your e-mail, because it's over 600 lines right now and I don't want to post that here and take up that much space. Thanks

I'll also give points to whomever helps me fix this problem.

Dark_Rulera at 2007-7-20 23:01:46 > top of Java-index,Other Topics,Java Game Development...
# 24

Ok, I finally finished my game (Current Version: 1.1.8r2), now I need some help with something I cannot figure out...

How would you code a MenuBar for a Java Application in a Frame? I tried yesterday to figure it out, only getting as far as this:

Menu main_menu = new Menu();

//or

MenuItem main_menu = new MenuItem();

Now, they are both called main_menu because i only used one at a time, i didn't have both called at the same time. Their is also something about MenuContainer and MenuCheckboxItem and MenuBar, but they stumped me. I'll give out at least 5 DD to who can help me figure out how the heck to code this. Thank you.

Also, if you want to test it on my code (since it's nearly 1000 lines of code, and I don't want to post it on here because of that) send me an E-mail and i'll send the code (zipped with an DOS execute file within the zip file that would autocompile and autorun the java code, if your running windows). This is optional, because I know you don't like to do the work for other people. I'll keep you updated.

Dark_Ruler

Dark_Rulera at 2007-7-20 23:01:46 > top of Java-index,Other Topics,Java Game Development...
# 25

public void Map() {

if (UP == true) {

if ((x-1) >= 0) {

x = x - 1;

}

else {

x = 0;

N = false;

}

UP = false;

}

Here I'm just showing what I would do. I just did this first part as an example. Down, left, and right would be similar. Instead of checking that the x value is out of bounds separately, I would check it before you take action. So in this example, if the user wants to go up it will check that x is greater than zero first and if it isn't then it will be set to zero and the north button will be disabled. You would have to add in something in the down section where the north button is re-enabled though. I hope this makes sense.

Edited to add that I being particularly unsmart today only read the first page, not realizing there were 3 WHOLE PAGES to read. I hope this can help someone else though.

Message was edited by:

KittieMalice

KittieMalicea at 2007-7-20 23:01:46 > top of Java-index,Other Topics,Java Game Development...
# 26

Alright now that I've read through all 3 pages. I have somewhat of an RPG I'm working on too although I've been too busy for it lately. I know yours is done already, but I've implemented a textbox that only appears when you press enter. Then you type your message in and press enter again and it will close the textbox and process the input message. Let me know if you want to see how I did this because I have it at home so I can't post it right now.

I also have a program similar to textpad that I had to create for an assignment. I wasn't required to finish it so it isn't, but the point of it was to learn menus. If you want I can email it to you and hopefully you can figure out how menus work. It's a lot of code because apparently menus take quite a bit of coding. Here is just a sample of one menu item in the file:

//Add Save to the file menu and functionality.

public JMenuItem createFileSaveItem() {

JMenuItem item = new JMenuItem("Save");

item.setFont(menuFont);

item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));

class MenuItemListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

PrintWriter outFile;

try {

outFile = new PrintWriter(new FileWriter(lastSaved));

outFile.println(textArea.getText());

outFile.close();

}

catch (IOException ev) {

System.err.println("error in FileReader.txt " + e.toString());

System.exit(1);

}

}

}

ActionListener listener = new MenuItemListener();

item.addActionListener(listener);

return item;

}

KittieMalicea at 2007-7-20 23:01:46 > top of Java-index,Other Topics,Java Game Development...
# 27

Hi,

If you want a MenuBar, perhaps inserting some modification of the following into wherever you build your GUI might help:

//Name stuff for later use:

MenuBar bar = new MenuBar();

MenuItem item;

Menu menu;

//Create the first menu:

menu = new Menu("Menu 1");

//Create the first item to go into ``Menu 1''

item = new MenuItem("Menu Item 1");

item.setActionCommand("Item1");

item.addActionListener(this);

menu.add(item);

//...Create more items by copying and pasting the above code [omitted for the sake of concision]

//Add the first menu to the MenuBar:

bar.add(menu);

//Create the second menu:

menu = new Menu("Menu 2!");

//...Create menu items as above and then, when that's done, add this second menu to the menu bar by using:

bar.add(menu);

//When you've created all your menus, add the menu bar to the Frame by using:

myFrame.setMenuBar(bar);

Later, in your actionPerformed method you can have

if(e.getActionCommand().equals("Item1")

{

Do whatever you want to do when item 1 is pressed.

}

Of course, if your program uses another class that implements ActionListener (rather than implementing it in your main class), you'll need to use this as the parameter in your call to addActionListener

.

I'm a bit pressed for time at the moment, so I wasn't able to examine all your code, but I will do so if you need further assistance with the menus.

xplodesa at 2007-7-20 23:01:46 > top of Java-index,Other Topics,Java Game Development...
# 28
Thank you, xplodes. After I looked around the net for a bit, i found the JMenuBar and how thats coded, so i took the J out of it and I got a semi-working MenuBar. I'm going to give you the 5 DD anyway, since I didn't find out how to add actionlisteners to it.
Dark_Rulera at 2007-7-20 23:01:47 > top of Java-index,Other Topics,Java Game Development...
# 29

I think the biggest problem is your foundation with the language. I'm not trying to be mean, but if you're in the class then what are they teaching? If you didn't know Random and basic File I/O stuff, why not work on TicTacToe or a smaller project?

I'm not saying you're not a good programmer, or that you can't do it. But, speaking from experience I can say that you will kick your own butt later because of the new level you'll be at. You'll look back and be like "wow, I can do that so much better now." The sad part is that you will always look back and say that.

The only reason I'm saying all of this is because I spent like 2 years programming very sloppily, and didn't care about comments, modularity, or any of that good stuff. To be honest, it still feels like wasted time.

Regardless, off my soap box:

Your program is action driven. That will get sticky once you get everything you want moving at once. I'd recommend using a main while loop... something like:

while(!finished){

...

}

Also, basically each game loop should function something like this:

private boolean[] keys;

private boolean finished;

. . .

while(!finished)[

processInput(); // checks all mouse stuff, and keyboard entries

checkCases(); // after all your characters and stuff is updated

// then it kills any units at HP with value of 0, ect

drawWorld(g); // draws your world using the graphics object

}

. . .

public void keyPressed(KeyEvent e){

int key = e.getKeyCode();

if(key==KeyEvent.VK_UP)

keys[0]=true;

. . . //do that for each key you want to watch

}

public void keyReleased(KeyEvent e){

// do the same thing but keys[i]=false;

. . .

}

//rest of your mouse stuff should be here, and use booleans to keep track of it all, or actually integers as a 'stage' thing. Like stage 0 is the title screen, 1 is load or new game, ect... whatever.

Hope that helps some. I know that programming properly from the start makes it much easier to update, maintain, and code in general. Looking good so far though. Hope this RPG kicks butt and gets an A. =)

Sangea at 2007-7-20 23:01:47 > top of Java-index,Other Topics,Java Game Development...
# 30

> I think the biggest problem is your foundation with

> the language. I'm not trying to be mean, but if

> you're in the class then what are they teaching? If

> you didn't know Random and basic File I/O stuff, why

> not work on TicTacToe or a smaller project?

>

> I'm not saying you're not a good programmer, or that

> you can't do it. But, speaking from experience I can

> say that you will kick your own butt later because of

> the new level you'll be at. You'll look back and be

> like "wow, I can do that so much better now." The

> sad part is that you will always look back and say

> that.

>

> The only reason I'm saying all of this is because I

> spent like 2 years programming very sloppily, and

> didn't care about comments, modularity, or any of

> that good stuff. To be honest, it still feels like

> wasted time.

>

> Regardless, off my soap box:

>

> Your program is action driven. That will get sticky

> once you get everything you want moving at once. I'd

> recommend using a main while loop... something like:

>

> while(!finished){

> ...

> }

>

> Also, basically each game loop should function

> something like this:

>

> > private boolean[] keys;

> private boolean finished;

>

> . . .

>

> while(!finished)[

> processInput(); // checks all mouse stuff,

> and keyboard entries

> checkCases(); // after all your characters

> and stuff is updated

> // then it kills

> any units at HP with value of 0, ect

> drawWorld(g); // draws your world using the

> graphics object

>

> . .

>

> public void keyPressed(KeyEvent e){

>int key = e.getKeyCode();

> if(key==KeyEvent.VK_UP)

>keys[0]=true;

> . . . //do that for each key you want to watch

> }

>

> public void keyReleased(KeyEvent e){

>// do the same thing but keys[i]=false;

> . .

> }

>

> //rest of your mouse stuff should be here, and use

> booleans to keep track of it all, or actually

> integers as a 'stage' thing. Like stage 0 is the

> title screen, 1 is load or new game, ect... whatever.

>

>

>

>

> Hope that helps some. I know that programming

> properly from the start makes it much easier to

> update, maintain, and code in general. Looking good

> so far though. Hope this RPG kicks butt and gets an

> A. =)

Well, Sange, They didn't teach me any File I/O stuff or Random stuff because it's Intro to Java Programming. I taught myself the Random and File I/O stuff. Thats why I'm asking questions, to see if what I'm doing is right. I know my programming language is different, because my teacher wants it to be 'readable'.

Well, good news, I FINALLY fixed my blasted Save/Load/Create scripts so they now will load a game WITHOUT creating a new one first. Thank you FileDialog!

And yes, my program is action driven because I haven't had anytime to completely rewrite my code to do otherwise. When school is over and I get my labtop back, I'm going to rewrite it to programming standards, like cammelCase, ect.

Dark_Rulera at 2007-7-20 23:01:51 > top of Java-index,Other Topics,Java Game Development...