get key input without pausing the program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 11
Reputation: egolovin is an unknown quantity at this point 
Solved Threads: 0
egolovin egolovin is offline Offline
Newbie Poster

get key input without pausing the program

 
0
  #1
Jan 15th, 2009
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <windows.h>
  6. #include <ctime>
  7. #include <conio.h>
  8. #include <ctype.h>
  9. #include <iomanip>
  10.  
  11. using namespace std;
  12.  
  13. void gotoy(int y);
  14. void timechange(time_t start1);
  15. void timeevent();
  16.  
  17. int boost=0;
  18. int Eboost=0;
  19. int power=40;
  20. int HP=100;
  21. int EHP=100;
  22. int Epower=10;
  23. int dtime=0;
  24.  
  25. time_t start, end,start1;
  26.  
  27. int main()
  28. {
  29.  
  30. start=time(NULL);
  31. srand((unsigned)time(0));
  32. cout<<"Fighting Game\n";
  33. cout<<"-------------\n";
  34. cout<<"Player 1\n";
  35. cout<<"HP: "<<HP<<"/100"<<endl<<endl<<endl;
  36. cout<<"Player 2\n";
  37. cout<<"HP: "<<EHP<<"/100"<<endl<<endl<<endl;
  38. cout<<"1. Attack (10 power)\n";
  39. cout<<"2. Power Attack (10 power)\n";
  40. cout<<"3. Cocaine (10 power)\n";
  41. cout<<"4. Heal (10 power)\n";
  42.  
  43. do
  44. {
  45.  
  46. gotoy(3);
  47. cout<<"Player 1\n";
  48. cout<<"HP: "<<right<<setw(3)<<HP<<"/100"<<endl;
  49. cout<<"Power: "<<right<<setw(3)<<power<<"/100"<<endl<<endl;
  50. gotoy(7);
  51. cout<<"Player 2\n";
  52. cout<<"HP: "<<right<<setw(3)<<EHP<<"/100"<<endl;
  53. cout<<"Power: "<<right<<setw(3)<<Epower<<"/100"<<endl<<endl;
  54. timechange(start);
  55.  
  56.  
  57. if (GetAsyncKeyState(VK_UP)&&power>=10)
  58. {
  59. power-=10;
  60. EHP-=10;
  61. }
  62.  
  63.  
  64.  
  65. }
  66. while(EHP>1||HP>1);
  67.  
  68. return 0;
  69. }
  70. void gotoy (int y)
  71. {
  72. y-=1;
  73. COORD coord;
  74. coord.X = 0; coord.Y = y;
  75. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  76. }
  77. void timechange(time_t start1)
  78. {
  79. end=time(NULL);
  80. dtime=difftime(end, start1);
  81. if (dtime==1)
  82. {
  83. timeevent();
  84. dtime=0;
  85. start=end;
  86. }
  87.  
  88.  
  89. }
  90. void timeevent()
  91. {
  92. power+=1;
  93. if (boost>0)
  94. {
  95. boost-=1;
  96. power+=3;
  97. }
  98. if (power>100)
  99. {power=100;}
  100.  
  101. Epower+=1;
  102. if (Eboost>0)
  103. {
  104. Eboost-=1;
  105. Epower+=3;
  106. }
  107. if (Epower>100)
  108. {Epower=100;}
  109.  
  110. }
here is my code i tried GetAsyncKeyState but it happens too fast
and getch pauses the program.

so ill explain how my program works it uses a timer and the power builds up based on seconds so it needs to be in a constant loop to keep my screen updated

once a player has enough power you can preform a move

is there a command where it reads the key when the finger leaves the keyboard

Thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: get key input without pausing the program

 
1
  #2
Jan 16th, 2009
Somehow I get the impression that you want to run the timer in a separate thread.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: get key input without pausing the program

 
0
  #3
Jan 16th, 2009
Yay!
*Votes In Favor of Boost::Threads*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 818 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC