欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java编写实现坦克大战小游戏

程序员文章站 2022-03-07 15:08:36
本文实例为大家分享了java实现坦克大战小游戏的具体代码,供大家参考,具体内容如下创作背景:n年前的学期末课题设计,从b站上学的,一个代码一个代码敲出来的。小游戏介绍:红色坦克是我们的操纵坦克,黑色是...

本文实例为大家分享了java实现坦克大战小游戏的具体代码,供大家参考,具体内容如下

创作背景:n年前的学期末课题设计,从b站上学的,一个代码一个代码敲出来的。

小游戏介绍:

红色坦克是我们的操纵坦克,黑色是敌人坦克。
上下左右键控制坦克移动方向
按ctrl键发射炮弹
红色坦克可以穿墙,黑色不可以

具体页面如下:

Java编写实现坦克大战小游戏

奉上全部源代码:

tank.java

import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class tank {
    private int x;
    private int y;
    private  int oldx;
    private  int oldy;
    private int life = 100;
private boolean bl = false ,bu = false ,br = false ,bd= false;
//产生随机数
private static random r = new random();
//九种坦克运动方向
enum direction{l,lu,u,ru,r,rd,d,ld,stop};
//初始化坦克方向
private direction  dir = direction.stop;
//初始化炮筒方向
private direction ptdir = direction.u;
//坦克移动速度
private static final int xspeed = 5;
private static final int  yspeed = 5;
//坦克大小
private static final int width = 30;
private static final int hight =30;
//定义tankclient类
tankclient tc;
public int getlife(){
    return  life;
    }
public void setlife(int life){
    this.life =life;
}
private boolean good =true ;//定义坦克类型,敌方还是我方
public boolean isgood()
{
    return good;
}
//定义坦克状态
private boolean live = true;
//设置enemy坦克随机移动步数
private static int step = r.nextint(12)+3;
//构造坦克状态方法
public boolean islive ()
{
    return live;
}
public void setlive(boolean live)
{
    this.live = live;
}
//构造方法
public tank(int x, int y) {
        this.x = x;
        this.y = y;
    }
public tank (int x,int y,boolean good ,direction dir,tankclient tc)
{
    this (x,y);
    this.good = good;
    this.dir = dir ;
    this.tc = tc;
}
public void draw (graphics g)
    {
        if (!live) 
        {
            if (!good)
            {
                tc.tanks.remove(this);
            }
            return;
        }
        color c = g.getcolor();//?
        if(good==true)
            {g.setcolor(color.red);}//定义我方坦克颜色
        else g.setcolor(color.black);//定义敌方坦克颜色
        g.filloval(x,y, width, hight);//定义坦克位置及大小
        g.setcolor(c);//?
        move();
        switch (ptdir)//画炮筒
        {
        case l:
            g.drawline(x + tank.width / 2, y + tank.hight / 2, x, y
                    + tank.hight / 2);
            break;
        case lu:
            g.drawline(x + tank.width / 2, y + tank.hight / 2, x, y);
            break;
        case u:
            g.drawline(x + tank.width / 2, y + tank.hight / 2, x + tank.width
                    / 2, y);
            break;
        case ru:
            g.drawline(x + tank.width / 2, y + tank.hight / 2, x + tank.width,
                    y);
            break;
        case r:
            g.drawline(x + tank.width / 2, y + tank.hight / 2, x + tank.width,
                    y + tank.hight / 2);
            break;
        case rd:
            g.drawline(x + tank.width / 2, y + tank.hight / 2, x + tank.width,
                    y + tank.hight);
            break;
        case d:
            g.drawline(x + tank.width / 2, y + tank.hight / 2, x + tank.width
                    / 2, y + tank.hight);
            break;
        case ld:
            g.drawline(x + tank.width / 2, y + tank.hight / 2, x, y
                    + tank.hight);
            break;
        }
    }

private void stay()
{
    this.x=oldx;
    this.y=oldy;
    }
//坦克移动
void move ()
{
    this.oldx=x;
    this.oldy=y;
    switch(dir)
    {
    case l:
    x-=xspeed;
    break;
    case lu:
    x-=xspeed;
    y-=yspeed;
    break;
    case u:
    y-=yspeed;
    break;
    case ru:
    x+=xspeed;
    y-=yspeed;
    break;
    case r:
    x+=xspeed;
    break;
    case rd:
    x+=xspeed;
    y+=yspeed;
    break;
    case d:
    y+=yspeed;
    break;
    case ld:
    x-=xspeed;
    y+=yspeed;
    break;
    case stop:
    break;
    }
    if (this.dir!=direction.stop)
    {
        ptdir = dir;
    }
    if (x<0) x=0;
    if (y<20) y=20;
    if (x>tankclient.winwidth-tank.width) x=tankclient.winwidth-tank.width;
    if (y>tankclient.winhigh-tank.hight) y=tankclient.winhigh-tank.hight;
    //让enemy坦克*移动
    if (!good)
    {
        direction[] dirs= direction.values();//将枚举转化为数组
        if (step ==0)
        {
            step = r.nextint(12)+3;
            int rn = r.nextint(dirs.length);//产生随机数
            dir = dirs[rn];
        }
        step--;
    if (r.nextint(40)>38)
            {this.fire();}
    }
}
//坦克方向
void localdirection()
{
    if (bl&&!bu&&!br&&!bd) dir= direction.l;
    else if (bl&&bu&&!br&&!bd) dir= direction.lu;
    else if (!bl&&bu&&!br&&!bd) dir= direction.u;
    else if (!bl&&bu&&br&&!bd) dir= direction.ru;
    else if (!bl&&!bu&&br&&!bd) dir= direction.r;
    else if (!bl&&!bu&&br&&bd) dir= direction.rd;
    else if (!bl&&!bu&&!br&&bd) dir= direction.d;
    else if (bl&&!bu&&!br&&bd) dir= direction.ld;
    else dir =direction.stop;
}
public void keypressed(keyevent e) 
    {
        int key = e.getkeycode();
            switch (key)
        {
            case keyevent.vk_control:
                fire();
                break;
            case keyevent.vk_left:
                bl=true;
                break;
            case keyevent.vk_up:
                bu=true;
                break;
            case keyevent.vk_right:
                br=true;
                break;
            case keyevent.vk_down:
                bd=true;
                break;
        }
        localdirection();//获取执行方向

    }
public void keyreleased(keyevent e) {
    int key = e.getkeycode();
    switch (key)
{
    case keyevent.vk_left:
        bl=false;
        break;
    case keyevent.vk_up:
        bu=false;
        break;
    case keyevent.vk_right:
        br=false;
        break;
    case keyevent.vk_down:
        bd=false;
        break;
}
    localdirection();
    
}
//定义坦克发射子弹类
    public missile fire()
    {
        if (!live) return null;
        int x= this.x+tank.width/2-missile.width/2;
        int y= this.y +tank.hight/2-missile.hight/2;
        missile m = new missile(x,y,ptdir,good,tc);
        tc.missiles.add(m);
        return m;
    }
//《碰撞检测》获取坦克矩形属性
    public rectangle getrect()
    {
        return new rectangle(x,y,width,hight);
    }
    //《碰撞检测》
    public boolean tankhitwall(wall w)
    {
        if(this.getrect().intersects(w.getrect()))
        {
            stay();
            return true;
        }
        return false;
    }
    public boolean tankhittank(java.util.list<tank> tanks)
    {
        for (int i=0;i<tanks.size();i++)
        {
            tank t = tanks.get(i);
        
            if (this!=t)
            {
                if (this.live&&t.islive()&&this.getrect().intersects(t.getrect()))
                {
                    this.stay();
                    t.stay();
                    return true ;
                }
            }
        }
        return false;
    }
}

