首頁> Java> java教程> 主體

如何使用Java編寫潛水艇遊戲?

PHPz
發布: 2023-04-22 09:46:17
轉載
699 人瀏覽過

首先是主方法裡定義的視窗(這些都是固定的格式,不會的也沒什麼事,直接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
        
登入後複製

定義一個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編寫潛水艇遊戲?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!