本篇博客是對飛機大戰游戲使用代碼的展示
重難點:
首先需要滑鼠能夠控制戰機,使滑鼠在視窗內時始終能夠使戰機的位置與滑鼠相同,實作滑鼠控制戰斗機移動,
其次需要能夠以一定的速度產生子彈和敵機,并且以一定的速度移動,為了實作這部分的功能,我們需要使用執行緒來完成,(Timer也可以實作這塊功能,但過多使用Timer會使程式變得不穩定,會經常報錯)JAVA 執行緒的介紹與使用
再然后就是判斷子彈與敵機相撞,戰機與敵機相撞,這就需要用到遍歷,需要遍歷的是場上的子彈與敵機,這就需要我們使用ArrayList來存盤場上的敵機與子彈,當敵機與子彈離開畫面使,便從ArrayList中移除,這樣便可以保證遍歷的是場上的敵機與子彈,
滑鼠控制戰機
class mouse1 extends MouseMotionAdapter{
@Override
public void mouseMoved(MouseEvent e) {
X=e.getX();//得到滑鼠X坐標
Y=e.getY();//得到滑鼠Y坐標
jl2.setLocation(X-30, Y-35);
//戰機根據滑鼠設定位置
//通過監聽器,只要滑鼠移動就執行,
//圖片根據顯示效果更改坐標值
}
}
子彈與敵機的產生
//敵機生成(執行緒代碼)
class enemyCreate extends Thread{
public void run() {
while(true) {
try {
this.sleep(1000);//1000毫秒執行一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ImageIcon image_enemy=new ImageIcon("game_image/enemy.png");//敵機圖片路徑
enemy[enemy_num]=new JLabel(image_enemy);
Random random=new Random();
enemy[enemy_num].setBounds(random.nextInt(image1.getIconWidth()), 0, image_enemy.getIconWidth(), image_enemy.getIconHeight());//設定敵機生成位置和大小(位置橫坐標通過Random隨機生成)
layeredPane.add(enemy[enemy_num], JLayeredPane.MODAL_LAYER);
a1.add(enemy[enemy_num]);//將每一個生成的敵機存放到ArrayList中
enemy_num++;
}
}
}
//子彈生成(執行緒代碼)
class ammoCreate extends Thread{
public void run() {
while(true) {
try {
this.sleep(1000);//1000毫秒執行一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ImageIcon image_ammo=new ImageIcon("game_image/ammo.png");//子彈圖片路徑
ammo[ammo_num]=new JLabel(image_ammo);
ammo[ammo_num].setBounds(X, Y-40, image_ammo.getIconWidth(), image_ammo.getIconHeight());//子彈位置和大小(X,Y為滑鼠的X,Y坐標)
layeredPane.add(ammo[ammo_num], JLayeredPane.MODAL_LAYER);
a2.add(ammo[ammo_num]);//將每一個生成的子彈存放到ArrayList中
ammo_num++;
}
}
}
敵機與子彈的移動
//敵機移動(執行緒代碼)
class enemyMove extends Thread{
public void run() {
while(true) {
try {
this.sleep(100);//100毫秒執行一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<a1.size();i++) {//遍歷敵機對應的ArrayList
a1.get(i).setLocation(a1.get(i).getX(), a1.get(i).getY()+10);//敵機移動,在原基礎向下移動10
if(a1.get(i).getY()>=image1.getIconHeight()) {//敵機移動到視窗外,變得不可見,然后從對應的ArrayList中移除
a1.get(i).setVisible(false);
a1.remove(i);
}
}
}
}
}
//子彈移動(執行緒代碼)
class ammoMove extends Thread{
public void run() {
while(true) {
for(int i=0;i<a2.size();i++) {//遍歷子彈的ArrayList
a2.get(i).setLocation(a2.get(i).getX(),a2.get(i).getY()-10);//位置移動,在原基礎向上移動10
if(a2.get(i).getY()<=-30) {//子彈移動到視窗外,變得不可見,然后從對應的ArrayList中移除
a2.get(i).setVisible(false);
a2.remove(i);
}
}
try {
this.sleep(100);//100毫秒執行一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
相撞判斷
//戰機與敵機相撞(執行緒代碼)
class playerBoom extends Thread{
public void run() {
while(true) {
try {
this.sleep(100);//100毫秒執行一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<a1.size();i++) {//遍歷敵機對應的ArrayList
if(a1.get(i).isVisible()) {//當敵機可視
if(jl2.getY()-a1.get(i).getY()<=a1.get(i).getHeight()&&jl2.getY()-a1.get(i).getY()>=-jl2.getHeight()) {//敵機與戰機相遇
if(jl2.getX()-a1.get(i).getX()>=-jl2.getWidth()&&jl2.getX()-a1.get(i).getX()<=a1.get(i).getWidth()) {
a1.get(i).setVisible(false);//敵機不可視
//游戲結束
new game_plane_3();
dispose();
return;//結束該執行緒
}
}
}
}
}
}
}
//子彈與敵機相撞(執行緒代碼)
class ammoBoom extends Thread{
public void run() {
while(true) {
try {
this.sleep(100);//100毫秒執行一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<a1.size();i++) {//遍歷敵機對應的ArrayList
for(int j=0;j<a2.size();j++) {//遍歷子彈對應的ArrayList
if(a1.get(i).isVisible()&&a2.get(j).isVisible()) {//當子彈與敵機都可視
if(a2.get(j).getY()-a1.get(i).getY()<=a1.get(i).getHeight()&&a2.get(j).getY()-a1.get(i).getY()>=-10) {//子彈與敵機相遇
if(a2.get(j).getX()-a1.get(i).getX()>=-a2.get(j).getWidth()&&a2.get(j).getX()-a1.get(i).getX()<=a1.get(i).getWidth()) {
a1.get(i).setVisible(false);//對應的敵機不可視
a2.get(j).setVisible(false);//對應的子彈不可視
}
}
}
}
}
}
}
}
完整代碼
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class game_plane_2 extends JFrame{
ImageIcon image1=new ImageIcon("game_image/background.png");//背景圖片路徑
ImageIcon image2=new ImageIcon("game_image/player1.png");//戰機圖片路徑
JLabel[] enemy=new JLabel[50000];
ArrayList<JLabel> a1=new ArrayList<JLabel>();//存放敵機
JLabel[] ammo=new JLabel[50000];
ArrayList<JLabel> a2=new ArrayList<JLabel>();//存放子彈
JLabel jl1=new JLabel(image1);
JLabel jl2=new JLabel(image2);
JLayeredPane layeredPane=new JLayeredPane();
int X=200,Y=500;//記錄滑鼠位置X,Y
int enemy_num=0,ammo_num=0;
public game_plane_2() {
jl1.addMouseMotionListener(new mouse1());//添加監聽器
jl2.setBounds(X, Y, image2.getIconWidth(), image2.getIconHeight());//戰機位置和大小
jl1.setBounds(0, 0, image1.getIconWidth(), image1.getIconHeight());//背景位置和大小
layeredPane.add(jl1, JLayeredPane.DEFAULT_LAYER);
layeredPane.add(jl2, JLayeredPane.MODAL_LAYER);
new enemyCreate().start();//敵機生成的執行緒啟動
new ammoCreate().start();//子彈生成的執行緒啟動
new enemyMove().start();//敵機移動的執行緒啟動
new ammoMove().start();//子彈移動的執行緒啟動
new ammoBoom().start();//子彈與敵機相撞的執行緒啟動
new playerBoom().start();//戰機與敵機相撞的執行緒啟動
this.setLayeredPane(layeredPane);
this.setTitle("飛機大戰");//表單標題
this.setResizable(false);//表單不可最大化
this.setSize(image1.getIconWidth(),image1.getIconHeight());//設定大小
this.setLocationRelativeTo(null);//表單位置
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);//表單關閉
this.setVisible(true);//表單可視
}
class mouse1 extends MouseMotionAdapter{
@Override
public void mouseMoved(MouseEvent e) {
X=e.getX();//得到滑鼠X坐標
Y=e.getY();//得到滑鼠Y坐標
jl2.setLocation(X-30, Y-35);
//戰機根據滑鼠設定位置
//通過監聽器,只要滑鼠移動就執行,
//圖片根據顯示效果更改坐標值
}
}
//戰機與敵機相撞(執行緒代碼)
class playerBoom extends Thread{
public void run() {
while(true) {
try {
this.sleep(100);//100毫秒執行一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<a1.size();i++) {//遍歷敵機對應的ArrayList
if(a1.get(i).isVisible()) {//當敵機可視
if(jl2.getY()-a1.get(i).getY()<=a1.get(i).getHeight()&&jl2.getY()-a1.get(i).getY()>=-jl2.getHeight()) {//敵機與戰機相遇
if(jl2.getX()-a1.get(i).getX()>=-jl2.getWidth()&&jl2.getX()-a1.get(i).getX()<=a1.get(i).getWidth()) {
a1.get(i).setVisible(false);//敵機不可視
//游戲結束
new game_plane_3();
dispose();
return;//結束該執行緒
}
}
}
}
}
}
}
//子彈與敵機相撞(執行緒代碼)
class ammoBoom extends Thread{
public void run() {
while(true) {
try {
this.sleep(100);//100毫秒執行一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<a1.size();i++) {//遍歷敵機對應的ArrayList
for(int j=0;j<a2.size();j++) {//遍歷子彈對應的ArrayList
if(a1.get(i).isVisible()&&a2.get(j).isVisible()) {//當子彈與敵機都可視
if(a2.get(j).getY()-a1.get(i).getY()<=a1.get(i).getHeight()&&a2.get(j).getY()-a1.get(i).getY()>=-10) {//子彈與敵機相遇
if(a2.get(j).getX()-a1.get(i).getX()>=-a2.get(j).getWidth()&&a2.get(j).getX()-a1.get(i).getX()<=a1.get(i).getWidth()) {
a1.get(i).setVisible(false);//對應的敵機不可視
a2.get(j).setVisible(false);//對應的子彈不可視
}
}
}
}
}
}
}
}
//敵機移動(執行緒代碼)
class enemyMove extends Thread{
public void run() {
while(true) {
try {
this.sleep(100);//100毫秒執行一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<a1.size();i++) {//遍歷敵機對應的ArrayList
a1.get(i).setLocation(a1.get(i).getX(), a1.get(i).getY()+10);//敵機移動,在原基礎向下移動10
if(a1.get(i).getY()>=image1.getIconHeight()) {//敵機移動到視窗外,變得不可見,然后從對應的ArrayList中移除
a1.get(i).setVisible(false);
a1.remove(i);
}
}
}
}
}
//子彈移動(執行緒代碼)
class ammoMove extends Thread{
public void run() {
while(true) {
for(int i=0;i<a2.size();i++) {//遍歷子彈的ArrayList
a2.get(i).setLocation(a2.get(i).getX(),a2.get(i).getY()-10);//位置移動,在原基礎向上移動10
if(a2.get(i).getY()<=-30) {//子彈移動到視窗外,變得不可見,然后從對應的ArrayList中移除
a2.get(i).setVisible(false);
a2.remove(i);
}
}
try {
this.sleep(100);//100毫秒執行一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//敵機生成(執行緒代碼)
class enemyCreate extends Thread{
public void run() {
while(true) {
try {
this.sleep(1000);//1000毫秒執行一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ImageIcon image_enemy=new ImageIcon("game_image/enemy.png");//敵機圖片路徑
enemy[enemy_num]=new JLabel(image_enemy);
Random random=new Random();
enemy[enemy_num].setBounds(random.nextInt(image1.getIconWidth()), 0, image_enemy.getIconWidth(), image_enemy.getIconHeight());//設定敵機生成位置和大小(位置橫坐標通過Random隨機生成)
layeredPane.add(enemy[enemy_num], JLayeredPane.MODAL_LAYER);
a1.add(enemy[enemy_num]);//將每一個生成的敵機存放到ArrayList中
enemy_num++;
}
}
}
//子彈生成(執行緒代碼)
class ammoCreate extends Thread{
public void run() {
while(true) {
try {
this.sleep(1000);//1000毫秒執行一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ImageIcon image_ammo=new ImageIcon("game_image/ammo.png");//子彈圖片路徑
ammo[ammo_num]=new JLabel(image_ammo);
ammo[ammo_num].setBounds(X, Y-40, image_ammo.getIconWidth(), image_ammo.getIconHeight());//子彈位置和大小(X,Y為滑鼠的X,Y坐標)
layeredPane.add(ammo[ammo_num], JLayeredPane.MODAL_LAYER);
a2.add(ammo[ammo_num]);//將每一個生成的子彈存放到ArrayList中
ammo_num++;
}
}
}
}
如有錯誤
歡迎指出
下一篇:用JAVA制作小游戲——飛機大戰(三)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/252047.html
標籤:其他
上一篇:python登錄介面測驗問題記錄與解決 ( 干 貨 )
下一篇:Deep Video Super-Resolution using HR Optical Flow Estimation | 視頻超分重建 | 測驗簡記 |