wall.java

import java.awt.*;
public class wall {
     int x;
     int y;
     int width;
     int height;
     tankclient tc;

    public wall (int x,int y,int width,int height, tankclient tc)
    {
        this.x= x;
        this.y= y;
        this.width = width;
        this.height = height;
        this.tc = tc;
    }
    public void draw(graphics g)
    {
        color c = g.getcolor();
        g.setcolor(color.gray);
        g.fillrect(x, y, width, height);
        g.setcolor(c);
        
    }
    //《碰撞检测》获取矩形属性
        public rectangle getrect()
        {
            return new rectangle(x,y,width,height);
        }
    
}

tankclient.java

import java.awt.*;
import java.awt.event.*;
import java.util.list;
import java.util.arraylist;
public class tankclient extends frame {
    tank mytank = new tank(400,430,true,tank.direction.stop,this);//创建自己的坦克
    list <tank>  tanks= new arraylist<tank>(); //创建敌人坦克容器
    list<explode> explodes = new arraylist<explode>();//创建爆炸容器
    list<missile> missiles = new arraylist<missile>();//定义容器,存放炮弹
    wall wall1 = new wall(100,100,30,400,this);
    wall wall2 = new wall(350,400,400,30,this);
    /*
     * 定义窗口大小变量
     */
    public static final int winwidth=800;
    public static final int winhigh=600;
    //定义框架大小
    public void launchframe()
    {
        //添加10辆敌人坦克
        for (int i = 0;i<10;i++)
        {
            tanks.add(new tank(50+40*i,100,false,tank.direction.d,this));
        }
        this.setlocation(40,40);//定义窗口位置
        this.setsize(winwidth,winhigh);//设置窗口大小
        this.settitle("坦克大战");//设置窗口标题
        //设置监听器,使窗口关闭
        this.addwindowlistener(new windowadapter()
                {
                    public void windowclosing(windowevent e)
                    {
                        system.exit(0);
                    }
                });
        this.setbackground(color.white);//设置窗口背景颜色
        this.setvisible(true);//设置窗口可见
        this.setresizable(false);//设置为不可调整窗口大小
        this.addkeylistener(new keymonitor());
        new thread(new paintthread()).start();
    }
    //定义一个新图片为空,双缓冲
    image offscreenimage = null;
    /*
     * 定义画板
     */
    public void paint(graphics g)
    {    //g.drawstring("当前炮弹数"+missiles.size(), 40, 80);
        //g.drawstring("当前爆炸数"+explodes.size(), 40, 100);
        g.drawstring("tank数量"+tanks.size(),40,40);
        g.drawstring("mytank血量"+mytank.getlife(),40,60);
        for (int i=0;i<missiles.size();i++)
        {
            missile m = missiles.get(i);     
            m.hittanks(tanks);
            m.hittank(mytank);
            m.hitwall(wall1);
            m.hitwall(wall2);
            m.draw(g);
        }
        for (int i=0;i<explodes.size();i++)
        {
            explode e = explodes.get(i);
            e.draw(g);
        }
        for (int i = 0;i<tanks.size();i++)
        {
            tank t = tanks.get(i);
            t.tankhitwall(wall1);
            t.tankhitwall(wall2);
            t.tankhittank(tanks);
            t.draw(g);
        }
        mytank.draw(g);
        wall1.draw(g);
        wall2.draw(g);
    }
    //重写update 刷新屏幕先调用update方法再调用画笔工具,故刷新屏幕让其直接调用update方法
    public void update (graphics g)
    {
        if (offscreenimage == null )
        {
            offscreenimage = this.createimage(winwidth,winhigh);
        }
        graphics goffscreen = offscreenimage.getgraphics();
        color c = goffscreen.getcolor();
        goffscreen.setcolor(color.white);
        goffscreen.fillrect(0,0,winwidth,winhigh);
        goffscreen.setcolor(c);
        paint(goffscreen);
        g.drawimage(offscreenimage,0,0,null);
    }
    public static void main(string[] args)
    {
        tankclient tankclient = new tankclient();
        tankclient.launchframe();

    }
    //线程类--刷新屏幕
    private class paintthread implements runnable
    {
        public void run()
        {
            while(true)
            {
                repaint();  //刷新屏幕
                try
                {
                    thread.sleep(40);
                }catch (interruptedexception e)
                {
                    e.printstacktrace();
                }
            }
        }
    }
    /**
     * 
     * 内部类 添加键盘监听
     *
     */
        private class keymonitor extends keyadapter {
            //按键时
            public void keypressed(keyevent e) {
                mytank.keypressed(e);
            }
            //松键时
            public void keyreleased(keyevent e) 
            {
                mytank.keyreleased(e);
            }
            
        }
}

