實作思路
1.如何分析?
第一步:發現類(物件)
人物-小丑(敵方-友方)
子彈-帽子
墻體
爆炸物
第二步:發現屬性
小丑: 寬高 , 位置(x y), 移動速度
帽子: 寬高 , 位置(x y), 移動速度
墻體: 寬高 , 位置(x y)
爆炸物:寬高 , 位置(x y)
第三步:發現方法
小丑: 移動 , 攻擊 , 人物撞邊界 ,
子彈: 移動 , 子彈撞墻 , 子彈撞邊界 ,
墻體: 靜止不動
爆炸物: 爆炸物消失
2.難點在哪里?
1 如何將圖片加載到表單里
- 背景圖片加載
- 人物-小丑加載
- 發射物-帽子加載 Missile
- 墻體-加載 Wall
- 爆炸物-加載 Explode
2 如何創建表單
3 如何發射子彈(如何使用鍵盤觸發事件)
3.二期版本
- 接入網路,多人作戰
第一天的實作
1.創建一個專案(ylm)
2.匯入需要使用到的圖片檔案到Java
3.在專案的src中創建包(com.ytzl.ylm1)
4.在com.ytzl.ylm1包中創建子包——工具包(util)并寫呼叫圖片方法
public static Image getImage(String imgPath) {
ImageIcon imageIcon = new ImageIcon(imgPath);
return imageIcon.getImage();
}
5.在com.ytzl.ylm1包中創建游戲客戶端類(GameClient)并繼承Frame類
6.實作游戲視窗
//定義背景圖片的路徑
public static final String BG_PATH = "images/bg.png";
// 定義一個圖片的靜態變數
private static Image image;
// 靜態塊,所有資源(圖片,音頻,視頻)只需要加載一次
static {
image = CommonUtils.getImage(BG_PATH);
}
/**
* 游戲入口
*/
public static void main(String[] args) {
GameClient gameClient = new GameClient();
// 開始游戲
gameClient.start();
}
/**
* 開始游戲
*/
public void start() {
System.out.println("游戲馬上開始,請玩家做好準備");
//TODO 游戲業務
//設定標題
this.setTitle("原諒帽帶戰");
//設定表單的大小以及位置
this.setBounds(0, 0, 700, 500);
// 讓表單顯示出來
this.setVisible(true);
System.out.println("Game Over!!!");
}
7.插入背景圖片
/**
* 畫畫,重寫父類的paint方法
* Graphics 畫筆類
*
* @param g 畫筆
*/
@Override
public void paint(Graphics g) {
// 畫背景圖
g.drawImage(image, 0, 0, 700, 500, this);
}
8.插入人物圖片(創建人物類 Buffoon)
// 人物圖片屬性
public static final Image buffoonImage= CommonUtils.getImage("images/body/s-left.png");
// 人物-橫坐標
private int x;
// 人物-縱坐標
private int y;
// 圖片-寬度
private int width;
// 圖片-高度
private int height;
// 人物-速度
private int speed;
8.1插入人物圖片(客戶端實作)
// 創建人物-小丑
private Buffoon buffoon = new Buffoon();
// 畫人物圖-小丑
g.drawImage(Buffoon.buffoonImage, 160, 100, 50, 50, this);
9.插入帽子圖片(創建帽子類)
//發射物圖片路徑屬性
public static final Image missileImage = CommonUtils.getImage("images/missile.png");
// 帽子-高度
private int height;
// 帽子-長度
private int width;
// 帽子-速度
private int speed;
// 帽子-橫坐標
private int x;
// 帽子-縱坐標
private int y;
9.1插入帽子圖片(客戶端實作)
// 創建發射物-帽子
private Missile missile = new Missile();
// 畫發射物圖-帽子
g.drawImage(Missile.missileImage,200,200,20,20,this);
10.插入障礙物圖片(創建障礙物類Wall)
//橫墻體圖片屬性
public static final Image wallImageH = CommonUtils.getImage("images/wall-h.png");
//縱墻體圖片屬性
public static final Image wallImageZ = CommonUtils.getImage("images/wall-v.png");
//墻體-高度
private int height;
//墻體-長度
private int width;
//墻體-橫坐標
private int x;
//墻體-縱坐標
private int y;
10.1插入障礙物圖片(在客戶端實作)
// 創建墻體
private Wall wall=new Wall();
// 畫墻
g.drawImage(Wall.wallImageH,220,120,70,30,this);
g.drawImage(Wall.wallImageZ,430,230,30,70,this);
11.插入爆炸物圖片(創建爆炸物類Explode)
//發射物圖片路徑屬性
public static final Image explodeImage = CommonUtils.getImage("images/explode.png");
//墻體-高度
private int height;
//墻體-長度
private int width;
//墻體-橫坐標
private int x;
//墻體-縱坐標
private int y;
11.1插入爆炸物圖片(在客戶端實作)
// 創建爆炸物
private Exception exception=new Exception();
// 畫爆炸物
g.drawImage(Explode.explodeImage,500,300,70,70,this);
第一天(完成)效果圖如下:

第二天(人物以及帽子移動方向)
public void move(String dir){
if("U".equals(dir)){
this.y-=this.speed;
}
if("R".equals(dir)){
this.x+=this.speed;
}
if("D".equals(dir)){
this.y+=this.speed;
}
if ("L".equals(dir)){
this.x-=this.speed;
}
if ("UR".equals(dir)){
this.x+=this.speed;
this.y-=this.speed;
}
if ("DR".equals(dir)){
this.x+=this.speed;
this.y+=this.speed;
}
if ("LD".equals(dir)){
this.x-=this.speed;
this.y+=this.speed;
}
if ("LU".equals(dir)){
this.x-=this.speed;
this.y-=this.speed;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/211299.html
標籤:其他
