fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. // to jest tylko przyklad
  5. class Text
  6. {
  7. public:
  8. // jakaś pozycja i wymiary oraz jaki tekst ma być renderowany
  9. Text(int x, int y, int w, int h, std::string render_text)
  10. {
  11. // ustawie tylko nazwe, bo to jest tylko przyklad
  12. text = std::move(render_text);
  13. }
  14.  
  15. void update(std::string render_text) // aktualizujesz tekst tutaj
  16. {
  17. text = std::move(render_text);
  18. }
  19.  
  20. // To tylko dla przykładu. Zwykle tekst się renderuje w bilbiotece graficznej
  21. void print()
  22. {
  23. using namespace std;
  24. cout << text << endl;
  25. }
  26. private:
  27. std::string text;
  28. };
  29.  
  30. int main() {
  31. using namespace std;
  32.  
  33.  
  34. Text play{ 2, 3, 4, 5, "PLAY" };
  35. Text quit{ 23, 45, 3, 5, "QUIT" };
  36.  
  37. // Powiedzmy, że w jakiejś częsci projektu jest ta zmienna:
  38. int lives = 10;
  39.  
  40. // Tak można to zrobić, żeby zmienna była wyświetlana obok:
  41. Text lives_text{ 23, 32, 45, 67, "LIVES: " + std::to_string(lives) };
  42.  
  43. cout << "Zycia przed upadkiem: \n";
  44. lives_text.print();
  45.  
  46. // Powiedzmy, że coś spadlo i tracisz życie(taki przykład):
  47. cout << "Zycia po upadku: \n";
  48. lives--;
  49.  
  50. // Tekst "LIVES: ", można ustawić jako stały w jakiejś części projektu
  51. lives_text.update("LIVES: " + std::to_string(lives));
  52.  
  53. lives_text.print();
  54.  
  55. // I jakaś pętla
  56.  
  57. while (lives--){
  58. lives_text.update("LIVES: " + std::to_string(lives));
  59. lives_text.print();
  60. }
  61. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Zycia przed upadkiem: 
LIVES: 10
Zycia po upadku: 
LIVES: 9
LIVES: 8
LIVES: 7
LIVES: 6
LIVES: 5
LIVES: 4
LIVES: 3
LIVES: 2
LIVES: 1
LIVES: 0