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;
}
}
爆炸圖檔