explode.java

import java.awt.*;
public class explode {
    int x;
    int y;
    private boolean live = true;
    private tankclient tc ;
    int[] diameter = {30,40,50,40,30,10,5};
    int step = 0;
    //结构
    public explode (int x ,int y,tankclient tc) {
        this.x =x;
        this.y = y;
        this.tc = tc;
    }
    public void draw (graphics g) {
        if(!live){
        tc.explodes.remove(this);    
        return ;
        }
        if (step ==diameter.length) {
            live = false ;
            step = 0;
            return ;
        }
        color c = g.getcolor();
        g.setcolor(color.red);
        g.filloval(x, y, diameter[step], diameter[step]);
        g.setcolor(c);
        step++;
    }
}

missile.java

import java.awt.*;
import java.util.list;
//定义子弹类
public class missile {
    //定义子弹速度
    public static final int xspeed=10;
    public static final int yspeed=10;
    //定义子弹大小
    public static final int width =10;
    public static final int hight =10;
    private boolean live= true ;//定义炮弹状态
    //定义发出的炮弹归属
    private boolean good;
    public boolean islive()
    {
        return live;
    }
    int x,y ;//子弹坐标
    tankclient tc;
    tank.direction dir;
    public missile (int x,int y ,tank.direction dir)
    {
        this.x=x;
        this.y=y;
        this.dir=dir; 
    }
    public missile(int x,int y,tank.direction dir,boolean good,tankclient tc)
    {
        this(x,y,dir);
        this.good = good;
        this.tc=tc;
    }
    public void draw (graphics g)
    {    if (!live) {
        tc.missiles.remove(this);
    }
        color c= g.getcolor();
        if (good)
        {
            g.setcolor(color.red);
        }else {
            g.setcolor(color.black);
        }
        g.filloval(x, y, width,hight);//设置炮弹大小
        g.setcolor(c);
        move();
    }
    void move ()

