java 实现坦克大战2.1版

/*
 * 
 * 功能:坦克大战2.1.版
 * 画出坦克
 * 可以上下左右移动
 */

package com.haitao.tank3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class MyTankGame3  extends JFrame{

	  MyPanel mp=null;

	public static void main(String[] args) {
		MyTankGame3 mt=new MyTankGame3();

	}
	
	
	//构造函数
	public MyTankGame3()
	{
		mp=new MyPanel();
		//启动mp线程
		Thread t=new Thread(mp);
		t.start();
		
		
		this.add(mp);
		
		
		//注册监听
		this.addKeyListener(mp);
		
		this.setTitle("坦克大战");
		this.setSize(400,500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		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 (170,260);
		//初始化敌人的坦克
		for(int i=0;i<enSize;i++)
		{
			//创建一辆敌人的坦克对象
		EnemyTank et=new EnemyTank((i+1)*50,0);
		et.setColor(0);
		et.setDircet(2);
		//加入到Vector中
		ets.add(et);
			
		}
		
	}
	
	
	//重写paint
	public void paint(Graphics g)
	{
		
		super.paint(g);
		g.fillRect(0,0,400,300);
		
		//画出自己的坦克
		this.drawTank(hero.getX(), hero.getY(), g, this.hero.getDircet(),1);
		
		//画出子弹
		if(hero.s!=null&&hero.s.islive==true)
		{
			g.draw3DRect(hero.s.x, hero.s.y, 1, 1, false);
			
			
		}
		//画出敌人的坦克
		for(int i=0;i<ets.size();i++)
		{
			this.drawTank(ets.get(i).getX  (), ets.get(i).getY(), g, ets.get(i).getDircet(), 0);
			  
			
			
		} 
		
	}
	
	//画出坦克函数
	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;
		}
		//判断方向
		switch(direct)
		{
		//向上
		case 0: 
			//画出我的坦克(到时再封装成一个函数)
			//1、画出左边的矩形
			g.fillRect(x, y, 5, 30);
			//画出右边的矩形
			g.fillRect(x+15, y, 5, 30);
			//画出中间矩形
			g.fill3DRect(x+5, y+5, 10, 20, false);
			//画出中间的圆形
			g.fillOval(x+5, y+10, 10, 10);
			//画出炮筒
			g.drawLine(x+10, y+15, x+10, 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+5, 10, 10);
			//画出炮筒
			g.drawLine(x+15, y+10,x+30,y+10);
			break;
			
			//向下
		case 2:
			//画出我的坦克(到时再封装成一个函数)
			//1、画出左边的矩形
			g.fillRect(x, y, 5, 30);
			//画出右边的矩形
			g.fillRect(x+15, y, 5, 30);
			//画出中间矩形
			g.fill3DRect(x+5, y+5, 10, 20, false);
			//画出中间的圆形
			g.fillOval(x+5, y+10, 10, 10);
			//画出炮筒
			g.drawLine(x+10, y+15, x+10, 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+5, 10, 10);
			//画出炮筒
			g.drawLine(x+15, y+10,x,y+10);
			break;
			

		}
		 	
	}


	//键按下处理,a表示向左,s表示向下,d表示向右,w表示向上
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if(e.getKeyCode()==KeyEvent.VK_W)
		{
			//设置我的坦克的方向
			//向上
			this.hero.setDircet(0);
			this.hero.moveUp();
			
		
		}
		else if(e.getKeyCode()==KeyEvent.VK_D)
		{
			//向右
			this.hero.setDircet(1);
			this.hero.moveRight();
			}
			
		else if(e.getKeyCode()==KeyEvent.VK_S)
		{
			//向下
			this.hero.setDircet(2);
			this.hero.moveDown();
		
			
		}
		else if(e.getKeyCode()==KeyEvent.VK_A)
		{
			//向左
			this.hero.setDircet(3);
			this.hero.moveLeft();
			
		}
		
		//判断玩家是否发射子弹
		if(e.getKeyCode()==KeyEvent.VK_J)
		{
			this.hero.ShotEnemy();
			
		}
	
		//必须重新绘制Panel
			this.repaint();
		
	}


	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}


	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}


	public void run() {
		
		//每隔100毫秒重绘
	while(true)
	{
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	//重绘
			this.repaint();
	}	
	
	
		
		
	}
	
}

package com.haitao.tank3;

//定义子弹类
class Shot implements Runnable
{
	int x;
	int y;
	int direct;
	int speed=5;
	boolean islive=true;
	
	public Shot(int x,int y,int direct)
	{
		this.x=x; 
		this.y=y;	
		this.direct=direct;
		
		
		
	}
	public void run() {

		
		 while(true)
		 {
			 try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				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.out.println("子弹坐标x="+x+"y="+y);
			 //判断该子弹是否碰到边缘
			 if(x<0||x>400||y<0||y>300)
			 {
				 this.islive=false;
				 break;
				 
				 
			 }
			 
		 }
		
	}
}



//定义一个坦克类
class Tank
{
	//表示坦克的横坐标
	 int x=0;
	//表示坦克纵坐标
	 int y=0;
	 
	 //坦克方向,0表示上,1表示右,2表示下,3表示左
	 int dircet=0;
	 
	//设置坦克的速度
	 int speed=5;
	 //定义坦克的颜色
	int color;
	public Tank(int x,int y)
	{
		this.x=x;
		this.y=y;	
	}

	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 int getDircet() {
		return dircet;
	}

	public void setDircet(int dircet) {
		this.dircet = dircet;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

}

//敌人的坦克
class EnemyTank extends Tank
{

	public EnemyTank(int x, int y) {
		super(x, y);
		
	}
	


}


//定义我的坦克
class Hero extends Tank 
{

	//子弹
	Shot s=null;
	

	public Hero(int x, int y) {
		super(x, y);
		
	
	}
	
	//开火
	public void ShotEnemy()
	{
		
		switch(this.dircet)
		{
		case 0:
			s=new Shot(x+10,y,0);
			break;
		case 1:
			s=new Shot(x+30,y+10,1);
			break;
		case 2:
			s=new Shot(x+10,y+30,2);
			break;
		case 3:
			s=new Shot(x,y+10,3);
			break;
		
		
		}
		//启动子弹线程
		Thread t=new Thread(s);
		t.start();
		
	}
	
	//坦克向上移动
	public void moveUp()
	{
		 this.y-=speed;
	
	}
	//坦克向右移动4
	public void moveRight()
	{
	this.x+=speed;	
		
		
	}
	
	//坦克向下移动
	public void moveDown()
	{
		this.y+=speed;
		
		
		
	}
	//坦克向左移动
	public void moveLeft()
	{
		this.x-=speed;
		
		
	}
	
	}
	




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值