Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.04 KB | None | 0 0
  1. import java.awt.EventQueue;
  2. import javax.swing.JFrame;
  3. import javax.swing.JButton;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.ActionEvent;
  6. import javax.swing.JLabel;
  7. import java.awt.Color;
  8. import java.awt.Font;
  9. import javax.swing.SwingConstants;
  10.  
  11. public class Main {
  12.  
  13. private JFrame frmEscapeGame;
  14. private JButton btn1;
  15. private JButton btn2;
  16. private JLabel lblBegin;
  17. private JLabel lblLose;
  18. private JLabel lblWin;
  19.  
  20.  
  21.  
  22. /**
  23. * Launch the application.
  24. */
  25. public static void main(String[] args) {
  26. EventQueue.invokeLater(new Runnable() {
  27. public void run() {
  28. try {
  29. Main window = new Main();
  30. window.frmEscapeGame.setVisible(true);
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. });
  36. }
  37.  
  38. /**
  39. * Create the application.
  40. */
  41. public Main() {
  42. initialize();
  43. }
  44. private void initialize() {
  45. frmEscapeGame = new JFrame();
  46. frmEscapeGame.setTitle("Escape Game");
  47. frmEscapeGame.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 10));
  48. frmEscapeGame.setBounds(100, 100, 450, 300);
  49. frmEscapeGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50. frmEscapeGame.getContentPane().setLayout(null);
  51. //FRAME
  52.  
  53. lblBegin = new JLabel();
  54. lblBegin.setFont(new Font("Segoe UI", Font.BOLD, 13));
  55. lblBegin.setHorizontalAlignment(SwingConstants.CENTER);
  56. lblBegin.setBackground(Color.DARK_GRAY);
  57. lblBegin.setBounds(10, 64, 397, 70);
  58.  
  59. //RIGHT BRANCH
  60.  
  61. frmEscapeGame.getContentPane().add(lblBegin);
  62. btn2 = new JButton();
  63. btn2.setFont(new Font("Segoe UI", Font.BOLD, 11));
  64. btn2.setBounds(256, 180, 151, 70);
  65. btn2.setVisible(false);
  66.  
  67. frmEscapeGame.getContentPane().add(btn2);
  68.  
  69. //LEFT BRANCH
  70.  
  71. btn1 = new JButton();
  72. btn1.setFont(new Font("Segoe UI", Font.BOLD, 11));
  73. btn1.setBounds(25, 180, 151, 70);
  74. frmEscapeGame.getContentPane().add(btn1);
  75.  
  76. lblWin = new JLabel("You Win!");
  77. lblWin.setFont(new Font("Segoe UI", Font.BOLD, 26));
  78. lblWin.setBounds(164, 11, 122, 38);
  79. frmEscapeGame.getContentPane().add(lblWin);
  80. lblWin.setVisible(false);
  81.  
  82.  
  83. lblLose = new JLabel("You Lose!");
  84. lblLose.setFont(new Font("Segoe UI", Font.BOLD, 26));
  85. lblLose.setBounds(164, 11, 122, 38);
  86. lblLose.setVisible(false);
  87.  
  88. frmEscapeGame.getContentPane().add(lblLose);
  89. startscreen();
  90.  
  91.  
  92. }
  93. void startscreen(){
  94. lblBegin.setText("<html>You are in WWII, Nazi Germany. You are an American spy who was imprisoned in Berlin for espionage. You have just escaped prison and are trying to leave the city without being caught.</html>");
  95. btn1.setBounds(135, 180, 151, 70);
  96. btn1.setText("Begin Game");
  97. btn1.addActionListener(new ActionListener() {
  98. public void actionPerformed(ActionEvent e) {
  99. begin();
  100. }
  101. });
  102.  
  103. }
  104. void begin(){
  105. lblLose.setVisible(false);
  106. lblWin.setVisible(false);
  107. for (ActionListener i : btn2.getActionListeners())
  108. btn2.removeActionListener(i);
  109.  
  110. for (ActionListener i : btn1.getActionListeners())
  111. btn1.removeActionListener(i);
  112. lblBegin.setText("<html>It's midnight. What's your first step?</html>");
  113. btn1.setText("<html>Hide in the bushes for the night</html>");
  114. btn2.setText("<html>Stay at a friend's house</html>");
  115. btn2.setVisible(true);
  116. btn1.setBounds(26,180, 151, 70);
  117. left();
  118. right();
  119.  
  120. }
  121.  
  122. void lose(String y){
  123. lblBegin.setText(y);
  124. btn1.setBounds(135, 180, 151, 70);
  125. btn1.setText("<html>Restart Game</html>");
  126. lblLose.setVisible(true);
  127. lblWin.setVisible(false);
  128. btn2.setVisible(false);
  129. for (ActionListener i : btn1.getActionListeners())
  130. btn1.removeActionListener(i);
  131. btn1.addActionListener(new ActionListener() {
  132. public void actionPerformed(ActionEvent e) {
  133. begin();
  134.  
  135. }
  136. });
  137.  
  138. }
  139. void win(String x){
  140. lblBegin.setText(x);
  141. btn1.setBounds(135, 180, 151, 70);
  142. btn1.setText("<html>Restart Game</html>");
  143. lblWin.setVisible(true);
  144. lblLose.setVisible(false);
  145. btn2.setVisible(false);
  146. for (ActionListener i : btn1.getActionListeners())
  147. btn1.removeActionListener(i);
  148. btn1.addActionListener(new ActionListener() {
  149. public void actionPerformed(ActionEvent e) {
  150. begin();
  151.  
  152. }
  153. });
  154. }
  155. void left(){
  156. for (ActionListener i : btn1.getActionListeners()) {
  157. btn1.removeActionListener(i);}
  158. btn1.addActionListener(new ActionListener() {
  159. public void actionPerformed(ActionEvent e) {
  160. lblBegin.setText("<html>No one saw you, but the public is hearing about your escape. How will you travel?</html>");
  161. btn1.setText("<html>Steal a car and risk being caught</html>");
  162. btn2.setText("<html>Walk and play it safe</html>");
  163. //Stealing a car
  164.  
  165. for (ActionListener i : btn1.getActionListeners()) {
  166. btn1.removeActionListener(i);}
  167. double car= Math.random();
  168. btn1.addActionListener(new ActionListener() {
  169. public void actionPerformed(ActionEvent e) {
  170. if(car>.05){
  171. lose("<html>Unfortunately, a pedestrian reported your theft, and you were caught shortly after.</html>");
  172. }
  173. else{
  174. win("<html>Looks like you got extremely lucky. You were able to steal a car and flee the city without anybody knowing,<html>");
  175. }
  176. }
  177. });
  178.  
  179. //Walking
  180.  
  181. for (ActionListener i : btn2.getActionListeners())
  182. btn2.removeActionListener(i);
  183. btn2.addActionListener(new ActionListener() {
  184. public void actionPerformed(ActionEvent e) {
  185.  
  186. lblBegin.setText("<html>You see multiple police cars patrolling the streets. Where will you go?</html>");
  187. btn1.setText("<html>Back into the woods</html>");
  188. btn2.setText("<html>The city, and try to blend in with the public</html>");
  189.  
  190. for (ActionListener i : btn2.getActionListeners())
  191. btn2.removeActionListener(i);
  192.  
  193. for (ActionListener i : btn1.getActionListeners())
  194. btn1.removeActionListener(i);
  195.  
  196. btn1.addActionListener(new ActionListener() {
  197. public void actionPerformed(ActionEvent e) {
  198. lblBegin.setText("<html>You must go to the city now, to find a way to escape the police for good. </html>");
  199. btn1.setBounds(135, 180, 151, 70);
  200. btn1.setText("<html>Go to the city</html>");
  201. btn2.setVisible(false);
  202.  
  203. for (ActionListener i : btn2.getActionListeners())
  204. btn2.removeActionListener(i);
  205.  
  206. for (ActionListener i : btn1.getActionListeners())
  207. btn1.removeActionListener(i);
  208.  
  209. btn1.addActionListener(new ActionListener() {
  210. public void actionPerformed(ActionEvent e) {
  211. btn1.setBounds(26,180, 151, 70);
  212. btn2.setVisible(true);
  213. lblBegin.setText("<html>Now that you're in the city, what is your best bet in leaving?</html>");
  214. btn1.setText("<html>By boat</html>");
  215. btn2.setText("<html>By plane</html>");
  216.  
  217. //Plane
  218.  
  219. for (ActionListener i : btn2.getActionListeners())
  220. btn2.removeActionListener(i);
  221.  
  222. btn2.addActionListener(new ActionListener() {
  223. public void actionPerformed(ActionEvent e) {
  224. lose("<html>You arranged for a plane to arrive.The cops were watching the airport 24/7. You were caught immediately and sent back to jail.</html>");
  225. }
  226. });
  227. for (ActionListener i : btn1.getActionListeners())
  228. btn1.removeActionListener(i);
  229.  
  230. //Boat
  231.  
  232. btn1.addActionListener(new ActionListener() {
  233. public void actionPerformed(ActionEvent e) {
  234. win("<html>You arranged for a boat to arrive. Luckily the cops were too busy at the airport to check the harbor. You fled away safely.</html>");
  235. }
  236. });
  237. }
  238. });
  239. }
  240. });
  241.  
  242. //City
  243.  
  244. btn2.addActionListener(new ActionListener() {
  245. public void actionPerformed(ActionEvent e) {
  246. lblBegin.setText("<html>Now that you're in the city, what is your best bet in leaving?</html>");
  247. btn1.setText("<html>By boat</html>");
  248. btn2.setText("<html>By plane</html>");
  249. for (ActionListener i : btn2.getActionListeners())
  250. btn2.removeActionListener(i);
  251.  
  252. for (ActionListener i : btn2.getActionListeners())
  253. btn2.removeActionListener(i);
  254.  
  255. for (ActionListener i : btn1.getActionListeners())
  256. btn1.removeActionListener(i);
  257.  
  258. //Plane
  259. btn2.addActionListener(new ActionListener() {
  260. public void actionPerformed(ActionEvent e) {
  261. lose("<html>You arranged for a plane to arrive.The cops were watching the airport 24/7. You were caught immediately and sent back to jail.</html>");
  262. }
  263. });
  264.  
  265. for (ActionListener i : btn1.getActionListeners())
  266. btn1.removeActionListener(i);
  267.  
  268. //Boat
  269.  
  270. btn1.addActionListener(new ActionListener() {
  271. public void actionPerformed(ActionEvent e) {
  272. win("<html>You arranged for a boat to arrive. Luckily the cops were too busy at the airport to check the harbor. You fled away safely.</html>");
  273. }
  274. });
  275. }
  276. });
  277.  
  278. }
  279. });
  280. }
  281.  
  282. });
  283.  
  284. }
  285. void right(){
  286. btn2.addActionListener(new ActionListener() {
  287.  
  288. public void actionPerformed(ActionEvent e) {
  289.  
  290. btn2.setText("<html>Ask him for help</html>");
  291. lblBegin.setText("<html>Your friend let you stay for the night, but he says you have to figure something out quickly. What will you do?</html>");
  292. btn1.setText("<html>Take his car and use it as transportation</html>");
  293.  
  294. //Taking the car
  295. for (ActionListener i : btn1.getActionListeners())
  296. btn1.removeActionListener(i);
  297. btn1.addActionListener(new ActionListener() {
  298. public void actionPerformed(ActionEvent e) {
  299. lblBegin.setText("<html>You took his car, and fled to an abandoned area of town. Now that you have a car, all you have to do is find an escape route. </html>");
  300. btn1.setText("<html>Go to a convience store and steal a map</html>");
  301. btn2.setText("<html>Ask a random civilian for directions to a backroad out of town</html>");
  302. for (ActionListener i : btn2.getActionListeners())
  303. btn2.removeActionListener(i);
  304.  
  305. //Asking a civilian
  306.  
  307. btn2.addActionListener(new ActionListener() {
  308. public void actionPerformed(ActionEvent e) {
  309. double civ= Math.random();
  310. if (civ > .5){
  311. lblBegin.setText("<html>The civilian didn't notice that you were an escapee, so he gladly gave you directions. Now that you got what you wanted, you can finally flee the city quietly.</html>");
  312. btn1.setBounds(135, 180, 151, 70);
  313. btn1.setText("<html>Proceed</html>");
  314. btn2.setVisible(false);
  315. btn1.addActionListener(new ActionListener() {
  316. public void actionPerformed(ActionEvent e) {
  317. double flee= Math.random();
  318. if (flee>.20){
  319. win("<html>No cops were on the route you took, leading to a swift, clean escape.</html>");
  320. }
  321. else{
  322. lose("<html>A small squadron happened to pass by the road the night you attempted to leave, they caught you and sent you back behind bars.</html>");
  323. }
  324.  
  325.  
  326. } });
  327. }
  328.  
  329.  
  330. lblBegin.setText("<html>The civilian gave you the route, but he soon recognized you and reported you to the cops after. You must leave now.</html>");
  331. btn1.setBounds(135, 180, 151, 70);
  332. btn1.setText("<html>Proceed</html>");
  333. btn2.setVisible(false);
  334.  
  335. for (ActionListener i : btn1.getActionListeners())
  336. btn1.removeActionListener(i);
  337. btn1.addActionListener(new ActionListener() {
  338. public void actionPerformed(ActionEvent e) {
  339. double dip=Math.random();
  340. if(dip>.2){
  341. win("<html>Luckily you fled before the cops could arrive. You did it.</html>");
  342. }
  343. else{
  344. lose("<html>You were too late. The cops covered all exits out of the city and caught you.</html>");
  345. }
  346. }
  347. });
  348. } });
  349. //Stealing the map
  350.  
  351. for (ActionListener i : btn1.getActionListeners())
  352. btn1.removeActionListener(i);
  353. btn1.addActionListener(new ActionListener() {
  354. public void actionPerformed(ActionEvent e) {
  355. lblBegin.setText("<html>You were able to steal a map without being caught, You must leave before the cops come.</html>");
  356. btn1.setBounds(135, 180, 151, 70);
  357. btn1.setText("<html>Proceed</html>");
  358. btn2.setVisible(false);
  359. for (ActionListener i : btn1.getActionListeners())
  360. btn1.removeActionListener(i);
  361. btn1.addActionListener(new ActionListener() {
  362. public void actionPerformed(ActionEvent e) {
  363. double map= Math.random();
  364. if (map> .1){
  365. win("<html>You made it without the cops noticing. You're free.</html>");
  366. }
  367. lose("<html>A lone cop found you on the way out, you're now back behind bars</html>");
  368. }
  369.  
  370. });
  371.  
  372. }
  373. });
  374.  
  375.  
  376. }
  377. });
  378.  
  379. //Taking your friend's advice
  380.  
  381. for (ActionListener i : btn2.getActionListeners())
  382. btn2.removeActionListener(i);
  383. btn2.addActionListener(new ActionListener() {
  384. public void actionPerformed(ActionEvent e) {
  385. lblBegin.setText("<html>He tells you that he can smuggle you out of the city if you can hide in his car trunk for 2 days, without food or water.</html>");
  386. btn1.setText("<html>Accept the offer</html>");
  387. btn2.setText("<html>Reject it</html>");
  388.  
  389. //Rejecting it
  390.  
  391. for (ActionListener i : btn2.getActionListeners())
  392. btn2.removeActionListener(i);
  393. btn2.addActionListener(new ActionListener() {
  394. public void actionPerformed(ActionEvent e) {
  395. lose("<html>Your friend respects your decision and you leave, not having a clue as to what you are going to do. As you walk out of his neighborhood, a patrolling officer sees you and arrests you right away.</html>");
  396. }
  397. });
  398. for (ActionListener i : btn1.getActionListeners())
  399. btn1.removeActionListener(i);
  400.  
  401. //Accepting it
  402.  
  403. btn1.addActionListener(new ActionListener() {
  404. public void actionPerformed(ActionEvent e) {
  405. lblBegin.setText("<html>After a couple of days, your friend gets an arrangement made for you, and you are ready to do it, knowing the risks.</html>");
  406. btn1.setBounds(135, 180, 151, 70);
  407. btn1.setText("<html>Proceed</html>");
  408. btn2.setVisible(false);
  409. for (ActionListener i : btn1.getActionListeners())
  410. btn1.removeActionListener(i);
  411. btn1.addActionListener(new ActionListener() {
  412. public void actionPerformed(ActionEvent e) {
  413. double smug= Math.random();
  414. if(smug >.6){
  415. win("<html>You were able to survive without any resources, now you made it out of the city.</html>");
  416. }
  417. lose("<html>You weren't able to survive without any food or water, leading to your death.</html>");
  418.  
  419. }
  420.  
  421. });
  422. }
  423. });
  424. }
  425. });
  426. }
  427. });
  428. }
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement