package s;
//***********************************************************
//author: FT
//date: 2016/4/9
//打砖块游戏
//实现了多关卡,多地图,背景音乐、音效,暂停、重新开始等功能
//************************************************************
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.event.*;
import javax.swing.*;
public class Game extends JFrame implements Runnable, KeyListener,
ActionListener {
private String s = new String("打砖块 V3.0 by FT");
// 定义组件
JToolBar toolbar = new JToolBar(); // 工具栏
JButton gstart = null; // 开始按钮
JButton grestart = null; // 重新开始按钮
JLabel clife=null;
JComboBox gmodel = null; // 砖块地图
JComboBox setgate=null; //设置关卡
// 定义变量
public static int through=0;
public static int n=5;//关卡列数
public static int m=4;//关卡行数
double dxb = -10, dyb = -10;//定义球速度
static int speed = 100; // 速度
static int toolgate = 0;
static boolean respeed = false;
int life = 3; // 生命
boolean isstop = true;// 开始 暂停
public static boolean setpaddle = true;
public static boolean setball = true;
int model = 1; // 砖块叠放的地图
BackMusic bm; // 音乐窗口
public static Treasure[][] trea;
FireVoice firevoice; // 碰撞声音
FireVoice click,clickiron;
Thread t = new Thread(this);
// 定义图形
public static Brick brick[][] = null; // 砖块
public static Ball ball = new Ball(); // 球
public static Paddle paddle = new Paddle(); // 板块
MyPanel panel; // 画板
public static BufferedImage img1; // 图片
public static BufferedImage img2;
public static BufferedImage img3;
public static BufferedImage img4,img5;
public Game() {//构造函数初始化
init();
}
public void initvariable() {//变量初始化
dxb=-10;
dyb=-10;
through=0;
speed = 50;
life = 3;
isstop = true;
setpaddle = true;
setball = true;
//model = 0;
switch (toolgate) {//不同关卡
case 0:
m = 6;
n = 5;
ball.r = 25;
paddle.width = 150;
try {
img2 = ImageIO.read(new File("1.jpg"));
img3=ImageIO.read(new File("5.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
break;
case 1:
m = 8;
n = 5;
ball.r = 25;
paddle.width = 150;
try {
img2 = ImageIO.read(new File("2.jpg"));
img3=ImageIO.read(new File("6.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
break;
default://其它关
m = 10;
n = 5;
ball.r = 20;
paddle.width = 120;
try {
img2 = ImageIO.read(new File("3.jpg"));
img3=ImageIO.read(new File("7.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
}
ball.xBall = paddle.xpaddle + paddle.width * 0.5;//设置小球初始位置(木板正中间)
ball.yBall = paddle.ypaddle - ball.r;
brick = new Brick[m][n];
trea=new Treasure[m][n];
for (int j = 0; j < brick[0].length; j++) {//创建砖块
for (int i = 0; i < brick.length; i++) {
brick[i][j] = new Brick();
brick[i][j].visual = 3;
brick[i][j].width=640/Game.m;
brick[i][j].heigh=200/Game.n;
brick[i][j].xBrick = i * (640 / Game.m)+80;
brick[i][j].yBrick = j * (200 / Game.n)+20;
trea[i][j]=new Treasure(brick[i][j].xBrick+brick[i][j].width/2-10,brick[i][j].yBrick);
}
}
switch (toolgate) {//不同关卡
case 0:
Map.setmodel(0);
break;
case 1:
Map.setmodel(1);
break;
default://其它关
Map.setmodel(2);
break;
}
try {
img1 = ImageIO.read(new File("4.jpg"));//木板
img4=ImageIO.read(new File("9.jpg"));
img5=ImageIO.read(new File("8.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void init() {//对主界面初始化
initvariable();//变量初始化
model = 0;
bm = new BackMusic();// 开启音乐
bm.Music1Start();
firevoice = new FireVoice();
click=new FireVoice();
clickiron=new FireVoice();
// 设置窗体
//setSize(640, 500);
setSize(800,850);
setBackground(Color.black );
panel = new MyPanel();
this.setResizable(false);
this.setLocation(300, 100);
Container contentPane = getContentPane();//容器
contentPane.setLayout(new BorderLayout());//布局方式
this.setTitle(s);
// 初始化按钮
gstart = new JButton("开始");
grestart = new JButton("重新开始");
clife=new JLabel("生命:3");
String[] smodel = { "地图1", "地图2", "地图3"};// 初始化下拉栏
gmodel = new JComboBox(smodel);
String[] sgate={"第1关","第2关","第3关"};
setgate=new JComboBox(sgate);
// 将组件添加到工具栏
toolbar.add(gstart);
toolbar.addSeparator();//添加分隔栏
toolbar.add(grestart);
toolbar.addSeparator();//添加分隔栏
toolbar.add(gmodel);
toolbar.addSeparator();//添加分隔栏
toolbar.add(setgate);
toolbar.addSeparator();//添加分隔栏
toolbar.add(clife);
//添加组件到画板
contentPane.add(toolbar, BorderLayout.NORTH);//在上方添加工具栏
contentPane.add(panel, BorderLayout.CENTER);//在中部添加游戏界面
// 监听事件
gstart.addActionListener(this);
grestart.addActionListener(this);
gmodel.addActionListener(this);
setgate.addActionListener(this);
addMouseMotionListener(new MouseMove());
panel.addKeyListener(this);
panel.requestFocus();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// 初始化变量
t.start();//线程开始
}
//键盘监听事件
public void keyPressed(KeyEvent e) {
grestart.setFocusable(false);
gmodel.setEnabled(false);
setgate.setEnabled(false);
if (e.getKeyCode() == KeyEvent.VK_SPACE) {//空格暂停
if (isstop) {//如果当前是暂停的
isstop = false;//切换状态
setpaddle = true;//允许木板活动
setball = false;//允许球活动
gstart.setEnabled(false);
} else {//如果当前是活动的
isstop = true;
setpaddle =false;
gstart.setEnabled(true);
}
}
}
//响应事件
public void actionPerformed(ActionEvent e) {//按钮监听
Object obj = e.getSource();
if (obj == gstart) { // 开始
isstop = false;
setpaddle = true;
setball = false;
panel.requestFocus();//重新获得焦点
} else if (obj == grestart) {//重新开始
isrestarts();
gmodel.setEnabled(true);
grestart.setFocusable(false);
panel.requestFocus();
} else if (obj == gmodel) {//设置地图
model = gmodel.getSelectedIndex();
initvariable();
Map.setmodel(model);
panel.requestFocus();
}else if(obj==setgate){//选择关卡
toolgate=setgate.getSelectedIndex();
initvariable();
panel.requestFocus();
}
}
public void iscompete() {//判断是否过关
int h;
for (int j = 0; j < Game.brick[0].length; j++) {
for (int i = 0; i < Game.brick.length; i++) {
if (Game.brick[i][j].visual !=0) {//只要还有一个砖块存在
if(Game.brick[i][j].treasure!=11){
return;//退出方法
}
}
}
}
h = JOptionPane.showConfirmDialog(null, " 是否进入下一关?", "过关 ",JOptionPane.YES_NO_OPTION);//弹出是否过关的选择框
if (h == 1) {
isrestarts();//选否仍是这关
} else {
nextgate();
}
}
public void isrestarts() { // 重新开始
gmodel.setSelectedIndex(0);
setgate.setSelectedIndex(0);
speed = 50;
toolgate = 0;
respeed = true;
life=3;
clife.setText("生命:"+life);
setgate.setEnabled(true);
initvariable(); // 初始化变量
}
public void nextgate() {//过关
gmodel.setSelectedIndex(0);//重置地图
toolgate++;
initvariable(); // 初始化变量
}
public void run() {
while (true) {
if (ball.xBall <= 0 || (ball.xBall + 1.5 * ball.r >= getWidth())) { // 超出左右边界
dxb = -dxb;//边框反弹
}
if (ball.yBall <= 0) { // 超出上边界