超级精简的flappy Bird.

package testPackage;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class FlappyBird extends JFrame implements MouseListener{
 
 
 private JButton jbStart =new JButton("Start");
 private JButton jbStop=new JButton("Stop");
 private JPanel jp1=new JPanel();
 private JLabel jl=new JLabel("Score: ");
 
 final Lines l=new Lines();
 final Thread th1=new Thread(l);
 
 public FlappyBird(){
  setLayout(new BorderLayout());
  addMouseListener(this);
  jbStart.addActionListener(new ActionListener() {   
   @Override
   public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    th1.start();
   }
  });
  
  jbStop.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    th1.stop();
   }
  });
  add(l,BorderLayout.CENTER);
  
  jp1.setLayout(new FlowLayout());
  jp1.add(jbStart);
  jp1.add(jbStop);
  jp1.add(jl);
  
  add(jp1,BorderLayout.SOUTH);
 }

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  FlappyBird frame =new FlappyBird();
  frame.setVisible(true);
  frame.setSize(1366, 768);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 
 class Lines extends JPanel implements Runnable{
  private int []upLines=new int [10];
  private int []downLines=new int[10];
  protected int bx=0,by=350;
  
  Lines(){
   int k=0;
   do{
    upLines[k]=(int)(Math.random()*450);
    downLines[k]=upLines[k]+150;
    if(k!=0&&Math.abs(upLines[k]-upLines[k-1])>150)
     continue;
    k++;
   }while(k<10);
  }
  @Override
  public void run() {
   // TODO Auto-generated method stub   
   while(true){ 
    try{
     bx+=10;
     by+=10;
    for(int i=0;i<10;i++)
     if(bx>=i*100){
      jl.setText("Score: "+i);
      //break;
     }
     repaint();
     if(!isClick()){
      System.out.println("Click!");
     }
     Thread.sleep(100);
    }catch(InterruptedException e){
     e.printStackTrace();
    }
   }
  }
  public void up(){
   bx+=10;
   by-=50;
   repaint();
  }
  
  public boolean isClick(){
   boolean is=true;
   for(int i=0;i<8;i++){
    if(Math.abs(bx-(i+1)*100)<20&&Math.abs(by-downLines[i])<20&&Math.abs(by-upLines[i])<20)
     is=false;
   }
   return is;
  }
  
  public void paint(Graphics g){
   for(int i=0;i<8;i++){
    g.drawLine((i+1)*120,0,(i+1)*120,upLines[i]);
    g.drawLine((i+1)*120,downLines[i],(i+1)*120,700);
   }
   g.setColor(Color.GREEN);
   g.fillOval(bx, by, 30, 30);
  }
 }
 public void mouseClicked(MouseEvent e){
  l.up();
 }
 @Override
 public void mouseEntered(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseExited(MouseEvent e) {
  // TODO Auto-generated method stub
 }
 @Override
 public void mousePressed(MouseEvent e) {
  // TODO Auto-generated method stub
  l.up();
 }
 @Override
 public void mouseReleased(MouseEvent e) {
  // TODO Auto-generated method stub
  
 } 
}

你可能感兴趣的:(超级精简的flappy Bird.)