How To move something in console using keyboad keys in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How To move something in console using keyboad keys in C++

2nd Jan 2020, 9:40 AM
Prasad
14 Answers
+ 2
And because of what Martin Taylor said. You're gonna need to rely on the operating system's API because it does have access to your keyboard and video memory. On windows, some functions that come to mind would be: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate To check if a key is down. And https://docs.microsoft.com/en-us/windows/console/setconsolecursorposition To set the cursor's position so you can write to the correct spot. Just make sure to read the documentation and don't expect it to be portable to other operating systems. If you check all the console API functions, you might find out how to clear the screen as well without using something as silly as a system command.
2nd Jan 2020, 2:03 PM
Dennis
Dennis - avatar
0
I need to develop centipede game. So that snake have to move.How can I do it?
2nd Jan 2020, 3:23 PM
Prasad
0
For the centipede you could use an array of points ( coordinate ). Every move delete the last point and add a new point to the front based on the direction you're traveling in. That way it looks like everything is moving. Then simply iterate through it and draw something at each point.
2nd Jan 2020, 3:32 PM
Dennis
Dennis - avatar
0
how to delete the last point? I meant is there a specific function or something
3rd Jan 2020, 4:34 AM
Prasad
0
Prasad The easy way would be to use an std::deque, where you can simply call .pop_back() to remove the last element and .push_front() to add something to the front. https://en.cppreference.com/w/cpp/container/deque
3rd Jan 2020, 8:53 AM
Dennis
Dennis - avatar
0
Can you send that piece of code?😓
3rd Jan 2020, 5:52 PM
Prasad
0
Well, sure, why not. As an example: https://code.sololearn.com/c515XsicXPAW/?ref=app The centipede will move down and then all the way to the right until it overflows ( probably want to prevent that ) Read the documentation for more.
3rd Jan 2020, 6:18 PM
Dennis
Dennis - avatar
0
thanku broh❤️
3rd Jan 2020, 6:22 PM
Prasad
0
Да я всё равно не чего не поиму
4th Jan 2020, 12:13 AM
Qaz
Qaz - avatar
0
COORD{5,2}, };a error in this line
4th Jan 2020, 3:55 AM
Prasad
0
Prasad No errors for me, m8. What would be wrong about it?
4th Jan 2020, 8:30 AM
Dennis
Dennis - avatar
0
Dennis see inbox bro
5th Jan 2020, 4:22 AM
Prasad
0
Zert:dgh
23rd Jan 2020, 1:14 PM
Qaz
Qaz - avatar
0
I maked that❤️
24th Jan 2020, 12:55 PM
Prasad