📄 gamepanel.java
字号:
import java.awt.*;import java.util.Vector;import javax.swing.*;/** * * @author Roselyn, Abi, KAt, Heryze */public class GamePanel extends JPanel { Image[] images; int[] prizes; int currentImage = 0; String category[]; int totalCash = 0; int randomCat; int turnsLeft = 5; String playerAnswer = ""; int randomAnswer; String clue = ""; Main m; int round; //possible words that will appear per category. String[][] answers = new String[][]{ {"Rico Yan","Raul Ponay", "Angel Locsin", "Say Alonzo", "Mariah Carey", "Gloria Arroyo","Jose Rizal", "Paris Hilton", "Piolo Pascual", "Adam Sandler", "Anne Curtis", "Sam Milby"}, {"Tortang Talong", "Zagu", "Bibingka", "Kunchinta", "Mangga", "Balot", "Gulaman", "Siomai", "Fishball", "Red Tea", "Coffee", "Rice", "Ice Cream", "Adobong Baboy", "Menudo", "Chicken Curry", "Cheese Stick"}, {"Comfort Room","Tayuman", "Laboratory", "Cuneta", "Dapitan", "Megamall", "Quiapo", "Luneta", "Intramuros", "Lacson", "Cubao", "Sampaloc"}, {"Vietman", "Cambodia", "Bataan", "Zamboanga", "Germany", "France", "Egypt", "Bohol", "Palawan", "Leyte", "Manila", "Davao", "Cebu", "Singapore", "Thailand", "Japan", "Paris", "New York", "Hongkong", "China", "California", "Taiwan"}, {"Pants", "Scarf", "Hanger", "Radio", "Paper", "Umbrella","Toothbrush", "Toothpaste", "Thumbler", "Book", "Cellphone", "Jacket", "Watch", "Chair", "Pillow", "Clock", "Table", "Curtain", "Mirror", "Computer", "Carpet"} }; Vector consonant = new Vector(); Vector vowel = new Vector(); /* * Constructor, loads all the images required in the game * initializes, prizes, questions */ public GamePanel(Main m, int round, int initialCash) { totalCash = initialCash; this.round = round; this.m = m; images = new Image[12]; images[0] = Toolkit.getDefaultToolkit().getImage("circle1.jpg"); images[1] = Toolkit.getDefaultToolkit().getImage("circle2.jpg"); images[2] = Toolkit.getDefaultToolkit().getImage("circle3.jpg"); images[3] = Toolkit.getDefaultToolkit().getImage("circle4.jpg"); images[4] = Toolkit.getDefaultToolkit().getImage("circle5.jpg"); images[5] = Toolkit.getDefaultToolkit().getImage("circle6.jpg"); images[6] = Toolkit.getDefaultToolkit().getImage("circle7.jpg"); images[7] = Toolkit.getDefaultToolkit().getImage("circle8.jpg"); images[8] = Toolkit.getDefaultToolkit().getImage("circle9.jpg"); images[9] = Toolkit.getDefaultToolkit().getImage("circle10.jpg"); images[10] = Toolkit.getDefaultToolkit().getImage("circle11.jpg"); images[11] = Toolkit.getDefaultToolkit().getImage("circle12.jpg"); prizes = new int[12]; prizes[0] = 100000; prizes[1] = 0; prizes[2] = 2000; prizes[3] = 5000; prizes[4] = 10000; prizes[5] = 0; prizes[6] = 7000; prizes[7] = 4000; prizes[8] = 8000; prizes[9] = 0; prizes[10] = 9000; prizes[11] = 0; category = new String[5]; category[0] = "Same Name"; category[1] = "Food and Drinks"; category[2] = "Places"; category[3] = "On the Map"; category[4] = "Things"; String[] sc = new String[]{ "B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z" }; String[] sv = new String[]{ "A", "E", "I", "O", "U" }; for (int i = 0; i < sc.length; i++) { consonant.add(sc[i]); } for (int i = 0; i < sv.length; i++) { vowel.add(sv[i]); } randomCat = (int) (Math.random() * 10) % 5; randomAnswer = (int) (Math.random() * 10) % 4; for (int i = 0; i < answers[randomCat][randomAnswer].length(); i++) { if (answers[randomCat][randomAnswer].charAt(i) == ' ') { clue += " "; } else { clue += "_"; } } } /* * generates a random # from 0 to 11 to be used * for drawing image. Also shows an input dialog * for asking a question, if the answered correctly * total cash will be incremented */ public void spinTheWheel() { int lastImage = ((int) (Math.random() * 10)) % 3 + (int) (Math.random() * 10); lastImage += 12; int delay = 200; //int lastImage = 0; System.out.println(answers[randomCat][randomAnswer]); for (currentImage = 0; currentImage < lastImage; currentImage++) { //System.out.println(currentImage); try { Thread.sleep(delay); } catch (InterruptedException ex) { } repaint(); if (currentImage == 8) { delay = delay + 100; } if (currentImage == 12) { delay = delay + 200; } } System.out.println("asdf:" + lastImage / 2); Vector<String> choices = new Vector<String>(); if (!consonant.isEmpty()) { choices.add("Guess a consonant"); } if (!vowel.isEmpty() && totalCash >= 1000) { choices.add("Buy a vowel"); } choices.add("Solve"); String choice = null; switch (currentImage % 12) { case 0: case 2: case 3: case 4: case 6: case 7: case 8: case 10: boolean notSelected = true; while (notSelected) { choice = (String) JOptionPane.showInputDialog(this, "You won " + prizes[currentImage % 12], "Selection", JOptionPane.QUESTION_MESSAGE, null, choices.toArray(), choices.firstElement()); if (choice == null) { notSelected = true; } else { if (choice.equals("Guess a consonant")) { boolean correct = false; boolean hasNoInput = true; String c = null; while (hasNoInput) { c = (String) JOptionPane.showInputDialog(this, "Select a consonant", "Select", JOptionPane.QUESTION_MESSAGE, null, consonant.toArray(), consonant.firstElement()); if (c != null) { hasNoInput = false; } } for (int i = 0; i < answers[randomCat][randomAnswer].length(); i++) { if (c.equalsIgnoreCase(answers[randomCat][randomAnswer].charAt(i) + "")) { char[] sa = clue.toCharArray(); sa[i] = c.charAt(0); clue = new String(sa); correct = true; } } if (!correct) { turnsLeft--; } if (correct) { for (int i = 0; i < consonant.size(); i++) { String s = (String) consonant.elementAt(i); if (s.charAt(0) == c.charAt(0)) { consonant.remove(i); } } totalCash += prizes[currentImage % 12]; } } if (choice.equals("Solve")) { String a = JOptionPane.showInputDialog("Enter your answer"); if (a.equalsIgnoreCase(answers[randomCat][randomAnswer])) { totalCash += prizes[currentImage % 12]; if (round != 3) { round++; JOptionPane.showMessageDialog(this, "Get Ready for the next round"); m.startGame(round, totalCash); } else { JOptionPane.showMessageDialog(this, "Congratulations! You Won! SMILE NAMAN DYAN!" + totalCash + ". Claim your price at the Dean's Office. Goodluck!"); m.loadMainMenu(); } } else turnsLeft--; } if (choice.equals("Buy a vowel")) { String c = null; boolean hasNoInput = true; boolean correct = false; while (hasNoInput) { c = (String) JOptionPane.showInputDialog(this, "Select a vowel", "Select", JOptionPane.QUESTION_MESSAGE, null, vowel.toArray(), vowel.firstElement()); if (c != null) { hasNoInput = false; } } for (int i = 0; i < answers[randomCat][randomAnswer].length(); i++) { if (c.equalsIgnoreCase(answers[randomCat][randomAnswer].charAt(i) + "")) { char[] sa = clue.toCharArray(); sa[i] = c.charAt(0); clue = new String(sa); correct = true; totalCash = totalCash - 1000; } } if (correct) { vowel.remove(c); } } notSelected = false; } } break; case 5: case 9: JOptionPane.showMessageDialog(this, "BEHLAT!!! You Lose a Turn!!!"); turnsLeft--; break; case 11: case 1: JOptionPane.showMessageDialog(this, "SORRY, La ka ng PERA! I short Bankrupt!!!"); totalCash = 0; } if (clue.equalsIgnoreCase(answers[randomCat][randomAnswer])) { if (round != 3) { round++; JOptionPane.showMessageDialog(this, "Get Ready for the next round"); m.startGame(round,totalCash); } else { JOptionPane.showMessageDialog(this, "Congratulations! You won " + totalCash + ". Claim your price at the Dean's Office. Goodluck!"); m.loadMainMenu(); } } if (turnsLeft == 0) { JOptionPane.showMessageDialog(this, "Game Over! Thank you for playing"); m.loadMainMenu(); } Main.spun(false); Main.labelTotalCash.setText("Total Cash: " + totalCash); repaint(); } /* * Displays the image */ @Override protected void paintComponent(Graphics g) { g.drawImage(images[currentImage % 12], 0, 0, this); g.drawString(category[randomCat], 10, 10); g.drawString("Turns Left: " + turnsLeft, 10, 23); g.drawString(clue, 10, 43); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -