Java趣味ウィジェット:パチンコ

5997 ワード

最近作ったswingで作った小さなプログラムを共有します.レンガを打つのと同じです.「≪実行|Go|emdw≫」をクリックし、「≪一時停止|Stop|emdw≫」をクリックし、再び「≪ゲームの続行|Continue|emdw≫」をクリックし、マウスをウィンドウから離して一時停止します.マウスはコントロールパネルを移動して、ボールはバッフルに落ちて反発して、底の生命値に落ちて1つを引いて、1つの成績に1つ追加します.初心者は書くと、小さな練習になります.
最初のクラス:ウィンドウクラス、JFrameで描かれたウィンドウ.
import javax.swing.JFrame;

public class BallJframe {
	//         
	JFrame frame = new JFrame();

	//             
	public void init() {
		//     
		frame.setTitle("   ");
		//       、  
		frame.setBounds(200, 100, 800, 600);
		//         
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//       
		BallJpanel jp = new BallJpanel();
		//             
		frame.add(jp);
		//       
		frame.setVisible(true);

	}

	public static void main(String[] args) {
		BallJframe bf = new BallJframe();
		bf.init();
	}
}

2つ目のクラス:パネルクラス、スレッドとマウスリスナーで機能を実現します.
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;


import javax.swing.JPanel;


public class BallJpanel extends JPanel implements MouseMotionListener,
		MouseListener {


	//        
	static int dx = 200, dy = 550;
	//  
	static int score = 0;
	 //  
	static int n=10;
	//   
	static int hp=n; 
	//    
	int speed = 5;
	//    
	static final int START = 0;
	//    
	static final int RUNNING = 1;
	//    
	static final int PASS = 2;
	//    
	static final int OVER=3;
	//       
	int state = START;
	//  List    Ball 
	List list = new ArrayList();
	public BallJpanel() {
		//      
		for(int i=0;i= 40) {
			speed = 4;
		}
		if (score >= 60) {
			speed = 3;
		}
		if (score >= 80) {
			speed = 2;
		}
		if (score >= 100) {
			speed = 1;
		}
	}


	public void move() {
		new Thread() {
			public void run() {
				while (true) {
					//    
					if (state == RUNNING) {
						for(int i=0;i= 800 - 116) {
				dx = 684;
			}
			repaint();
		}
	}


	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		//                             
		if (state == START) {
			state = RUNNING;
		} else if (state == RUNNING) {
			state = PASS;
		} else if(state==PASS){
			state = RUNNING;
		}else if(state==OVER){
			state=START;
			//       
			for(int i=0;i

3つ目のクラス:小球クラス、小球の座標、半径、運動方向、移動、境界処理、衝突検出および衝突後の反発.
public class Ball {
	//          
	public static final int LEFT_UP = 0;
	public static final int LEFT_DOWN = 1;
	public static final int RIGHT_UP = 2;
	public static final int RIGHT_DOWN = 3;
	//            
	private int f = LEFT_UP;
	//        
	private int x, y;
	//        
	private int r;

	public Ball() {
		x = (int) (Math.random() * 800);
		y = (int) (Math.random() * 400);
		r = 10;
		int ran = (int) (Math.random() * 2);
		if (ran == 0) {
			f = LEFT_UP;
		} else if (ran == 1) {
			f = RIGHT_UP;
		}
	}

	public Ball(int x, int y, int r, int f) {
		super();
		this.x = x;
		this.y = y;
		this.r = r;
		this.f = f;
	}

	public int getF() {
		return f;
	}

	public void setF(int f) {
		this.f = f;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getR() {
		return r;
	}

	public void setR(int r) {
		this.r = r;
	}
	//    
	public void ballMove() {
		if (f == LEFT_UP) {
			x--;
			y--;
		}
		if (f == LEFT_DOWN) {
			x--;
			y++;
		}
		if (f == RIGHT_UP) {
			x++;
			y--;
		}
		if (f == RIGHT_DOWN) {
			x++;
			y++;
		}
	}

	//       
	public void ballBorder() {
		if (y <= 0) {
			if (f == LEFT_UP) {
				f = LEFT_DOWN;
			}
			if (f == RIGHT_UP) {
				f = RIGHT_DOWN;
			}
		}
		if (x <= 0) {
			if (f == LEFT_UP) {
				f = RIGHT_UP;
			}
			if (f == LEFT_DOWN) {
				f = RIGHT_DOWN;
			}
		}
		//       
		if (y >= BallJpanel.dy - 2 * r) {
			if (x >= BallJpanel.dx && x <= BallJpanel.dx + 100) {
				if (f == LEFT_DOWN) {
					f = LEFT_UP;
				}
				if (f == RIGHT_DOWN) {
					f = RIGHT_UP;
				}
				y-=3;
				BallJpanel.score++;
			} else if (y == 600 - 2 * r - 40) {
				BallJpanel.hp--;
			} else if (y > 600 - 2 * r - 40) {
				x = 900;
				y = 900;
			}
		}
		if (x >= 800 - 2 * r - 15) {
			if (f == RIGHT_UP) {
				f = LEFT_UP;
			}
			if (f == RIGHT_DOWN) {
				f = LEFT_DOWN;
			}
		}
	}
	
	//      
	public boolean isCrash(Ball ball){
		int lx = Math.abs(x+r-(ball.x+ball.r));
		int ly = Math.abs(y+r-(ball.y+ball.r));
		boolean h = lx<=(r+ball.r)&&ly<=(r+ball.r);
		return h;
	}
	
	//      
	public void ballCrash(Ball another) {
		if(isCrash(another)){
			x++;
			y++;
			int temp = this.f;
			this.f = another.f;
			another.f = temp;
		}
	}

	//        
	public void reset() {
		x = (int) (Math.random() * 800);
		y = (int) (Math.random() * 500);
		int ran = (int) (Math.random() * 2);
		if (ran == 0) {
			f = LEFT_UP;
		} else if (ran == 1) {
			f = RIGHT_UP;
		}
	}
}