Guest User

Untitled

a guest
Sep 29th, 2025
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <chrono>
  4. #include <string>
  5.  
  6. void init(int pixels[], int width) {
  7. for (int i = 0; i < (width * width); i++) {
  8. pixels[i] = 0;
  9. }
  10. }
  11.  
  12. void clear() {
  13. std::string output = "";
  14. for (int i = 0; i < 40; i++) {
  15. output = output + "\n";
  16. }
  17. std::cout << output;
  18. }
  19.  
  20. void render(int pixels[], int width) {
  21. std::string output = "PowderBox\n";
  22. for (int i = 0; i < 40; i++) {
  23. output = output + "\n";
  24. }
  25. output = output + "|";
  26. for (int i = 0; i < (width * width); i++) {
  27. std::string symbol = std::to_string(pixels[i]);
  28. if (symbol == "0") {
  29. symbol = " ";
  30. }
  31. if (symbol == "1") {
  32. symbol = "x";
  33. }
  34. if (symbol == "2") {
  35. symbol = "#";
  36. }
  37. output = output + symbol + " ";
  38. if (i % width == width-1) {
  39. output = output + "|\n|";
  40. }
  41. }
  42. for (int i = 0; i < (width*2); i++) {
  43. output = output + "_";
  44. }
  45. output = output + "|";
  46. std::cout << output;
  47. }
  48.  
  49. void physics(int pixels[], int width) {
  50. int n;
  51. for (int i = 0; i < (width * width); i++) {
  52. n = (width * width) - i;
  53. if (pixels[n] == 1) {
  54. if (n + width < width * width) {
  55. if (pixels[n + width] == 0) {
  56. pixels[n + width] = pixels[n];
  57. pixels[n] = 0;
  58. }
  59. else if (pixels[n + (width - 1)] == 0) {
  60. pixels[n + (width - 1)] = pixels[n];
  61. pixels[n] = 0;
  62. }
  63. else if (pixels[n + (width + 1)] == 0) {
  64. pixels[n + (width + 1)] = pixels[n];
  65. pixels[n] = 0;
  66. }
  67. }
  68. }
  69. if (pixels[n] == 2) {
  70. if (pixels[n + width] == 0) {
  71. pixels[n + width] = pixels[n];
  72. pixels[n] = 0;
  73. }
  74. }
  75. }
  76. }
  77.  
  78. void simulate(int pixels[], int width) {
  79. std::chrono::milliseconds timespan(100);
  80. int c = 0;
  81. while (1) {
  82. physics(pixels, width);
  83. render(pixels, width);
  84. std::this_thread::sleep_for(timespan);
  85. if (c == 2) {
  86. int pos = width / 3;
  87. pixels[pos-width] = 1;
  88. pixels[pos+1] = 1;
  89. pixels[pos+width] = 1;
  90. pixels[pos-1] = 1;
  91. pixels[pos-width*2] = 1;
  92. c = 0;
  93. }
  94.  
  95. c++;
  96. }
  97. }
  98.  
  99. int main()
  100. {
  101. int width = 30;
  102. std::cout << "Powderbox by Axolay\n";
  103. std::cout << "Enter sandbox size\n(Recommended: 5<n<35)\nn = ";
  104. std::cin >> width;
  105. int pixels[10000];
  106. init(pixels, width);
  107. render(pixels, width);
  108. std::thread physx(simulate, pixels, width);
  109. while (1) {
  110. int pixel;
  111. std::cin >> pixel;
  112. pixels[pixel] = 1;
  113. render(pixels, width);
  114. }
  115. physx.join();
  116. };
Advertisement
Add Comment
Please, Sign In to add comment