package ball;
import javax.swing.JFrame;
public class BallFrame {
public static void main(String[] args) {
JFrame f=new JFrame();
BallPanel p=new BallPanel();
f.add(p);
f.setTitle("挡板接小球");
f.setSize(800, 600);
f.setLocationRelativeTo(null);//居中
f.setDefaultCloseOperation(3);
f.setVisible(true);
f.addMouseMotionListener(p);
f.addMouseListener(p);
p.move();
}
}
package ball;
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;
/*
* 状态:1.就绪 2.运行 3.暂停 4.结束 5.重新开始
* 就绪--->运行 点击鼠标
* 运行--->暂停 移出鼠标
* 运行--->结束
* 暂停---.>运行 移入鼠标
* 结束----> 重新开始 点击鼠标
* 重新开始---. >运行
*/
import javax.swing.JOptionPane;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class BallPanel extends JPanel implements MouseMotionListener,MouseListener {
int state=1;
public static final int START=1;
public static final int RUNNING=2;
public static final int PAUSE=3;
public static final int OVER=4;
public static final int RESTART=5;
int px = 200;
int bx = 300, by = 100;
int score=0,pw=200;
boolean flag=true;//游戏没结束
// 定义变量表示小球的路径
int dir = (int) (Math.random() * 4) + 1;
int sleep=30;
Color c=Color.RED;
@Override
public void paint(Graphics g) {
// TODO 自动生成的方法存根
super.paint(g);
// int c=getRandColorCode();
g.setColor(c);
g.fillOval(bx, by, 50, 50);
g.setColor(Color.BLACK);
g.fillRect(px, 500, pw, 10);
g.setColor(Color.BLUE);
g.setFont(new Font("宋体",Font.BOLD,24));
g.drawString("游戏得分:"+score, 20,50);
if(state==PAUSE){
g.drawString("游戏暂停", 300, 200);
}
}
public void move() {
new Thread() {
public void run() {
while (true) {
if(state==RUNNING){
if (dir == 1) {
bx += 10;
by += 10;
}
if (dir == 2) {
bx -= 10;
by += 10;
}
if (dir == 3) {
bx -= 10;
by -= 10;
}
if (dir == 4) {
bx += 10;
by -= 10;
}
if (bx > 700) {
if (dir == 1) {
dir = 2;
}
if (dir == 4) {
dir = 3;
}
}
if (bx < 0) {
if (dir == 2) {
dir = 1;
}
if (dir == 3) {
dir = 4;
}
}
if (by > 450) {
if (px-50<bx&&bx < (px + 200)) {
if (dir == 1) {
dir = 4;
}
if (dir == 2) {
dir = 3;}
score+=5;
//长度
if(score%20==0){
if(pw>200){
pw-=20; } }
c=new Color ((int)(Math.random()*256),
(int)(Math.random()*256),
(int)(Math.random()*256));}
else
{
by = 450;
state=OVER;
//JOptionPane.showMessageDialog(null,"游戏结束,你得了"+score+"分","GAMEOVER",JOptionPane.INFORMATION_MESSAGE);
}
}
if (by < 0) {
if (dir == 4) {
dir = 1;
}
if (dir == 3) {
dir = 2;
}
}
repaint();
try {
Thread.sleep(sleep-score/20);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}}
}.start();
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO 自动生成的方法存根
px = e.getX() - 200;
// 把鼠标的坐标给挡板
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO 自动生成的方法存根
if(state==START){
state=RUNNING;}
else if (state==PAUSE){
state=RUNNING;}
else if(state==OVER){
score=0;
bx=400;
by=100;
state=RUNNING;
}
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO 自动生成的方法存根
if(state==PAUSE){
state=RUNNING;
}
}
@Override
public void mouseExited(MouseEvent e) {
// TODO 自动生成的方法存根
if(state==RUNNING){
state=PAUSE;}
}
}