Flappy Bird (Java实现)
运行效果:
代码:
1 package bird; 2 3 import java.awt.Graphics; 4 import java.awt.Image; 5 import java.awt.Toolkit; 6 import java.awt.event.KeyAdapter; 7 import java.awt.event.KeyEvent; 8 import java.io.BufferedReader; 9 import java.io.BufferedWriter; 10 import java.io.FileNotFoundException; 11 import java.io.FileReader; 12 import java.io.FileWriter; 13 import java.io.IOException; 14 import java.util.Random; 15 16 import javax.swing.JFrame; 17 import javax.swing.JOptionPane; 18 import javax.swing.JPanel; 19 20 21 public class Bird extends JPanel { 22 private final int WIDTH=288; 23 private int HEIGHT=512+112; 24 // Image img; 25 private Image bg,land,pipe_up,pipe_down,bird,score_h,score_t,score_n; 26 private int y=300,land_x=0,pipe_x=200,current_y=0,time=0,down=0,bg_x=0,t=0,score=0,yt=-1; 27 private int a = 0, b = 0, c = 0; 28 private int[] pipe_ys=new int[4]; 29 private Image [] score_number=new Image[10]; 30 private Random random; 31 Bird(){ 32 getBestScores() ; 33 Toolkit tool = this.getToolkit(); 34 bg= tool.getImage("images/bg_day.png"); 35 land=tool.getImage("images/land.png"); 36 pipe_up= tool.getImage("images/pipe_up.png"); 37 pipe_down= tool.getImage("images/pipe_down.png"); 38 bird= tool.getImage("images/bird0_01.png"); 39 random = new Random(); 40 for(int i=0;i<=9;i++){ 41 score_number[i]= tool.getImage("images/number_score_0"+i+".png"); 42 } 43 init(); 44 this.addKeyListener( 45 new KeyAdapter() { 46 public void keyPressed(KeyEvent e) { 47 int c = e.getKeyCode(); 48 //System.out.print(c); 49 50 switch (c) { 51 case KeyEvent.VK_LEFT : 52 53 break; 54 case KeyEvent.VK_RIGHT : 55 56 break; 57 case KeyEvent.VK_UP : 58 System.exit(0); 59 break; 60 case KeyEvent.VK_DOWN : 61 62 break; 63 case KeyEvent.VK_SPACE : 64 down=0; 65 if(time<=0)time=16; 66 break; 67 68 default : 69 // System.out.print("hey"); 70 71 } 72 } 73 }); 74 this.setFocusable(true); 75 } 76 void init(){ 77 78 y=300; 79 land_x=0; 80 pipe_x=200; 81 current_y=0; 82 time=0; 83 down=0; 84 bg_x=0; 85 t=0; 86 score=0; 87 yt=-1; 88 for(int i=0;i<=3;i++){ 89 pipe_ys[i]=-1* Math.abs(random.nextInt()) % 150; 90 } 91 } 92 public int get_Height(){return HEIGHT;} 93 public int get_Width(){return WIDTH;} 94 private void move(){ 95 while(true){ 96 moveImg(); 97 try { 98 Thread.sleep(10); 99 } catch (InterruptedException e) { 100 e.printStackTrace(); 101 } 102 moveBird(); 103 repaint(); 104 check(); 105 t++; 106 } 107 } 108 private void moveBird(){ 109 time--; 110 if(t%1==0){if(time>0){y-=4;}} 111 if(time<=-5&&y<=512-37){if(t%3==0){down+=1;y+=down;}} 112 } 113 private void moveImg(){ 114 bg_x--; 115 if(bg_x<-288)bg_x+=288; 116 land_x-=1; 117 if(land_x<-20)land_x+=23; 118 pipe_x-=1; 119 if(pipe_x<=-52){ 120 pipe_x+=200; 121 int ran = Math.abs(random.nextInt()) % 200; 122 ran=-ran; 123 pipe_ys[current_y]=ran; 124 current_y++; 125 current_y%=4; 126 } 127 } 128 private void check(){ 129 int temp=current_y; 130 for(int i=0;i<=3;i++){ 131 if(80+34>=pipe_x&&80<=pipe_x+52){ 132 if(yt!=current_y){score++;yt=current_y;} 133 if(y<=pipe_ys[temp]+320||y+25>=pipe_ys[temp]+450){ 134 gameOver(); 135 init(); 136 } 137 } 138 } 139 } 140 private int gameOver() { 141 142 if (score > a) { 143 c = b; 144 b = a; 145 a = score; 146 save(); 147 //System.exit(0); 148 } 149 else if (score > b) { 150 c = b; 151 b = score; 152 save(); 153 //System.exit(0); 154 return 0; 155 } 156 else if (score > c) { 157 c = score; 158 save(); 159 //System.exit(0); 160 return 0; 161 } 162 JOptionPane.showMessageDialog(null, "GAME OVER!\n\nBEST SCORE:\n"+a+"\n"+b+"\n"+c, "你死了(按任意键再来一次)", 163 JOptionPane.PLAIN_MESSAGE); 164 //System.exit(0); 165 return 0; 166 } 167 private void getBestScores() { 168 169 BufferedReader reader = null; 170 try { 171 reader = new BufferedReader(new FileReader("score.txt")); 172 } catch (FileNotFoundException e) { 173 e.printStackTrace(); 174 } 175 176 try { 177 a = Integer.parseInt(reader.readLine()); 178 b = Integer.parseInt(reader.readLine()); 179 c = Integer.parseInt(reader.readLine()); 180 } catch (NumberFormatException e) { 181 e.printStackTrace(); 182 } catch (IOException e) { 183 e.printStackTrace(); 184 } 185 try { 186 reader.close(); 187 } catch (IOException e) { 188 e.printStackTrace(); 189 } 190 /*String bestS = new String("Best Score: " + new Integer(a).toString() 191 + " " + new Integer(b).toString() + " " 192 + new Integer(c).toString());*/ 193 return ; 194 } 195 private int save() { 196 197 BufferedWriter writer = null; 198 try { 199 writer = new BufferedWriter(new FileWriter("score.txt")); 200 } catch (IOException e) { 201 e.printStackTrace(); 202 } 203 String d = new Integer(a).toString(), e = new Integer(b).toString(), 204 f = new Integer(c).toString(); 205 try { 206 writer.write(d); 207 writer.newLine(); 208 writer.write(e); 209 writer.newLine(); 210 writer.write(f); 211 writer.flush(); 212 writer.close(); 213 } catch (NumberFormatException ev) { 214 ev.printStackTrace(); 215 } catch (IOException ev) { 216 ev.printStackTrace(); 217 } 218 return 0; 219 } 220 private void showScore(Graphics g){ 221 int score_h=score/100,score_t=score%100/10,score_n=score%10; 222 if(score_h!=0){g.drawImage(score_number[score_h], 80,20,this);} 223 g.drawImage(score_number[score_t], 80+20,20,this); 224 g.drawImage(score_number[score_n], 80+20+20,20,this); 225 } 226 public void paint(Graphics g) {//Component 227 super.paintComponent(g); 228 //g.drawLine(x,y,200,200); 229 g.drawImage(bg, bg_x,0,this); 230 g.drawImage(bg, bg_x+288,0,this); 231 //g.drawImage(land_1, landx_1, HEIGHT-land_1.getHeight(this), WIDTH,WIDTH*land_1.getHeight(this)/land_1.getWidth(this), this); 232 233 int temp=current_y; 234 for(int i=0;i<=3;i++){ 235 g.drawImage(pipe_down,pipe_x+i*200,pipe_ys[temp],this); 236 g.drawImage(pipe_up,pipe_x+i*200,pipe_ys[temp]+450,this); 237 g.drawImage(bird,80,y,this); 238 temp++; 239 temp%=4; 240 } 241 g.drawImage(land, land_x,512,this); 242 showScore(g); 243 } 244 public static void main(String[] args) { 245 JFrame frame = new JFrame("(按UP键退出)"); 246 Bird b=new Bird(); 247 frame.getContentPane() 248 .add(b); 249 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 250 frame.setSize(b.get_Width(),b.get_Height()); 251 frame.setLocation(200, 100); 252 frame.setVisible(true); 253 b.move(); 254 } 255 256 }
素材:
bg_day.png
bird0_01.png
land.png
pipe_up.png
pipe_down.png
number_score_00.png~number_score_09.png (数字均为白色,图片背景为透明。为能够在博客中显示出来,特设置为灰色背景)
程序写于大三上学期。
2016.4.12更新博客。