Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <chrono>
- #include <string>
- void init(int pixels[], int width) {
- for (int i = 0; i < (width * width); i++) {
- pixels[i] = 0;
- }
- }
- void clear() {
- std::string output = "";
- for (int i = 0; i < 40; i++) {
- output = output + "\n";
- }
- std::cout << output;
- }
- void render(int pixels[], int width) {
- std::string output = "PowderBox\n";
- for (int i = 0; i < 40; i++) {
- output = output + "\n";
- }
- output = output + "|";
- for (int i = 0; i < (width * width); i++) {
- std::string symbol = std::to_string(pixels[i]);
- if (symbol == "0") {
- symbol = " ";
- }
- if (symbol == "1") {
- symbol = "x";
- }
- if (symbol == "2") {
- symbol = "#";
- }
- output = output + symbol + " ";
- if (i % width == width-1) {
- output = output + "|\n|";
- }
- }
- for (int i = 0; i < (width*2); i++) {
- output = output + "_";
- }
- output = output + "|";
- std::cout << output;
- }
- void physics(int pixels[], int width) {
- int n;
- for (int i = 0; i < (width * width); i++) {
- n = (width * width) - i;
- if (pixels[n] == 1) {
- if (n + width < width * width) {
- if (pixels[n + width] == 0) {
- pixels[n + width] = pixels[n];
- pixels[n] = 0;
- }
- else if (pixels[n + (width - 1)] == 0) {
- pixels[n + (width - 1)] = pixels[n];
- pixels[n] = 0;
- }
- else if (pixels[n + (width + 1)] == 0) {
- pixels[n + (width + 1)] = pixels[n];
- pixels[n] = 0;
- }
- }
- }
- if (pixels[n] == 2) {
- if (pixels[n + width] == 0) {
- pixels[n + width] = pixels[n];
- pixels[n] = 0;
- }
- }
- }
- }
- void simulate(int pixels[], int width) {
- std::chrono::milliseconds timespan(100);
- int c = 0;
- while (1) {
- physics(pixels, width);
- render(pixels, width);
- std::this_thread::sleep_for(timespan);
- if (c == 2) {
- int pos = width / 3;
- pixels[pos-width] = 1;
- pixels[pos+1] = 1;
- pixels[pos+width] = 1;
- pixels[pos-1] = 1;
- pixels[pos-width*2] = 1;
- c = 0;
- }
- c++;
- }
- }
- int main()
- {
- int width = 30;
- std::cout << "Powderbox by Axolay\n";
- std::cout << "Enter sandbox size\n(Recommended: 5<n<35)\nn = ";
- std::cin >> width;
- int pixels[10000];
- init(pixels, width);
- render(pixels, width);
- std::thread physx(simulate, pixels, width);
- while (1) {
- int pixel;
- std::cin >> pixel;
- pixels[pixel] = 1;
- render(pixels, width);
- }
- physx.join();
- };
Advertisement
Add Comment
Please, Sign In to add comment