import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TanKGame extends JFrame{
MyPanel mp=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
TanKGame tg=new TanKGame();
}
public TanKGame()
{
mp=new MyPanel();
//启动Mypanel的线程
Thread t=new Thread(mp);
t.start();
this.add(mp);
this.addKeyListener(mp);
this.setSize(400, 300);
this.setVisible(true);
}
}
class MyPanel extends JPanel implements KeyListener,Runnable
{
//定义一个坦克
Hero hero=null;
//定义一个敌人的坦克
Vector<EnemyTanK> ets=new Vector<EnemyTanK>();
int enSize=3;
public MyPanel()
{
hero=new Hero(50, 50);
//初始化敌人坦克
for(int i=0; i<enSize; i++)
{
//创建一辆敌人的坦克
EnemyTanK et=new EnemyTanK((i+1)*50, 0);
et.setDrect(2);
et.setColor(0);
//加入
ets.add(et);
}
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
//画出自己的坦克
this.drawTanK(hero.getX(), hero.getY(), g, this.hero.drect, 1);
//画出炮弹
for(int i=0;i<this.hero.ss.size();i++)
{
Shot myShot=hero.ss.get(i);
if(myShot!=null&&myShot.isLive==true)
{
g.draw3DRect(myShot.x, myShot.y, 1, 1, false);
}
if(myShot.isLive==false)
{
hero.ss.remove(myShot);
}
}
//画出敌人的坦克
for(int i=0;i<ets.size();i++)
{
EnemyTanK et=ets.get(i);
if(et.isLive)
{
this.drawTanK(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).getDrect(), 0);
}
}
}
public void hitTanK(Shot s,EnemyTanK et)
{
switch(et.drect)
{
case 0:
case 2:
if(s.x>et.x&&s.x<et.x+20&&s.y>et.y&&s.y<et.y+30)
{
//击中
//子弹死亡
s.isLive=false;
//敌人坦克死亡
et.isLive=false;
}
case 1:
case 3:
if(s.x>et.x&&s.x<et.x+30&&s.y>et.y&&s.y<et.y+20)
{
//击中
//子弹死亡
s.isLive=false;
//敌人坦克死亡
et.isLive=false;
}
}
}
public void drawTanK(int x,int y,Graphics g,int direct,int type)
{
switch(type)
{
case 0:
g.setColor(Color.red);
break;
case 1:
g.setColor(Color.yellow);
break;
}
switch(direct)
{
//向上
case 0:
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.fillOval(x+4, y+10, 10, 10);
g.drawLine(x+9, y+15,x+9, y);
break;
//向右
case 1:
g.fill3DRect(x, y, 30, 5, false);
g.fill3DRect(x, y+15, 30, 5, false);
g.fill3DRect(x+5, y+5, 20, 10, false);
g.fillOval(x+10, y+4, 10, 10);
g.drawLine(x+15, y+9,x+29, y+9);
break;
//向下
case 2:
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.fillOval(x+4, y+10, 10, 10);
g.drawLine(x+9, y+15,x+9, y+30);
break;
//向左
case 3:
g.fill3DRect(x, y, 30, 5, false);
g.fill3DRect(x, y+15, 30, 5, false);
g.fill3DRect(x+5, y+5, 20, 10, false);
g.fillOval(x+10, y+4, 10, 10);
g.drawLine(x+15, y+9,x, y+9);
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()==KeyEvent.VK_W)
{
this.hero.setDrect(0);
this.hero.moveUp();
}else if(e.getKeyCode()==KeyEvent.VK_D)
{
this.hero.setDrect(1);
this.hero.moveRight();
}else if(e.getKeyCode()==KeyEvent.VK_S)
{
this.hero.setDrect(2);
this.hero.moveDown();
}else if(e.getKeyCode()==KeyEvent.VK_A)
{
this.hero.setDrect(3);
this.hero.moveLeft();
}
if(e.getKeyCode()==KeyEvent.VK_J)
{
if(this.hero.ss.size()<=4)
{
this.hero.shotEnemy();
}
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void run() {
// TODO Auto-generated method stub
//每个100毫秒重绘子弹
while(true)
{
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
for(int i=0;i<hero.ss.size();i++)
{
Shot myShot=hero.ss.get(i);
if(myShot.isLive)
{
for(int j=0;j<ets.size();j++)
{
EnemyTanK et=ets.get(j);
if(et.isLive)
{
this.hitTanK(myShot, et);
}
}
}
}
this.repaint();
}
}
}
//封装的坦克类
package com.tank2;
import java.util.Vector;
//子弹类
class Shot implements Runnable
{
int x;
int y;
int direct;
int speed=1;
boolean isLive=true;
public Shot(int x,int y,int direct)
{
this.x=x;
this.y=y;
this.direct=direct;
}
@Override
public void run() {
while(true)
{
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
}
switch(direct)
{
case 0:
y-=speed;
break;
case 1:
x+=speed;
break;
case 2:
y+=speed;
break;
case 3:
x-=speed;
break;
}
System.out.println("x="+x+"y="+y);
if(x<0||x>400||y<0||y>300)
{
this.isLive=false;
break;
}
}
}
}
//坦克类
class TanK
{
//坦克颜色
int color;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
//坦克的速度
int speed=1;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
//坦克的方向
int drect=0;
public int getDrect() {
return drect;
}
public void setDrect(int drect) {
this.drect = drect;
}
//坦克的横坐标
int x=0;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
//坦克的纵坐标
int y=0;
public TanK(int x,int y)
{
this.x=x;
this.y=y;
}
}
//敌人的坦克
class EnemyTanK extends TanK
{
boolean isLive=true;
public EnemyTanK(int x, int y) {
super(x, y);
}
}
//我的坦克
class Hero extends TanK
{
//子弹
Vector<Shot> ss=new Vector<Shot>();
Shot s=null;
public Hero(int x,int y)
{
super(x, y);
}
public void shotEnemy()
{
switch(this.drect)
{
case 0:
//创建一颗子弹
s=new Shot(x+9, y,0);
//将子弹加入向量
ss.add(s);
break;
case 1:
s=new Shot(x+30, y+9,1);
ss.add(s);
break;
case 2:
s=new Shot(x+9, y+30,2);
ss.add(s);
break;
case 3:
s=new Shot(x, y+9,3);
ss.add(s);
break;
}
Thread t=new Thread(s);
t.start();
}
public void moveUp()
{
this.y-=this.speed;
}
public void moveRight()
{
this.x+=this.speed;
}
public void moveDown()
{
this.y+=this.speed;
}
public void moveLeft()
{
this.x-=this.speed;
}
}