标题:Java游戏开发之坦克大战代码
关键词:Java坦克大战,坦克大战Java代码
这是一个Java坦克大战的源码,实现了大部分功能,是学习Java绘图技术的好例子
主类:
package com.qq.TankGame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Paint;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
//
///**
// * 功能:java绘图
// * @author Administrator
// *
// */
public class TankGame extends JFrame implements ActionListener{
MyPanel mp = null;
myStartPanel msp = null;
EnemyTask em = null;
JMenuBar jmb = null;
JMenu jm = null;
JMenuItem jmi1 = null;
JMenuItem jmi2 = null;
JMenuItem jmi3 = null;
public static void main(String[] args) {
TankGame tankGame = new TankGame();
}
public TankGame(){
jmb = new JMenuBar();
jm = new JMenu("游戏(G)");
jm.setMnemonic('G');
jmi1 = new JMenuItem("开始游戏(S)");
jmi1.setMnemonic('S');
jmi1.addActionListener(this);
jmi1.setActionCommand("newGame");
jmi2 = new JMenuItem("继续游戏(O)");
jmi2.setMnemonic('O');
jmi3 = new JMenuItem("退出游戏(X)");
jmi3.setMnemonic('X');
jmi3.addActionListener(this);
jmi3.setActionCommand("exit");
//启动mp线程
//em = new EnemyTask();
//mp = new MyPanel();
msp = new myStartPanel();
this.setJMenuBar(jmb);
this.add(msp);
//this.add(mp);
jmb.add(jm);
jm.add(jmi1);
jm.add(jmi2);
jm.add(jmi3);
this.setSize(600,500);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
//this.addKeyListener(mp);
Thread t1 = new Thread(msp);
t1.start();
// Thread t = new Thread(mp);
// t.start();
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getActionCommand().equals("newGame")){
mp = new MyPanel();
Thread t = new Thread(mp);
t.start();
this.remove(msp);
this.remove(mp);
this.add(mp);
this.setVisible(true);
this.addKeyListener(mp);
//Recoder.getRecording();
}else if(arg0.getActionCommand()== "exit"){
//Recoder.KeepRecording();
Recoder.KeepExit();
//KeepExit();
System.exit(0);
}
}
}
class myStartPanel extends JPanel implements Runnable{
int times = 0;
public void paint(Graphics g ){
super.paint(g);
g.fillRect(0, 0, 400, 300);
if(times%2 ==0){
g.setColor(Color.BLUE);
Font f = new Font("华文新魏", Font.BOLD, 30);
g.setFont(f);
g.drawString("stage:1", 150, 150);
}
}
@Override
public void run() {
while(true){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.repaint();
times ++;
}
}
}
class MyPanel extends JPanel implements KeyListener,Runnable{
int [] TanksX = new int[20];
int [] TanksY = new int[20];
int [] ShotsX = new int[100];
int [] ShotsY = new int[100];
// int [][]Tanks = new int[20][2];
// int [][]Shots = new int[20][2];
int [] ETdirects = new int[20];
int [] HeroShotsX = new int[10];
int [] HeroShotsY = new int[10];
static Hero hero = null;
EnemyTask et = null;
shot s = null;
boolean isPaintShot = true;
int a = 0, b = 0 , c = 0;
int [] directs = new int [10];
static //敌人坦克集合
Vector<EnemyTask> ets = new Vector<EnemyTask>();
int enSize = 3; //敌人坦克初始数量
int enSizes = 5; //敌人坦克画面总数量
int hintEnemyTanks = 0;//击中敌人坦克数量
public void showInfo(Graphics g ){//画提示信息
//画出提出坦克
this.drawTank(80, 330, g, 0, 1);
this.drawTank(160, 330, g, 0, 0);
g.setColor(Color.BLACK);
g.drawString(Recoder.getEnNum() + "", 105, 350);
g.drawString(Recoder.getMyLife() + "", 185, 350);
//画出成绩
g.setColor(Color.BLACK);
Font f = new Font("宋体", Font.BOLD, 15);
g.setFont(f);
g.drawString("你的总成绩:", 400, 20);
this.drawTank(410, 30, g, 0, 1);
g.drawString((20 - Recoder.getEnNum()) + "", 435, 50);
}
public void paint(Graphics g){
super.paint(g);
g.fillRect(0, 0, 400, 300);
this.showInfo(g);//画出提示信息
//自己坦克
if(hero.isLive == true){
this.drawTank(hero.getX(), hero.getY(), g, hero.direct, hero.type);
}else{
if(Recoder.getMyLife() > 0){
hero = new Hero(30, 270 ,0,0,10 , true);
hero.isLive = true;
}
}
//画出敌人坦克
for(int i = 0; i <ets.size(); i++){
EnemyTask et = ets.get(i);
if(!et.isLive){
ets.remove(i);
if(Recoder.getEnNum() >0){
this.drawTank(50, 0, g, 1, 1);
ets.add(et);
//et.isLive = true;
Thread t = new Thread(et);
t.start();
}
// if(Recoder.getEnNum() > 0){
// et.isLive = true;
// }
}
if(et.isLive){
this.drawTank(et.getX(), et.getY(),
g, et.direct, et.type);
System.out.println("et.ss.size()"+et.ss.size());
for(int j = 0; j < et.ss.size(); j++){//敌人坦克子弹
s = et.ss.get(j);
if(s.isLive){
if(isPaintShot){
g.fill3DRect(s.x, s.y, 5, 10, false);
}
}else{
et.ss.remove(j);
}
}
}
// if(!et.isLive){
// ets.remove(i);
// }
}
//画出子弹
for(int i = 0; i< hero.ss.size(); i++){
if(hero.ss.get(i)!= null && hero.ss.get(i).isLive == true){
g.fill3DRect(hero.ss.get(i).x, hero.ss.get(i).y, 5, 10, false);
}
if(hero.ss.get(i).isLive == false){
hero.ss.remove(hero.ss.get(i));
}
}
}
// public void hintTank2(shot s , Hero et){
// switch (et.direct) {
// case 0:
// case 1:
// if(s.x < et.x + 20 && s.x > et.x
// && s.y < et.y + 30 && s.y > et.y){
// s.isLive = false;
// et.isLive = false;
// }
// //break;
// case 2:
// case 3:
// if(s.x < et.x + 30 && s.x > et.x
// && s.y < et.y + 20 && s.y > et.y){
// s.isLive = false;
// et.isLive = false;
// }
// //break;
//
//// default:
//// break;
// }
//
// }
public boolean hintTank(shot s , Tank et){
boolean b = false;
switch (et.direct) {
case 0:
case 1:
if(s.x < et.x + 20 && s.x > et.x
&& s.y < et.y + 30 && s.y > et.y){
s.isLive = false;
et.isLive = false;
b = true;
}
break;
case 2:
case 3:
if(s.x < et.x + 30 && s.x > et.x
&& s.y < et.y + 20 && s.y > et.y){
s.isLive = false;
et.isLive = false;
b = true;
}
break;