I'd previously posted about integrating a store into an RPG game for a homework assignment. I had programmed the store into the game and given the player the ability to "enter" the store.
I ran into a small problem when getting to the next step. How did I want to set up the menu, and did I want to limit the choices of armor and weapons between the classes? For instance, should a wizard be able to buy steel armor meant for a fighter? The wizard class has little health and armor, but has access to a lot of magic points to be able to cast healing spells and powerful damage dealing fireballs. I decided that heavy armor was just too much to allow them.
I decided to go with a different kind of method for solving this problem. I could have made everything visible to every class, but only allowed them to buy certain things, but I decided this wasn't necessary.
I am going to hard code the inventory into the store in the background, and display the correct items to each class. When the player picks their class at the beginning of the game, the program will be told to load those items into the store's displayed inventory.
That means that when you pick a wizard, and visit the store, you will be presented with an array of staves and robes, and when you pick a fighter, you will be able to choose from metal breastplates and swords. :D
Tuesday, April 1, 2014
Monday, March 24, 2014
Operator Overloading
I've been learning something really cool.
Basically, C++ has some built in operators. These can manipulate the data in the program. For instance, there are mathematical operators. For instance, if I tell my program:
5 + 5
It will return 10.
But what if I create my own class?
Take, for instance, a class for vectors. Vectors have direction and magnitude. What they are and how they work can be read here. Vectors can be simulated with a set of 3 coordinates, similar to a point on a 3D graph, which shows where the vector terminates and from which its magnitude can be calculated.
So, if the vector has three coordinates, how can I tell the program to add two together? I would have to write a function telling the program to add the corresponding coordinates from each vector (AX + BX, AY + BY, AZ + BZ). This works alright, but the syntax behind it can be confusing, or at best, hard to read. Perhaps I write a method called "add" to add vectorA and vectorB together. To implement this into the program, I might have to write something like this:
vectorC = vectorA.add(vectorB);
Using operator overloading, I can now make my vector class work just like built in data types in C++. If I overload the + operator, I can tell the program that when I use + with two vectors, it means to add each corresponding coordinate. Now, to have the program do what I want it to do, all I have to do is type:
vectorC = vectorA + vectorB;
It seems trivial, but it makes things a lot easier to read and understand when I'm creating my own data types.
Basically, C++ has some built in operators. These can manipulate the data in the program. For instance, there are mathematical operators. For instance, if I tell my program:
5 + 5
It will return 10.
But what if I create my own class?
Take, for instance, a class for vectors. Vectors have direction and magnitude. What they are and how they work can be read here. Vectors can be simulated with a set of 3 coordinates, similar to a point on a 3D graph, which shows where the vector terminates and from which its magnitude can be calculated.
So, if the vector has three coordinates, how can I tell the program to add two together? I would have to write a function telling the program to add the corresponding coordinates from each vector (AX + BX, AY + BY, AZ + BZ). This works alright, but the syntax behind it can be confusing, or at best, hard to read. Perhaps I write a method called "add" to add vectorA and vectorB together. To implement this into the program, I might have to write something like this:
vectorC = vectorA.add(vectorB);
Using operator overloading, I can now make my vector class work just like built in data types in C++. If I overload the + operator, I can tell the program that when I use + with two vectors, it means to add each corresponding coordinate. Now, to have the program do what I want it to do, all I have to do is type:
vectorC = vectorA + vectorB;
It seems trivial, but it makes things a lot easier to read and understand when I'm creating my own data types.
Thursday, March 20, 2014
I'm still here!
Just wanted to say that I'm still around. Things got a little crazy for a while. I was pretty sick for a while and out of commission.
I've been feeling a lot better and getting back into the swing of things.
In the course that I am taking, one of my homework assignments was to add features to a text based RPG game, where the source of the base game was pretty much given as an introduction to classes.
The basic RPG asks you to pick a class, and gives you the option to move in four directions (it keeps track of your position using coordinates), rest, view your character's stats, or quit the game. If you move, There is a chance you will be attacked by one of several types of enemies, from weak goblins to very strong orc lords.
Some of the features I was asked to implement were easy. I had to add character classes and races into the game, and give bonuses and different stats to each. One other thing they had me to was to add a chance of being attacked while resting. This one was especially easy, but for some reason I was stumped for a while. It turned out I was making things a lot more complicated than they really were, and when I figured out what I was doing wrong it took about 2 minutes to implement this.
I was pretty confused about passing pointers and references into functions, which was covered in the previous chapter, and while working on the assignments I had some ideas about things I could add in, but was having trouble doing what I wanted, and got a little frustrated. Eventually I gave up on the assignments and moved on to the next chapter.. Progress was slow, though, because I felt bad about not finishing the assignments.
So finally, I've begun working on that game again. So far, I've added a store, that will be randomly assigned to a set of coordinates from ([-5 - 5], [ -5, 5]), so that every game the store will be located in a different place, but won't be super hard to get to. In addition, I've added a "check map" option to the main game menu, so it's easy to figure out where the store is.
I've also added a message and a new menu that comes up when you enter the store, and disabled the ability to be attacked when you're on the coordinate the store occupies.
The next step is to add inventory to the store in the form of weapons, armor, and potions. Then I'll need to add the methods that will allow you to purchase and use the items in the store. I think I'm going to hard code items into the store that will be available, but I'm also considering upgrades for weapons and armor instead of actual new items. I feel like that will make it easier to keep progressing in the game after a few levels.
After I'm done with that, the last assignment I have is to make it possible to be attacked by more than one enemy at a time. This one sounds tricky, and I'm sure I'm going to have to work at getting that one right. I'm looking forward to getting this project out of the way. Once it's done and working, I'll have something to be proud of. It'll be a real morale booster!
I'll update this blog after a little more progress.
I've been feeling a lot better and getting back into the swing of things.
In the course that I am taking, one of my homework assignments was to add features to a text based RPG game, where the source of the base game was pretty much given as an introduction to classes.
The basic RPG asks you to pick a class, and gives you the option to move in four directions (it keeps track of your position using coordinates), rest, view your character's stats, or quit the game. If you move, There is a chance you will be attacked by one of several types of enemies, from weak goblins to very strong orc lords.
Some of the features I was asked to implement were easy. I had to add character classes and races into the game, and give bonuses and different stats to each. One other thing they had me to was to add a chance of being attacked while resting. This one was especially easy, but for some reason I was stumped for a while. It turned out I was making things a lot more complicated than they really were, and when I figured out what I was doing wrong it took about 2 minutes to implement this.
I was pretty confused about passing pointers and references into functions, which was covered in the previous chapter, and while working on the assignments I had some ideas about things I could add in, but was having trouble doing what I wanted, and got a little frustrated. Eventually I gave up on the assignments and moved on to the next chapter.. Progress was slow, though, because I felt bad about not finishing the assignments.
So finally, I've begun working on that game again. So far, I've added a store, that will be randomly assigned to a set of coordinates from ([-5 - 5], [ -5, 5]), so that every game the store will be located in a different place, but won't be super hard to get to. In addition, I've added a "check map" option to the main game menu, so it's easy to figure out where the store is.
I've also added a message and a new menu that comes up when you enter the store, and disabled the ability to be attacked when you're on the coordinate the store occupies.
The next step is to add inventory to the store in the form of weapons, armor, and potions. Then I'll need to add the methods that will allow you to purchase and use the items in the store. I think I'm going to hard code items into the store that will be available, but I'm also considering upgrades for weapons and armor instead of actual new items. I feel like that will make it easier to keep progressing in the game after a few levels.
After I'm done with that, the last assignment I have is to make it possible to be attacked by more than one enemy at a time. This one sounds tricky, and I'm sure I'm going to have to work at getting that one right. I'm looking forward to getting this project out of the way. Once it's done and working, I'll have something to be proud of. It'll be a real morale booster!
I'll update this blog after a little more progress.
Saturday, January 11, 2014
The Secret Life of Bloggers Blog Party: Post 1
My lovely Stephanie Ann at World Turn'd Upside Down is hosting The Secret Life of Bloggers blog party. The premise for this is that every day you keep a short log of something you did, and take a picture of it. Every week you share the pictures and a . It was difficult to get started this week..I found it hard to get into the routine of carrying my camera or getting my iPod when there was something worthy of a story, but I did my best, and here's what I came up with.
Created a work space for myself. I have my main computer on another desk, but it doesn't really facilitate spreading out my work, so I reallocated this space.
It rained all day. This is the last of the snow from the last storm. I thought the stream of water in the gutter was really cool going under the little bridge of ice.
It may be a little while until my next post. My current homework assignment requires a bit of trigonometric knowledge, and I've long since forgotten what I learned in high school years ago. I've been trying to rapidly relearn what I need to complete the program, but I'm going to continue learning, because it seems like it'd be good to know, and I have been feeling really good about learning things again.
1-3-14
Shoveled snow. Had a good bit of snow the previous night into the next morning. I did the neighbors' sidewalk as well because if we're all at work they normally help us out.
1-5-14
Created a work space for myself. I have my main computer on another desk, but it doesn't really facilitate spreading out my work, so I reallocated this space.
1-10-14
Hung out with my oldest friend. My iPod wasn't handy, so I asked him to take a picture on his phone and send it to me. I think he forgot. :)1-11-14
Took two pictures to make up for the lack of during the rest of the week. It was really cold last night, and Coal cuddled all night. He was still cold so I covered him up while I was getting ready for work.It rained all day. This is the last of the snow from the last storm. I thought the stream of water in the gutter was really cool going under the little bridge of ice.
It may be a little while until my next post. My current homework assignment requires a bit of trigonometric knowledge, and I've long since forgotten what I learned in high school years ago. I've been trying to rapidly relearn what I need to complete the program, but I'm going to continue learning, because it seems like it'd be good to know, and I have been feeling really good about learning things again.
Tuesday, January 7, 2014
A lot of work!
Many people think of coding as just typing a few lines of text into a computer and the job being done. I knew so many kids when I was young that wanted to learn to make video games, thinking it would be the best job in the world.
What they never realized is how much work goes into a program. I've only been programming a few weeks, and the programs I'm writing are still very small and impractical, but I'm sometimes amazed at how many steps it takes to do something that seems fairly simple.
One of my homework assignments recently asked me to write a program that calculates the distance between several points in 3D space using a function. The thing I thought was interesting was the way you have to calculate the formula.
The formula to calculate the distance between the two points is:
This doesn't seem terribly complicated, and it really isn't, but the computer needs you to tell it how to solve the problem, step by step. You can't just plug this in and expect the computer to know what to do.
The first thing I did was create the function, with 6 floating point parameters named after the variables in the formula.
The next step is to create 3 more variables to store the calculations from the subtractions inside the square root. I initialized each of them immediately to be equal to the square of each subtraction.
Finally I can create another variable to hold the total. I initialize it to hold the square root of the sum of variables x, y, and z, and tell the function to return the result.
Now we are ready to call the function in the program. When you call the function, you'll give the program the numbers to use. The order in which you type them is very important, or else the calculations will be off.
The entire program:
What they never realized is how much work goes into a program. I've only been programming a few weeks, and the programs I'm writing are still very small and impractical, but I'm sometimes amazed at how many steps it takes to do something that seems fairly simple.
One of my homework assignments recently asked me to write a program that calculates the distance between several points in 3D space using a function. The thing I thought was interesting was the way you have to calculate the formula.
The formula to calculate the distance between the two points is:
This doesn't seem terribly complicated, and it really isn't, but the computer needs you to tell it how to solve the problem, step by step. You can't just plug this in and expect the computer to know what to do.
The first thing I did was create the function, with 6 floating point parameters named after the variables in the formula.
float Distance(float ux, float uy, float uz, float vx, float vy, float vz)
The next step is to create 3 more variables to store the calculations from the subtractions inside the square root. I initialized each of them immediately to be equal to the square of each subtraction.
float x = powf(ux - vx, 2); float y = powf(uy - vy, 2); float z = powf(uz - vz, 2);
Finally I can create another variable to hold the total. I initialize it to hold the square root of the sum of variables x, y, and z, and tell the function to return the result.
float distance = sqrt(x + y + z); return distance;
Now we are ready to call the function in the program. When you call the function, you'll give the program the numbers to use. The order in which you type them is very important, or else the calculations will be off.
cout << "The distance between (1, 2, 3,) and (0, 0, 0) = " << Distance(1, 2, 3, 0, 0, 0); cout << endl;
The entire program:
// 3D Distance // Calculates the distance between two points in 3D space #include#include using namespace std; float Distance(float ux, float uy, float uz, float vx, float vy, float vz); int main() { cout << "The distance between (1, 2, 3,) and (0, 0, 0) = " << Distance(1, 2, 3, 0, 0, 0); cout << endl; cout << "The distance between (1, 2, 3,) and (1, 2, 3) = " << Distance(1, 2, 3, 1, 2, 3); cout << endl; cout << "The distance between (1, 2, 3) and (7, -4, 5) = " << Distance(1, 2, 3, 7, -4, 5); cout << endl; } float Distance(float ux, float uy, float uz, float vx, float vy, float vz) { float x = powf(ux - vx, 2); float y = powf(uy - vy, 2); float z = powf(uz - vz, 2); float distance = sqrt(x + y + z); return distance; }
Sunday, January 5, 2014
The beginning...
My name is Andy. A few months ago I decided to start learning some HTML and CSS. Soon after, I started to learn some Javascript.. I started getting really confused after a few lessons, and started looking into some other books to help me out. At that point, I remembered always wanting to learn to program applications when I was younger, so I did some research and decided to learn C++.
So now I've got a few books, and I'm taking a sort of online self-guided course. I decided to write the blog to help keep me on track, and to keep track of my progress. One day I hope to be good enough to start writing my own programs. Maybe one day I'll be able to do it for a living.
I know it's going to be a lot of hard work, but so far I've been really enjoying the challenges. I'll be writing a post on what I'm working on in the next few days.
Thanks for reading!
So now I've got a few books, and I'm taking a sort of online self-guided course. I decided to write the blog to help keep me on track, and to keep track of my progress. One day I hope to be good enough to start writing my own programs. Maybe one day I'll be able to do it for a living.
I know it's going to be a lot of hard work, but so far I've been really enjoying the challenges. I'll be writing a post on what I'm working on in the next few days.
Thanks for reading!
Subscribe to:
Posts (Atom)