首页 > 作文

Java练习之潜艇小游戏的实现

更新时间:2023-04-06 03:59:47 阅读: 评论:0

首先是主方法里定义的窗口(这些都是固定的格式,不会的也没什么事,直接ctrl+c+v就行,基本上看一下数据都能看的懂)

写一个超类,超类里需要有潜艇,深水炸弹,水雷和战舰的宽,高,以及出场时的x,y坐标,和移动速度。所有对象的图片,所有对象的移动方法,以及碰撞

然后再写派生类,根据击败一个水雷潜艇,战舰会获得一条命,击败其他潜艇,战舰会获得分数,所以,需要定义两个接口,一个是用来加命,另一个用来加分

完整代码如下(图片什么的可以自己去找,只需要改一下image类和各个对象的宽高就可以)

游戏世界world类

package cn.tedu.sunarine;import javax.swing.jframe;import javax.swing.jpanel;import java.awt.graphics;import java.util.arrays;import java.util.random;import java.util.timer;import java.util.timertask;import java.awt.event.keyadapter;import java.awt.event.keyevent;//整个游戏世界public class world extends jpanel {    public static final int width = 641;    public static final int height = 479;      public static final int running =0;    public static final int game_over=1;    private int state = running;     //窗口所显示的对象    private battleship ship = new battleship(); //战舰    private aobject[] submarines = {}; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)    private mine[] mines = {}; //水雷    private bomb[] bombs = {}; //深水炸弹      //随机生成潜艇    public aobject nextsubmarine(){        random rand = new random();        int type = rand.nextint(30);        if(type<10){            return new obrvesubmarine();        }el if(type<15){            return new torpedosubmarine();        }el{            return new minesubmarine();        }    }     private int subenterindex = 0;    //潜艇入场    public void submarineenteraction(){ //每10毫秒走一次        subenterindex++;        if(subenterindex%40==0){ //每40毫秒            aobject obj = nextsubmarine();            submarines = arrays.copyof(submarines,submarines.length+1);            submarines[submarines.length-1] = obj;        }    }     private int mineenterindex = 0;    //鱼雷,水雷入场    public void mineenteraction(){        mineenterindex++;        if(mineenterindex%100==0){            for (int i=0;i<submarines.length;i++){                if (submarines[i] instanceof  minesubmarine){                    if (submarines[i].islive()) {                        minesubmarine ms = (minesubmarine) submarines[i];                        mine obj = ms.shootmine();                        mines = arrays.copyof(mines, mines.length + 1);                        mines[mines.length - 1] = obj;             快乐崇拜舞蹈教学分解动作       }                }            }        }    }     public void gameover(){        if (ship.getlife()<=0){            state = game_over;        }    }     //海洋对象移动    public void moveaction(){        for(int i=0;i<submarines.length;i++){            submarines[i].move();        }        for(int i=0;i<mines.length;i++){            mines[i].move();        }        for(int i=0;i<bombs.length;i++){            bombs[i].move();        }    }     //删除越界对象    public void outofboundsaction(){        for(int i=0;i<submarines.length;i++){            if(submarines[i].isoutofbounds()){                submarines[i] = submarines[submarines.length-1];                submarines = arrays.copyof(submarines,submarines.length-1);            }        }         for(int i=0;i<mines.length;i++){            if(mines[i].isoutofbounds()){                mines[i] = mines[mines.length-1];                mines = arrays.copyof(mines,mines.length-1);            }        }         for(int i=0;i<bombs.length;i++){            if(bombs[i].isoutofbounds()){                bombs[i] = bombs[bombs.length-1];                bombs = arrays.copyof(bombs,bombs.length-1);            }        }    }     private  int score = 0;     public void bombsbangaction(){//深水炸弹炸潜艇        for (int i=0;i<bombs.length;i++){            bomb b =bombs[i];            for (int j=0;j&l治疗失眠最好的药t;submarines.length;j++){                aobject s = submarines[j];                if (b.islive()&& s.islive()&&s.ishit(b)){                    b.godead();                    s.godead();                    if (s instanceof enemyscore){                        enemyscore es = (enemyscore) s;                        score += es.getscore();                    }                    if (s instanceof enemylife){                        enemylife ea = (enemylife) s;                        int num = ea.getlife();                        ship.addlife(num);                    }                 }            }        }    }     public void minebangaction(){        for (int i=0;i<mines.length;i++){            mine m= mines[i];            if (m.islive()&& ship.islive()&&m.ishit(ship)){                m.godead();                ship.subtratlife();            }        }    }      /** 启动程序的运行 */    public void action(){        keyadapter k = new keyadapter(){             public void keypresd(keyevent e) {                if(e.getkeycode() == keyevent.vk_space){                    bomb obj = ship.shoot(); //深水炸弹入场                    bombs = arrays.copyof(bombs,bombs.length+1);                    bombs[bombs.length-1] = obj;                }                if(e.getkeycode() == keyevent.vk_left){                    ship.moveleft();                }                if(e.getkeycode() == keyevent.vk_right){                    ship.moveright();                }            }        };        this.addkeylistener(k);         timer timer = new timer();        int interval = 10;        timer.schedule(new timertask() {            public void run() {                submarineenteraction(); //潜艇(侦察、水雷、鱼雷)入场                mineenteraction();      //水雷入场                moveaction();           //海洋对象移动                bombsbangaction();      //深水炸弹和潜艇碰撞                minebangaction();       //水雷和战舰碰撞                outofboundsaction();    //删除越界的对象                gameover();                repaint();            }        }, interval, interval);    }     public void paint (graphics g ){        switch (state) {            ca game_over:                images.gameover.painticon(null,g,0,0);                break;            ca running:            images.a.painticon(null, g, 0, 0);            ship.paintimage(g);            for (int i = 0; i < submarines.length; i++) {                submarines[i].paintimage(g);            }            for (int i = 0; i < mines.length; i++) {                mines[i].paintimage(g);            }            for (int i = 0; i < bombs.length; i++) {                bombs[i].paintimage(g);            }            g.drawstring("score" + score, 200, 50);            g.drawstring("life" + ship.getlife(), 400, 50);        }    }        public static void main(string[] args) {        jframe frame = new jframe();        world world = new world();        world.tfocusable(true);        frame.add(world);        frame.tresizable(fal);        frame.tdefaultclooperation(jframe.exit_on_clo);        frame.tsize(width, height+19);        frame.tlocationrelativeto(null);        frame.tvisible(true);        world.action();    }}

