1 packagebird;2
3 importjava.awt.Graphics;4 importjava.awt.Image;5 importjava.awt.Toolkit;6 importjava.awt.event.KeyAdapter;7 importjava.awt.event.KeyEvent;8 importjava.io.BufferedReader;9 importjava.io.BufferedWriter;10 importjava.io.FileNotFoundException;11 importjava.io.FileReader;12 importjava.io.FileWriter;13 importjava.io.IOException;14 importjava.util.Random;15
16 importjavax.swing.JFrame;17 importjavax.swing.JOptionPane;18 importjavax.swing.JPanel;19
20
21 public class Bird extendsJPanel {22 private final int WIDTH=288;23 private int HEIGHT=512+112;24 //Image img;
25 privateImage 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 privateRandom 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 = newRandom();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 newKeyAdapter() {46 public voidkeyPressed(KeyEvent e) {47 int c =e.getKeyCode();48 //System.out.print(c);
49
50 switch(c) {51 caseKeyEvent.VK_LEFT :52
53 break;54 caseKeyEvent.VK_RIGHT :55
56 break;57 caseKeyEvent.VK_UP :58 System.exit(0);59 break;60 caseKeyEvent.VK_DOWN :61
62 break;63 caseKeyEvent.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 voidinit(){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(){returnHEIGHT;}93 public int get_Width(){returnWIDTH;}94 private voidmove(){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 voidmoveBird(){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 voidmoveImg(){114 bg_x--;115 if(bg_x=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 intgameOver() {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 voidgetBestScores() {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 intsave() {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 = newInteger(b).toString(),204 f = newInteger(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 voidshowScore(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 voidmain(String[] args) {245 JFrame frame = new JFrame("(按UP键退出)");246 Bird b=newBird();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 }