import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Vector;
//坦克2.0版本增加了坦克移动
//坦克3.0版本包含开始界面、菜单\敌人的坦克
//坦克4.0版本增加发射子弹最大发射5颗
//子弹发射
当按下发射键就绘制一颗子弹添加到Vector向量中
import com.tank3.*;
public class Tank4 extends JFrame{
MyPanel panel;
JPanel panelr;
JMenuBar menubar;
JMenu menu,mode;
JMenuItem start,exit;
JMenuItem pay,nopay;//付费
JLabel labelSpeed;//设置坦克速度
public static void main(String[] args) {
Tank4 tank_4 = new Tank4();
}
Tank4(){
panel = new MyPanel();
panelr = new JPanel();
menubar = new JMenuBar();
menu = new JMenu("菜单(F)");
mode = new JMenu("模式(M)");
mode.setMnemonic('M');
start = new JMenuItem("开始");
exit = new JMenuItem("退出");
pay = new JMenuItem("会员模式(E)");
nopay = new JMenuItem("普通模式(R)");
//添加到窗体
menu.setMnemonic('F');//ALT+F快捷键
//设置开始快捷方式
start.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,ActionEvent.CTRL_MASK));
//退出
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,ActionEvent.CTRL_MASK));
//会员模式
pay.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,ActionEvent.CTRL_MASK));
//普通模式
nopay.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,ActionEvent.CTRL_MASK));
menu.add(start);
menu.add(exit);
mode.add(nopay);
mode.add(pay);
menubar.add(menu);
menubar.add(mode);
this.setJMenuBar(menubar);
//添加事件监听
this.addKeyListener(panel);
start.addActionListener(panel);
exit.addActionListener(panel);
pay.addActionListener(panel);
nopay.addActionListener(panel);
//设置按下后返回的数据
start.setActionCommand("开始");
exit.setActionCommand("退出");
pay.setActionCommand("会员");
nopay.setActionCommand("普通");
//启动刷新线程
Thread th = new Thread(panel);
th.start();
this.add(panel,BorderLayout.CENTER);
this.setTitle("坦克大战3.0");
this.setIconImage(Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/头像.png")));
this.setSize(670,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}
//创建面板
class MyPanel extends JPanel implements KeyListener,ActionListener,Runnable{
MyTank mytank=new MyTank(250,250);
int x=mytank.getX();
int y=mytank.getY();
int mode=0;
int setspeed=1;
int ensize=10;
int shotSize=5;//发子弹的最大数
//出于线程安全的考虑选用Vector
Vector<EnemyTank> enemyt=new Vector<EnemyTank>();
Vector<Shot> shot=new Vector<Shot>();
MyPanel(){
//初始化敌人坦克
for(int i=0;i<ensize;i++){
EnemyTank ek = new EnemyTank(i*50,0);
ek.setColor(Color.cyan);
ek.setDirection(1);
enemyt.add(ek);
}
}
public void paint(Graphics g){
super.paint(g);
//设置面板背景
//默认画笔是黑色所以不用设置
g.fillRect(0,0,500,500);
mytank.setColor(Color.RED);
if(this.mode == 0)
{ //开始界面
g.setColor(Color.GREEN);
g.setFont(new Font("华文行楷",Font.PLAIN,50));//Font.BOLD是设置字体是粗体 50是大小
g.drawString("Mango",210,250);
}
else if(this.mode == 1)
{ //画出我的坦克
this.DrawTank(mytank.getX(),mytank.getY(),mytank.getColor(),mytank.getDirection(),g);
//画出敌人的坦克
//因为敌人的坦克会死亡 所以要使用enemyt.size方法来确定绘制几个坦克
for(int i=0;i<enemyt.size();i++){
this.DrawTank(enemyt.get(i).getX(), enemyt.get(i).getY(), enemyt.get(i).getColor(), enemyt.get(i).getDirection(), g);
}
//画出子弹
for(int i=0;i<shot.size();i++){
if(shot.get(i) != null && shot.get(i).life == true){
//当shot!=null则说明有按键按下
//g.setColor(Color.RED);
g.draw3DRect(shot.get(i).x, shot.get(i).y, 2, 2, true);
}else {
shot.remove(i);
}
}
}
String speed_s=String.valueOf(mytank.getSpeed());
String shotSpeed = String.valueOf(Shot.speed);
g.setFont(new Font("宋体",Font.PLAIN,20));//Font.BOLD是设置字体是粗体 50是大小
g.drawString("坦克速度",510,25);
g.drawString("子弹速度",510,75);
g.setColor(Color.RED);
g.drawString(speed_s,550,50);//坦克速度值
g.drawString(shotSpeed,550,100);//子弹速度值
}
//画坦克
public void DrawTank(int x,int y,Color color,int direction,Graphics g){
g.setColor(color);
if(direction == 0 || direction == 1){
//1.画出左履带
g.fill3DRect(x,y,7,40,false);
//2.画出右履带
g.fill3DRect(x+25,y,7,40,false);
//3.画出中间
g.fill3DRect(x+7,y+8,18,24,false);
//4.画炮筒 上
if(direction == 0)g.fill3DRect(x+14,y-3,3,25,true);
else g.fill3DRect(x+14,y+20,3,25,true);
//5.画圆
g.fillOval(x+7, y+12, 15, 15);
}else if(direction == 2 || direction == 3){//左右
//1.画出左履带
g.fill3DRect(x,y,40,7,false);
//2.画出右履带
g.fill3DRect(x,y+25,40,7,false);
//3.画出中间
g.fill3DRect(x+8,y+7,24,18,false);
//4.画炮筒 左
if(direction == 2)g.fill3DRect(x-3,y+14,25,3,true);
else g.fill3DRect(x+20,y+14,25,3,true);//右
//5.画圆
g.fillOval(x+11, y+7, 15, 15);
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO 自动生成的方法存根
}
@Override//键被按下
public void keyPressed(KeyEvent e) {
if(this.mytank.getPay()){
//设置坦克速度 只有交钱才允许设置坦克速度
if(e.getKeyCode() == KeyEvent.VK_F1)//+速度
{
this.mytank.speedUp();
}
else if(e.getKeyCode() == KeyEvent.VK_F2)//-速度
{
this.mytank.speedDown();
}
//设置坦克子弹
if(e.getKeyCode() == KeyEvent.VK_F3)//+速度
{
this.mytank.shot.speedUp();
}else if(e.getKeyCode() == KeyEvent.VK_F4){//-速度
this.mytank.shot.speedDown();
}
}
//坦克动作
if(e.getKeyCode() == KeyEvent.VK_UP)
{
this.mytank.move_Up();
this.mytank.setDirection(0);
}else if(e.getKeyCode() == KeyEvent.VK_DOWN){
this.mytank.moveDown();
this.mytank.setDirection(1);
}else if(e.getKeyCode() == KeyEvent.VK_LEFT){
this.mytank.moveLeft();
this.mytank.setDirection(2);
}else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
this.mytank.moveRight();
this.mytank.setDirection(3);
}
//发射子弹
if(e.getKeyCode() == KeyEvent.VK_X){
//子弹数小于设置值就添加子弹
if(shot.size() < shotSize)
{
this.mytank.shotenemy();
shot.add(this.mytank.shot);
Thread th = new Thread(this.mytank.shot);
th.start();
}
}
this.repaint();//刷新显示
}
@Override
public void keyReleased(KeyEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(e.getActionCommand().equals("开始"))
{
this.mode=1;//开始
this.repaint();//重绘
}
else if(e.getActionCommand().equals("退出"))
System.exit(0);
else if(e.getActionCommand().equals("会员"))
this.mytank.setPay(true);
else if(e.getActionCommand().equals("普通"))
this.mytank.setPay(false);
}
@Override
// 定时刷新显示
public void run() {
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
this.repaint();
}
}
}
——————————————————————————————————————————————
package com.tank4;
import java.awt.Color;
class Tank{
//由于不是联网
//private使不使用都可以
int x;
int y;
private int direction=0;
//方向
int speed=3;//坦克速度
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
Color color;
//颜色
Tank(int x,int y){
this.x=x;
this.y=y;
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
}
//敌人坦克
class EnemyTank extends Tank{
EnemyTank(int x, int y) {
super(x, y);
}
}
//首先创建一个子弹类
class Shot implements Runnable{
int x;
int y;
//子弹也有自己的方向
int direction;
//方向
static int speed=1;
boolean life;
int size=5;
//我认为子弹也应该有自己的速度
public Shot(int x,int y,int direction,boolean lift){
this.x=x;
this.y=y;
this.direction = direction;
this.life = lift;
}
public void speedUp(){//子弹速度增加
if(this.speed<6)
this.speed+=1;
}
public void speedDown(){//子弹速度减少
if(this.speed>2)
this.speed-=1;
}
public void run(){
while(true){
switch(this.direction){
case 0 :y-=this.speed;
break;
case 1 :y+=this.speed;
break;
case 2 :x-=this.speed;
break;
case 3 :x+=this.speed;
break;
}
try {
//线程运行到这等待1000毫秒
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//判断什么时候线程死亡
//1.当子弹接触到边界 死亡
if(x < 2 || y < 2 || x > 498 || y > 498){
this.life = false;
//子弹生命死亡
break;
}
}
}
}
//我的坦克
//我认为我的坦克可以通过付费的方式增强
class MyTank extends Tank{
private boolean pay=false;
//付费模式
//当按下x键才创建对象
Shot shot = null;
//发射子弹
public void shotenemy(){
switch(this.getDirection()){
case 0://上
shot = new Shot(x+14,y-4,0,true);
break;
case 1://下
shot = new Shot(x+14,y+46,1,true);
break;
case 2://左
shot = new Shot(x-3,y+14,2,true);
break;
case 3://右
shot = new Shot(x+45,y+14,3,true);
break;
}
}
public boolean getPay() {
return pay;
}
public void setPay(boolean pay) {
this.pay = pay;
}
MyTank(int x,int y){
super(x,y);
}
public void move_Up(){
this.y=this.y-this.speed;
if(y<0)y=0;
}
public void moveDown(){
y+=speed;
if(y>472)y=472;
}
public void moveLeft(){
x=x-speed;
if(x<0)x=0;
}
public void moveRight(){
x+=speed;
if(x>468)x=468;
}
public void speedUp(){
if(speed<15)speed+=1;
}
public void speedDown(){
if(speed>2)speed-=1;
}
}