Advertisement
Guest User

Untitled

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