Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Conrad Pragnell
- package jracer;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import static java.lang.System.exit;
- import javax.swing.Icon;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- public class Racer extends JFrame
- {
- int points = 100;
- private final String CHOICES[] = {"Green Car", "Yellow Car", "Black Car"};
- String car = CHOICES[0];
- //create objects
- private JLabel lblEnter;
- private JLabel lblYourPoints;
- private JLabel lblPoints;
- private JLabel imgYourCar;
- private JLabel imgEnemyCar;
- private JTextField txtBet;
- private JButton btnRace;
- private JButton btnChooseCar;
- private Container container;
- public Racer()
- {
- container = this.getContentPane();
- //put objects in container
- lblEnter = new JLabel("Enter points to bet:");
- lblYourPoints = new JLabel("Total Points =");
- lblPoints = new JLabel("" + points);
- imgYourCar = new JLabel();
- imgEnemyCar = new JLabel();
- txtBet = new JTextField();
- btnRace = new JButton("Race!");
- btnChooseCar = new JButton("Choose Car");
- Icon enemyCar = new ImageIcon("H:\\CP30\\Ch.9\\White Car.png");
- imgEnemyCar.setIcon(enemyCar);
- Icon yourCar = new ImageIcon("H:\\CP30\\Ch.9\\" + car + ".png");
- imgYourCar.setIcon(yourCar);
- //add objects to container
- container.add(lblEnter);
- container.add(lblYourPoints);
- container.add(lblPoints);
- container.add(imgYourCar);
- container.add(imgEnemyCar);
- container.add(txtBet);
- container.add(btnRace);
- container.add(btnChooseCar);
- container.setLayout(null);
- //get roll button working
- ActionListener buttonListener = new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- points = race(points);
- }
- };
- btnRace.addActionListener(buttonListener);
- //get choose car button working
- ActionListener buttonListener3 = new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- chooseCar();
- }
- };
- btnChooseCar.addActionListener(buttonListener3);
- //set bounds for objects
- lblEnter.setBounds(30, 30, 130, 20);
- txtBet.setBounds(170, 30, 80, 20);
- btnRace.setBounds(470, 40, 100, 40);
- lblYourPoints.setBounds(450, 700, 100, 20 );
- lblPoints.setBounds(550, 700 , 50, 20);
- btnChooseCar.setBounds(470, 120, 100, 40);
- imgYourCar.setBounds(80, 650, 60, 120);
- imgEnemyCar.setBounds(300, 650, 60, 120);
- //set up frame
- this.setSize(600,800);
- this.setVisible(true);
- this.setTitle("Racing game");
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setResizable(false);
- }
- private int race(int points)
- {
- //create random numbers
- int yourRandom = 1 + (int)(Math.random() * 20);
- int enemyRandom = 1 + (int)(Math.random() * 20);
- yourRandom = yourRandom * 25;
- enemyRandom = enemyRandom * 25;
- //get number from the textBox
- String bet = txtBet.getText();
- //convert to int
- int betInt = Integer.parseInt(bet);
- //you can't bet more than you have
- if(betInt > points)
- {
- JOptionPane.showMessageDialog(null, "You have bet more points than "
- + "you have. Enter a valid bet.");
- }
- else if(betInt <= 0)
- JOptionPane.showMessageDialog(null, "You must bet at least 1 point"
- + "");
- else
- {
- //set bounds based off random numbers
- imgYourCar.setBounds(80, 650 - yourRandom, 60, 120);
- imgEnemyCar.setBounds(300, 650 - enemyRandom, 60, 120);
- //display win, loss, or tie
- if(yourRandom > enemyRandom)
- {
- points += betInt;
- JOptionPane.showMessageDialog(null, "You Win!");
- }
- else if(yourRandom == enemyRandom)
- {
- JOptionPane.showMessageDialog(null, "It's a tie");
- }
- else
- {
- points -= betInt;
- JOptionPane.showMessageDialog(null, "You lose!");
- }//end ifs
- lblPoints.setText("" + points);
- imgYourCar.setBounds(80, 650, 60, 120);
- imgEnemyCar.setBounds(300, 650, 60, 120);
- }//end if
- if(points == 0)
- {
- JOptionPane.showMessageDialog(null, "You ran out of points, you lose"
- + " the game.");
- exit(0);
- }//end if
- return points;
- }
- private void chooseCar()
- {
- //displays box so you can choose a car
- car = JOptionPane.showInputDialog(null, "", "Choose Car",
- JOptionPane.PLAIN_MESSAGE, null, CHOICES,
- CHOICES[0]).toString();
- //sets the car that corresponds to your choice
- Icon yourCar = new ImageIcon("H:\\CP30\\Ch.9\\" + car + ".png");
- imgYourCar.setIcon(yourCar);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement