Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.80 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.*;
  5. import java.io.*;
  6. import java.util.ArrayList;
  7. import java.util.Random;
  8.  
  9. public class Game implements ActionListener{
  10. static JFrame f_play, f_highscores, f_htp;
  11. private JButton b_red, b_blue, b_green, b_yellow, b_purple, b_orange, b_back, b_next;
  12. private JPanel p_game, p_play;
  13. private JLabel l_correct, l_game_over, l_level;
  14. ArrayList<Color> color = new ArrayList<Color>();
  15. ArrayList<Color> button = new ArrayList<Color>();
  16. Color red = Color.RED, orange = new Color(255, 128, 0), yellow = Color.YELLOW, green = new Color(0, 199, 13),
  17. blue = new Color(0, 77, 255), purple = new Color(170, 34, 228);
  18. int highscore = 0, rounds = 4, time = 1000;
  19. String name = "";
  20. Timer timer;
  21. public static int num = 0;
  22. int color_count = 0, buttonCount = 0;
  23. Color[] colors = {red, blue, green, yellow, purple, orange};
  24.  
  25. public Game() {
  26.  
  27. num++;
  28.  
  29. f_play = new JFrame("Play!");
  30. initializeFrame(f_play);
  31.  
  32. p_play = new JPanel();
  33. p_play.setSize(900,700);
  34. p_play.setVisible(true);
  35. p_play.setLayout(null);
  36. f_play.add(p_play);
  37.  
  38. b_red = new JButton("Red");
  39. p_play.add(b_red);
  40. b_red.setBounds(150,550,100,75);
  41. b_red.setBackground(red);
  42. b_red.setVisible(false);
  43. b_red.addActionListener(this);
  44.  
  45. b_orange = new JButton("Orange");
  46. p_play.add(b_orange);
  47. b_orange.setBounds(250,550,100,75);
  48. b_orange.setBackground(orange);
  49. b_orange.setVisible(false);
  50. b_orange.addActionListener(this);
  51.  
  52. b_yellow = new JButton("Yellow");
  53. p_play.add(b_yellow);
  54. b_yellow.setBounds(350,550,100,75);
  55. b_yellow.setBackground(yellow);
  56. b_yellow.setVisible(false);
  57. b_yellow.addActionListener(this);
  58.  
  59. b_green = new JButton("Green");
  60. p_play.add(b_green);
  61. b_green.setBounds(450,550,100,75);
  62. b_green.setBackground(green);
  63. b_green.setVisible(false);
  64. b_green.addActionListener(this);
  65.  
  66. b_blue = new JButton("Blue");
  67. p_play.add(b_blue);
  68. b_blue.setBounds(550,550,100,75);
  69. b_blue.setBackground(blue);
  70. b_blue.setVisible(false);
  71. b_blue.addActionListener(this);
  72.  
  73. b_purple = new JButton("Purple");
  74. p_play.add(b_purple);
  75. b_purple.setBounds(650,550,100,75);
  76. b_purple.setBackground(purple);
  77. b_purple.setVisible(false);
  78. b_purple.addActionListener(this);
  79.  
  80. p_game = new JPanel();
  81. p_play.add(p_game);
  82. p_game.setVisible(false);
  83. p_game.setBounds(250,80,400,400);
  84.  
  85. timer = new Timer(time, this);
  86. timer.start();
  87.  
  88. f_play.setVisible(true);
  89.  
  90. l_level = new JLabel("Level " + num);
  91. p_play.add(l_level);
  92. l_level.setBounds(390,200,200,80);
  93. l_level.setFont(new Font("Serif", Font.PLAIN, 44));
  94.  
  95. l_correct = new JLabel("Correct!");
  96. p_play.add(l_correct);
  97. l_correct.setVisible(false);
  98. l_correct.setBounds(368,100,150,100);
  99. l_correct.setFont(new Font("Serif", Font.PLAIN, 44));
  100.  
  101. l_game_over = new JLabel("Game Over!");
  102. p_play.add(l_game_over);
  103. l_game_over.setVisible(false);
  104. l_game_over.setBounds(300,100,300,100);
  105. l_game_over.setFont(new Font("Serif", Font.PLAIN, 44));
  106.  
  107. b_back = new JButton("Back to Main Menu");
  108. p_play.add(b_back);
  109. b_back.setBounds(350,550,200,75);
  110. b_back.setVisible(false);
  111. b_back.addActionListener(this);
  112.  
  113. b_next = new JButton("Next Level");
  114. p_play.add(b_next);
  115. b_next.setBounds(350,550,200,75);
  116. b_next.setVisible(false);
  117. b_next.addActionListener(this);
  118.  
  119. }
  120.  
  121. public static void howToPlay() {
  122. f_htp = new JFrame("How to Play");
  123. initializeFrame(f_htp);
  124. }
  125.  
  126. public static void highscores() {
  127. f_highscores = new JFrame("Highscores");
  128. Game.initializeFrame(f_highscores);
  129. }
  130.  
  131. public static void initializeFrame(JFrame frame) {
  132. frame.setSize(900,700);
  133. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  134. frame.setVisible(true);
  135. frame.setLayout(null);
  136. frame.setLocationRelativeTo(null);
  137. }
  138.  
  139. public void actionPerformed(ActionEvent e) {
  140.  
  141. if(color_count < num){
  142. p_game.setVisible(true);
  143. Color randomColor = colors[new Random().nextInt(colors.length)];
  144. p_game.setBackground(randomColor);
  145. color.add(randomColor);
  146. color_count++;
  147. }
  148. else if(color_count == num){
  149. p_game.setVisible(false);
  150. b_red.setVisible(true);
  151. b_yellow.setVisible(true);
  152. b_orange.setVisible(true);
  153. b_green.setVisible(true);
  154. b_blue.setVisible(true);
  155. b_purple.setVisible(true);
  156. }
  157. if(e.getSource() == b_red){
  158. button.add(red);
  159. }
  160. if(e.getSource() == b_blue){
  161. button.add(blue);
  162. }
  163. if(e.getSource() == b_green){
  164. button.add(green);
  165. }
  166. if(e.getSource() == b_orange){
  167. button.add(orange);
  168. }
  169. if(e.getSource() == b_yellow){
  170. button.add(yellow);
  171. }
  172. if(e.getSource() == b_purple){
  173. button.add(purple);
  174. }
  175.  
  176. if(button.size() > 0){
  177.  
  178. try{
  179. for(int x = 0; x < color.size(); x++){
  180. if(button.get(x) != color.get(x)){
  181. l_game_over.setVisible(true);
  182. l_level.setVisible(false);
  183. b_back.setVisible(true);
  184. b_red.setVisible(false);
  185. b_yellow.setVisible(false);
  186. b_orange.setVisible(false);
  187. b_green.setVisible(false);
  188. b_blue.setVisible(false);
  189. b_purple.setVisible(false);
  190. timer.stop();
  191. break;
  192. // READ AND WRITE HIGHSCORE
  193. // RESET LEVEL
  194. }
  195. }
  196. }
  197.  
  198. catch(Exception e1){
  199.  
  200. }
  201. }
  202.  
  203. if(color.equals(button)){
  204. l_correct.setVisible(true);
  205. b_next.setVisible(true);
  206. b_red.setVisible(false);
  207. b_yellow.setVisible(false);
  208. b_orange.setVisible(false);
  209. b_green.setVisible(false);
  210. b_blue.setVisible(false);
  211. b_purple.setVisible(false);
  212. timer.stop();
  213. // f_play.setVisible(false);
  214. // new Game();
  215. }
  216.  
  217. if(e.getSource() == b_back) {
  218. String username = JOptionPane.showInputDialog(f_play, "Enter Name", "Add Highscore", JOptionPane.PLAIN_MESSAGE);
  219. f_play.dispose();
  220. new ColorFlash();
  221. }
  222. if(e.getSource() == b_next) {
  223. f_play.dispose();
  224. new Game();
  225.  
  226. }
  227. }
  228. }import java.awt.*;
  229. import java.awt.event.ActionEvent;
  230. import java.awt.event.ActionListener;
  231. import javax.swing.*;
  232. import java.io.*;
  233. import java.util.ArrayList;
  234. import java.util.Random;
  235.  
  236. public class Game implements ActionListener{
  237. static JFrame f_play, f_highscores, f_htp;
  238. private JButton b_red, b_blue, b_green, b_yellow, b_purple, b_orange, b_back, b_next;
  239. private JPanel p_game, p_play;
  240. private JLabel l_correct, l_game_over, l_level;
  241. ArrayList<Color> color = new ArrayList<Color>();
  242. ArrayList<Color> button = new ArrayList<Color>();
  243. Color red = Color.RED, orange = new Color(255, 128, 0), yellow = Color.YELLOW, green = new Color(0, 199, 13),
  244. blue = new Color(0, 77, 255), purple = new Color(170, 34, 228);
  245. int highscore = 0, rounds = 4, time = 1000;
  246. String name = "";
  247. Timer timer;
  248. public static int num = 0;
  249. int color_count = 0, buttonCount = 0;
  250. Color[] colors = {red, blue, green, yellow, purple, orange};
  251.  
  252. public Game() {
  253.  
  254. num++;
  255.  
  256. f_play = new JFrame("Play!");
  257. initializeFrame(f_play);
  258.  
  259. p_play = new JPanel();
  260. p_play.setSize(900,700);
  261. p_play.setVisible(true);
  262. p_play.setLayout(null);
  263. f_play.add(p_play);
  264.  
  265. b_red = new JButton("Red");
  266. p_play.add(b_red);
  267. b_red.setBounds(150,550,100,75);
  268. b_red.setBackground(red);
  269. b_red.setVisible(false);
  270. b_red.addActionListener(this);
  271.  
  272. b_orange = new JButton("Orange");
  273. p_play.add(b_orange);
  274. b_orange.setBounds(250,550,100,75);
  275. b_orange.setBackground(orange);
  276. b_orange.setVisible(false);
  277. b_orange.addActionListener(this);
  278.  
  279. b_yellow = new JButton("Yellow");
  280. p_play.add(b_yellow);
  281. b_yellow.setBounds(350,550,100,75);
  282. b_yellow.setBackground(yellow);
  283. b_yellow.setVisible(false);
  284. b_yellow.addActionListener(this);
  285.  
  286. b_green = new JButton("Green");
  287. p_play.add(b_green);
  288. b_green.setBounds(450,550,100,75);
  289. b_green.setBackground(green);
  290. b_green.setVisible(false);
  291. b_green.addActionListener(this);
  292.  
  293. b_blue = new JButton("Blue");
  294. p_play.add(b_blue);
  295. b_blue.setBounds(550,550,100,75);
  296. b_blue.setBackground(blue);
  297. b_blue.setVisible(false);
  298. b_blue.addActionListener(this);
  299.  
  300. b_purple = new JButton("Purple");
  301. p_play.add(b_purple);
  302. b_purple.setBounds(650,550,100,75);
  303. b_purple.setBackground(purple);
  304. b_purple.setVisible(false);
  305. b_purple.addActionListener(this);
  306.  
  307. p_game = new JPanel();
  308. p_play.add(p_game);
  309. p_game.setVisible(false);
  310. p_game.setBounds(250,80,400,400);
  311.  
  312. timer = new Timer(time, this);
  313. timer.start();
  314.  
  315. f_play.setVisible(true);
  316.  
  317. l_level = new JLabel("Level " + num);
  318. p_play.add(l_level);
  319. l_level.setBounds(390,200,200,80);
  320. l_level.setFont(new Font("Serif", Font.PLAIN, 44));
  321.  
  322. l_correct = new JLabel("Correct!");
  323. p_play.add(l_correct);
  324. l_correct.setVisible(false);
  325. l_correct.setBounds(368,100,150,100);
  326. l_correct.setFont(new Font("Serif", Font.PLAIN, 44));
  327.  
  328. l_game_over = new JLabel("Game Over!");
  329. p_play.add(l_game_over);
  330. l_game_over.setVisible(false);
  331. l_game_over.setBounds(300,100,300,100);
  332. l_game_over.setFont(new Font("Serif", Font.PLAIN, 44));
  333.  
  334. b_back = new JButton("Back to Main Menu");
  335. p_play.add(b_back);
  336. b_back.setBounds(350,550,200,75);
  337. b_back.setVisible(false);
  338. b_back.addActionListener(this);
  339.  
  340. b_next = new JButton("Next Level");
  341. p_play.add(b_next);
  342. b_next.setBounds(350,550,200,75);
  343. b_next.setVisible(false);
  344. b_next.addActionListener(this);
  345.  
  346. }
  347.  
  348. public static void howToPlay() {
  349. f_htp = new JFrame("How to Play");
  350. initializeFrame(f_htp);
  351. }
  352.  
  353. public static void highscores() {
  354. f_highscores = new JFrame("Highscores");
  355. Game.initializeFrame(f_highscores);
  356. }
  357.  
  358. public static void initializeFrame(JFrame frame) {
  359. frame.setSize(900,700);
  360. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  361. frame.setVisible(true);
  362. frame.setLayout(null);
  363. frame.setLocationRelativeTo(null);
  364. }
  365.  
  366. public void actionPerformed(ActionEvent e) {
  367.  
  368. if(color_count < num){
  369. p_game.setVisible(true);
  370. Color randomColor = colors[new Random().nextInt(colors.length)];
  371. p_game.setBackground(randomColor);
  372. color.add(randomColor);
  373. color_count++;
  374. }
  375. else if(color_count == num){
  376. p_game.setVisible(false);
  377. b_red.setVisible(true);
  378. b_yellow.setVisible(true);
  379. b_orange.setVisible(true);
  380. b_green.setVisible(true);
  381. b_blue.setVisible(true);
  382. b_purple.setVisible(true);
  383. }
  384. if(e.getSource() == b_red){
  385. button.add(red);
  386. }
  387. if(e.getSource() == b_blue){
  388. button.add(blue);
  389. }
  390. if(e.getSource() == b_green){
  391. button.add(green);
  392. }
  393. if(e.getSource() == b_orange){
  394. button.add(orange);
  395. }
  396. if(e.getSource() == b_yellow){
  397. button.add(yellow);
  398. }
  399. if(e.getSource() == b_purple){
  400. button.add(purple);
  401. }
  402.  
  403. if(button.size() > 0){
  404.  
  405. try{
  406. for(int x = 0; x < color.size(); x++){
  407. if(button.get(x) != color.get(x)){
  408. l_game_over.setVisible(true);
  409. l_level.setVisible(false);
  410. b_back.setVisible(true);
  411. b_red.setVisible(false);
  412. b_yellow.setVisible(false);
  413. b_orange.setVisible(false);
  414. b_green.setVisible(false);
  415. b_blue.setVisible(false);
  416. b_purple.setVisible(false);
  417. timer.stop();
  418. break;
  419. // READ AND WRITE HIGHSCORE
  420. // RESET LEVEL
  421. }
  422. }
  423. }
  424.  
  425. catch(Exception e1){
  426.  
  427. }
  428. }
  429.  
  430. if(color.equals(button)){
  431. l_correct.setVisible(true);
  432. b_next.setVisible(true);
  433. b_red.setVisible(false);
  434. b_yellow.setVisible(false);
  435. b_orange.setVisible(false);
  436. b_green.setVisible(false);
  437. b_blue.setVisible(false);
  438. b_purple.setVisible(false);
  439. timer.stop();
  440. // f_play.setVisible(false);
  441. // new Game();
  442. }
  443.  
  444. if(e.getSource() == b_back) {
  445. String username = JOptionPane.showInputDialog(f_play, "Enter Name", "Add Highscore", JOptionPane.PLAIN_MESSAGE);
  446. f_play.dispose();
  447. new ColorFlash();
  448. }
  449. if(e.getSource() == b_next) {
  450. f_play.dispose();
  451. new Game();
  452.  
  453. }
  454. }
  455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement