<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
首先是主方法裡定義的視窗(這些都是固定的格式,不會的也沒什麼事,直接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 SeaObject[] submarines = {}; //潛艇(偵察潛艇、魚雷潛艇、水雷潛艇) private Mine[] mines = {}; //水雷 private Bomb[] bombs = {}; //深水炸彈 //隨機生成潛艇 public SeaObject nextSubmarine(){ Random rand = new Random(); int type = rand.nextInt(30); if(type<10){ return new ObserveSubmarine(); }else if(type<15){ return new TorpedoSubmarine(); }else{ return new MineSubmarine(); } } private int subEnterIndex = 0; //潛艇入場 public void submarineEnterAction(){ //每10毫秒走一次 subEnterIndex++; if(subEnterIndex%40==0){ //每40毫秒 SeaObject 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<submarines.length;j++){ SeaObject 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 keyPressed(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) { case GAME_OVER: Images.gameover.paintIcon(null,g,0,0); break; case RUNNING: Images.sea.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.setFocusable(true); frame.add(world); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(WIDTH, HEIGHT+19); frame.setLocationRelativeTo(null); frame.setVisible(true); world.action(); } }
定義一個SeaObject的類當作超類(父類別),然後再寫其他的派生類(子類)
package cn.tedu.sunarine; import javax.swing.ImageIcon; import java.awt.Graphics; import java.util.Random; public abstract class SeaObject { 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 SeaObject(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 SeaObject(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(SeaObject 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 SeaObject 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 SeaObject 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.y; return new Mine(x,y); } public boolean isOutOfBounds() { return x>=World.WIDTH; } public int getLife(){ return 1; } }
偵察潛艇類
package cn.tedu.sunarine; import javax.swing.ImageIcon; //偵察潛艇 public class ObserveSubmarine extends SeaObject implements EnemyScore{ ObserveSubmarine(){ super(63,19); } @Override public void move() { x+=speed; } public ImageIcon getImage(){ return Images.observesubm; } public boolean isOutOfBounds() { return x>=World.WIDTH; } public int getScore(){ return 10; } }
魚雷類
package cn.tedu.sunarine; //魚雷 import javax.swing.ImageIcon; public class Mine extends SeaObject{ 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 SeaObject{ 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 SeaObject{ 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 observesubm; public static ImageIcon mine; public static ImageIcon bomb; public static ImageIcon sea; 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 = new ImageIcon("./img/gameover.png"); mine = new ImageIcon("./img/mine.png"); minesubm = new ImageIcon("./img/minesubm.png"); observesubm = new ImageIcon("./img/obsersubm.png"); sea = new ImageIcon("./img/sea.png"); torpedo = new ImageIcon("./img/torpesubm.png"); } public static void main(String[] args) { System.out.println(battleship.getImageLoadStatus()); System.out.println(observesubm.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(sea.getImageLoadStatus()); } }
以上就是Java練習之潛艇小遊戲的實現的詳細內容,更多關於Java潛艇遊戲的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45