一、 本次作业要求
1. 把普通的平面棋类游戏(围棋,象棋,跳棋……)变成三维的游戏。
2. 在过去的一些流行游戏的基础上设计出新游戏,增量式的创新。
我们小组选择了第二题。
二、 摘要
相信大家都玩过“躲避子弹”类的游戏,玩家控制四个方向躲避子弹,如果“中弹”,游戏则会结束。在经过一番思考之后。我们决定在传统的“躲避子弹”类游戏上进行改编。创造出我们自己的游戏。我们的“子弹雨“相比以前大家都玩过有一些不同。我们加进了我们自己的一些,为了使游戏的难易程度能让玩家自行选择,我们设置了不同的关卡,随着关卡的提升,子弹数量也随之升高,对玩家的反应速度要求也越来越高。
三、 设计模块
1、 关卡选择
游戏按难易程度分为三等:简单、中等、复杂。不同的关卡,游戏主角的移动速度和子弹的数量有所不同,对游戏玩家的游戏的素质、反应能力也会逐级上升。玩家可以根据自身情况选择适应自己的管卡,以愉悦身心。
2、 游戏主界面开发
(1) 子弹类模块:我们定义了一个子弹类,定义子弹数量,以及控制子弹随机发射的方向。
(2) 游戏主角移动策略
游戏者可以宏观控制游戏主角,根据子弹发射的方向躲避子弹。我们设计了四个移动大方向:
(2-1)Kup:游戏主角向上移动,坐标位置相应改变。
(2-2)Kdown:游戏主角向下移动,坐标位置相应改变。
(2-3)Kleft:游戏主角向左移动,坐标位置相应改变。
(2-4)Kright:游戏主角向右移动,坐标位置相应改变。
四种方向移动后的情况,用一个线程来处理,具体情况如下:
class Move implements Runnable {
public void run() {
while(true){
while (gamexunhuan) {
p = jButton.getLocation();
if (kup) {
if (kleft) {
x = p.getX();
y = p.getY();
if (x > 0 && y > 0) {
jButton.setLocation((int) x - step, (int) y
- step);
}
} else if (kright) {
x = p.getX();
y = p.getY();
if (x + 40 < 700 && y > 0) {
jButton.setLocation((int) x + step, (int) y
- step);
}
} else {
x = p.getX();
y = p.getY();
if (y > 0) {
jButton.setLocation((int) x, (int) y - step);
}
}
}
if (kdown) {
if (kleft) {
x = p.getX();
y = p.getY();
if (y + 60 < 700 && x > 0) {
jButton.setLocation((int) x - step, (int) y
+ step);
}
} else if (kright) {
x = p.getX();
y = p.getY();
if (x + 40 < 700 && y + 60 < 700) {
jButton.setLocation((int) x + step, (int) y
+ step);
}
} else {
x = p.getX();
y = p.getY();
if (y + 60 < 700) {
jButton.setLocation((int) x, (int) y + step);
}
}
}
if (kleft) {
if (kup) {
x = p.getX();
y = p.getY();
if (x > 0 && y > 0) {
jButton.setLocation((int) x - step, (int) y
- step);
}
} else if (kdown) {
x = p.getX();
y = p.getY();
if (y + 60 < 700 && x > 0) {
jButton.setLocation((int) x - step, (int) y
+ step);
}
} else {
x = p.getX();
y = p.getY();
if (x > 0) {
jButton.setLocation((int) x - step, (int) y);
}
}
}
if (kright) {
if (kup) {
x = p.getX();
y = p.getY();
if (x + 40 < 700 && y > 0) {
jButton.setLocation((int) x + step, (int) y
- step);
}
} else if (kdown) {
x = p.getX();
y = p.getY();
if (x + 40 < 700 && y + 60 < 700) {
jButton.setLocation((int) x + step, (int) y
+ step);
}
} else {
x = p.getX();
y = p.getY();
if (x + 40 < 700) {
jButton.setLocation((int) x + step, (int) y);
}
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
}
(3) 游戏规则定制
(3-1)游戏角色根据游戏者的操作上下左右移动,躲避子弹。一旦被子弹射中,游戏结束。
(3-2)出现“不服“界面,玩家若想继续游戏,可点击”不服“按钮,游戏将重新开始。
(3-3)若想选择其他关卡,可以点击鼠标右键,重新选择关卡。
3、 皮肤切换
鼠标右击窗口任意空白处,玩家可根据自身喜好选择游戏背景。
一、 源代码展示
Game.java
package com.software.games;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Date;
public class Game extends JFrame {
// private static Game game;
private long startTime = new Time().getSartTime();
private long endTime = new Time().getEndTime();
private static final long serialVersionUID = 1L;
private JPanel jPanel = null;
private JButton jButton = null;
private boolean kup ;
private boolean kdown ;
private boolean kleft ;
private boolean kright ;
// 定义玩家的行走步伐,数值越大,移动速度越快
private int step = 3;
Point p; // @jve:decl-index=0:
double x = 0.0;
double y = 0.0;
// 定义了子弹的个数
int zidanshu ;
JRadioButtonMenuItem items[]; //菜单项
Color[] colors={Color.blue,Color.pink,Color.yellow,Color.red,Color.orange}; //颜色数组
JPopupMenu popupMenu; //弹出菜单
public int getZidanshu() {
return zidanshu;
}
public void setZidanshu(int zidanshu) {
this.zidanshu = zidanshu;
}
// 定义子弹初始值,这个是不变的
// int chushihua = 0;
// 定义控制子弹行走的循环false就不走了
private boolean gamexunhuan = true;
private JLabel jLabel = null;
private JButton jButton1 = null;
private ArrayList buttonal = new ArrayList();
private ArrayList threadal = new ArrayList();
private URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
URL fileLoc = urlLoader.findResource("MyGameIcons/gwl1.gif"); // @jve:decl-index=0:
URL fileLoc1 = urlLoader.findResource("MyGameIcons/gwls1.gif");
URL bac_1 = urlLoader.findResource("MyGameIcons/bac-01.gif"); // @jve:decl-index=0:
URL bac_2 = urlLoader.findResource("MyGameIcons/bac-02.gif");
URL bac_3 = urlLoader.findResource("MyGameIcons/bac-03.gif"); // @jve:decl-index=0:
URL bac_4 = urlLoader.findResource("MyGameIcons/bac-04.gif");
URL bac_5 = urlLoader.findResource("MyGameIcons/bac-05.gif"); // @jve:decl-index=0:
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(478, 361, 164, 51));
jButton1.setText("不服!!!");
jButton1.setFont(new java.awt.Font("Dialog", 1, 17));
jButton1.setVisible(false);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jButton1.setVisible(false);
jLabel.setVisible(false);
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
reset();
}
});
}
return jButton1;
}
public void reset() {
kup = false;
kdown = false;
kleft = false;
kright = false;
int chushihua = 0;
while (chushihua < zidanshu) {
((JButton) buttonal.get(chushihua)).setBounds(new Rectangle(-50,
-50, 10, 10));
chushihua++;
}
gamexunhuan = true;
if(fileLoc != null)
{
jButton.setIcon(new ImageIcon(fileLoc));
}else
{
}
jButton.setLocation(320, 320);
p = jButton.getLocation();
x=p.getX();
y=p.getY();
startTime=new Date().getTime();
}
public void start() {
int chushihua = 0;
while (chushihua < zidanshu) {
JButton jb = new JButton();
jb.setBounds(new Rectangle(-50, -50, 10, 10));
jb.setEnabled(false);
Threads ths = new Threads(jb);
Thread th = new Thread(ths);
buttonal.add(jb);
threadal.add(th);
chushihua++;
}
Game.Move move = new Move();
Thread tm = new Thread(move);
tm.start();
}
public void gogo() {
int chushihua = 0;
while (chushihua < zidanshu) {
((Thread) threadal.get(chushihua)).start();
chushihua++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
/**
* This is the default constructor
*/
public Game() {
super();
//关闭窗口时退出程序
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setBounds(350, 0, 700, 700);
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(1);
}
});
ChangeColorAction action = new ChangeColorAction(); //菜单项事件处理
String[] str = {"Blue","Pink","Yellow","Red","Orange"}; //菜单项名称
ButtonGroup colorGroup=new ButtonGroup(); //实例化按钮组
popupMenu=new JPopupMenu(); //实例化弹出菜单
items=new JRadioButtonMenuItem[5]; //初始化数组
for (int i=0;i<items.length;i++) {
items[i]=new JRadioButtonMenuItem(str[i]); //实例化菜单项
popupMenu.add(items[i]); //增加菜单项到菜单上
colorGroup.add(items[i]); //增加菜单项到按钮组
items[i].addActionListener(action); //菜单项事件处理
}
addMouseListener(new MouseAdapter(){ //窗口的鼠标事件处理
public void mousePressed( MouseEvent event ) { //点击鼠标
triggerEvent(event); //调用triggerEvent方法处理事件
}
public void mouseReleased( MouseEvent event ) { //释放鼠标
triggerEvent(event);
}
private void triggerEvent(MouseEvent event) { //处理事件
if (event.isPopupTrigger()) //如果是弹出菜单事件(根据平台不同可能不同)
popupMenu.show(event.getComponent(),event.getX(),event.getY()); //显示菜单
}
});
this.setResizable(false);
this.setContentPane(getJPanel());
this.setTitle("测试的小游戏!(模拟撑过20秒的就很NB!)");
this.setVisible(true);
}
class ChangeColorAction implements ActionListener { //菜单项事件处理
public void actionPerformed(ActionEvent event) {
for (int i=0;i<items.length;i++)
if (event.getSource()==items[i]) { //判断事件来自于哪个菜单项
// getContentPane().setBackground(colors[i]); //设置窗口背景
System.out.println(bac_1);
jLabel.setIcon(new ImageIcon(bac_1));
repaint(); //重绘窗口
return;
}
}
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(42, -33, 595, 308));
jLabel.setFont(new Font("Dialog", Font.BOLD, 24));
jLabel.setForeground(new Color(250, 2, 2));
jLabel.setEnabled(true);
jLabel.setVisible(false);
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.add(getJButton(), null);
jPanel.setForeground(new Color(1, 1, 1));
jPanel.setBackground(new Color(1, 1, 1));
jPanel.setVisible(true);
jPanel.add(jLabel, null);
jPanel.add(getJButton1(), null);
}
return jPanel;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
class Move implements Runnable {
public void run() {
while(true){
while (gamexunhuan) {
p = jButton.getLocation();
if (kup) {
if (kleft) {
x = p.getX();
y = p.getY();
if (x > 0 && y > 0) {
jButton.setLocation((int) x - step, (int) y
- step);
}
} else if (kright) {
x = p.getX();
y = p.getY();
if (x + 40 < 700 && y > 0) {
jButton.setLocation((int) x + step, (int) y
- step);
}
} else {
x = p.getX();
y = p.getY();
if (y > 0) {
jButton.setLocation((int) x, (int) y - step);
}
}
}
if (kdown) {
if (kleft) {
x = p.getX();
y = p.getY();
if (y + 60 < 700 && x > 0) {
jButton.setLocation((int) x - step, (int) y
+ step);
}
} else if (kright) {
x = p.getX();
y = p.getY();
if (x + 40 < 700 && y + 60 < 700) {
jButton.setLocation((int) x + step, (int) y
+ step);
}
} else {
x = p.getX();
y = p.getY();
if (y + 60 < 700) {
jButton.setLocation((int) x, (int) y + step);
}
}
}
if (kleft) {
if (kup) {
x = p.getX();
y = p.getY();
if (x > 0 && y > 0) {
jButton.setLocation((int) x - step, (int) y
- step);
}
} else if (kdown) {
x = p.getX();
y = p.getY();
if (y + 60 < 700 && x > 0) {
jButton.setLocation((int) x - step, (int) y
+ step);
}
} else {
x = p.getX();
y = p.getY();
if (x > 0) {
jButton.setLocation((int) x - step, (int) y);
}
}
}
if (kright) {
if (kup) {
x = p.getX();
y = p.getY();
if (x + 40 < 700 && y > 0) {
jButton.setLocation((int) x + step, (int) y
- step);
}
} else if (kdown) {
x = p.getX();
y = p.getY();
if (x + 40 < 700 && y + 60 < 700) {
jButton.setLocation((int) x + step, (int) y
+ step);
}
} else {
x = p.getX();
y = p.getY();
if (x + 40 < 700) {
jButton.setLocation((int) x + step, (int) y);
}
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(320, 320, 30, 30));
jButton.setBackground(new Color(1, 1, 1));
p = jButton.getLocation();
x = p.getX();
y = p.getY();
if( fileLoc != null)
{
jButton.setIcon(new ImageIcon(fileLoc));
}else
{
URL fileLoc = urlLoader.findResource("MyGameIcons/gwl1.gif"); // @jve:decl-index=0:
URL fileLoc1 = urlLoader.findResource("MyGameIcons/gwls1.gif");
System.out.println(fileLoc);
jButton.setIcon(new ImageIcon(fileLoc));
}
jButton.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent e) {
if(e.getKeyCode()==10){
if(!gamexunhuan){
jButton1.setVisible(false);
jLabel.setVisible(false);
reset();
}
}
if (e.getKeyCode() == 37) {
kleft = false;
}
if (e.getKeyCode() == 38) {
kup = false;
}
if (e.getKeyCode() == 39) {
kright = false;
}
if (e.getKeyCode() == 40) {
kdown = false;
}
}
public void keyPressed(java.awt.event.KeyEvent e) {
if (e.getKeyCode() == 37) {
kleft = true;
}
if (e.getKeyCode() == 38) {
kup = true;
}
// 触发按右键
if (e.getKeyCode() == 39) {
kright = true;
}
if (e.getKeyCode() == 40) {
kdown = true;
}
}
});
}
return jButton;
}
class Threads implements Runnable {
public Threads(JButton jjb) {
jb = jjb;
}
JButton jb = null;
private boolean first = true;
public void run() {
while (gamexunhuan) {
go();
}
}
public void go() {
int zzx = 0;
int zzy = 0;
int zx = 0;
int zy = 0;
while (true) {
if(gamexunhuan){
int fangxiang = (int) (Math.random() * 4 + 1);
// 四个if随即从四个边发射子弹
if (fangxiang == 1) {
zx = 0;
zy = (int) (Math.random() * 701);
}
if (fangxiang == 2) {
zx = (int) (Math.random() * 701);
zy = 0;
}
if (fangxiang == 3) {
zx = 700;
zy = (int) (Math.random() * 701);
}
if (fangxiang == 4) {
zx = (int) (Math.random() * 701);
zy = 700;
}
// 初始化子弹,有了就不在加了
if (first) {
jPanel.add(jb, null);
first = false;
}
jb.setBounds(new Rectangle(zx, zy, 10, 10));
// 定义子弹与物体之间的步长
zzx = (int) (((x + 15) - zx) / 30);
zzy = (int) (((y + 15) - zy) / 30);
}
while (gamexunhuan) {
try {
zx += zzx;
zy += zzy;
jb.setLocation(zx, zy);
if (zx + 5 > x & zx + 5 < x + 30 & zy + 5 > y
& zy + 5 < y + 30) {
jButton.setIcon(new ImageIcon(fileLoc1));
gamexunhuan = false;
first = true;
jButton1.setVisible(true);
jLabel.setVisible(true);
endTime = new Date().getTime();
Date gametime = new Date(endTime-startTime);
int min =0;
int sec =0;
min = gametime.getMinutes();
sec = gametime.getSeconds();
String endtime = "";
if(min!=0){
endtime=min + "分 " + sec + "秒";
}else{
endtime=sec + "秒";
}
jLabel.setText(" GAME OVER!!! \n用时:" + endtime);
break;
}
// 超出边线停止循环
if (zx > 700 | zy > 700 | zx < 0 | zy < 0) {
break;
}
Thread.sleep(60);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
}
} // @jve:decl-index=0:visual-constraint="10,10"
GameStart.java
/*
* Test.java
*
* Created on __DATE__, __TIME__
*/
package com.software.games;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.net.URLClassLoader;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
/**
*
* @author __USER__
*/
public class GameStart extends javax.swing.JFrame {
//GEN-BEGIN:variables
// Variables declaration - do not modify
private java.awt.Button chapterOne;
private java.awt.Button chapterTwo;
private java.awt.Button chapterThree;
private javax.swing.JFrame jFrame1;
private java.awt.TextField textField1;
//创建一个JLayeredPane用于分层的。
JLayeredPane layeredPane;
//创建一个Panel和一个Label用于存放图片,作为背景。
JPanel jp;
JLabel jl;
ImageIcon image;
private int select = 0;
public int getSelect() {
return select;
}
public void setSelect(int select) {
this.select = select;
}
// End of variables declaration//GEN-END:variables
/** Creates new form Test */
public GameStart() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jFrame1 = new javax.swing.JFrame();
chapterOne = new java.awt.Button();
chapterTwo = new java.awt.Button();
chapterThree = new java.awt.Button();
textField1 = new java.awt.TextField();
layeredPane=new JLayeredPane();
image=new ImageIcon("images\\bac-01.gif");//随便找一张图就可以看到效果。
//创建背景的那些东西
jp=new JPanel();
jp.setBounds(0,0,image.getIconWidth(),image.getIconHeight()); jl=new JLabel(image);
// jl.setBounds(0,0,image.getIconWidth(),image.getIconHeight());
jp.add(jl);
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(
jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(jFrame1Layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400,
Short.MAX_VALUE));
jFrame1Layout.setVerticalGroup(jFrame1Layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 300,
Short.MAX_VALUE));
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
chapterOne.setLabel("简单");
chapterOne.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chapterOneActionPerformed(evt);
select = 1;
GameStart gaStart = new GameStart();
gaStart.setSelect(1);
gaStart.start();
}
});
chapterTwo.setLabel("中等");
chapterTwo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chapterOneActionPerformed(evt);
select = 2;
GameStart gaStart = new GameStart();
gaStart.setSelect(2);
gaStart.start();
}
});
chapterThree.setLabel("难!");
chapterThree.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chapterOneActionPerformed(evt);
select = 3;
GameStart gaStart = new GameStart();
gaStart.setSelect(3);
gaStart.start();
}
});
textField1.setText(" 请选择关卡!!!");
textField1.setEditable(false);
textField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textField1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout
.setHorizontalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGap(
200,
200,
200)
.addComponent(
textField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
176,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED))
.addGroup(
layout
.createSequentialGroup()
.addGap(
260,
260,
260)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(
chapterTwo,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
chapterOne,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
chapterThree,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(126, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addGap(52, 52, 52).addComponent(
textField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE).addGap(42, 42,
42).addComponent(chapterOne,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE).addGap(28, 28,
28).addComponent(chapterTwo,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE).addGap(34, 34,
34).addComponent(chapterThree,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(44, Short.MAX_VALUE)));
pack();
//将jp放到最底层。
layeredPane.add(jp,JLayeredPane.DEFAULT_LAYER);
//将jb放到高一层的地方
layeredPane.add(textField1, JLayeredPane.MODAL_LAYER);
layeredPane.add(chapterOne,JLayeredPane.MODAL_LAYER);
layeredPane.add(chapterTwo,JLayeredPane.MODAL_LAYER);
layeredPane.add(chapterThree,JLayeredPane.MODAL_LAYER);
setLocation(400,200);
this.setLayeredPane(layeredPane);
this.setSize(image.getIconWidth(),image.getIconHeight());
//this.setLocation(image.getIconWidth(),image.getIconHeight());
this.setVisible(true);
}// </editor-fold>
private void chapterOneActionPerformed(java.awt.event.ActionEvent evt) {
}
private void textField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
class Go implements Runnable{
@Override
public void run() {
System.out.println(select);
if(select == 1)
{
Game game = new Game();
game.zidanshu = 15;
game.start();
game.reset();
game.gogo();
}else if(select == 2)
{
Game game = new Game();
game.zidanshu = 30;
game.start();
game.reset();
game.gogo();
}else if(select == 3)
{
Game game = new Game();
game.zidanshu = 60;
game.start();
game.reset();
game.gogo();
}
try {
Thread.sleep(3);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
public void start ()
{
GameStart.Go go = new Go();
Thread th = new Thread(go);
th.start();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
GameStart gaStart = new GameStart();
gaStart.setVisible(true);
}
}
Time.java
package com.software.games;
public class Time {
private long sartTime;
public long getSartTime() {
return sartTime;
}
public void setSartTime(long sartTime) {
this.sartTime = sartTime;
}
public long getEndTime() {
return endTime;
}
public void setEndTime(long endTime) {
this.endTime = endTime;
}
private long endTime;
}
二、 心得收获
我们小组成员各司其职,团结合作,一起完成了这次作业。游戏的开发过程中也遇到过很多困难,比如说java swing 的编写,因为原来没有接触过这些,所以花费了不少精力,但是也有很多收获,我们相互鼓励、一起解决,最终完成了这个小工程。