943,864 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1897
  • C++ RSS
Jan 15th, 2009
0

get key input without pausing the program

Expand Post »
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
egolovin is offline Offline
11 posts
since Oct 2008
Jan 16th, 2009
1

Re: get key input without pausing the program

Somehow I get the impression that you want to run the timer in a separate thread.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 16th, 2009
0

Re: get key input without pausing the program

Yay!
*Votes In Favor of Boost::Threads*
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: break statement
Next Thread in C++ Forum Timeline: Simple File i/o Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC