MainFrame
package myFrame;
public class Game {
public static void main (String[] args) {
MyFrame mf = new MyFrame();
mf.addListener();
}
}
Shot(子弹类)
class Shot implements Runnable {
int x;
int y;
int direct;
int speed=3 ;
boolean isLive = true ;
public Shot(int x,int y,int direct){
this .x=x;
this .y=y;
this .direct=direct;
}
@Override
public void run() {
while (true ){
try {
Thread.sleep(50 );
} catch (InterruptedException e) {
e.printStackTrace();
}
switch (direct){
case 0 :
y-=speed;break ;
case 1 :
x+=speed;break ;
case 2 :
y+=speed;break ;
case 3 :
x-=speed;break ;
}
if (x<0 ||x>400 ||y<0 ||y>300 ){
this .isLive=false ;
break ;
}
}
}
}
Tank(坦克类)
class Tank {
int x=0 ;
int y=0 ;
int direct=0 ;
int speed=5 ;
int color;
boolean isLive=true ;
public int getColor () {
return color;
}
public void setColor (int color) {
this .color = color;
}
public int getSpeed () {
return speed;
}
public void setSpeed (int speed) {
this .speed = speed;
}
public int getDirect () {
return direct;
}
public void setDirect (int direct) {
this .direct = direct;
}
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 Tank (int x,int y){
this .x=x;
this .y=y;
}
}
Enemy类(敌军类)
class EnemyTank extends Tank implements Runnable {
int times=0 ;
Vector<EnemyTank> ets=new Vector<EnemyTank>();
Vector<Shot> ss = new Vector<Shot>();
public EnemyTank(int x,int y){
super (x,y);
}
public void setEts(Vector<EnemyTank> vv){
this .ets=vv;
}
public boolean isTouchOtherEnemy(){
boolean b=false ;
switch (this .direct){
case 0 :
for (int i=0 ;i<ets.size();i++){
EnemyTank et=ets.get(i);
if (et!=this ){
if (et.direct==0 ||et.direct==2 ){
if (this .x>=et.x && this .x<=et.x+20 && this .y>=et.y && this .y<=et.y+30 ){
return true ;
}
if (this .x+20 >=et.x && this .x+20 <=et.x+20 && this .y>=et.y && this .y<=et.y+30 ){
return true ;
}
}
if (et.direct==1 ||et.direct==3 ){
if (this .x>=et.x && this .x<=et.x+30 && this .y>=et.y && this .y<=et.y+20 ){
return true ;
}
if (this .x+20 >=et.x && this .x+20 <=et.x+30 && this .y>=et.y && this .y<=et.y+20 ){
return true ;
}
}
}
}
break ;
case 1 :
for (int i=0 ;i<ets.size();i++){
EnemyTank et=ets.get(i);
if (et!=this ){
if (et.direct==0 ||et.direct==2 ){
if (this .x+30 >=et.x && this .x+30 <=et.x+20 && this .y>=et.y && this .y<=et.y+30 ){
return true ;
}
if (this .x+30 >=et.x && this .x+30 <=et.x+20 && this .y+20 >=et.y && this .y+20 <=et.y+30 ){
return true ;
}
}
if (et.direct==1 ||et.direct==3 ){
if (this .x+30 >=et.x && this .x+30 <=et.x+30 && this .y>=et.y && this .y<=et.y+20 ){
return true ;
}
if (this .x+30 >=et.x && this .x+30 <=et.x+30 && this .y+20 >=et.y && this .y+20 <=et.y+20 ){
return true ;
}
}
}
}
break ;
case 2 :
for (int i=0 ;i<ets.size();i++){
EnemyTank et=ets.get(i);
if (et!=this ){
if (et.direct==0 ||et.direct==2 ){
if (this .x>=et.x && this .x<=et.x+20 && this .y+30 >=et.y && this .y+30 <=et.y+30 ){
return true ;
}
if (this .x+20 >=et.x && this .x+20 <=et.x+20 && this .y>=et.y && this .y<=et.y+30 ){
return true ;
}
}
if (et.direct==1 ||et.direct==3 ){
if (this .x>=et.x && this .x<=et.x+30 && this .y+30 >=et.y && this .y+30 <=et.y+20 ){
return true ;
}
if (this .x+20 >=et.x && this .x+20 <=et.x+30 && this .y+30 >=et.y && this .y+30 <=et.y+20 ){
return true ;
}
}
}
}
break ;
case 3 :
for (int i=0 ;i<ets.size();i++){
EnemyTank et=ets.get(i);
if (et!=this ){
if (et.direct==0 ||et.direct==2 ){
if (this .x>=et.x && this .x<=et.x+20 && this .y>=et.y && this .y<=et.y+30 ){
return true ;
}
if (this .x>=et.x && this .x<=et.x+20 && this .y>=et.y && this .y<=et.y+30 ){
return true ;
}
}
if (et.direct==1 ||et.direct==3 ){
if (this .x>=et.x && this .x<=et.x+30 && this .y+20 >=et.y && this .y+20 <=et.y+20 ){
return true ;
}
if (this .x>=et.x && this .x<=et.x+30 && this .y+20 >=et.y && this .y+20 <=et.y+20 ){
return true ;
}
}
}
}
break ;
}
return b;
}
@Override
public void run() {
while (true ){
switch (this .direct){
case 0 :
for (int i=0 ;i<30 ;i++){
if (y>5 &&!this .isTouchOtherEnemy()){
y-=speed;
}
try {
Thread.sleep(50 );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break ;
case 1 :
for (int i=0 ;i<30 ;i++){
if (x<350 &&!this .isTouchOtherEnemy()){
x+=speed;
}
try {
Thread.sleep(50 );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break ;
case 2 :
for (int i=0 ;i<30 ;i++){
if (y<230 &&!this .isTouchOtherEnemy()){
y+=speed;
}
try {
Thread.sleep(50 );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break ;
case 3 :
for (int i=0 ;i<30 ;i++){
if (x>5 &&!this .isTouchOtherEnemy()){
x-=speed;
}
try {
Thread.sleep(50 );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break ;
}
this .direct=(int )(Math.random()*4 );
if (this .isLive==false ){
break ;
}
this .times++;
if (times%2 ==0 ){
if (isLive){
if (ss.size()<5 ){
Shot s=null ;
switch (direct)
{
case 0 :
s=new Shot(x+10 ,y,0 );
ss.add(s);
break ;
case 1 :
s=new Shot(x+30 ,y+10 ,1 );
ss.add(s);
break ;
case 2 :
s=new Shot(x+10 ,y+30 ,2 );
ss.add(s);
break ;
case 3 :
s=new Shot(x,y+10 ,3 );
ss.add(s);
break ;
}
Thread t=new Thread(s);
t.start();
}
}
}
this .direct=(int )(Math.random()*4 );
}
}
}
Hero类(本机类)
class Hero extends Tank
{
Vector<Shot> ss=new Vector<Shot>();
Shot s=null ;
public Hero(int x,int y)
{
super (x,y);
}
public void shotEnemy()
{
switch (this .direct)
{
case 0 :
s=new Shot(x+10 ,y,0 );
ss.add(s);
break ;
case 1 :
s=new Shot(x+30 ,y+10 ,1 );
ss.add(s);
break ;
case 2 :
s=new Shot(x+10 ,y+30 ,2 );
ss.add(s);
break ;
case 3 :
s=new Shot(x,y+10 ,3 );
ss.add(s);
break ;
}
Thread t=new Thread(s);
t.start();
}
public void moveUp()
{
y-=speed;
}
public void moveRight()
{
x+=speed;
}
public void moveDown()
{
y+=speed;
}
public void moveLeft()
{
x-=speed;
}
}
音乐播放类
class AePlayWave extends Thread {
private String filename;
public AePlayWave(String wavfile) {
filename = wavfile;
}
public void run() {
File soundFile = new File(filename);
AudioInputStream audioInputStream = null ;
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e1) {
e1.printStackTrace();
return ;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null ;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (Exception e) {
e.printStackTrace();
return ;
}
auline.start();
int nBytesRead = 0 ;
byte [] abData = new byte [512 ];
try {
while (nBytesRead != -1 ) {
nBytesRead = audioInputStream.read(abData, 0 , abData.length);
if (nBytesRead >= 0 )
auline.write(abData, 0 , nBytesRead);
}
} catch (IOException e) {
e.printStackTrace();
return ;
} finally {
auline.drain();
auline.close();
}
}
}
缓存节点类
class Node{
public Node (int x, int y, int direct) {
this .x=x;
this .y=y;
this .direct=direct;
}
int x;
int y;
int direct;
}
记录节点类
class Recorder{
private static int enNum=20 ;
private static int myLisfe=3 ;
private static int allEnNum=0 ;
static Vector<Node> nodes=new Vector<Node>();
private static FileWriter fw=null ;
private static BufferedWriter bw=null ;
private static FileReader fr=null ;
private static BufferedReader br=null ;
private static Vector<EnemyTank> ets=new Vector<EnemyTank>();
public Vector<Node> getNodesAndEnNums ()
{
try {
fr=new FileReader("f:\\myRecording.txt" );
br=new BufferedReader(fr);
String n="" ;
n=br.readLine();
allEnNum=Integer.parseInt(n);
while ((n=br.readLine())!=null )
{
String []x=n.split(" " );
Node node=new Node(Integer.parseInt(x[0 ]),Integer.parseInt(x[1 ]),Integer.parseInt(x[2 ]));
nodes.add(node);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
br.close();
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return nodes;
}
public static void keepRecAndEnemyTank ()
{
try {
fw=new FileWriter("f:\\myRecording.txt" );
bw=new BufferedWriter(fw);
bw.write(allEnNum+"\r\n" );
System.out .println("size=" +ets.size());
for (int i=0 ;i<ets.size();i++)
{
EnemyTank et=ets.get (i);
if (et.isLive)
{
String recode=et.x+" " +et.y+" " +et.direct;
bw.write(recode+"\r\n" );
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
bw.close();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void getRecoring ()
{
try {
fr=new FileReader("f:\\myRecording.txt" );
br=new BufferedReader(fr);
String n=br.readLine();
allEnNum=Integer.parseInt(n);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
br.close();
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void keepRecording ()
{
try {
fw=new FileWriter("f:\\myRecording.txt" );
bw=new BufferedWriter(fw);
bw.write(allEnNum+"\r\n" );
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
bw.close();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static Vector<Node> getNodes () {
return nodes;
}
public static void setNodes (Vector<Node> nodes) {
Recorder.nodes = nodes;
}
public static Vector<EnemyTank> getEts () {
return ets;
}
public static void setEts (Vector<EnemyTank> ets) {
Recorder.ets = ets;
}
public static int getAllEnNum () {
return allEnNum;
}
public static void setAllEnNum (int allEnNum) {
Recorder.allEnNum = allEnNum;
}
public static int getEnNum () {
return enNum;
}
public static void setEnNum (int enNum) {
Recorder.enNum = enNum;
}
public static int getMyLisfe () {
return myLisfe;
}
public static void setMyLisfe (int myLisfe) {
Recorder.myLisfe = myLisfe;
}
public static void reduceEnNum (){
enNum--;
}
public static void reduceMylife (){
myLisfe--;
}
public static void addEnNumRec (){
allEnNum++;
}
}
炸弹类
class Bomb{
int x,y;
int life=16 ;
boolean isLive=true ;
public Bomb (int x,int y)
{
this .x=x;
this .y=y;
}
public void lifeDown () {
if (life>0 ){
life--;
}else {
this .isLive=false ;
}
}
}