Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.23 KB | None | 0 0
  1. import processing.sound.*;
  2.  
  3.  
  4. char[][] disp;
  5. int wsx = 100;
  6. int wsy = 100;
  7. int ts = 16;
  8. int px = 15;
  9. int py = 15;
  10. boolean right = true;
  11. String status = "";
  12. int statuschar = 0;
  13. SoundFile[] audio;
  14. boolean haskey = false;
  15. int spos = 0;
  16. int counter = 0;
  17. boolean gamestate = true;
  18. int sel = 0;
  19. Enemy[] e;
  20. int scalls = 0;
  21. int rooms;
  22. ArrayList<String> menu;
  23. boolean hardmode = false;
  24. int coins = 0;
  25. int[] inv; //1 = Boulders 2 = Potions
  26. boolean hasboulder = false;
  27. boolean haspotion = false;
  28.  
  29. void setup() {
  30.   hasboulder = false;
  31.   haspotion = false;
  32.   rooms = 0;
  33.   coins = 0;
  34.   scalls++;
  35.   wsx = width/16;
  36.   wsy = height/16;
  37.   disp = new char[wsx][wsy];
  38.   menu = new ArrayList<String>();
  39.   for(int i = 0; i < wsx; i++) {
  40.     for(int j = 0; j < wsy; j++) {
  41.       disp[i][j] = ' ';
  42.     }
  43.   }
  44.   menu.add("Play");
  45.   menu.add("Options");
  46.   menu.add("Credits");
  47.   e = new Enemy[2];
  48.   audio = new SoundFile[3];
  49.   audio[0] = new SoundFile(this, "talk.wav");
  50.   audio[1] = new SoundFile(this, "music.mp3");
  51.   audio[2] = new SoundFile(this, "coin.wav");
  52.   if(scalls == 1) {
  53.     PImage titlebaricon = loadImage("icon.png");
  54.     surface.setIcon(titlebaricon);
  55.   }
  56.   clearDisp();
  57.       for(int i = 10; i < 33; i++) {
  58.         disp[i][10] = '#';
  59.       }
  60.       for(int i = 10; i < 20; i++) {
  61.         disp[10][i] = '#';
  62.       }
  63.       for(int i = 10; i < 33; i++) {
  64.         disp[i][20] = '#';
  65.       }
  66.       for(int i = 10; i < 21; i++) {
  67.         disp[32][i] = '#';
  68.       }
  69.       for(int i = 10; i < 21; i++) {
  70.         disp[21][i] = '#';
  71.       }
  72.       disp[21][15] = 'D';
  73.       changeRoom(false);
  74. }
  75. void settings() {
  76.   size(700, 600);
  77. }
  78. void draw() {
  79.   //println(audio[1].isPlaying());
  80.   if((counter)%ceil(audio[1].frames()/600) == 1) {
  81.     audio[1].play();
  82.   }
  83.   background(0);
  84.     fill(255);
  85.     textSize(ts);
  86.     for(int i = 0; i < wsx; i++) {
  87.       for(int j = 0; j < wsy; j++) {
  88.         fill(255);
  89.         if(disp[i][j] == '#') {
  90.           fill(130, 74, 13);
  91.         }
  92.         if(disp[i][j] == 'D') {
  93.           fill(191, 74, 13);
  94.         }
  95.         if(disp[i][j] == '▲') {
  96.           fill(15, 255, 255);
  97.         }
  98.         if(disp[i][j] == '♣') {
  99.           fill(0, 255, 0);
  100.         }
  101.         if(disp[i][j] == 'B') {
  102.           fill(128);
  103.         }
  104.         if(disp[i][j] == '☻') {
  105.           fill(255, 255, 0);
  106.         }
  107.         if(disp[i][j] == 'K') {
  108.           fill(255, 255, 0);
  109.           if(counter%20 >= 15) {
  110.             fill(255);
  111.           }
  112.         }
  113.         if(disp[i][j] == '♦') {
  114.           fill(255, 255, 0);
  115.           if(counter%20 >= 15) {
  116.             fill(255);
  117.           }
  118.         }
  119.         if(disp[i][j] == '=') {
  120.           fill(255, 0, 0);
  121.           disp[i+1][j] = '-';
  122.         }
  123.         if(disp[i][j] == '▒') {
  124.           fill(0, 0, 255);
  125.         }
  126.         if(disp[i][j] == '>') {
  127.           fill(255, 0, 0);
  128.         }
  129.         if(disp[i][j] == '<') {
  130.           fill(255, 0, 0);
  131.         }
  132.         if(disp[i][j] == '^') {
  133.           fill(255, 0, 0);
  134.         }
  135.         if(disp[i][j] == 'v') {
  136.           fill(255, 0, 0);
  137.         }
  138.         if(disp[i][j] == '-') {
  139.           fill(255, 0, 0);
  140.           if(disp[i+1][j] != '#' && disp[i+1][j] != 'B') {
  141.             disp[i+1][j] = '-';
  142.           }
  143.           int k = i;
  144.           boolean t = false;
  145.           while(true) {
  146.             if(disp[k][j] == '=') {
  147.               t = true;
  148.               break;
  149.             }
  150.             if(disp[k][j] == 'B') {
  151.               t = false;
  152.               break;
  153.             }
  154.             k--;
  155.             if(k <= 0) {
  156.               break;
  157.             }
  158.           }
  159.           if(!t) {
  160.             disp[i][j] = ' ';
  161.           }
  162.         }
  163.         if(!(i == px && j == py)) {
  164.           text(disp[i][j], i*ts, j*ts);
  165.         }
  166.       }
  167.     }
  168.   if(!gamestate) {
  169.     counter++;
  170.     for(int i = 0; i < e.length; i++) {
  171.       if(e[i] != null) {
  172.         e[i].render();
  173.       }
  174.     }
  175.     if(counter < 5) {
  176.       clearDisp();
  177.       for(int i = 10; i < 33; i++) {
  178.         disp[i][10] = '#';
  179.       }
  180.       for(int i = 10; i < 20; i++) {
  181.         disp[10][i] = '#';
  182.       }
  183.       for(int i = 10; i < 33; i++) {
  184.         disp[i][20] = '#';
  185.       }
  186.       for(int i = 10; i < 21; i++) {
  187.         disp[32][i] = '#';
  188.       }
  189.       for(int i = 10; i < 21; i++) {
  190.         disp[21][i] = '#';
  191.       }
  192.       disp[21][15] = 'D';
  193.       changeRoom(false);
  194.     }
  195.     if(counter == 80) {
  196.       addStatus("Where am I?");
  197.     }
  198.     if(counter == 260) {
  199.       addStatus("I should go to the door");
  200.     }
  201.     if(counter == 500) {
  202.       addStatus("wasd or arrow keys to move");
  203.     }
  204.     if(statuschar < status.length()) {
  205.       if(statuschar < wsx) {
  206.         disp[statuschar][1] = status.charAt(statuschar);
  207.       } else {
  208.         disp[statuschar-wsx][2] = status.charAt(statuschar);
  209.       }
  210.     }
  211.     if(counter%5 == 1) {
  212.       statuschar++;
  213.       if(statuschar < status.length() && !status.equals("")) {
  214.         audio[0].play();
  215.       }
  216.       if(statuschar == status.length()+4) {
  217.         if(status.equals("The door is locked") && spos == 0) {
  218.           addStatus("Hmm... is that a key (K) over there?");
  219.           spos = 1;
  220.         }
  221.         if(status.equals("You got the key") && spos == 1) {
  222.           addStatus("That'll be useful for that door...");
  223.           spos = 2;
  224.         }
  225.         if(status.equals("You used the key") && spos == 2) {
  226.           addStatus("Is that a laser? Maybe I can block it with the boulders (B)");
  227.           spos = 3;
  228.         }
  229.       }
  230.     }
  231.     String k = "Rooms: "+rooms;
  232.     centerText(k,36);
  233.     centerText("♦"+coins, 35);
  234.     String t = "";
  235.     if(hasboulder) t += "Boulder | ✓ "; else t += "Boulder | X ";
  236.     if(haspotion) t += "Potion | ✓"; else t += "Potion | X";
  237.     centerText(t, 34);
  238.     fill(0, 255, 128);
  239.     text('@', px*ts, py*ts);
  240.     if(disp[px][py] == 'D') {
  241.       if(right) {
  242.         px++;
  243.         right = !right;
  244.       } else {
  245.         px--;
  246.         right = !right;
  247.       }
  248.       changeRoom(true);
  249.       disp[px][py] = ' ';
  250.       rooms++;
  251.     }
  252.     if(disp[px][py] == '-') {
  253.       px = 15;
  254.       py = 15;
  255.       right = true;
  256.       setup();
  257.     }
  258.     if(disp[px][py] == '>' || disp[px][py] == '^' || disp[px][py] == '<' || disp[px][py] == 'v') {
  259.       px = 15;
  260.       py = 15;
  261.       right = true;
  262.       setup();
  263.     }
  264.     if(disp[px][py] == '♦') {
  265.       disp[px][py] = ' ';
  266.       coins++;
  267.       audio[2].play();
  268.     }
  269.   } else {
  270.     clearDisp();
  271.     centerText("TwoRoomRogue", 10);
  272.     for(int i = 0; i < menu.size(); i++) {
  273.       centerText(menu.get(i), 12+i);
  274.       if(i == sel) {
  275.         centerText("["+menu.get(i)+"]", 12+i);
  276.       }
  277.     }
  278.   }
  279. }
  280. void keyPressed() {
  281.   if(!gamestate) {
  282.     int npx = px;
  283.     int npy = py;
  284.     int dir = 0; //1 = up 2 = down 3 = left 4 = right
  285.     if(key == 'w') {
  286.       npy--;
  287.       dir = 1;
  288.     }
  289.     if(key == 's') {
  290.       npy++;
  291.       dir = 2;
  292.     }
  293.     if(key == 'a') {
  294.       npx--;
  295.       dir = 3;
  296.     }
  297.     if(key == 'd') {
  298.       npx++;
  299.       dir = 4;
  300.     }
  301.     if(keyCode == UP) {
  302.       npy--;
  303.       dir = 1;
  304.     }
  305.     if(keyCode == DOWN) {
  306.       npy++;
  307.       dir = 2;
  308.     }
  309.     if(keyCode == LEFT) {
  310.       npx--;
  311.       dir = 3;
  312.     }
  313.     if(keyCode == RIGHT) {
  314.       npx++;
  315.       dir = 4;
  316.     }
  317.     boolean say = true;
  318.     if(key == ENTER) {
  319.       say = false;
  320.       if(disp[px][py] == '.') {
  321.         if(!hasboulder) {
  322.           if(coins >= 1) {
  323.             coins -= 1;
  324.             addStatus("You bought the boulder. Press B to deploy");
  325.             hasboulder = true;
  326.           } else {
  327.             addStatus("Not enough coins");
  328.           }
  329.         } else {
  330.           addStatus("You already have a boulder");
  331.         }
  332.       }
  333.       if(disp[px][py] == ',') {
  334.         if(!haspotion) {
  335.           if(coins >= 4) {
  336.             coins -= 4;
  337.             addStatus("You bought the potion. Press P to deploy");
  338.             haspotion = true;
  339.           } else {
  340.             addStatus("Not enough coins");
  341.           }
  342.         } else {
  343.           addStatus("You already have a boulder");
  344.         }
  345.       }
  346.     }
  347.     if(key == 'b') {
  348.       if(hasboulder) {
  349.         if(disp[px-1][py] == ' ') {
  350.           disp[px-1][py] = 'B';
  351.           addStatus("You placed the boulder");
  352.           hasboulder = false;
  353.         }
  354.       }
  355.     }
  356.     if(key == 'p') {
  357.       if(haspotion) {
  358.         if(disp[px-1][py] == '^' || disp[px-1][py] == 'v' || disp[px-1][py] == '>' || disp[px-1][py] == '<') {
  359.           disp[px-1][py] = ' ';
  360.           addStatus("You used the potion.");
  361.           haspotion = false;
  362.         }
  363.       }
  364.     }
  365.     if(key == 'f') {
  366.       counter += 500;
  367.     }
  368.     if(counter < 500) {
  369.       npx = px;
  370.       npy = py;
  371.     }
  372.     if(disp[npx][npy] != '#' && disp[npx][npy] != '♣' && disp[npx][npy] != 'D' && disp[npx][npy] != '▲' && disp[npx][npy] != 'B' && disp[npx][npy] != '=' && disp[npx][npy] != '▒') {
  373.       px = npx;
  374.       py = npy;
  375.     }
  376.     if(disp[npx][npy] == 'K') {
  377.       haskey = true;
  378.       disp[npx][npy] = ' ';
  379.       addStatus("You got the key");
  380.     }
  381.     if(disp[npx][npy] == 'D') {
  382.       if(haskey == false) {
  383.         addStatus("The door is locked");
  384.       } else {
  385.         px = npx;
  386.         py = npy;
  387.         addStatus("You used the key");
  388.         haskey = false;
  389.       }
  390.     }
  391.     if(disp[npx][npy] == '.') {
  392.         if(say) addStatus("Boulder. They cost 1♦ each. Press B to deploy [They're always placed to the left]");
  393.       }
  394.       if(disp[npx][npy] == ',') {
  395.         if(say) addStatus("Death Potion. They cost 4♦ each. Press P to deploy [Kills enemy to the left]");
  396.       }
  397.     if(disp[npx][npy] == 'B') {
  398.       boolean k = false;
  399.       switch(dir) {
  400.         case 1:
  401.           if(disp[npx][npy-1] == ' ' || disp[npx][npy-1] == '-') {
  402.             disp[npx][npy-1] = 'B';
  403.             k = true;
  404.           }
  405.           break;
  406.         case 2:
  407.           if(disp[npx][npy+1] == ' ' || disp[npx][npy+1] == '-') {
  408.             disp[npx][npy+1] = 'B';
  409.             k = true;
  410.           }
  411.           break;
  412.         case 3:
  413.           if(disp[npx-1][npy] == ' ' || disp[npx-1][npy] == '-') {
  414.             disp[npx-1][npy] = 'B';
  415.             k = true;
  416.           }
  417.           break;
  418.         case 4:
  419.           if(disp[npx+1][npy] == ' ' || disp[npx+1][npy] == '-') {
  420.             disp[npx+1][npy] = 'B';
  421.             k = true;
  422.           }
  423.           break;
  424.       }
  425.       if(k == true) {
  426.         px = npx;
  427.         py = npy;
  428.       }
  429.       disp[px][py] = ' ';
  430.       if(k == true) {
  431.         disp[npx][npy] = ' ';
  432.       }
  433.     }
  434.   } else {
  435.     if(keyCode == UP) {
  436.       sel--;
  437.       if(sel < 0) {
  438.         sel = 0;
  439.       }
  440.     }
  441.     if(keyCode == DOWN) {
  442.       sel++;
  443.       if(sel > menu.size()-1) {
  444.         sel = 1;
  445.       }
  446.     }
  447.     if(key == ENTER) {
  448.       if(menu.get(sel).equals("Play")) {
  449.         gamestate = !gamestate;
  450.       }
  451.       if(menu.get(sel).equals("Back")) {
  452.         String[] m = {"Play","Options","Credits"};
  453.         setMenu(m);
  454.       }
  455.       if(menu.get(sel).equals("Options")) {
  456.         String[] m = {"???", "Warning: Hardmode is buggy", "Back"};
  457.         if(hardmode) {
  458.           m[0] = "Disable hardmode";
  459.         } else {
  460.           m[0] = "Enable hardmode";
  461.         }
  462.         setMenu(m);
  463.       } else
  464.       if(menu.get(sel).equals("Enable hardmode")) {
  465.         hardmode = true;
  466.         String[] m = {"Play","Options","Credits"};
  467.         setMenu(m);
  468.       } else
  469.       if(menu.get(sel).equals("Disable hardmode")) {
  470.         hardmode = false;
  471.         String[] m = {"Play","Options","Credits"};
  472.         setMenu(m);
  473.       } else
  474.       if(menu.get(sel).equals("Credits")) {
  475.         String[] m = {"TheZipCreator | Coding","Gil H. Steinberg | Music", "Back"};
  476.         setMenu(m);
  477.       }
  478.     }
  479.   }
  480. }
  481. void changeRoom(boolean lasers) {
  482.   int rx = 0;
  483.   int ry = 0;
  484.   if(right) {
  485.     rx = 11;
  486.     ry = 10;
  487.   } else {
  488.     rx = 22;
  489.     ry = 10;
  490.   }
  491.   if(random(10) < 9) {
  492.     int roomType = floor(random(3));
  493.     if(roomType == 0) {
  494.         for(int i = rx; i < rx+9; i++) {
  495.           for(int j = ry+1; j < ry+10; j++) {
  496.             disp[i][j] = ' ';
  497.             if(random(1) < 0.1) {
  498.               disp[i][j] = '♣';
  499.             }
  500.           }
  501.         }
  502.       }
  503.       if(roomType == 1) {
  504.         for(int i = rx; i < rx+9; i++) {
  505.           for(int j = ry+1; j < ry+10; j++) {
  506.             disp[i][j] = ' ';
  507.             if(random(1) < 0.1) {
  508.               disp[i][j] = '▲';
  509.             }
  510.           }
  511.         }
  512.       }
  513.       if(roomType == 2) {
  514.       for(int i = rx; i < rx+9; i++) {
  515.         for(int j = ry+1; j < ry+10; j++) {
  516.           disp[i][j] = ' ';
  517.           if(random(1) < 0.1) {
  518.             disp[i][j] = '▒';
  519.           }
  520.         }
  521.       }
  522.     }
  523.     if(lasers) {
  524.       int ly = floor(random(ry+1,ry+10));
  525.       while(ly == 15) {
  526.         ly = floor(random(ry+1,ry+10));
  527.       }
  528.       int l2y = 0;
  529.       if(hardmode) {
  530.         l2y = floor(random(ry+1,ry+10));
  531.         while(l2y == 15) {
  532.           ly = floor(random(ry+1,ry+10));
  533.         }
  534.         disp[rx][l2y] = '=';
  535.       }
  536.       disp[rx][ly] = '=';
  537.       disp[floor(random(rx+1,rx+9))][floor(random(ry+1,ry+10))] = 'B';
  538.       disp[floor(random(rx+1,rx+9))][floor(random(ry+1,ry+10))] = 'B';
  539.       e[0] = new Enemy(floor(random(rx+1,rx+9)),floor(random(ry+1,ry+10)),1);
  540.       e[1] = new Enemy(floor(random(rx+1,rx+9)),floor(random(ry+1,ry+10)),2);
  541.       int ky = floor(random(ry+1,ry+10));
  542.       if(!hardmode) {
  543.         while(ky == ly) {
  544.           ky = floor(random(ry+1,ry+10));
  545.           println("d"+random(1));
  546.         }
  547.       } else {
  548.         while(ky == ly || ky == l2y) {
  549.           ky = floor(random(ry+1,ry+10));
  550.           println("d"+random(1));
  551.         }
  552.       }
  553.       disp[floor(random(rx+1,rx+9))][ky] = 'K';
  554.     } else {
  555.       disp[floor(random(rx+1,rx+9))][floor(random(ry+1,ry+10))] = 'K';
  556.     }
  557.     if(rooms%2 == 1) {
  558.       int numcoins = floor(random(1,4));
  559.       for(int i = 0; i < numcoins; i++) {
  560.         disp[floor(random(rx+1,rx+9))][floor(random(ry+1,ry+10))] = '♦';
  561.       }
  562.     }
  563.   } else {
  564.     if(lasers) {
  565.       println("ttt");
  566.       addStatus("You have entered a shop. Press ENTER to order an item while under it");
  567.       for(int i = rx; i < rx+9; i++) {
  568.         for(int j = ry+1; j < ry+10; j++) {
  569.           disp[i][j] = ' ';
  570.         }
  571.       }
  572.       disp[rx][ry+3] = '=';
  573.       disp[rx+4][ry+1] = '☻';
  574.       disp[rx][ry+4] = 'K';
  575.       disp[rx+2][ry+2] = 'B';
  576.       disp[rx+6][ry+2] = '▲';
  577.       disp[rx+2][ry+4] = '.';
  578.       disp[rx+6][ry+4] = ',';
  579.       for(int i = 0; i < e.length; i++) {
  580.         e[i] = null;
  581.       }
  582.     } else {
  583.       disp[rx][ry+4] = 'K';
  584.     }
  585.   }
  586. }
  587. void addStatus(String t) {
  588.   for(int i = 0; i < wsx; i++) {
  589.     disp[i][1] = ' ';
  590.   }
  591.   for(int i = 0; i < wsx; i++) {
  592.     disp[i][2] = ' ';
  593.   }
  594.   status = t;
  595.   statuschar = 0;
  596. }
  597. void centerText(String c, int o) {
  598.   int i = (43/2)-(c.length()/2);
  599.   for(int j = 0; j < c.length(); j++) {
  600.     disp[i+j][o] = c.charAt(j);
  601.   }
  602. }
  603. void clearDisp() {
  604.   for(int i = 0; i < wsx; i++) {
  605.     for(int j = 0; j < wsy; j++) {
  606.       disp[i][j] = ' ';
  607.     }
  608.   }
  609. }
  610. void setMenu(String[] m) {
  611.   sel = 0;
  612.   menu = new ArrayList<String>();
  613.   for(int i = 0; i < m.length; i++) {
  614.     menu.add(m[i]);
  615.   }
  616. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement