日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

西瓜大战java_Java中的线程及(简单飞机大战)实现

發布時間:2023/12/1 java 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 西瓜大战java_Java中的线程及(简单飞机大战)实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在講到線程之前我們先聊一下程序,進程,線程這三者的概念:

程序是指為了實現特定目標或解決特定問題而用計算機語言編寫的命令序列的集合。

程序存儲在磁盤上,由一系列的文件+數據組成。

進程:程序運行起來之后就是一個進程

進程存在系統分配的內存里,并且進程之間可以進行通信。

而我們這里需要講到的線程是進程中的一個獨立運行的單位,是CPU執行的最小單位。

我們在使用記事本寫程序時需要先執行Javac命令編譯,將java程序編譯成一個字節碼class文件,然后再執行Java命令時JVM(Java虛擬機)就會啟動線程,讓線程調用main函數,線程根據main函數中的代碼的順序逐行執行。

那么同樣的線程在哪里執行呢?

線程在cpu中執行,線程執行時所涉及到對象的屬性和方法等數據都在內存中,電腦使用緩存存儲線程所需要執行的數據。線程只會改變緩存中的數據而不會改變內存中的數據。

線程與進程不同,每個進程系統都會分配一定的內存空間,線程都是共享存儲空間。

多線程就顯而易見了就是指一個進程中有多個線程,能同時做多件事。

那么Java中如何實現線程呢?

1.實現Runnable接口

run();線程執行的方法

2.Thread類實現Runnable接口

run();線程執行的方法

start();方法線程的啟動方法

接下來我們來講一些簡單的飛機大戰的實現:

一、界面的實現

首先呢還是需要一個相對簡單的界面

public class MainFrame extends JFrame implements Planeinte{

public static void main(String[] args) {

MainFrame mf=new MainFrame();

mf.initUI();

}

public void initUI(){

//設置窗體屬性this.setTitle("飛機大戰V1.0");

this.setSize(600, 800);

this.setDefaultCloseOperation(3);

this.setLocationRelativeTo(null);

this.setVisible(true);

首先我們需要定義一個飛行物的類,用來定義它們的屬性和方法

因為敵機和子彈都需要自動產生及運動,所以我們在這里需要三個線程,一個自動產生飛機,一個自動產生子彈,還有一個自動畫子彈和飛機的類。

產生的飛機和子彈分別用數組隊列來存儲

線程一(自動產生飛機):

public void run(){

while(true){

if(true){

int x=rand.nextInt(mf.getWidth());

int y=0;

int speedX=rand.nextInt(5)+1;

int speedY=rand.nextInt(5)+1;

int blood=10;

boolean flag=false;

ImageIcon icon=new ImageIcon(this.getClass().getResource("敵機1.png"));

Plane plane=new Plane(x, y, speedX, speedY, blood, flag,icon);

list.add(plane);

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

線程二(自動產生子彈):

public void run(){

while(stopflag){

if(true){

int x=myplane.getX();

int y=myplane.getY();

int speedX=0;

int speedY=-40;

int blood=1;

boolean flag=true;

ImageIcon icon=new ImageIcon(this.getClass().getResource("bullet13.png"));

Plane bullet=new Plane(x, y, speedX, speedY, blood, flag,icon);

listb.add(bullet);

for(int i=1;i

Plane plane=list.get(i);

x=plane.getX();

y=plane.getY();

speedX=0;

speedY=20;

blood=1;

flag=false;

icon=new ImageIcon(this.getClass().getResource("bullet14.png"));

Plane bulletb=new Plane(x, y, speedX, speedY, blood, flag,icon);

listb.add(bulletb);

}

try {

Thread.sleep(300);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

線程三(自動畫飛機和子彈):

public void run(){

while(true){

if(g==null)

g=mf.getGraphics();

if(true&&g!=null){

if(y==0){

y=mf.getHeight()-icon.getIconHeight();

}

ig.drawImage(icon.getImage(), 0, y, mf);

y+=5;

for(int i=0;i

Plane plane=list.get(i);

plane.drawPlane(ig);

plane.movePlane(mf);

}

for(int j=0;j

Plane bullet=listb.get(j);

bullet.drawPlane(ig);

bullet.bulletcrash();

}

mf.getGraphics().drawImage(img, 0, 0, mf);

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

在這里如果我們直接畫在JFrame窗體上,會導致有閃屏以及有殘影的出現,為了解決這個問題,在這里我們采用雙緩沖技術,就是我們不直接將對象畫在窗體上,而是在這之前先創建一塊次畫布先依次在線程里將對象畫在次畫布上之后,再將整個次畫布畫在窗體上。

//創建次畫布

Image img =mf.createImage(mf.getWidth(), mf.getHeight());

Graphics ig=img.getGraphics();

ImageIcon icon=new ImageIcon(this.getClass().getResource("背景0.jpg"));

int y=mf.getHeight()-icon.getIconHeight();

在得到所有飛機之后,我們在窗體主函數出先將我們要控制的飛機存入,并且給窗體加上監聽以鼠標的拖動來控制我方飛機,也就是list.get(0);

ImageIcon icon=new ImageIcon(this.getClass().getResource("飛機1.png"));

Plane myplane=new Plane(300, 750, 0, 0, 100, true,icon);

list.add(myplane);

啟動線程并添加監聽:

PlaneAI pAI=new PlaneAI(this);

PlaneMoveDraw pmd=new PlaneMoveDraw(this);

pAI.start();

pmd.start();

PlaneListener pl=new PlaneListener();

this.addMouseListener(pl);

this.addMouseMotionListener(pl);

總結

以上是生活随笔為你收集整理的西瓜大战java_Java中的线程及(简单飞机大战)实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。