package com.zhj;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
public class Game extends JFrame {
/**
*
*/
private static final long serialVersionUID = 7637532487372849088L;
private static final SimpleDateFormat format = new SimpleDateFormat(
"MMM yyyy HH:mm");
private Boolean pause;
private JMenuBar menuBar;
private JMenu operationMenu;
private JMenuItem startItem;
private JMenuItem pauseItem;
private JMenuItem exitItem;
private GameDiasplay displayPanel;
private MenuActionListener menuActionListenner;
private ShipEntity ship;
private ArrayList<InvaderEntity> invaders;
private ArrayList<ShotEntity> shots;
private long fireTime;
private long previousInvaderMove;
private HashMap<Integer, InvaderEntity> stableInvaders;
private JPanel statusPanel;
private JLabel lifeLabel;
private JLabel scoreLabel;
private int lifeNum = 3;
private int invaderNum = 72;
private int totalPtx = 0;
private long invaderMovePeriod = 3000;
private int ptx_level = 1;
private File recordFile;
private JDialog rankDialog;
private ArrayList<Record> records;
public Game() {
try {
initData();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
JOptionPane.showConfirmDialog(this,
"Couldn't open and create the history records", "Warning",
JOptionPane.WARNING_MESSAGE);
}
createContainer();
openHistoryDialog();
}
/**
*
* @throws IOException
*/
private void initEntities() throws IOException {
if (ship == null) {
ship = new ShipEntity(Constants.center_direction, true, new Point(
400, Constants.baseLine - Constants.size_ship),
new Rectangle(Constants.size_ship, Constants.size_ship),
Constants.speed_2);
} else {
ship.setPosition(new Point(400, Constants.baseLine
- Constants.size_ship));
}
if (invaders == null) {
invaders = new ArrayList<InvaderEntity>();
}
if (stableInvaders == null) {
stableInvaders = new HashMap<Integer, InvaderEntity>();
}
InvaderEntity invader = null;
int lineNumber = 0;
int rowNumber = 0;
int seperateNumber = invaderNum / 3;
if (seperateNumber > 24) {
seperateNumber = 24;
}
for (int i = 0; i < invaderNum; i++) {
if (i % seperateNumber == 0) {
lineNumber++;
rowNumber = 0;
}
rowNumber++;
invader = new InvaderEntity(i, Constants.center_direction, true,
new Point(Constants.begin_blank + rowNumber
* Constants.size_invader + Constants.size_blank,
lineNumber * Constants.size_invader
+ Constants.size_blank), new Rectangle(
Constants.size_invader, Constants.size_invader),
Constants.speed_l, ptx_level);
invaders.add(invader);
stableInvaders.put(i, invader);
}
if (shots == null) {
shots = new ArrayList<ShotEntity>();
}
}
/**
* read history records form file
*
* @throws IOException
*/
private void initData() throws IOException {
recordFile = new File("record.data");
if (!recordFile.exists()) {
recordFile.createNewFile();
records = new ArrayList<Record>();
for (int i = 0; i < 5; i++) {
records.add(new Record(new Date(), "anonymity", 0));
}
} else {
if (recordFile.length() == 0) {
records = new ArrayList<Record>();
for (int i = 0; i < 5; i++) {
records.add(new Record(new Date(), "anonymity", 0));
}
} else {
ObjectInputStream input = null;
try {
input = new ObjectInputStream(new FileInputStream(
recordFile));
Object object = input.readObject();
records = (ArrayList<Record>) object;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
input.close();
input = null;
}
}
}
}
/**
* write the best rank to external file
*
* @throws IOException
* @throws FileNotFoundException
*/
private void writeData(){
records.remove(5);
ObjectOutputStream writer = null;
try {
if (!recordFile.exists()) {
recordFile.createNewFile();
}
writer = new ObjectOutputStream(
new FileOutputStream(recordFile));
writer.writeObject(records);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
JOptionPane.showMessageDialog(this,
"The record file may not exist", "Warning",
JOptionPane.ERROR_MESSAGE);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
JOptionPane.showMessageDialog(this,
"Could not save the record data", "Warning",
JOptionPane.ERROR_MESSAGE);
}finally {
if(writer !=null) {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
writer = null;
}
}
}
}
/**
* if the user break the record,return a new record, else return null
*
* @return
*/
private Record breakRecord() {
Record record = null;
for (int i = 0; i < records.size(); i++) {
record = records.get(i);
if (totalPtx > record.getScore()) {
Record newRecord = new Record(new Date(), "anonymity", totalPtx);
records.add(i, newRecord);
return newRecord;
}
}
return null;
}
/**
* generate root container
*/
protected void createContainer() {
setLayout(new BorderLayout());
setTitle("Space Invader");
menuBar = new JMenuBar();
operationMenu = new JMenu("Operation");
operationMenu.setToolTipText("operation about the game");
startItem = new JMenuItem("Start new game");
startItem.setActionCommand("start");
pauseItem = new JMenuItem("Pause the game");
pauseItem.setActionCommand("pause");
pauseItem.setEnabled(false);
exitItem = new JMenuItem("Exit game");
exitItem.setActionCommand("exit");
menuActionListenner = new MenuActionListener();
startItem.addActionListener(menuActionListenner);
pauseItem.addActionListener(menuActionListenner);
exitItem.addActionListener(menuActionListenner);
menuBar.add(operationMenu);
operationMenu.add(startItem);
operationMenu.add(pauseItem);
operationMenu.addSeparator();
operationMenu.add(exitItem);
statusPanel = new JPanel();
statusPanel.setLayout(new GridLayout(1, 6));
JLabel label1 = new JLabel("Lives:");
label1.setHorizontalAlignment(SwingConstants.CENTER);
label1.setVerticalAlignment(SwingConstants.TOP);
statusPanel.add(label1);
lifeLabel = new JLabel("3");
lifeLabel.setHorizontalAlignment(SwingConstants.CENTER);
lifeLabel.setVerticalAlignment(SwingConstants.CENTER);
statusPanel.add(lifeLabel);
statusPanel.add(new JLabel(""));
statusPanel.add(new JLabel(""));
JLabel label2 = new JLabel("Scores:");
label2.setHorizontalAlignment(SwingConstants.CENTER);
statusPanel
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
space invader.rar (29个子文件)
space invader
bin
icons
background.jpg 85KB
shot.gif 935B
alien.gif 1KB
ship.gif 1KB
com
zhj
Record.class 1KB
Game.class 19KB
Game$MenuActionListener.class 2KB
Constants.class 959B
Game$2.class 2KB
Game$1.class 805B
Game$3.class 754B
ShotEntity.class 801B
GameDiasplay.class 4KB
InvaderEntity.class 2KB
Entity.class 2KB
ShipEntity.class 997B
record.data 0B
run.bat 32B
diagram.xml 646KB
diagram.png 74KB
diagram.umr 24KB
src
com
zhj
InvaderEntity.java 1KB
Game.java 24KB
Record.java 753B
ShotEntity.java 578B
ShipEntity.java 733B
Constants.java 681B
Entity.java 1KB
GameDiasplay.java 3KB
共 29 条
- 1
资源评论
- yimeierskkk2012-10-16怎么做有移动呢? 我运行无法移动
- qq_160627172014-06-04下载完 好像少图片文件
zhjdenis
- 粉丝: 1
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功