定义一个aobject的类当作超类(父类),然后再写其他的派生类(子类)

package cn.tedu.sunarine; import javax.swing.imageicon;import java.awt.graphics;import java.util.random; public abstract class aobject {     public static final int live = 0;    public static final int dead = 1;    protected int state=live;     protected int width;    protected int height;    protected int x;    protected int y;    protected int speed;     //三个潜艇    public aobject(int width, int height) {        this.width = width;        this.height = height;        x =-width;        random rand = new random();        y = rand.nextint(497 - height - 150 + 1) + 150;        speed = rand.nextint(3) + 1;    }//水雷,战舰,炸弹    public aobject(int width, int height, int x, int y, int speed) {        this.width = width;        this.height = height;        this.x = x;        this.y = y;        this.speed = speed;    }     public abstract void move();     public abstract imageicon getimage();     public boolean islive(){        return state ==live;    }    public void paintimage(graphics g){        if (islive()){            this.getimage().painticon(null,g,this.x,this.y);        }    }    public boolean isoutofbounds(){        return x>=world.width;    }    public boolean ishit(aobject other){        int x1 = this.x-other.width;        int x2 = this.x+this.width;        int y1 = this.y-other.height;        int y2 = this.y+this.height;        int x=other.x;        int y=other.y;        return x>=x1 && x<=x2 && y>=y1 && y<=y2;    }     public void godead(){        state =dead;    }}

在派生类的引用超类

鱼雷潜艇类

package cn.tedu.sunarine;import javax.swing.imageicon; //鱼雷潜艇public class torpedosubmarine extends aobject implements enemyscore{    torpedosubmarine(){        super(64,20);    }     @override    public void move() {        x+=speed;    }    public imageicon getimage(){        return images.torpedo;    }    public boolean isoutofbounds() {        return x>=world.width;    }    public int getscore(){        return 20;    }}

水雷潜艇类

package cn.tedu.sunarine;import javax.swing.imageicon; //水雷潜艇public class minesubmarine extends aobject implements enemyscore{    minesubmarine(){       super(63,19);    }     @override    public void move() {        x+=speed;    }    public imageicon getimage(){        return images.minesubm;    }    public mine shootmine(){        int x = this.x+(this.width/2);        int y =this.神奇宝贝剧场版2013y;        return new mine(x,y);    }    public boolean isoutofbounds() {        return x>=world.width;    }    public ikt sktnt getlife(){        return 1;    }}

侦察潜艇类

package cn.tedu.sunarine;import javax.swing.imageicon; //侦察潜艇public class obrvesubmarine extends aobject implements enemyscore{    obrvesubmarine(){        super(63,19);    }     @override    public void move() {        x+=speed;    }    public imageicon getimage(){        return images.obrvesubm;    }    public boolean isoutofbounds() {        return x>=world.width;    }    public int getscore(){        return 10;    }}

鱼雷类

package cn.tedu.sunarine;//鱼雷import javax.swing.imageicon;public class mine extends aobject{        mine(int x,int y){            super(11,11,x,y,1);        }     @override    public void move() {        y-=speed;    }     public imageicon getimage(){            return images.mine;    }    public boolean isoutofbounds(){            return y<=150-(height/2);    }}

深水炸弹类

package cn.tedu.sunarine;//深水炸弹import javax.swing.imageicon;public class bomb extends aobject{    bomb(int x,int y){        super(9,12,x,y,3);    }     @override    public void move() {        y+=speed;    }    public imageicon getimage(){        return images.bomb;    }    public boolean isoutofbounds(){        return y>=world.height;    }}

战舰类

package cn.tedu.sunarine; import javax.swing.*; //战舰public class battleship extends aobject{    int life;    battleship(){       super(66,26,270,124,20);       life=1;    }     @override    public void move() {        system.out.println("战舰移动");    }    public imageicon getimage(){        return images.battleship;    }    public bomb shoot(){        return new bomb(this.x,this.y+height);    }    //限制移动范围    public void moveleft(){        x-=speed;        x=math.max(0,x);    }    public void moveright(){        x+=speed;        x=math.min(x,world.width-this.width);    }    public void addlife(int num){        life+=num;    }    public int getlife(){        return life;    }    public void subtratlife(){        life--;    }}

加命接口

package cn.tedu.sunarine; public interface enemylife {    public int getlife();}

加分接口

package cn.tedu.sunarine; public interface enemyscore {    public int getscore();}

最后,image类(可根据自己的图片改)

package cn.tedu.sunarine;import javax.swing.*;public class images {    public static imageicon battleship;    public static imageicon obrvesubm;    public static imageicon mine;    public static imageicon bomb;    public static imageicon a;    public static imageicon torpedo;    public static imageicon minesubm;    public static imageicon gameover;    static {        battleship = new imageicon("./img/battleship.png");        bomb = new imageicon("./img/bomb.png");        gameover = 610402new imageicon("./img/gameover.png");        mine = new imageicon("./img/mine.png");        minesubm = new imageicon("./img/minesubm.png");        obrvesubm = new imageicon("./img/obrsubm.png");        a = new imageicon("./img/a.png");        torpedo = new imageicon("./img/torpesubm.png");    }     public static void main(string[] args) {        system.out.println(battleship.getimageloadstatus());        system.out.println(obrvesubm.getimageloadstatus());        system.out.println(mine.getimageloadstatus());        system.out.println(battleship.getimageloadstatus());        system.out.println(bomb.getimageloadstatus());        system.out.println(gameover.getimageloadstatus());        system.out.println(minesubm.getimageloadstatus());        system.out.println(a.getimageloadstatus());    }}

以上就是java练习之潜艇小游戏的实现的详细内容,更多关于java潜艇游戏的资料请关注www.887551.com其它相关文章!

本文发布于:2023-04-06 03:59:44,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/a6c4b439333081f5d78c15b6914b856e.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Java练习之潜艇小游戏的实现.doc

本文 PDF 下载地址:Java练习之潜艇小游戏的实现.pdf

标签:潜艇   水雷   深水炸弹   战舰
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图