Java编写实现坦克大战小游戏

本文实例为大家分享了Java实现坦克大战小游戏的具体代码,供大家参考,具体内容如下
创作背景:n年前的学期末课题设计,从b站上学的,一个代码一个代码敲出来的。
小游戏介绍: 红色坦克是我们的操纵坦克,黑色是敌人坦克。
上下左右键控制坦克移动方向
按ctrl键发射炮弹
红色坦克可以穿墙,黑色不可以
具体页面如下:
Java编写实现坦克大战小游戏
文章图片

奉上全部源代码:
Tank.java

import java.awt.*; import java.awt.event.*; import java.util.*; public class Tank {private int x; private int y; privateint oldx; privateint oldy; private int life = 100; private boolean bL = false ,bU = false ,bR = false ,bD= false; //产生随机数private static Random r = new Random(); //九种坦克运动方向enum Direction{L,LU,U,RU,R,RD,D,LD,STOP}; //初始化坦克方向private Directiondir = Direction.STOP; //初始化炮筒方向private Direction ptDir = Direction.U; //坦克移动速度private static final int XSPEED = 5; private static final intYSPEED = 5; //坦克大小private static final int WIDTH = 30; private static final int HIGHT =30; //定义TankClient类TankClient tc; public int getLife(){returnlife; }public void setLife(int life){this.life =life; }private boolean good =true ; //定义坦克类型,敌方还是我方public boolean isgood(){return good; }//定义坦克状态private boolean live = true; //设置enemy坦克随机移动步数private static int step = r.nextInt(12)+3; //构造坦克状态方法public boolean islive (){return live; }public void setlive(boolean live){this.live = live; }//构造方法public Tank(int x, int y) {this.x = x; this.y = y; }public Tank (int x,int y,Boolean good ,Direction dir,TankClient tc){this (x,y); this.good = good; this.dir = dir ; this.tc = tc; }public void draw (Graphics g){if (!live) {if (!good){tc.tanks.remove(this); }return; }Color c = g.getColor(); //?if(good==true){g.setColor(Color.RED); }//定义我方坦克颜色else g.setColor(Color.BLACK); //定义敌方坦克颜色g.fillOval(x,y, WIDTH, HIGHT); //定义坦克位置及大小g.setColor(c); //?move(); switch (ptDir)//画炮筒{case L:g.drawLine(x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x, y+ Tank.HIGHT / 2); break; case LU:g.drawLine(x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x, y); break; case U:g.drawLine(x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x + Tank.WIDTH/ 2, y); break; case RU:g.drawLine(x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x + Tank.WIDTH,y); break; case R:g.drawLine(x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x + Tank.WIDTH,y + Tank.HIGHT / 2); break; case RD:g.drawLine(x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x + Tank.WIDTH,y + Tank.HIGHT); break; case D:g.drawLine(x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x + Tank.WIDTH/ 2, y + Tank.HIGHT); break; case LD:g.drawLine(x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x, y+ Tank.HIGHT); break; }}private void stay(){this.x=oldx; this.y=oldy; }//坦克移动void move (){this.oldx=x; this.oldy=y; switch(dir){case L:x-=XSPEED; break; case LU:x-=XSPEED; y-=YSPEED; break; case U:y-=YSPEED; break; case RU:x+=XSPEED; y-=YSPEED; break; case R:x+=XSPEED; break; case RD:x+=XSPEED; y+=YSPEED; break; case D:y+=YSPEED; break; case LD:x-=XSPEED; y+=YSPEED; break; case STOP:break; }if (this.dir!=Direction.STOP){ptDir = dir; }if (x<0) x=0; if (y<20) y=20; if (x>TankClient.WinWidth-Tank.WIDTH) x=TankClient.WinWidth-Tank.WIDTH; if (y>TankClient.WinHigh-Tank.HIGHT) y=TankClient.WinHigh-Tank.HIGHT; //让enemy坦克自由移动if (!good){Direction[] dirs= Direction.values(); //将枚举转化为数组if (step ==0){step = r.nextInt(12)+3; int rn = r.nextInt(dirs.length); //产生随机数dir = dirs[rn]; }step--; if (r.nextInt(40)>38){this.fire(); }}}//坦克方向void localDirection(){if (bL&&!bU&&!bR&&!bD) dir= Direction.L; else if (bL&&bU&&!bR&&!bD) dir= Direction.LU; else if (!bL&&bU&&!bR&&!bD) dir= Direction.U; else if (!bL&&bU&&bR&&!bD) dir= Direction.RU; else if (!bL&&!bU&&bR&&!bD) dir= Direction.R; else if (!bL&&!bU&&bR&&bD) dir= Direction.RD; else if (!bL&&!bU&&!bR&&bD) dir= Direction.D; else if (bL&&!bU&&!bR&&bD) dir= Direction.LD; else dir =Direction.STOP; }public void keyPressed(KeyEvent e) {int key = e.getKeyCode(); switch (key){case KeyEvent.VK_CONTROL:fire(); break; case KeyEvent.VK_LEFT:bL=true; break; case KeyEvent.VK_UP:bU=true; break; case KeyEvent.VK_RIGHT:bR=true; break; case KeyEvent.VK_DOWN:bD=true; break; }localDirection(); //获取执行方向}public void keyReleased(KeyEvent e) {int key = e.getKeyCode(); switch (key){case KeyEvent.VK_LEFT:bL=false; break; case KeyEvent.VK_UP:bU=false; break; case KeyEvent.VK_RIGHT:bR=false; break; case KeyEvent.VK_DOWN:bD=false; break; }localDirection(); }//定义坦克发射子弹类public Missile fire(){if (!live) return null; int x= this.x+Tank.WIDTH/2-Missile.WIDTH/2; int y= this.y +Tank.HIGHT/2-Missile.HIGHT/2; Missile m = new Missile(x,y,ptDir,good,tc); tc.Missiles.add(m); return m; }//《碰撞检测》获取坦克矩形属性public Rectangle getRect(){return new Rectangle(x,y,WIDTH,HIGHT); }//《碰撞检测》public boolean tankHitWall(Wall w){if(this.getRect().intersects(w.getRect())){stay(); return true; }return false; }public boolean tankHitTank(java.util.List tanks){for (int i=0; i
【Java编写实现坦克大战小游戏】Wall.java
import java.awt.*; public class Wall {int x; int y; int width; int height; TankClient tc; public Wall (int x,int y,int width,int height, TankClient tc){this.x= x; this.y= y; this.width = width; this.height = height; this.tc = tc; }public void draw(Graphics g){Color c = g.getColor(); g.setColor(Color.GRAY); g.fillRect(x, y, width, height); g.setColor(c); }//《碰撞检测》获取矩形属性public Rectangle getRect(){return new Rectangle(x,y,width,height); }}

TankClient.java
import java.awt.*; import java.awt.event.*; import java.util.List; import java.util.ArrayList; public class TankClient extends Frame {Tank myTank = new Tank(400,430,true,Tank.Direction.STOP,this); //创建自己的坦克List tanks= new ArrayList(); //创建敌人坦克容器List Explodes = new ArrayList(); //创建爆炸容器List Missiles = new ArrayList(); //定义容器,存放炮弹Wall wall1 = new Wall(100,100,30,400,this); Wall wall2 = new Wall(350,400,400,30,this); /** 定义窗口大小变量*/public static final int WinWidth=800; public static final int WinHigh=600; //定义框架大小public void launchFrame(){//添加10辆敌人坦克for (int i = 0; i<10; i++){tanks.add(new Tank(50+40*i,100,false,Tank.Direction.D,this)); }this.setLocation(40,40); //定义窗口位置this.setSize(WinWidth,WinHigh); //设置窗口大小this.setTitle("坦克大战"); //设置窗口标题//设置监听器,使窗口关闭this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0); }}); this.setBackground(Color.WHITE); //设置窗口背景颜色this.setVisible(true); //设置窗口可见this.setResizable(false); //设置为不可调整窗口大小this.addKeyListener(new KeyMonitor()); new Thread(new PaintThread()).start(); }//定义一个新图片为空,双缓冲Image OffScreenImage = null; /** 定义画板*/public void paint(Graphics g){//g.drawString("当前炮弹数"+Missiles.size(), 40, 80); //g.drawString("当前爆炸数"+Explodes.size(), 40, 100); g.drawString("Tank数量"+tanks.size(),40,40); g.drawString("MyTank血量"+myTank.getLife(),40,60); for (int i=0; i
Explode.java
import java.awt.*; public class Explode {int x; int y; private boolean live = true; private TankClient tc ; int[] diameter = {30,40,50,40,30,10,5}; int step = 0; //结构public Explode (int x ,int y,TankClient tc) {this.x =x; this.y = y; this.tc = tc; }public void draw (Graphics g) {if(!live){tc.Explodes.remove(this); return ; }if (step ==diameter.length) {live = false ; step = 0; return ; }Color c = g.getColor(); g.setColor(Color.RED); g.fillOval(x, y, diameter[step], diameter[step]); g.setColor(c); step++; }}

Missile.java
import java.awt.*; import java.util.List; //定义子弹类public class Missile {//定义子弹速度public static final int XSPEED=10; public static final int YSPEED=10; //定义子弹大小public static final int WIDTH =10; public static final int HIGHT =10; private boolean live= true ; //定义炮弹状态//定义发出的炮弹归属private boolean good; public boolean islive(){return live; }int x,y ; //子弹坐标TankClient tc; Tank.Direction dir; public Missile (int x,int y ,Tank.Direction dir){this.x=x; this.y=y; this.dir=dir; }public Missile(int x,int y,Tank.Direction dir,boolean good,TankClient tc){this(x,y,dir); this.good = good; this.tc=tc; }public void draw (Graphics g){if (!live) {tc.Missiles.remove(this); }Color c= g.getColor(); if (good){g.setColor(Color.RED); }else {g.setColor(Color.BLACK); }g.fillOval(x, y, WIDTH,HIGHT); //设置炮弹大小g.setColor(c); move(); }void move (){switch(dir){case L:x-=XSPEED; break; case LU:x-=XSPEED; y-=YSPEED; break; case U:y-=YSPEED; break; case RU:x+=XSPEED; y-=YSPEED; break; case R:x+=XSPEED; break; case RD:x+=XSPEED; y+=YSPEED; break; case D:y+=YSPEED; break; case LD:x-=XSPEED; y+=YSPEED; break; }if (x<0||y<0||x>TankClient.WinWidth||y>TankClient.WinHigh){live =false; }}//《碰撞检测》获取子弹矩形属性public Rectangle getRect(){return new Rectangle(x,y,WIDTH,HIGHT); }//《碰撞检测》public boolean hitTank(Tank t){if(this.live&&this.getRect().intersects(t.getRect())&&t.islive()&& this.good!=t.isgood()){if (t.isgood()){t.setLife(t.getLife()-20); if (t.getLife()<=0){t.setlive(false); }}else{t.setlive(false); }this.live = false; Explode e = new Explode(x-10,y-10,tc); tc.Explodes.add(e); return true; }return false; }public boolean hitTanks(List tanks){for (int i=0; i
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读