forked from Arnob-sen/our_java_game_project_flappy_bird
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePanel.java
More file actions
71 lines (66 loc) · 2.5 KB
/
GamePanel.java
File metadata and controls
71 lines (66 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package FlappyBird;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class GamePanel extends JPanel {
private Bird bird;
private ArrayList<Rectangle> rects;
private FlappyBird fb;
private Font scoreFont, pauseFont;
public static final Color bg = new Color(204, 204, 255);
public static final int PIPE_W = 50, PIPE_H = 30;
private Image pipeHead, pipeLength;
public GamePanel(FlappyBird fb, Bird bird, ArrayList<Rectangle> rects) {
this.fb = fb;
this.bird = bird;
this.rects = rects;
scoreFont = new Font("Comic Sans MS", Font.BOLD, 18);
pauseFont = new Font("Arial Italic", Font.BOLD, 32);
try {
pipeHead = ImageIO.read(new File("pipe-p.png"));
pipeLength = ImageIO.read(new File("pipe_part.png"));
}
catch(IOException e) {
e.printStackTrace();
}
}
@Override
public void paintComponent(Graphics g) {
g.setColor(bg);
g.fillRect(0,0,FlappyBird.WIDTH,FlappyBird.HEIGHT);
bird.update(g);
g.setColor(Color.RED);
for(Rectangle r : rects) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.GREEN);
//g2d.fillRect(r.x, r.y, r.width, r.height);
AffineTransform old = g2d.getTransform();
g2d.translate(r.x+PIPE_W/2, r.y+PIPE_H/2);
if(r.y < FlappyBird.HEIGHT/2) {
g2d.translate(0, r.height);
g2d.rotate(Math.PI);
}
g2d.drawImage(pipeHead, -PIPE_W/2, -PIPE_H/2, GamePanel.PIPE_W, GamePanel.PIPE_H, null);
g2d.drawImage(pipeLength, -PIPE_W/2, PIPE_H/2, GamePanel.PIPE_W, r.height, null);
g2d.setTransform(old);
}
g.setFont(scoreFont);
g.setColor(Color.BLACK);
g.drawString("Score: "+fb.getScore(), 10, 20);
if(fb.paused()) {
g.setFont(pauseFont);
g.setColor(new Color(0,0,0,170));
g.drawString(" It's Your Flappy Bird! ", FlappyBird.WIDTH/2-200, FlappyBird.HEIGHT/2-100);
g.drawString(" Press Space To Begin ", FlappyBird.WIDTH/2-200, FlappyBird.HEIGHT/2+50);
}
}
}