package com.test3;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyTankGame2 extends JFrame {
Mypanel mp = null;
public MyTankGame2() {
// TODO 自动生成的构造函数存
mp = new Mypanel();
this.add(mp);
Thread t=new Thread(mp);
t.start();
// 3,在事件源上注册监听
this.addKeyListener(mp);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
/**
* 监听的实现步骤
* 1,实现相应接口(keylistener,mouselistener···)
* 2,重写接口方法
* 3,在事件源上注册监听
* 4,事件传递靠事件对象(e.getactioncommand().equals())
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根
MyTankGame2 mt = new MyTankGame2();
}
}
// 我的面板
// 1,实现相应接口(keylistener,mouselistener···)
// 2,重写接口方法
class Mypanel extends JPanel implements KeyListener ,Runnable{
// 定义我的坦克
Mytank hero = null;
//定义炸弹集合
Vectorbombs=new Vector();
//定义三张图片
Image image1=null;
Image image2=null;
Image image3=null;
//定义敌人坦克
Vector ets=new Vector();
int enemynumber=3;
int x = 200;
int y = 200;
public Mypanel() {
hero = new Mytank(x, y);
//初始化敌人坦克
for(int i=0;i
EnemyTank et=new EnemyTank((i+1)*50,0);
et.setDirect(2);
et.setColor(0);
Thread t=new Thread(et);
t.start();
ets.add(et);
}
//初始化
image1 =Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/baozha0.jpg"));
image2 =Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/baozha1.jpg"));
image3 =Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/baozha2.jpg"));
}
// 重写paint
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 400, 300);
this.drawTank(hero.getX(), hero.getY(), g, this.hero.getDirect(), 1);
//画出子弹
for(int i=0;i
{
Shot myshot=hero.ss.get(i);
if(myshot!=null&&myshot.isLive==true)
{
g.draw3DRect(myshot.x, myshot.y, 2, 2, false);
}
if(myshot.isLive==false){
hero.ss.remove(myshot);
}
}
//画炸弹
for(int i=0;i
{
Bomb b=bombs.get(i);
if(b.life>6){
g.drawImage(image1, b.x,b.y,30, 30, this);
}else if(b.life>3){
g.drawImage(image2, b.x, b.y, 30,30,this);
}else {
g.drawImage(image3, b.x, b.y, 30,30,this);
}
b.lifedown();
if(b.life==0){
bombs.remove(b);
}
}
//画出敌人坦克
for(int i=0;i
EnemyTank et=ets.get(i);
if(et.isLive){
this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).getDirect(), 0);
}
}
}
//子弹是否集中敌人坦克
public void hitTank(Shot s,EnemyTank et) {
//判断坦克方向
switch (et.direct) {
case 0:
case 2:
if(s.x>et.x&&s.xet.y&&s.y
{
//击中
//子弹死亡
s.isLive=false;
//敌人死亡
et.isLive=false;
//创建炸弹,放入集合中
Bomb b=new Bomb(et.x,et. y);
bombs.add(b);
}
case 1:
case 3:
if(s.x>et.x&&s.xet.y&&s.y
{
//击中
//子弹死亡
s.isLive=false;
//敌人死亡
et.isLive=false;
Bomb b=new Bomb(et.x,et. y);
bombs.add(b);
}
break;
default:
break;
}
}
// 画出坦克的函数
public void drawTank(int x, int y, Graphics g, int direct, int type) {
switch (type) {
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.yellow);
break;
}
// 判断方向//坦克方向,0 up 2 down 1 right 3 left
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 + 5, y + 10, 10, 10);
g.drawLine(x + 9, y + 15, x + 9, y);
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+5, 10, 10);
g.drawLine(x+15, y+9, x, 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 + 5, y + 10, 10, 10);
g.drawLine(x + 9, y + 15, x + 9, y+30);
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+5, 10, 10);
g.drawLine(x+15, y+9, x+30, y+9);
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void keyPressed(KeyEvent e) {
// TODO 自动生成的方法存根
if (e.getKeyCode() == KeyEvent.VK_S) {
this.hero.setDirect(2);
this.hero.moveDown();
} else if (e.getKeyCode() == KeyEvent.VK_W) {
this.hero.setDirect(0);
this.hero.moveUp();
} else if (e.getKeyCode() == KeyEvent.VK_A) {
this.hero.setDirect(3);
this.hero.moveLeft();
} else if (e.getKeyCode() == KeyEvent.VK_D) {
this.hero.setDirect(1);
this.hero.moveRight();
}
//开火
if (e.getKeyCode() == KeyEvent.VK_J) {
if(this.hero.ss.size()<5){
this.hero.shotEnemy();
}
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void run() {
// TODO 自动生成的方法存根
while (true)
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//判断是否击中
for(int i=0;i
{
Shot myShot=hero.ss.get(i);
if(myShot.isLive){
for(int j=0;j
{
EnemyTank et=ets.get(j);
if(et.isLive){
this.hitTank(myShot, et);
}
}
}
}
this.repaint();
}
}
}
package com.test3;
import java.util.Vector;
class Tank { // 坦克横纵坐标
int x = 0;
int y = 0;
//设置速度
int speed=1;
int color;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
//坦克方向,0 up 2 down 1 right 3 left
int direct=0;
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
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;
}
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
}
class Mytank extends Tank {
//子弹
Shot s=null;
Vectorss=new Vector();
//向上
public void moveUp() {
y-=speed;
}
public void moveDown() {
y+=speed;
}
public void moveLeft() {
x-=speed;
}
public void moveRight() {
x+=speed;
}
public Mytank(int x, int y) {
super(x, y);
// TODO 自动生成的构造函数存根
}
public void shotEnemy()
{
switch (this.direct) {
case 0:
s=new Shot(x+10, y,0);
ss.add(s);
break;
case 1:
s=new Shot(x+30, y+10,1);
ss.add(s);
break;
case 2:
s=new Shot(x+10, y+30,2);
ss.add(s);
break;
case 3:
s=new Shot(x, y+10,3);
ss.add(s);
break;
}
Thread t=new Thread(s);
t.start();
}
}
//敌人坦克
class EnemyTank extends Tank implements Runnable
{
boolean isLive=true;
public EnemyTank(int x, int y) {
super(x, y);
// TODO 自动生成的构造函数存根
}
@Override
public void run() {
// TODO 自动生成的方法存根
while(true){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//坦克移动
switch (this.direct) {
case 0:
for(int i=0 ;i<30;i++){
y-=speed;
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
break;
case 1:
for(int i=0 ;i<30;i++){
x+=speed;
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
break;
case 2:
for(int i=0 ;i<30;i++){
y+=speed;
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
break;
case 3:
for(int i=0 ;i<30;i++){
x-=speed;
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
break;
}
//让坦克随机新方向
this.direct=(int)(Math.random()*4);
//判断是否死亡
if(this.isLive==false){
//退出线程
break;
}
}
}
}
//炸弹
class Bomb
{
int x,y;
int life=9;
boolean isLive=true;
public Bomb (int x ,int y) {
this.x=x;
this.y=y;
}
public void lifedown() {
if(life>0){
life--;
}else{
this.isLive=false;
}
}
}
//子弹
class Shot implements Runnable
{
int x ;
int y ;
int direct;
int speed=2;
boolean isLive=true;
public Shot(int x,int y,int direct) {
// TODO 自动生成的构造函数存根
this.x=x;
this.y=y;
this.direct=direct;
}
@Override
public void run() {
// TODO 自动生成的方法存根
while(true){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
switch (direct) {
case 0:
y-=speed;
break;
case 1:
x+=speed;
break;
case 2:
y+=speed;
break;
case 3:
x-=speed;
break;
}
System.err.println("子弹坐标"+x+","+y);
//判断子弹是否碰到边缘
if (x<0||x>400||y<0||y>300) {
this.isLive=false;
break;
}
}
}
}
转载本文请联系原作者获取授权,同时请注明本文来自倪升华科学网博客。
链接地址:http://blog.sciencenet.cn/blog-785542-715447.html
上一篇:kmeans算法修正2.0(实现多线程处理后,输出汇总结果)
下一篇:坦克大战3.2(敌人坦克可发子弹,加入边界限制)