写这个游戏已经有一段时间了,一直在实现各种新功能,从最开始的地图上只有坦克,发子弹还是一个大问题到现在可以两个人一起玩,还是花了不少心思的,现在坦克的速度更快,电脑坦克也不会撞墙.虽然游戏性没有经典坦克大战那么强,但是还是可以用来休闲娱乐一下,这个用了很多最近学到的新知识,模仿俄罗斯方块,还有一些小技巧,比如可以同时按触发多个按键事件,对子弹的处理等.
左边的坦克用W D S A控制移动,H发射子弹,每次最多出现5颗子弹,右边的坦克用上下左右箭头控制移动,L键发射子弹,互不干扰.Q键可以直接退出游戏,游戏结束后按Y键可以继续游戏.为了结构清晰游戏分为Mywar Shoot Tanks 3个类.
(第二次用插入代码,之前一直都不懂这功能,我奥特蛋了 (*^__^*) 嘻嘻……)
/********************MyWar类***********************/
package tank;
import Java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyWar{
public static void main(String[] args) {
JFrame frame = new JFrame("坦克大战");//新建一个窗口
War war = new War();//创建一个War类的对象
frame.add(war);//把war添加到窗口中
frame.setSize(750,530);//窗口宽高
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭时结束进程
frame.setLocationRelativeTo(null);//使窗口居中
frame.setVisible(true);//这句是干啥的?
war.action();//启动(war)战斗,亲,可以游戏了!
}
}
class War extends JPanel{
private boolean sUp,sDown,sRight,sLeft,sH;//定义右边坦克按键的开关
private boolean sW,sD,sS,sA,sL;//定义左边坦克按键的开关
public static final int WIDTH=750;//定义地图宽度
public static final int HEIGHT=530;//定义地图高度
private int score;//设置分数
private boolean gameOver;//gameover=false表示游戏没有结束
private Timer timer;//刚学的东西
private int shootNum,shootNum1;//可以射击的子弹数,防止作弊
MyTank[] myTank=new MyTank[2];//定义一个我方坦克对象数组
EnemyTanks[] enemyTank=new EnemyTanks[5];//初始化敌人坦克的数量
EnemyTanks newEnemyTank;//用来产生一辆敌人的坦克,补充死去的T_T
Random r = new Random();
/**用于产生一辆新的敌人坦克,返回参数是EnemyTanks类型*/
public EnemyTanks nextTank(){
int x = r.nextInt(2)*WIDTH;//随机x的值,坦克出生在角落
int y=r.nextInt(2)*HEIGHT;//随机y的值,坦克出生在角落
int step=r.nextInt(4)+1;//速度
int direct=r.nextInt(4)+1;//方向
int who = r.nextInt(3)+1;//谁?
newEnemyTank=new EnemyTanks(x,y,step,direct,who,true);
return newEnemyTank;//返回一个新坦克
}
public void action(){
startGame();//开始游戏函数
KeyAdapter l = new KeyAdapter(){//键盘监听
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(key==KeyEvent.VK_Q){
System.exit(0);//Q关闭进程结束游戏
}
if(gameOver){
if(key==KeyEvent.VK_Y){
startGame();//Y键开始游戏
}
return;
}
switch(key){
case KeyEvent.VK_W:sW=true;break;
case KeyEvent.VK_A:sA=true;break;
case KeyEvent.VK_D:sD=true;break;
case KeyEvent.VK_S:sS=true;break;
case KeyEvent.VK_L:sL=true;break;
case KeyEvent.VK_RIGHT:sRight=true;break;
case KeyEvent.VK_LEFT:sLeft=true;break;
case KeyEvent.VK_DOWN:sDown=true;break;
case KeyEvent.VK_UP:sUp=true;break;
case KeyEvent.VK_H:sH=true;break;
}
repaint();
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_W:sW=false;break;
case KeyEvent.VK_A:sA=false;break;
case KeyEvent.VK_D:sD=false;break;
case KeyEvent.VK_S:sS=false;break;
case KeyEvent.VK_H:sH=false;break;
case KeyEvent.VK_RIGHT:sRight=false;break;
case KeyEvent.VK_LEFT:sLeft=false;break;
case KeyEvent.VK_DOWN:sDown=false;break;
case KeyEvent.VK_UP:sUp=false;break;
case KeyEvent.VK_L:sL=false;break;
}
}
};
this.requestFocus();
this.addKeyListener(l);
}
public void level(){//每5分增加一辆敌人的坦克
int length = score/5+5;
if(length>enemyTank.length){
enemyTank=Arrays.copyOf(enemyTank,enemyTank.length+1);//坦克数组扩容
enemyTank[enemyTank.length-1]=nextTank();//初始化数组最后的一辆坦克
enemyTank[enemyTank.length-1].start();//启动这个坦克的线程
}
}
public void startGame(){
cleanShoot();//清空子弹
shootNum=5;//可以发射的子弹数为5
shootNum1=5;
score=0;
myTank[0]= new MyTank(550,370,4,1,-1,true);//初始化我的坦克
myTank[1]= new MyTank(100,100,4,1,0,true);
for (int i = 0; i < enemyTank.length; i++) {//初始化敌人的坦克
if(gameOver){//游戏结束,关闭每个坦克的线程
enemyTank[i].s=false;
}
}
enemyTank=new EnemyTanks[5];
for (int i = 0; i < enemyTank.length; i++) {//初始化敌人的坦克
enemyTank[i]=nextTank();
}
gameOver=false;//游戏没有结束,表示开始
myTank[0].start();//启动我的坦克线程
myTank[1].start();
for (int i = 0; i < enemyTank.length; i++) {//启动敌人坦克线程
enemyTank[i].start();
}
timer = new Timer();//匿名内部类Timer
timer.schedule(new TimerTask(){
public void run(){//重写run()函数
repaint();
shootAndRun();
}
}, 0,50);//50毫秒执行一次
}
/**检查游戏是否结束*/
public void checkGameOver(){//模仿俄罗斯方块里面写的
if(myTank[0].isLive()||myTank[1].isLive()){
return;
}
gameOver = true;
timer.cancel();
repaint();
}
public void shootAndRun(){
if(sW){//如果开关sW=true就执行{}里面的代码
myTank[1].moveUp();
}else if(sD){
myTank[1].moveRight();
}else if(sS){
myTank[1].moveDown();
}else if(sA){
myTank[1].moveLeft();
}if(sH&&myTank[1].isLive()){//如果sL=true并且坦克是活的,就执行
if(shootNum1>=0){//如果允许的子弹数小于于0了,不执行
myTank[1].shoot();
shootNum1--;//控制子弹数量,射击一次,子弹减少,消失加1
}
}
if(sUp){
myTank[0].moveUp();
}else if(sRight){
myTank[0].moveRight();
}else if(sLeft){
myTank[0].moveLeft();
}else if(sDown){
myTank[0].moveDown();
}if(sL&&myTank[0].isLive()){
if(shootNum>=0){
myTank[0].shoot();
shootNum--;//控制子弹数量,射击一次,子弹减少,消失加1
}
}
}
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());//画背景
paintWall(g);//画墙
//画我的坦克
paintTank(myTank[1].getX(),myTank[1].getY(),g,myTank[1].getDirect(),myTank[1].getWho(),myTank[1].isLive());
paintTank(myTank[0].getX(),myTank[0].getY(),g,myTank[0].getDirect(),myTank[0].getWho(),myTank[0].isLive());
for (int i = 0; i < enemyTank.length; i++) {//画敌人的坦克
paintTank(enemyTank[i].getX(),enemyTank[i].getY(),g,enemyTank[i].getDirect(),enemyTank[i].getWho(),enemyTank[i].isLive());
}
paintShoot(g);//画我射击的子弹
paintEemyShoot(g);//画敌人发出的子弹
paintScore(g);//画分数,及字符
//paintMap(g);
}
/* public void paintMap(Graphics g){//这是个方法可以生成漂亮的东西
for (int j = 0; j < 3; j++) {
Random color=new Random(); //通过Random生成随机颜色
int r = color.nextInt(256);
int g1 = color.nextInt(256);
int b = color.nextInt(256);
g.setColor(new Color(r, g1, b));
g.fillOval(color.nextInt(750), color.nextInt(530), 5, 5);
}
}*/
public void paintScore(Graphics g){//画字符相关的
g.setColor(Color.lightGray);
Font f = getFont();
Font font = new Font(f.getName(),Font.BOLD,0x1e);
int x = 130;
int y = 275;
String str = "SCORE:"+this.score;
g.setFont(font);
g.drawString(str, x, y);
str="TANK:"+enemyTank.length ;
x+=170;
g.drawString(str, x, y);
x+=140;
str = "[Q]Quit!";
if(gameOver){
str = "[Y]Start!";
}
g.drawString(str, x, y);
}
public void paintWall(Graphics g){//画中间的柱子
g.setColor(Color.LIGHT_GRAY);
g.fill3DRect(WIDTH/2-45,150 , 40, HEIGHT-300, false);
g.fill3DRect(130,HEIGHT/2-20 , WIDTH-300, 40, false);
}
/**画自己坦克子弹,同时判断子弹有没有击中敌人*/
public void paintShoot(Graphics g){
ShootDispeal();
for (int i = 0; i < Shoot.myShoot.length; i+=4) {
if(Shoot.myShoot[i]==0&&Shoot.myShoot[i+1]==0){
continue;
}
g.setColor(Color.RED);
g.fillOval(Shoot.myShoot[i], Shoot.myShoot[i+1], 10, 10);
int x = Shoot.myShoot[i];
int y = Shoot.myShoot[i+1];
for (int j = 0; j < enemyTank.length; j++) {
int ex = enemyTank[j].getX();
int ey = enemyTank[j].getY();
if(x>ex&&x<ex+40&&y>ey&&y<ey+40){
score+=1;
level();
enemyTank[j].s=false;//坦克死亡,线程关闭
enemyTank[j]=nextTank();
enemyTank[j].start();
Shoot.myShoot[i]=0;Shoot.myShoot[i+1]=0;//子弹消失
Shoot.myShoot[i+2]=0;Shoot.myShoot[i+3]=0;
shootNum++;
shootNum1++;
}
}
Shoot.myShoot[i]+=Shoot.myShoot[i+2];
Shoot.myShoot[i+1]+=Shoot.myShoot[i+3];
}
}
/**画敌人发出的子弹,同时判断是否击中了我的坦克*/
public void paintEemyShoot(Graphics g){
ShootDispeal();
for (int i = 0; i < Shoot.enemyShoot.length; i+=4) {
if(Shoot.enemyShoot[i]==0&&Shoot.enemyShoot[i+1]==0){
continue;
}
g.setColor(Color.blue);
g.fillOval(Shoot.enemyShoot[i], Shoot.enemyShoot[i+1], 10, 10);
int x = Shoot.enemyShoot[i];
int y = Shoot.enemyShoot[i+1];
int mx = myTank[0].getX();
int my = myTank[0].getY();
int mx1 = myTank[1].getX();
int my1 = myTank[1].getY();
if(x>mx&&x<mx+40&&y>my&&y<my+40){
myTank[0].setLive(false);
checkGameOver();
}
if(x>mx1&&x<mx1+40&&y>my1&&y<my1+40){
myTank[1].setLive(false);
checkGameOver();
}
Shoot.enemyShoot[i]+=Shoot.enemyShoot[i+2];//根据步伐,改变子弹的坐标
Shoot.enemyShoot[i+1]+=Shoot.enemyShoot[i+3];
}
}
/**画坦克*/
public void paintTank(int x,int y,Graphics g,int direct,int who,boolean isLive){
Color color = null;//设置颜色
if(isLive){
if(who==0){//我的坦克
color=Color.green;
}else if(who==-1){
color=Color.yellow;
}else if(who==1){//1,2,3敌人的坦克,3种颜色
color=Color.red;
}else if(who==2){
color=Color.magenta;
}else if(who==3){
color=Color.CYAN;
}
switch(direct){//根据方向画出不同方向的坦克
case 1:g.setColor(color);paintUpTank(x,y,g);break;
case 2:g.setColor(color);paintRightTank(x,y,g);break;
case 3:g.setColor(color);paintDownTank(x,y,g);break;
case 4:g.setColor(color);paintLeftTank(x,y,g);break;
}
}
}
/**纯画图打造坦克*/
public void paintUpTank(int x,int y,Graphics g){
g.fill3DRect(x, y, 15, 50, false);
g.fill3DRect(x+35, y, 15, 50, false);
g.fill3DRect(x+15, y+10, 20, 30, false);
g.setColor(Color.black);
g.fill3DRect(x+23, y-10, 5, 33, false);
g.setColor(Color.yellow);
g.fillOval(x+20, y+18, 10, 10);
}
public void paintLeftTank(int x,int y,Graphics g){
g.fill3DRect(x, y, 50, 15, false);
g.fill3DRect(x, y+35, 50, 15, false);
g.fill3DRect(x+10, y+15, 30, 20, false);
g.setColor(Color.black);
g.fill3DRect(x-10, y+22, 33, 5, false);
g.setColor(Color.yellow);
g.fillOval(x+20, y+18, 10, 10);
}
public void paintDownTank(int x,int y,Graphics g){
g.fill3DRect(x, y, 15, 50, false);
g.fill3DRect(x+35, y, 15, 50, false);
g.fill3DRect(x+15, y+10, 20, 30, false);
g.setColor(Color.black);
g.fill3DRect(x+23, y+25, 5, 33, false);
g.setColor(Color.yellow);
g.fillOval(x+20, y+18, 10, 10);
}
public void paintRightTank(int x,int y,Graphics g){
g.fill3DRect(x, y, 50, 15, false);
g.fill3DRect(x, y+35, 50, 15, false);
g.fill3DRect(x+10, y+15, 30, 20, false);
g.setColor(Color.black);
g.fill3DRect(x+23, y+22, 33, 5, false);
g.setColor(Color.yellow);
g.fillOval(x+20, y+18, 10, 10);
}
/**重新开始游戏的时候会清空子弹数组里面的数据*/
public void cleanShoot(){
for (int i = 0; i < Shoot.enemyShoot.length; i++) {
Shoot.enemyShoot[i]=0;
}
for (int i = 0; i < Shoot.myShoot.length; i++) {
Shoot.myShoot[i]=0;
}
}
/**子弹消失了*/
public void ShootDispeal(){
for (int i = 0; i < Shoot.myShoot.length; i+=4) {//撞到边缘
if(Shoot.myShoot[i]<0||Shoot.myShoot[i]>WIDTH||Shoot.myShoot[i+1]<0||Shoot.myShoot[i+1]>HEIGHT){
Shoot.myShoot[i]=0;Shoot.myShoot[i+1]=0;
Shoot.myShoot[i+2]=0;Shoot.myShoot[i+3]=0;
shootNum++;
shootNum1++;
}
int x=Shoot.myShoot[i];
int y=Shoot.myShoot[i+1];
//撞到柱子
if((x>330&&x<360&&y>150&&y<380)||(x>130&&x<570&&y>240&&y<280)){
Shoot.myShoot[i]=0;Shoot.myShoot[i+1]=0;
Shoot.myShoot[i+2]=0;Shoot.myShoot[i+3]=0;
shootNum++;
shootNum1++;
}
}
for (int i = 0; i < Shoot.enemyShoot.length; i+=4) {//撞到边缘
if(Shoot.enemyShoot[i]<0||Shoot.enemyShoot[i]>WIDTH||Shoot.enemyShoot[i+1]<0||Shoot.enemyShoot[i+1]>HEIGHT){
Shoot.enemyShoot[i]=0;Shoot.enemyShoot[i+1]=0;
Shoot.enemyShoot[i+2]=0;Shoot.enemyShoot[i+3]=0;
}
int x=Shoot.enemyShoot[i];
int y=Shoot.enemyShoot[i+1];
//撞到柱子
if((x>330&&x<360&&y>150&&y<380)||(x>130&&x<570&&y>240&&y<280)){
Shoot.enemyShoot[i]=0;Shoot.enemyShoot[i+1]=0;
Shoot.enemyShoot[i+2]=0;Shoot.enemyShoot[i+3]=0;
}
}
}
}
/********************Tanks类***********************/