Computer Science Canada who wants to be a millionaire help |
Author: | edkedned [ Mon Jun 13, 2011 11:27 pm ] |
Post subject: | who wants to be a millionaire help |
Hello ,I need help with my who wants to be a millionaire game...I am trying to figure out how to change screens from after a question is answered correctly and the program keeps messing up..and if anyone could also suggest an idea on the easiest way to implement phone a friend in the game // The "Millionaire" class. import java.applet.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Millionaire extends Applet implements ActionListener { // Place instance variables here AudioClip background; Image menuPic, gameInt, game_overPic, correct; ImageIcon start_button, rules_button, fifty_fifty, phoneFriend, switchQ; byte screen = 0, question = 0; JButton start_game, instructions, cont_game; JButton clicked = null; JButton chA, chB, chC, chD; JButton fiftyF, sQ, paF; //counters byte question_count = 0; int amount_earning = 0; boolean right_answer = false; boolean wrong = false; public void init () { //menuscreen picture menuPic = getImage (getCodeBase (), "images\\menu_screen.jpg"); gameInt = getImage (getCodeBase (), "images\\game_interfaced.jpg"); game_overPic = getImage (getCodeBase (), "images\\game_over.jpg"); correct = getImage (getCodeBase (), "correct.gif"); Color lightblue = new Color (160, 32, 240); start_button = new ImageIcon ("images\\start_button.jpg"); rules_button = new ImageIcon ("images\\rules_button.jpg"); background = getAudioClip (getCodeBase (), "audios\\question.wav"); background.loop (); //fires layout manager setLayout (null); start_game = new JButton (start_button); instructions = new JButton (rules_button); start_game.addActionListener (this); instructions.addActionListener (this); start_game.setBounds (15, 721, 300, 80); instructions.setBounds (390, 730, 295, 68); add (start_game); add (instructions); } // init method public void life_line () { fifty_fifty = new ImageIcon ("images\\50_button.jpg"); phoneFriend = new ImageIcon ("images\\PhoneAFriend_button.jpg"); switchQ = new ImageIcon ("images\\sQ_button.jpg"); fiftyF = new JButton (fifty_fifty); sQ = new JButton (switchQ); paF = new JButton (phoneFriend); fiftyF.addActionListener (this); sQ.addActionListener (this); paF.addActionListener (this); fiftyF.setBounds (175, 15, 90, 50); sQ.setBounds (308, 15, 90, 50); paF.setBounds (35, 15, 90, 50); add (fiftyF); add (sQ); add (paF); } public void correct () { screen = 5; cont_game = new JButton ("CONTINUE"); cont_game.setBounds (480, 730, 340, 48); cont_game.setForeground (Color.white); cont_game.setBackground (Color.magenta); cont_game.addActionListener (this); add (cont_game); validate (); repaint (); removeAll (); } public void question_one () { question = 1; question_count = 1; chA = new JButton ("Basketball"); chB = new JButton ("Hockey"); chC = new JButton ("Soccer"); chD = new JButton ("Wrestling"); chA.setBounds (20, 658, 340, 48); chB.setBounds (420, 658, 340, 48); chC.setBounds (20, 730, 340, 48); chD.setBounds (420, 730, 340, 48); chA.setForeground (Color.white); chA.setBackground (Color.black); chB.setForeground (Color.white); chB.setBackground (Color.black); chC.setForeground (Color.white); chC.setBackground (Color.black); chD.setForeground (Color.white); chD.setBackground (Color.black); chA.addActionListener (this); chB.addActionListener (this); chC.addActionListener (this); chD.addActionListener (this); add (chA); add (chB); add (chC); add (chD); validate (); repaint (); } public void question_two () { screen = 1; question = 2; question_count = 2; chA = new JButton ("Ludge"); chB = new JButton ("Triathlon"); chC = new JButton ("RC Skiing"); chD = new JButton ("Biathlon"); chA.setBounds (20, 658, 340, 48); chB.setBounds (420, 658, 340, 48); chC.setBounds (20, 720, 340, 48); chD.setBounds (420, 720, 340, 48); chA.setForeground (Color.white); chA.setBackground (Color.black); chB.setForeground (Color.white); chB.setBackground (Color.black); chC.setForeground (Color.white); chC.setBackground (Color.black); chD.setForeground (Color.white); chD.setBackground (Color.black); chA.addActionListener (this); chB.addActionListener (this); chC.addActionListener (this); chD.addActionListener (this); add (chA); add (chB); add (chC); add (chD); validate (); repaint (); } public void phone_friend () { } public void fifty_fifty (JButton test) { if (question == 1) { screen = 1; question = 1; question_count = 1; chB = new JButton ("Hockey"); chC = new JButton ("Soccer"); question_one (); chA.setEnabled (false); chB.setEnabled (false); repaint (); } } public void check_answer (JButton test) { if (question == 1) { if (test == chB) { right_answer = true; amount_earning = 100; try { Thread.sleep (500); } catch (InterruptedException e) { } repaint (); } else { wrong = true; removeAll (); repaint (); } } if (question == 2) { if (test == chD) { right_answer = true; amount_earning = 200; try { Thread.sleep (500); } catch (InterruptedException e) { } question = 3; removeAll (); repaint (); } else { screen = 3; removeAll (); repaint (); } } } public void actionPerformed (ActionEvent e) { JButton clicked = (JButton) e.getSource (); if (start_game == clicked) { background.play (); screen = 1; removeAll (); question_one (); question = 1; } else { check_answer (clicked); correct (); if (cont_game == clicked) { screen = 1; } } } //check_answer (clicked); public void paint (Graphics g) { Font f = new Font ("Cambria", Font.BOLD, 35); g.setFont (f); if (screen == 0) { //Intro Page .... Game start .... g.drawImage (menuPic, 0, 0, this); background.play (); } if (screen == 1) { //Game Interface g.drawImage (gameInt, 0, 0, this); } if (question == 1) { try { Thread.sleep (500); } catch (InterruptedException e) { } g.setColor (Color.white); g.drawString ("Which Sport Begins With A Face Off?", 45, 600); chA.repaint (); chB.repaint (); chC.repaint (); chD.repaint (); } if (question == 2) { f = new Font ("Cambria", Font.BOLD, 25); g.setFont (f); try { Thread.sleep (500); } catch (InterruptedException e) { } g.setColor (Color.white); g.drawString ("Cross country skiing and rifle shooting make up which sport? ", 45, 600); chA.repaint (); chB.repaint (); chC.repaint (); chD.repaint (); } if (question == 3) { f = new Font ("Cambria", Font.BOLD, 25); g.setFont (f); try { Thread.sleep (500); } catch (InterruptedException e) { } g.setColor (Color.white); g.drawString (" What company 's logo is called the 'swoosh'? ", 45, 600); chA.repaint (); chB.repaint (); chC.repaint (); chD.repaint (); } if (amount_earning == 100) { g.setColor (Color.red); g.drawRect (800, 730, 130, 50); } if (wrong == true) { g.drawImage (game_overPic, 0, 0, this); } if (screen == 5) { g.drawImage (correct, 0, 0, this); cont_game.repaint (); } } } // paint method // Millionaire class |
Author: | Tony [ Mon Jun 13, 2011 11:36 pm ] |
Post subject: | RE:who wants to be a millionaire help |
Before you get too far into this.. notice how your public void question_one () and public void question_two () are _almost_ the same? Abstract the common parts into a single function, and load whatever differences from arguments/arrays/data-file/whatever-you-want. This will also save you from having if (question == 1) else (question == 2) else (...) x20 madness. Basically it's a bit more complicated to set up, but at the end you end up writing 20 times less code. |
Author: | edkedned [ Tue Jun 14, 2011 9:39 am ] |
Post subject: | RE:who wants to be a millionaire help |
Oh ..ok..could you please explain to me how i would do that? |
Author: | apython1992 [ Tue Jun 14, 2011 11:57 am ] |
Post subject: | Re: RE:who wants to be a millionaire help |
Tony @ Mon Jun 13, 2011 11:36 pm wrote: Abstract the common parts into a single function
You have two functions that do pretty much the same thing. Can you tell what is common between the two functions? |
Author: | edkedned [ Tue Jun 14, 2011 12:48 pm ] |
Post subject: | RE:who wants to be a millionaire help |
The buttons and their locations |
Author: | apython1992 [ Tue Jun 14, 2011 1:10 pm ] |
Post subject: | RE:who wants to be a millionaire help |
So write this into one function, and determine the colour and location of the buttons based on a parameter that is passed in. |