频道栏目
首页 > 资讯 > Java实例 > 正文

【Java_项目篇(1)】--JAVA实现坦克大战游戏--坦克发射子弹(三)

16-08-22        来源:[db:作者]  
收藏   我要投稿

一、任务需求

添加hero坦克子弹并且发射。

二、思路

1.创建子弹类

1.由于每颗子弹都是一个独立的线程,会不断变换子弹坐标,所以子弹类要实现Runnable接口。
2.子弹需要坐标x,y以及方向,所以构造函数有三个参数。
3.实现Runnable接口后,要覆盖run方法:
    1.如果不让线程sleep,子弹飞出去很快,快到在屏幕如上瞬间消失。
    2.所以增加一个sleep()
    3.另外,子弹需要判断四个方向,增加switch语句
    4.子弹跑出屏幕需要死亡,增加变量islive判断是否存活。否则无限制飞行,不断占用内存空间。

2.hero坦克类中加入shotEnemy()方法,表示发射子弹

1.新建子弹变量s
    Shot s = null;
2.判断开火的方向,增加switch语句。
3.开火后,启动线程。
Thread t =new Thread(s);
    t.start();

3.完成子弹实现过程,接着要在屏幕中让子弹显示,即画出子弹

1.paintComponent方法里加入,画出子弹
    判断子弹是否存活
    hero.s!=null很重要,游戏刚开始没有发射子弹,hero.s=null,此时进入if去画子弹会出现异常。
    if(hero.s!=null&&hero.s.isLive==true){
        g.setColor(Color.red);
        g.draw3DRect(hero.s.x, hero.s.y, 1, 1, false);
    }
2.在KeyPressed监听器处添加,按下J键发射一颗子弹
    if(e.getKeyCode()==KeyEvent.VK_J){
        hero.shotEnemy();
    }

4.MyPanel实现Runnable接口

1.由于子弹打出去后,需要不断让它显示,屏幕每隔一段时间需要repaint()
    所以重载run()方法,让MyPanel每隔一段时间repaint()一次
    public void run() {
    while(true){
        try {
            Thread.sleep(100);//休息100ms,重画一次MyPanel
        } catch (Exception e) {
            e.printStackTrace();
        }
        repaint();
    }

5.由于MyPanel实现了Runnable接口,所以让该线程跑起来

1.在MyTankGame方法中启动线程即可
    p1 = new MyPanel();
        Thread t = new Thread(p1);
        t.start();

三、代码如下

MyTankGame.java

这里写图片描述

/**
 * 功能:坦克游戏的2.0
 * 1.画出坦克
 * 2.坦克的移动
 * 3.坦克发射子弹
 */

package Tank_03;

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;

@SuppressWarnings("serial")
public class MyTankGame2 extends JFrame{

    MyPanel p1 = null;

    public MyTankGame2(){
        p1 = new MyPanel();
        Thread t = new Thread(p1);
        t.start();
        add(p1);
        setSize(400, 300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        addKeyListener(p1);//注册监听

    }

    public static void main(String[] args) {
         new MyTankGame2();
    }
}

//画图
@SuppressWarnings("serial")
class MyPanel extends JPanel implements KeyListener,Runnable{

    Hero hero = null;
    Vector ets = new Vector();
    int enSize = 3;

    public MyPanel(){
        hero = new Hero(100,100);//设置坦克出现的位置(10,10)
        // 初始化敌人的坦克
        for(int i=0;i坦克的颜色(类型:敌方坦克,我方坦克),方向,出现的坐标
     * 
     * 如果type是default 则默认颜色为画出黑色坦克
     * 
     * 封装性:将坦克封装到方法中。
     * 
     */

    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);
        default:
            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:
            //向下w
            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 2:
            //向左
            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+9 , y+4, 10  , 10 );
            g.drawLine(x+5, y+9, x-4, y+9);
            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+9 , y+4, 10  , 10 );
            g.drawLine(x+15, y+9, x+30, y+9);
            break;
        default:
            break;
        }
        //repaint();  因为监听器里面有repaint()  所以不用在加repaint()了
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {//实现接口 根据按键上下左右移动 可以控制速度和移动
        if(e.getKeyCode()==KeyEvent.VK_W){
            hero.setDirect(0);
            hero.moveUp();
        }else if(e.getKeyCode()==KeyEvent.VK_S){
            hero.setDirect(1);
            hero.moveDown();
        }else if(e.getKeyCode()==KeyEvent.VK_A){
            hero.setDirect(2);
            hero.moveLeft();
        }else if(e.getKeyCode()==KeyEvent.VK_D){
            hero.setDirect(3);
            hero.moveRight();
        }
        if(e.getKeyCode()==KeyEvent.VK_J){
            //开火
            hero.shotEnemy();
        }
        //判断玩家是否按下J攻击键
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
    //@Override
    public void run() {
        //每隔100毫秒 重新画图
        while(true){
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
            repaint();
        }
    }
}

members.java

package Tank_03;



//坦克父类  可以设置坦克出现位置(x,y)
class Tank {
    int x = 0;
    int y = 0;
    int speed = 10;
    int Direct = 0;
    int color;
    public int getColor() {
        return color;
    }
    public void setColor(int color) {
        this.color = color;
    }
    public int getDirect() {
        return Direct;
    }
    public void setDirect(int direct) {
        Direct = direct;
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    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;
    }


    //视频中是把移动放在hero类中

}
//敌方坦克
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 (Direct) {
        case 0:
            s = new Shot(x+8,y-4,0);
            break;
        case 1:
            s = new Shot(x+9,y+32,1);
            break;
        case 2: 
            s = new Shot(x-8,y+8,2);
            break;
        case 3:
            s = new Shot(x+32,y+9,3);
            break;
        default:
            break;
        }
        Thread t =new Thread(s);
        t.start();
    }
    public void moveUp(){
        y-=speed;
    }
    public void moveDown(){
        y+=speed;
    }
    public void moveLeft(){
        x-=speed;
    }
    public void moveRight(){
        x+=speed;
    }
}
//子弹类
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;
    }
    @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:
                y+=speed;
                break;
            case 2:
                x-=speed;
                break;
            case 3:
                x+=speed;
                break;
            default:
                break;
            }

//          System.out.println(""+x+" "+y);
            //子弹何时死亡
            if(x<0||x>400||y<0||y>300){
                isLive = false;
                break;
            }
        }
    }
}

四、效果图

这里写图片描述

相关TAG标签
上一篇:node.js express安装及示例网站搭建
下一篇:cron表达式
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站