    {
        switch(dir)
        {
        case l:
        x-=xspeed;
        break;
        case lu:
        x-=xspeed;
        y-=yspeed;
        break;
        case u:
        y-=yspeed;
        break;
        case ru:
        x+=xspeed;
        y-=yspeed;
        break;
        case r:
        x+=xspeed;
        break;
        case rd:
        x+=xspeed;
        y+=yspeed;
        break;
        case d:
        y+=yspeed;
        break;
        case ld:
        x-=xspeed;
        y+=yspeed;
        break;
        }
        if (x<0||y<0||x>tankclient.winwidth||y>tankclient.winhigh)
        {
            live =false;
        }
    }
//《碰撞检测》获取子弹矩形属性
    public rectangle getrect()
    {
        return new rectangle(x,y,width,hight);
    }
//《碰撞检测》
    public boolean hittank(tank t)
    {
        if(this.live&&this.getrect().intersects(t.getrect())&&t.islive()&& this.good!=t.isgood())
        {    if (t.isgood())
            {
                t.setlife(t.getlife()-20);
                if (t.getlife()<=0)
                {t.setlive(false);}
            }else{t.setlive(false);}
            this.live = false;
            explode e = new explode(x-10,y-10,tc);
            tc.explodes.add(e);
            return true;
        }
        return false;
    }

    public boolean hittanks(list<tank> tanks)
    {
        for (int i=0;i<tanks.size();i++)
        {
            if (hittank(tanks.get(i)))
            {
                return true;
            }
        }
        return false ;
    }
    //撞墙碰撞检测
        public boolean hitwall (wall w)
        {
            if (this.live&&this.getrect().intersects(w.getrect()))
            {
                this.live =false;
                return true;
            }
            return false ;
        }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: Java 坦克大战