實作思路
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.ylm)
4.在游戲包(com.ytzl.ylm)下創建客戶端類GameClient并繼承Farme類
public class GameClient extends Frame {}
關于Farme類:
1.Farme類是Java自帶的一個系統類
2.Farme類的作用是可以制作出帶有標題和邊框的頂層視窗
5.實作游戲視窗的顯示
public void start(){
//啟動游戲時在控制臺輸出顯示"游戲開始"
System.out.println("游戲開始");
//視窗的標題設定
this.setTitle("原諒帽帶戰");
/*
*表單的大小以及位置設定——呼叫Frame類中的setBounds方法
* setBounds方法具體內容:setBounds(int橫坐標,int縱坐標,int長,int寬);
* 引數中橫縱坐標為0,表示視窗從計算機螢屏左上角開始鋪開顯示(視窗中心點)
*/
this.setBounds(0,0,700,500);
//讓表單顯示出來
this.setVisible(true);
//點擊視窗x關閉鍵回應關閉
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent windowEvent) {
//退出游戲后在控制臺輸出顯示"游戲結束"
System.out.println("游戲結束");
//呼叫System類中的exit方法以實作視窗關閉按鈕(X)的生效
System.exit(0);
}
});
}
//游戲入口
public static void main(String[] args) {
// 創建本類物件
GameClient gameClient = new GameClient();
// 使用本類物件呼叫start方法,開始游戲
gameClient.start();
}
6.給表單添加背景圖片
6.1在com.ytzl.ylm包下創建工具包util并且創建工具類CommonUtils
6.2在工具類(CommonUtils)中寫讀取圖片的方法
public class CommonUtils {
//讀取圖片方法getImage
public static Image getImage(String imgPath) {
//引數為圖片路徑地址
ImageIcon imageIcon = new ImageIcon(imgPath);
return imageIcon.getImage();
}
}
6.3在客戶端類中完成背景圖片的插入
// 將背景圖片的路徑賦值給常量BG_PATH
public static final String BG_PATH = "images/bg.png"/>
第二天(人物以及帽子移動方向)
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/qita/216749.html
標籤:其他
