Advertisement
Guest User

CarGameYeet

a guest
Jan 22nd, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. //Conrad Pragnell
  2. package jracer;
  3.  
  4. import java.awt.Container;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import static java.lang.System.exit;
  8. import javax.swing.Icon;
  9. import javax.swing.ImageIcon;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JOptionPane;
  14. import javax.swing.JTextField;
  15.  
  16.  
  17. public class Racer extends JFrame
  18. {
  19. int points = 100;
  20. private final String CHOICES[] = {"Green Car", "Yellow Car", "Black Car"};
  21. String car = CHOICES[0];
  22. //create objects
  23. private JLabel lblEnter;
  24. private JLabel lblYourPoints;
  25. private JLabel lblPoints;
  26. private JLabel imgYourCar;
  27. private JLabel imgEnemyCar;
  28. private JTextField txtBet;
  29. private JButton btnRace;
  30. private JButton btnChooseCar;
  31. private Container container;
  32. public Racer()
  33. {
  34. container = this.getContentPane();
  35. //put objects in container
  36. lblEnter = new JLabel("Enter points to bet:");
  37. lblYourPoints = new JLabel("Total Points =");
  38. lblPoints = new JLabel("" + points);
  39. imgYourCar = new JLabel();
  40. imgEnemyCar = new JLabel();
  41. txtBet = new JTextField();
  42. btnRace = new JButton("Race!");
  43. btnChooseCar = new JButton("Choose Car");
  44. Icon enemyCar = new ImageIcon("H:\\CP30\\Ch.9\\White Car.png");
  45. imgEnemyCar.setIcon(enemyCar);
  46. Icon yourCar = new ImageIcon("H:\\CP30\\Ch.9\\" + car + ".png");
  47. imgYourCar.setIcon(yourCar);
  48. //add objects to container
  49. container.add(lblEnter);
  50. container.add(lblYourPoints);
  51. container.add(lblPoints);
  52. container.add(imgYourCar);
  53. container.add(imgEnemyCar);
  54. container.add(txtBet);
  55. container.add(btnRace);
  56. container.add(btnChooseCar);
  57. container.setLayout(null);
  58.  
  59. //get roll button working
  60. ActionListener buttonListener = new ActionListener()
  61. {
  62. @Override
  63. public void actionPerformed(ActionEvent e)
  64. {
  65. points = race(points);
  66. }
  67. };
  68. btnRace.addActionListener(buttonListener);
  69. //get choose car button working
  70. ActionListener buttonListener3 = new ActionListener()
  71. {
  72. @Override
  73. public void actionPerformed(ActionEvent e)
  74. {
  75. chooseCar();
  76. }
  77. };
  78. btnChooseCar.addActionListener(buttonListener3);
  79.  
  80. //set bounds for objects
  81. lblEnter.setBounds(30, 30, 130, 20);
  82. txtBet.setBounds(170, 30, 80, 20);
  83. btnRace.setBounds(470, 40, 100, 40);
  84. lblYourPoints.setBounds(450, 700, 100, 20 );
  85. lblPoints.setBounds(550, 700 , 50, 20);
  86. btnChooseCar.setBounds(470, 120, 100, 40);
  87. imgYourCar.setBounds(80, 650, 60, 120);
  88. imgEnemyCar.setBounds(300, 650, 60, 120);
  89. //set up frame
  90. this.setSize(600,800);
  91. this.setVisible(true);
  92. this.setTitle("Racing game");
  93. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  94. this.setResizable(false);
  95. }
  96. private int race(int points)
  97. {
  98. //create random numbers
  99. int yourRandom = 1 + (int)(Math.random() * 20);
  100. int enemyRandom = 1 + (int)(Math.random() * 20);
  101. yourRandom = yourRandom * 25;
  102. enemyRandom = enemyRandom * 25;
  103. //get number from the textBox
  104. String bet = txtBet.getText();
  105. //convert to int
  106. int betInt = Integer.parseInt(bet);
  107. //you can't bet more than you have
  108. if(betInt > points)
  109. {
  110. JOptionPane.showMessageDialog(null, "You have bet more points than "
  111. + "you have. Enter a valid bet.");
  112. }
  113. else if(betInt <= 0)
  114. JOptionPane.showMessageDialog(null, "You must bet at least 1 point"
  115. + "");
  116. else
  117. {
  118. //set bounds based off random numbers
  119. imgYourCar.setBounds(80, 650 - yourRandom, 60, 120);
  120. imgEnemyCar.setBounds(300, 650 - enemyRandom, 60, 120);
  121. //display win, loss, or tie
  122. if(yourRandom > enemyRandom)
  123. {
  124. points += betInt;
  125. JOptionPane.showMessageDialog(null, "You Win!");
  126. }
  127. else if(yourRandom == enemyRandom)
  128. {
  129. JOptionPane.showMessageDialog(null, "It's a tie");
  130. }
  131. else
  132. {
  133. points -= betInt;
  134. JOptionPane.showMessageDialog(null, "You lose!");
  135. }//end ifs
  136. lblPoints.setText("" + points);
  137. imgYourCar.setBounds(80, 650, 60, 120);
  138. imgEnemyCar.setBounds(300, 650, 60, 120);
  139. }//end if
  140. if(points == 0)
  141. {
  142. JOptionPane.showMessageDialog(null, "You ran out of points, you lose"
  143. + " the game.");
  144. exit(0);
  145. }//end if
  146. return points;
  147.  
  148. }
  149.  
  150. private void chooseCar()
  151. {
  152. //displays box so you can choose a car
  153. car = JOptionPane.showInputDialog(null, "", "Choose Car",
  154. JOptionPane.PLAIN_MESSAGE, null, CHOICES,
  155. CHOICES[0]).toString();
  156. //sets the car that corresponds to your choice
  157. Icon yourCar = new ImageIcon("H:\\CP30\\Ch.9\\" + car + ".png");
  158. imgYourCar.setIcon(yourCar);
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement