Advertisement
nawamkihafahd

Untitled

Dec 14th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. package RedHazard;
  2.  
  3.  
  4. import java.awt.BasicStroke;
  5. import java.awt.Color;
  6. import java.awt.Font;
  7. import java.awt.Graphics2D;
  8. import java.awt.RenderingHints;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.KeyEvent;
  12. import java.awt.event.KeyListener;
  13. import java.util.Random;
  14.  
  15. import javax.swing.JFrame;
  16. import javax.swing.Timer;
  17. public class Redhazard implements ActionListener, KeyListener
  18. {
  19. public static Redhazard rh;
  20. public int width = 700, height = 700;
  21. public int minscore = -5;
  22. public int maxscore = 10;
  23. public Renderer renderer;
  24. public Ship ship;
  25. public Obstacle obstacle[];
  26. public boolean z, left, right;
  27. public JFrame jframe;
  28. public Random random;
  29. public int gameStatus = 0;
  30. public Goal goal;
  31. public Redhazard()
  32. {
  33. Timer timer = new Timer(20, this);
  34. random = new Random();
  35. jframe = new JFrame("RedHazard");
  36. renderer = new Renderer();
  37. jframe.setSize(width + 15, height + 15);
  38. jframe.setVisible(true);
  39. jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40. jframe.add(renderer);
  41. jframe.addKeyListener(this);
  42. timer.start();
  43. }
  44. public void start()
  45. {
  46. gameStatus = 1;
  47. ship = new Ship(this);
  48. obstacle = new Obstacle[8];
  49. obstacle[1] = new Obstacle(this, 1);
  50. obstacle[2] = new Obstacle(this, 1);
  51. obstacle[3] = new Obstacle(this, 1);
  52. obstacle[4] = new Obstacle(this, 1);
  53. obstacle[0] = new Obstacle(this, 0);
  54. obstacle[5] = new Obstacle(this, 0);
  55. obstacle[6] = new Obstacle(this, 0);
  56. obstacle[7] = new Obstacle(this, 0);
  57. goal = new Goal(this);
  58. }
  59. public void update()
  60. {
  61. if(ship.score <= minscore)
  62. {
  63. gameStatus = 2;
  64. }
  65. if(ship.score >= maxscore)
  66. {
  67. gameStatus = 2;
  68. }
  69. if(left)
  70. {
  71. ship.move(true);
  72. }
  73. if(right)
  74. {
  75. ship.move(false);
  76. }
  77. obstacle[0].update(ship);
  78. obstacle[1].update(ship);
  79. obstacle[2].update(ship);
  80. obstacle[3].update(ship);
  81. obstacle[4].update(ship);
  82. obstacle[5].update(ship);
  83. obstacle[6].update(ship);
  84. obstacle[7].update(ship);
  85. goal.update(ship);
  86. }
  87. public void render(Graphics2D g)
  88. {
  89. g.setColor(Color.BLACK);
  90. g.fillRect(0, 0, width, height);
  91. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  92. if(gameStatus == 0)
  93. {
  94. g.setColor(Color.WHITE);
  95. g.setFont(new Font("Arial", 1, 50));
  96. g.drawString("Red Hazard", width / 2 - 75, 50);
  97. g.setFont(new Font("Arial", 1, 30));
  98. g.drawString("Press Space to Play", width / 2 - 150, height / 2 - 25);
  99. }
  100. if(gameStatus == 1)
  101. {
  102. g.setColor(Color.WHITE);
  103. g.setFont(new Font("Arial", 1, 16));
  104. g.drawString(String.valueOf(ship.score), width / 2 - 90, 50);
  105. ship.render(g);
  106. obstacle[0].render(g);
  107. obstacle[1].render(g);
  108. obstacle[2].render(g);
  109. obstacle[3].render(g);
  110. obstacle[4].render(g);
  111. obstacle[5].render(g);
  112. obstacle[6].render(g);
  113. obstacle[7].render(g);
  114. goal.render(g);
  115. }
  116. if(gameStatus == 2)
  117. {
  118. if(ship.score <= minscore)
  119. {
  120. g.setColor(Color.RED);
  121. g.setFont(new Font("Arial", 1, 50));
  122. g.drawString("You Lose", width / 2 - 170, 200);
  123. }
  124. else if(ship.score >= maxscore)
  125. {
  126. g.setColor(Color.GREEN);
  127. g.setFont(new Font("Arial", 1, 50));
  128. g.drawString("You Win", width / 2 - 170, 200);
  129. }
  130. g.setColor(Color.GREEN);
  131. g.setFont(new Font("Arial", 1, 30));
  132. g.drawString("Press Space to Play Again", width / 2 - 170, height / 2 - 25);
  133. }
  134. }
  135. @Override
  136. public void actionPerformed(ActionEvent e)
  137. {
  138. if(gameStatus == 1)
  139. {
  140. update();
  141. }
  142. renderer.repaint();
  143. }
  144. public static void main(String[] args)
  145. {
  146. rh = new Redhazard();
  147. }
  148. @Override
  149. public void keyPressed(KeyEvent e)
  150. {
  151. int id = e.getKeyCode();
  152. if(id == KeyEvent.VK_LEFT)
  153. {
  154. left = true;
  155. }
  156. else if(id == KeyEvent.VK_RIGHT)
  157. {
  158. right = true;
  159. }
  160. else if(id == KeyEvent.VK_SPACE)
  161. {
  162. if(gameStatus == 2 || gameStatus == 0)
  163. {
  164. gameStatus = 1;
  165. start();
  166. }
  167. }
  168. }
  169. @Override
  170. public void keyReleased(KeyEvent e)
  171. {
  172. int id = e.getKeyCode();
  173. if(id == KeyEvent.VK_LEFT)
  174. {
  175. left = false;
  176. }
  177. else if(id == KeyEvent.VK_RIGHT)
  178. {
  179. right = false;
  180. }
  181. }
  182. @Override
  183. public void keyTyped(KeyEvent e)
  184. {
  185. }
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement