生活随笔
收集整理的這篇文章主要介紹了
王思聪吃热狗 - 飞机大战小游戏
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
這個(gè)小游戲是用java寫的,java swing, 源碼比較簡(jiǎn)單,一般都能看懂 ,素材找不到,看著比較low,如果你找到熱狗飛機(jī)圖片,改一下就好
運(yùn)行結(jié)果圖:
**
圖片素材:
**
https://blog.csdn.net/qq_40456064/article/details/84787388
分為8個(gè)class:
1. Bullet.java
2. GetImage.java
3. MyGame.java
4. Plane.java
5. SiCongPlaneFight_Main.java
6. Stone.java
7. Table.java
8. Window.java
程序入口在
SiCongPlaneFight_Main.java
Bullet.java:
import java
.awt
.*
;public class Bullet {int x
, y
;Image i
;boolean destory
= false;Stone stone
[];Bullet(Image img
, int x
,int y
, Stone stone
[]){this.x
= x
;this.y
= y
;this.i
= img
;this.stone
= stone
;}void shot() {y
-= Table
.table
*2;}void draw(Graphics g
) {g
.drawImage(i
, x
, y
, Table
.size
/2, Table
.size
, null
);ifdestroy();shot();}void ifdestroy() {if(this.y
< 0) {destory
= true;}for(int i
=0;i
< stone
.length
; i
++) {if(stone
[i
] != null
) {if(stone
[i
].x
-Table
.size
/2<this.x
&&this.x
<(stone
[i
].x
+Table
.size
/2)&&this.y
>stone
[i
].y
-Table
.size
/2&&this.y
<stone
[i
].y
+Table
.size
/2) {stone
[i
].live
--;destory
= true;}}}}
}
GetImage.java
import java
.awt
.image
.BufferedImage
;
import java
.io
.IOException
;
import java
.net
.URL
;import javax
.imageio
.ImageIO
;public class GetImage {private GetImage() {}public static BufferedImage
get(String path
) {URL u
= GetImage
.class.getClassLoader().getResource(path
);BufferedImage b
= null
;try {b
=ImageIO
.read(u
);} catch (IOException e
) {e
.printStackTrace();}return b
;}
}
MyGame.java
import java
.awt
.image
.BufferedImage
;
import java
.io
.IOException
;
import java
.net
.URL
;import javax
.imageio
.ImageIO
;public class GetImage {private GetImage() {}public static BufferedImage
get(String path
) {URL u
= GetImage
.class.getClassLoader().getResource(path
);BufferedImage b
= null
;try {b
=ImageIO
.read(u
);} catch (IOException e
) {e
.printStackTrace();}return b
;}
}
Plane.java
import java
.awt
.*
;import javax
.swing
.ImageIcon
;public class Plane {int x
, y
;Image i
;boolean destory
= false;Stone stone
[];double step
= Table
.table
;private boolean left
, right
, up
, down
;public boolean isLeft() {return left
;}public void setLeft(boolean left
) {this.left
= left
;}public boolean isRight() {return right
;}public void setRight(boolean right
) {this.right
= right
;}public boolean isUp() {return up
;}public void setUp(boolean up
) {this.up
= up
;}public boolean isDown() {return down
;}public void setDown(boolean down
) {this.down
= down
;}Plane(Image bi1
,int x
,int y
,Stone stone
[]){this.i
= bi1
;this.x
= x
;this.y
= y
;this.stone
= stone
;}public void move() {if(MyGame
.count
>= 2) {step
= Table
.table
/ 1.414;}else {step
= Table
.table
;}if(left
&& x
> 0) {this.x
-= step
;}if(right
&& x
< Table
.width
- Table
.size
) {this.x
+= step
;}if(up
&& y
> 0){this.y
-= step
;}if(down
&& y
< Table
.height
- Table
.size
) {this.y
+= step
;}}public void draw(Graphics g
) {move();ifdestory();g
.drawImage(i
, x
, y
, Table
.size
, Table
.size
, null
);}private void ifdestory() {for(int i
=0;i
< stone
.length
;i
++) {if(stone
[i
] != null
) {if(stone
[i
].x
- Table
.size
/2 < this.x
&& this.x
< (stone
[i
].x
+Table
.size
/2)&&this.y
>stone
[i
].y
-Table
.size
/2&&this.y
<stone
[i
].y
+Table
.size
/2) {destory
=true;}}}}
}
SiCongPlaneFight_Main.java
public class SiCongPlaneFight_Main {public static void main(String
[] args
) {new MyGame();}}
Stone.java
import java
.awt
.*
;
import java
.util
.Random
;
public class Stone {int x
, y
;Image i
;Plane p
;int live
;boolean destory
= false;public Stone(Image i
, Plane p
,int live
) {this.x
= getRandom();this.y
= 0;this.i
= i
;this.p
= p
;this.live
= live
;}int getRandom() {Random r
= new Random();return r
.nextInt(550) + 20;}void draw(Graphics g
) {g
.drawImage(i
, x
, y
, 50, 50, null
);ifdestory();movegetclose();}private void movegetclose() {y
+= 2;}void ifdestory() {if(live
<= 0) {destory
= true;MyGame
.score
++;}if(this.y
> Table
.height
) {destory
= true;}}
}
Table.java
public class Table {public static double table
=10; public static int size
=50;public static int width
=600;public static int height
=700;
}
Window.java
import java
.awt
.*
;
import java
.awt
.event
.ActionEvent
;
import java
.awt
.event
.ActionListener
;import javax
.swing
.*
;
public class Window extends JFrame{private JTextField t1
;private JButton btn1
;private JButton btn2
;boolean ificansee
= true;Window(){super("這么快就結(jié)束了???");setBounds((400 + Table
.width
), (Table
.height
/ 4), 500, 100);setVisible(ificansee
);t1
= new JTextField("是否重新開始?");t1
.setEnabled(false);btn1
= new JButton("重新開始");btn2
= new JButton("退出");btn1
.setBounds(31, 13, 13, 13);btn2
.setBounds(31, 13, 13, 13);Container c1
= getContentPane();c1
.setLayout(new FlowLayout());c1
.add(btn1
);c1
.add(btn2
);addListener();setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);}private void addListener() {btn1
.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e
) {MyGame
.replay
= true;dispose();}});btn2
.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e
) {System
.exit(0);}});}
}
總結(jié)
以上是生活随笔為你收集整理的王思聪吃热狗 - 飞机大战小游戏的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。