I'm not sure about that... What about system("cls")??? And before anyone jumps down my throat for suggesting bad programming, just want to clarify that it will do the job, won't it? It'll clear the screen, and then he can print the new position of A..
(I've tried this, and yes, it works, I used a similar thing for my Game of Life Program...)
Yes, it doesnt actually have to move A to a new line, it just has to give the illusion that it does when the player presses W (S later). so what would the code look like?
Stupebrett: A link or a tutorial please? Even I would like to know about this...
Ch1156: I don't know what you use to identify the pressing of the keys, [GetAsyncKeyState(0x41)] and how you use it, so I'm just making a small change in your code: You can modify it according to your own needs...
Though if you're making some kind of a game of some sorts, may I suggest you use a map of some sort...? Like a 2D array... I'm assuming you'll be wanting similar functionality for the other keys as well, so basically you can shift your character a row up, down, or a column left or right depending on the key pressed... Of course, if you're trying something else, don't mind me... :)
Thank you caprico, and yes i am trying to make a game :D i have been playing with my code and i got it working, but it doesnt appear that the character is moving down, i know it is because when i comment out system("cls") the A moves down but if i leave CLS in then it moves one space down and then just looks as if its blinking. But then again there is no background or anything. Can someone show me how to make a background? I tried making one but it messed up, the character either appeared next to the lines or on top or in front. just making a simple hallway would be fine, also you said something about Arrays to make a map, can you give me a good tutorial on Arrays i need to learn those. Also here is my newly modified code:
Oh and by the way 0x53 is S on the keyboard, so when you press S then it will move down.
Oooh... I think I understand the blinking... See, your clear screen function is in the while loop, right? So it keeps clearing the screen, and then printing your character, again and again and......
As for understanding arrays, I'd suggest this very site.. http://www.cplusplus.com/doc/tutorial/arrays/
If you're making a game in the console, I don't think there's a way to make your character appear over something... See, you could have like a printmap function, which analyses the key pressed, and accordingly prints the next instance of the map.. Like a frame-by-frame game..
Your map would be the array... Basically, it would be like a grid, in which your character would move, one cell at a time... That's my idea of a console game...
Yeah its in the while loop, it doesnt blink unless i press the S key which moves the player. Yeah thats all i wanted to do is just make a simple corridor or something for now and i'll add stuff later :P Thanks :D
Now i have a new proble, the character can go right and down but if i go right and press down he goes back to the first line and goes down, he doesnt go down from where he is at. This is how it looks.
(diagram 1)
aaaaaaaaaaaa
a
a
a
aaaaaaaaaa
a
a
aaaaaa
It should look like this:
(diagram 2)
1 2 3 4 5 6 7 8
aaaaaaaaaaaaaa
a
a
a
aaaaaaaaaaaa
a
a
a
Also note i commented out both of the the clear(); functions in the while loop to see what was going on exactly.
So my question is how do i make the character do what i want him to do in the diagram 2?
//#include stuff over here...
void mapreset(char **map) //Sets all grid cells to a blank.
{
int i, j;
for (i=0; i<80; i++)
for (j=0; j<80; j++)
map[i][j]=" ";
}
void printmap(char **map) //Accepts a 2D array. Dunno d exact syntax for accepting arrays
{ //since I had used classes..Though I'm pretty sure this will work.
int i, j;
for (i=0; i<80; i++)
for (j=0; j<80; j++)
cout<<map[i][j];
}
int main()
{
map[80][80]; //Square map of size 80x80.. You can experiment according to your screen size..
mapreset(map);
//Accept the initial position of the character here if you want..
//I'm just gonna expicitly set it somewhere in the middle..
map[40][40]="a";
//Here, your algorithm comes into play..
//If user presses down, just set map[i+1][j]="a"; (In this case, map[41][40]="a";
//Similarly, for other keys, do the same..
//Keep the printmap function in the while loop, where you're accepting the key..
//Somewhat like this:
while(true)
{
down=GetAsyncKeyState(0x53);
if(down)
{
chctrdwn();
printmap(map);
}
}
//Your chrctrdwn fnctn can simply accept the map, and the current position of your character
//(You might need another function for the current position thing)
//and then accordingly print the character below it..
//PS: I'd suggest using classes for this kind of a game.. Makes it easier to handle..
return 0;
}
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
usingnamespace std;
void chctrdwn();
void chctrrght();
void clear();
void mapreset(char **map) //Sets all grid cells to a blank.
{
int i, j;
for (i=0; i<80; i++)
for (j=0; j<80; j++)
map[i][j]=" ";
}
void printmap(char **map) //Accepts a 2D array. Dunno d exact syntax for accepting arrays
{ //since I had used classes..Though I'm pretty sure this will work.
int i, j;
for (i=0; i<80; i++)
for (j=0; j<80; j++)
cout<<map[i][j];
}
//Global
string character = "A";
//End of Global
int main()
{
map[80][80];
mapreset(map);
map[40][40]="a";
//int up; //W is up
int down; //S is down.
//int left; //A is left
int right; //D is right
cout << character;
while(true){
down=GetAsyncKeyState(0x53);
if(down){
//clear();
chctrdwn();
printmap(map);
}
right=GetAsyncKeyState(0x44);
if(right){
//clear();
chctrrght();
}
}
return 0;
}
void chctrdwn()
{
Sleep(150);
cout << "\n";
cout << character;
}
void chctrrght()
{
Sleep(150);
cout << "" << character;
}
void clear()
{
system("cls");
}
Here is the error:
C:\Users\Chay Hawk\Desktop\fstream test\main.cpp||In function 'void mapreset(char**)':|
C:\Users\Chay Hawk\Desktop\fstream test\main.cpp|18|error: invalid conversion from 'const char*' to 'char'|
C:\Users\Chay Hawk\Desktop\fstream test\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\fstream test\main.cpp|38|error: 'map' was not declared in this scope|
||=== Build finished: 2 errors, 0 warnings ===|
How silly of me... Line 38: Declare it as a character array as so:
char map[80][80]
Like I said, I'm not sure about passing 2D arrays to a function... Try changing **map to *map, map[80][80], or map[][]... Either of those should work.. If not, let me know, and I'll look into it deeper, and help you out.. :)
Also, change your chrctrdwn function... According to the 2D map way, it should change the element directly below the current position of the character, to change into that character... Like I have mentioned in the comments...
Im still getting errors. I dont have a clue on what im doing, i tried reading your comments but they dont help me, im a visual learner. I cant see snippets of code and then plug it in i have to see it working before i can understand it.
Here is the code again, I tried making some changes you saifd but gave me twice as many errors:
Hmm... This is a pickle... Okay, tell you what, I'll try writing the code from scratch, if it works, I'll PM you... Okay? Till then wait around, I'm sure someone will be able to help you out better...