1>myTankGame5.java
/**
* 功能:画出坦克
* 1>我方坦克一个
* 2>敌人坦克3个
* 3>坦克可以连发×××
* 4>添加×××效果
* 5>敌方坦克可以自己移动
* 新功能
* 6>控制坦克移动范围
* 7>让敌方坦克可以发射×××
*/
package com.tank5;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.*;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.*;
import javax.swing.plaf.SliderUI;
public class myTankGame5 extends JFrame{
public static void main(String[] args){
myTankGame5 myt1=new myTankGame5();
}
public myTankGame5(){
MyPanel myp=new MyPanel();
this.addKeyListener(myp);
//启整个画板线程
Thread t2 = new Thread(myp);
t2.start();
this.add(myp);
this.setTitle("坦克大战!");
this.setSize(400,300);
//退出时关闭进程
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class MyPanel extends JPanel implements KeyListener,Runnable{
hero myhero;
int x=330;
int y=30;
int esize=5;
Vector<EnemyTank> etank = new Vector<EnemyTank>();
Vector<Bomb> bombs = new Vector<Bomb>();
public MyPanel(){
myhero=new hero(x, y);
etank=new Vector<EnemyTank>();
for(int i=0;i<esize;i++){
EnemyTank tmptank=new EnemyTank((i+1)*70, 30);
this.etank.add(tmptank);
Thread tt = new Thread(tmptank);
tt.start();
}
}
//重写paint()函数
public void paint(Graphics g){
super.paint(g);
g.fillRect(0, 0, 400, 300);
//画出自己的坦克
this.drawTank(myhero.getX(), myhero.getY(), g, myhero.getDirection(), 0);
//画出我的坦克×××
for(int i=0;i<this.myhero.ss.size();i++){
Shot s=this.myhero.ss.get(i);
if(s!=null && s.isLive){
//画出×××
g.draw3DRect(s.getX(), s.getY(), 1,1,false);
}else{
this.myhero.ss.remove(s);
}
}
//画出敌人的坦克
for(int i=0;i<this.etank.size();i++){
EnemyTank tmptank=this.etank.get(i);
if(tmptank.isLive==true){
this.drawTank(tmptank.getX(), tmptank.getY(), g, tmptank.getDirection(), 1);
//画出敌方坦克的×××
for(int jj=0;jj<tmptank.ss.size();jj++){
Shot tmpShot = tmptank.ss.get(jj);
if(tmpShot.isLive){
g.draw3DRect(tmpShot.getX(), tmpShot.getY(), 1, 1, false);
}else{
tmptank.ss.remove(tmpShot);
}
}
}
}
//清除无效的坦克
for(int i=0;i<this.etank.size();i++){
EnemyTank tmptank=this.etank.get(i);
if(tmptank.isLive!=true){
this.etank.remove(tmptank);
}
}
//画出×××
for(int i=0;i<this.bombs.size();i++){
//取出×××
Bomb bom = this.bombs.get(i);
//显示×××图片
if(bom.isLive){
g.drawImage(bom.p_w_picpath, bom.x, bom.y, 30, 30, this);
}else{
bombs.remove(bom);
}
}
}
public void hitTank(Shot s,EnemyTank etank){
switch (etank.direction){
case 0:
if(s.x>=etank.x && s.x<=etank.x+30 && s.y>=etank.y && s.y<etank.y+20 ){
etank.isLive=false;
s.isLive=false;
Bomb bom = new Bomb(etank.x, etank.y);
bombs.add(bom);
//启动×××线程
Thread t = new Thread(bom);
t.start();
}
break;
case 1:
if(s.x>=etank.x && s.x<=etank.x+20 && s.y>=etank.y && s.y<=etank.y+30 ){
etank.isLive=false;
s.isLive=false;
Bomb bom = new Bomb(etank.x, etank.y);
bombs.add(bom);
//启动×××线程
Thread t = new Thread(bom);
t.start();
}
break;
case 2:
if(s.x>=etank.x && s.x<=etank.x+30 && s.y>=etank.y && s.y<etank.y+20 ){
etank.isLive=false;
s.isLive=false;
Bomb bom = new Bomb(etank.x, etank.y);
bombs.add(bom);
//启动×××线程
Thread t = new Thread(bom);
t.start();
}
break;
case 3:
if(s.x>=etank.x && s.x<=etank.x+20 && s.y>=etank.y && s.y<=etank.y+30 ){
etank.isLive=false;
s.isLive=false;
Bomb bom = new Bomb(etank.x, etank.y);
bombs.add(bom);
//启动×××线程
Thread t = new Thread(bom);
t.start();
}
break;
}
}
//画出坦克
public void drawTank(int x,int y,Graphics g,int derect,int type){
switch (type){
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.yellow);
break;
}
switch(derect){
case 0:
//坦克方向向左
//画出我的坦克,到时再封装到函数中
//画出坦克左侧的方框
/*x=x-30/2;
y=y-20/2;*/
//画出左侧链条车轮
g.fill3DRect(x, y+15, 30, 5,false);
//画出中间矩形
g.fill3DRect(x+5, y+5, 20, 11,false);
//画出右侧链条车轮
g.fill3DRect(x, y, 30, 5,false);
//画出中间坦克盖子
g.fillRoundRect(x+12, y+6, 6, 6, 200, 200);
//画出线
g.drawLine(x, y+9, x+15, y+9);
break;
case 1:
//坦克方向向上
//画出我的坦克,到时再封装到函数中
//画出坦克左侧的方框
g.fill3DRect(x, y, 5, 30,false);
//画出
g.fill3DRect(x+15, y, 5, 30,false);
g.fill3DRect(x+5, y+5, 10, 20,false);
//画出中间坦克盖子
g.fillRoundRect(x+6,y+12,6, 6, 200, 200);
//画出线
g.drawLine(x+9, y, x+9, y+15);
break;
case 2:
//坦克方向向右
//画出左侧链条车轮
g.fill3DRect(x, y+15, 30, 5,false);
//画出中间矩形
g.fill3DRect(x+5, y+5, 20, 11,false);
//画出右侧链条车轮
g.fill3DRect(x, y, 30, 5,false);
//画出中间坦克盖子
g.fillRoundRect(x+12, y+6, 6, 6, 200, 200);
//画出线
g.drawLine(x+15, y+9, x+30, y+9);
break;
case 3:
//坦克方向向下
//画出我的坦克,到时再封装到函数中
//画出坦克左侧的方框
g.fill3DRect(x, y, 5, 30,false);
//画出
g.fill3DRect(x+15, y, 5, 30,false);
g.fill3DRect(x+5, y+5, 10, 20,false);
//画出中间坦克盖子
g.fillRoundRect(x+6,y+12,6, 6, 200, 200);
//画出线
g.drawLine(x+9, y+15, x+9, y+30);
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==e.VK_A){
this.myhero.setDirection(0);
this.myhero.moveLeft();
}else if(e.getKeyCode()==e.VK_W){
this.myhero.setDirection(1);
this.myhero.moveUp();
}else if(e.getKeyCode()==e.VK_D){
this.myhero.setDirection(2);
this.myhero.moveRight();
}else if(e.getKeyCode()==e.VK_S){
this.myhero.setDirection(3);
this.myhero.moveDown();
}
if (e.getKeyChar()=='j'){
if(this.myhero.ss.size()<=4){
this.myhero.shotEnemy();
}
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try{
Thread.sleep(100);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
for(int i=0;i<myhero.ss.size();i++){
Shot s=myhero.ss.get(i);
if(s.isLive){
for(int j=0;j<this.etank.size();j++){
EnemyTank tmpTank=etank.get(j);
hitTank(s,tmpTank);
}
}
}
this.repaint();
}
}
}
2>Members.java
package com.tank5;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.p_w_picpath.ImageObserver;
import java.util.Vector;
/*
* ×××类
*/
class Bomb implements Runnable {
//定义×××的坐标
int x,y;
//×××的生命
int life=9;
Image p_w_picpath=null; //×××图片
boolean isLive = true;
public Bomb(int x,int y){
this.x=x;
this.y=y;
}
//减少生命值
public void lifeDown(){
if(this.life>0){
this.life--;
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try{
Thread.sleep(50);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
if(this.isLive){
//更换化×××图片,制作爆炸效果
if(this.life>6){
p_w_picpath = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
}else if(this.life>4){
p_w_picpath = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
}else{
p_w_picpath = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));
}
this.lifeDown();
if(this.life==0){
this.isLive=false;
}
}
}
}
}
/*
* ×××类
* @param: x横坐标
* @param: y纵坐标
* @param: derect 方向
* @param: speed×××速度
* @param: isLive×××是否有效
*/
class Shot implements Runnable{
int x;
int y;
int derect;
int speed=1;
boolean isLive=true;
public Shot(int x,int y,int derect){
this.x=x;
this.y=y;
this.derect=derect;
}
int getX() {
return this.x;
}
void setX(int x) {
this.x = x;
}
int getY() {
return this.y;
}
void setY(int y) {
this.y = y;
}
@Override
public void run() {
// TODO Auto-generated method stub
//修改×××位置,让×××动起来
while(true){
try{
Thread.sleep(40);
} catch (Exception e){
e.printStackTrace();
}
switch(derect){
case 0:
this.x-=speed;
break;
case 1:
this.y-=speed;
break;
case 2:
this.x+=speed;
break;
case 3:
this.y+=speed;
break;
}
if(x<0||x>400||y<0||y>300){
isLive = false;
}
}
}
}
//我的坦克
class hero extends Tank{
//×××
Shot s;
Vector<Shot> ss=new Vector<Shot>();
public hero(int x,int y){
super(x,y);
}
public void shotEnemy(){
switch(this.direction){
case 0:
s = new Shot(x,y+10,this.direction);
break;
case 1:
s = new Shot(x+10,y,this.direction);
break;
case 2:
s = new Shot(x+30,y+10,this.direction);
break;
case 3:
s = new Shot(x+10,y+30,this.direction);
break;
}
ss.add(s);
Thread t=new Thread(s);
t.start();
}
//我的坦克向左移动
public void moveLeft(){
if(this.x<=0){
x=x;
}else{
x-=speed;
}
}
//我的坦克向上移动
public void moveUp(){
if(this.y<=0){
y=y;
}else{
y-=speed;
}
}
//我的坦克向右移动
public void moveRight(){
if(this.x>=350){
x=x;
}else{
x+=speed;
}
}
//我的坦克向下移动
public void moveDown(){
if(this.y>=230){
y=y;
}else{
y+=speed;
}
}
}
//敌人的坦克
class EnemyTank extends Tank implements Runnable{
Boolean isLive=true;
Shot s=null;
Vector<Shot> ss=new Vector<Shot>();
public EnemyTank(int x,int y){
super(x,y);
int direction;
direction = (int)(Math.random()*4);
this.setDirection(direction);
this.speed=1;
}
//坦克向左移动
public void moveLeft(){
if(this.x<=0){
this.direction=2;
}else{
x-=speed;
}
}
//坦克向上移动
public void moveUp(){
if(this.y<=0){
this.direction=3;
}else{
y-=speed;
}
}
//坦克向右移动
public void moveRight(){
if(this.x>=350){
this.direction=0;
}else{
x+=speed;
}
}
//坦克向下移动
public void moveDown(){
if(this.y>=230){
this.direction=1;
}else{
y+=speed;
}
}
//
public void shotEnemy(){
switch(this.direction){
case 0:
s = new Shot(x,y+10,this.direction);
break;
case 1:
s = new Shot(x+10,y,this.direction);
break;
case 2:
s = new Shot(x+30,y+10,this.direction);
break;
case 3:
s = new Shot(x+10,y+30,this.direction);
break;
}
ss.add(s);
Thread t=new Thread(s);
t.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try{
Thread.sleep(100);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
switch(this.direction){
case 0:
for(int i=0;i<30;i++){
if(this.direction==0){
this.moveLeft();
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}else{
break;
}
}
break;
case 1:
for(int i=0;i<30;i++){
if(this.direction==1){
this.moveUp();
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}else{
break;
}
}
break;
case 2:
for(int i=0;i<30;i++){
if(this.direction==2){
this.moveRight();
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}else{
break;
}
}
break;
case 3:
for(int i=0;i<30;i++){
if(this.direction==3){
this.moveDown();
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}else{
break;
}
}
}
this.direction = (int)(Math.random()*4);
if(this.ss.size()==0){
this.shotEnemy();
}
}
}
}
//坦克类
class Tank{
//坦克的横坐标
int x=0;
//坦克的纵坐标
int y=0;
//移动方向
int direction=1;
int getDirection() {
return direction;
}
void setDirection(int direction) {
this.direction = direction;
}
//移动速度
int speed=2;
public Tank(int x,int y){
this.x=x;
this.y=y;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
}
爆炸图片
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiInBnauIzN4MWQjRWeFJzUFFUQtBjTtlmamRjYMFDTvl2S39CXFJ0LcFTMvwFMw00LcJDMzZWe39CXt92Yu8GdjFTNuMzcvw1LcpDc0RHaiojIsJye.jpg)