-
Map masks?
Hey, I'm working on an ASCII game. :) You know the ones I'm talking about (with the #'s and the *'s). Anyway, I came across a problem. When ever the player walks over the buildings I have made it basically deletes the building part that the player steps on. I did this in QB, and I used a mask; I was just wondering, how would I put the mask in with C++? Any hints/tips would be greatly appreciated.
Thanks
-Kavity
-
If you are useing an array for the map, then you would simply write in a if statement to check if the space your moveing to has anything already there.
-
Hmm :/
I don't completely understand how to use array's. I've read the tutorial here. And I still don't understand. If you could help out a little it would be Greatfully apreciated. Thanks even if you don't.
-Kavity
-
There are two ways to accomplish what you want to do.
Masks, by the way, can be implemented the same way in QB as in C++. You must use logical operators and bit masks.
Option 1 - In memory or in an array
If you are wanting to preserve the contents of an array or section of memory you do not need to use masks. Before you place your player, ASCII value, or whatever inside the array, save the current value at that location. When you move the player, you can then restore the value which will effectively erase the player and place what was there prior to the player occupying the location.
Option 2 - On the screen
If you wish to do this on screen, simply preserve the ASCII character at screen location row,column and place your player ASCII character at location row,column. When the player leaves location row,column simply place your stored value at that location. The background image or character will be preserved.
Option 3 - Other methods
There are other ways using buffering and other methods, but text mode is so fast, there is almost no need to create a buffer.
Using the array as the 'screen' in memory is one way to buffer the output (look at my first explanation). To draw the screen, simply start at 0,0 and place element 0 of the array there. Iterate through the array and place the value at the appropriate place on the screen. Effectively, your array is what the screen will look like.
Very simplistic explanation of arrays
While I cannot explain all about arrays and their uses here (there are about a million ways to use them), I can explain a simple one dimensional array.
For instance:
This creates an integer array of 11 elements. This is equivalent to the QB statement:
Code:
dim MyArray(0 to 10) as integer
All arrays in C start at 0, while in QBasic they can start at 1 or 0 depending on your OPTION BASE statement. Technically, there is no advantage or disadvantage in regards to whether an array starts at 0 or 1. It's just that in C, all arrays start at 0. However, for most projects you will find that starting at 0 is easier when you start working with offsets into memory, etc.
To access the array simply specify the array element you want.
For instance to access the 0th element of MyArray:
Code:
int test=MyArray[0];
or in QB:
Code:
dim test as integer
test=MyArray(0)
To loop through and print all the elements:
Code:
for (int i=0;i<10;i++)
{
printf("%d ",MyArray[i]);
}
Most of the time in C, you will want to access arrays via pointers, but that definitely cannot be explained in this post as it is a huge topic.
This prints the array in QB:
Code:
for int i=0 to 10 step 1
print MyArray(i);" ";
next
Hope that helps. Since I assumed you knew QB, switching between C and QB may help you understand C a bit better.
There is much, much, much more. Buy a book on C. Books by Bjarne Strousoup (correct spelling?) and Herbert Schildt are excellent and they also cover C++ in great depth. The Sams series of books are great, but I personally do not feel that you can learn and master C in 24 hours or less. C takes a lifetime to master because it is so powerful and versatile.
-
"For instance:
code:--------------------------------------------------------------------------------
int MyArray[10];
--------------------------------------------------------------------------------
This creates an integer array of 11 elements. "
You mean an array of 10 elements right.
-
Yes, I do mean 10 elements.
-
Thanks alot man. Aww.. just like qb, that's good to hear :P
Thanks, and I hope this helps me. Well thanks again, and see you later. ;)
-Kavity
-
Well,....not exactly like QB, C is much more powerful. But, if you get a good grasp on QB it will not be nearly as hard to move to C/C++ and other languages.
-
if you are moving from QB to C/++ [like so many of us have... around the age of 12/14...] i strongly recommend having a book to guide you... if not necessarily "C/++ for QB-ers"... a "C/++ for beginners" nonetheless... the Dummies series is good... oh, and if i may offer more advice... C/++ are only different as one is an extension and slight alteration of the other... do not let it be construed otherwise... notice how most posts in C/++ forums could have been in either...