taufiq123

Untitled

Dec 15th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. package flappyBird;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.Rectangle;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.KeyEvent;
  10. import java.awt.event.KeyListener;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.MouseListener;
  13. import java.util.ArrayList;
  14. import java.util.Random;
  15.  
  16. import javax.swing.JFrame;
  17. import javax.swing.Timer;
  18.  
  19. public class FlappyBird implements ActionListener, MouseListener, KeyListener
  20. {
  21.  
  22. public static FlappyBird flappyBird;
  23.  
  24. public final int WIDTH = 800, HEIGHT = 800;
  25.  
  26. public Renderer renderer;
  27.  
  28. public Rectangle bird;
  29.  
  30. public ArrayList<Rectangle> columns;
  31.  
  32. public int ticks, yMotion, score;
  33.  
  34. public boolean gameOver, started;
  35.  
  36. public Random rand;
  37.  
  38. public FlappyBird()
  39. {
  40. JFrame jframe = new JFrame();
  41. Timer timer = new Timer(20, this);
  42.  
  43. renderer = new Renderer();
  44. rand = new Random();
  45.  
  46. jframe.add(renderer);
  47. jframe.setTitle("Flappy Bird");
  48. jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  49. jframe.setSize(WIDTH, HEIGHT);
  50. jframe.addMouseListener(this);
  51. jframe.addKeyListener(this);
  52. jframe.setResizable(false);
  53. jframe.setVisible(true);
  54.  
  55. bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
  56. columns = new ArrayList<Rectangle>();
  57.  
  58. addColumn(true);
  59. addColumn(true);
  60. addColumn(true);
  61. addColumn(true);
  62.  
  63. timer.start();
  64. }
  65.  
  66. public void addColumn(boolean start)
  67. {
  68. int space = 300;
  69. int width = 100;
  70. int height = 50 + rand.nextInt(300);
  71.  
  72. if (start)
  73. {
  74. columns.add(new Rectangle(WIDTH + width + columns.size() * 300, HEIGHT - height - 120, width, height));
  75. columns.add(new Rectangle(WIDTH + width + (columns.size() - 1) * 300, 0, width, HEIGHT - height - space));
  76. }
  77. else
  78. {
  79. columns.add(new Rectangle(columns.get(columns.size() - 1).x + 600, HEIGHT - height - 120, width, height));
  80. columns.add(new Rectangle(columns.get(columns.size() - 1).x, 0, width, HEIGHT - height - space));
  81. }
  82. }
  83.  
  84. public void paintColumn(Graphics g, Rectangle column)
  85. {
  86. g.setColor(Color.green.darker());
  87. g.fillRect(column.x, column.y, column.width, column.height);
  88. }
  89.  
  90. public void jump()
  91. {
  92. if (gameOver)
  93. {
  94. bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
  95. columns.clear();
  96. yMotion = 0;
  97. score = 0;
  98.  
  99. addColumn(true);
  100. addColumn(true);
  101. addColumn(true);
  102. addColumn(true);
  103.  
  104. gameOver = false;
  105. }
  106.  
  107. if (!started)
  108. {
  109. started = true;
  110. }
  111. else if (!gameOver)
  112. {
  113. if (yMotion > 0)
  114. {
  115. yMotion = 0;
  116. }
  117.  
  118. yMotion -= 10;
  119. }
  120. }
  121.  
  122. @Override
  123. public void actionPerformed(ActionEvent e)
  124. {
  125. int speed = 10;
  126.  
  127. ticks++;
  128.  
  129. if (started)
  130. {
  131. for (int i = 0; i < columns.size(); i++)
  132. {
  133. Rectangle column = columns.get(i);
  134.  
  135. column.x -= speed;
  136. }
  137.  
  138. if (ticks % 2 == 0 && yMotion < 15)
  139. {
  140. yMotion += 2;
  141. }
  142.  
  143. for (int i = 0; i < columns.size(); i++)
  144. {
  145. Rectangle column = columns.get(i);
  146.  
  147. if (column.x + column.width < 0)
  148. {
  149. columns.remove(column);
  150.  
  151. if (column.y == 0)
  152. {
  153. addColumn(false);
  154. }
  155. }
  156. }
  157.  
  158. bird.y += yMotion;
  159.  
  160. for (Rectangle column : columns)
  161. {
  162. if (column.y == 0 && bird.x + bird.width / 2 > column.x + column.width / 2 - 10 && bird.x + bird.width / 2 < column.x + column.width / 2 + 10)
  163. {
  164. score++;
  165. }
  166.  
  167. if (column.intersects(bird))
  168. {
  169. gameOver = true;
  170.  
  171. if (bird.x <= column.x)
  172. {
  173. bird.x = column.x - bird.width;
  174.  
  175. }
  176. else
  177. {
  178. if (column.y != 0)
  179. {
  180. bird.y = column.y - bird.height;
  181. }
  182. else if (bird.y < column.height)
  183. {
  184. bird.y = column.height;
  185. }
  186. }
  187. }
  188. }
  189.  
  190. if (bird.y > HEIGHT - 120 || bird.y < 0)
  191. {
  192. gameOver = true;
  193. }
  194.  
  195. if (bird.y + yMotion >= HEIGHT - 120)
  196. {
  197. bird.y = HEIGHT - 120 - bird.height;
  198. gameOver = true;
  199. }
  200. }
  201.  
  202. renderer.repaint();
  203. }
  204.  
  205. public void repaint(Graphics g)
  206. {
  207. g.setColor(Color.cyan);
  208. g.fillRect(0, 0, WIDTH, HEIGHT);
  209.  
  210. g.setColor(Color.orange);
  211. g.fillRect(0, HEIGHT - 120, WIDTH, 120);
  212.  
  213. g.setColor(Color.green);
  214. g.fillRect(0, HEIGHT - 120, WIDTH, 20);
  215.  
  216. g.setColor(Color.red);
  217. g.fillRect(bird.x, bird.y, bird.width, bird.height);
  218.  
  219. for (Rectangle column : columns)
  220. {
  221. paintColumn(g, column);
  222. }
  223.  
  224. g.setColor(Color.white);
  225. g.setFont(new Font("Arial", 1, 100));
  226.  
  227. if (!started)
  228. {
  229. g.drawString("Click to start!", 75, HEIGHT / 2 - 50);
  230. }
  231.  
  232. if (gameOver)
  233. {
  234. g.drawString("Game Over!", 100, HEIGHT / 2 - 50);
  235. }
  236.  
  237. if (!gameOver && started)
  238. {
  239. g.drawString(String.valueOf(score), WIDTH / 2 - 25, 100);
  240. }
  241. }
  242.  
  243. public static void main(String[] args)
  244. {
  245. flappyBird = new FlappyBird();
  246. }
  247.  
  248. @Override
  249. public void mouseClicked(MouseEvent e)
  250. {
  251. jump();
  252. }
  253.  
  254. @Override
  255. public void keyReleased(KeyEvent e)
  256. {
  257. if (e.getKeyCode() == KeyEvent.VK_SPACE)
  258. {
  259. jump();
  260. }
  261. }
  262.  
  263. @Override
  264. public void mousePressed(MouseEvent e)
  265. {
  266. }
  267.  
  268. @Override
  269. public void mouseReleased(MouseEvent e)
  270. {
  271. }
  272.  
  273. @Override
  274. public void mouseEntered(MouseEvent e)
  275. {
  276. }
  277.  
  278. @Override
  279. public void mouseExited(MouseEvent e)
  280. {
  281. }
  282.  
  283. @Override
  284. public void keyTyped(KeyEvent e)
  285. {
  286.  
  287. }
  288.  
  289. @Override
  290. public void keyPressed(KeyEvent e)
  291. {
  292.  
  293. }
  294.  
  295. }
Advertisement
Add Comment
Please, Sign In to add comment