最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)雷霆戰(zhàn)機(jī)

 更新時(shí)間:2022年06月10日 14:36:43   作者:qq_1741337072  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)雷霆戰(zhàn)機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)雷霆戰(zhàn)機(jī)的具體代碼,供大家參考,具體內(nèi)容如下

GameFame.java

package cn. tx;
?
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Vector;
?
?class GameFrame extends JFrame {
?
? ? HeroPlane heroPlane;
?
? ? //定義子彈的集合
? ? Vector<Bullet> bullets = new Vector<>();
? ? //敵機(jī)集合
? ? ?Vector<EnemyPlane> enemys = new Vector<>();
?
? ? ?GameFrame ?frame;
?
? ? public GameFrame () {
? ? ? ? frame = this;
? ? ? ? //創(chuàng)建英雄機(jī)
? ? ? ? heroPlane =new HeroPlane();
? ? ? ? heroPlane.start();
? ? ? ? //設(shè)置窗體的寬高
? ? ? ? this.setSize(450, 730);
? ? ? ? //標(biāo)題
? ? ? ? this.setTitle("雷霆戰(zhàn)機(jī)");
? ? ? ? this.setResizable(false);
? ? ? ? this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
? ? ? ? this.setLocationRelativeTo(null);
? ? ? ? //窗口可見
? ? ? ? this.setVisible(true);
?
?
? ? ? ? new Thread(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? while (true) {
? ? ? ? ? ? ? ? ? ? repaint();
? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? Thread.sleep(10);
? ? ? ? ? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }).start();
?
? ? ? ? //產(chǎn)生敵機(jī)的線程
? ? ? ? new Thread(new Runnable() {
? ? ? ? ? ? //創(chuàng)建隨機(jī)數(shù)的對(duì)象
? ? ? ? ? ? Random r = new Random();
?
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? while (true){
? ? ? ? ? ? ? ? ? ? //啟動(dòng)敵機(jī)
? ? ? ? ? ? ? ? ? ? EnemyPlane enemyPlane = new EnemyPlane(r.nextInt(500), 0, frame);
? ? ? ? ? ? ? ? ? ? enemyPlane.start();
? ? ? ? ? ? ? ? ? ? //添加敵機(jī)的時(shí)候,讓x軸隨機(jī)
? ? ? ? ? ? ? ? ? ? enemys.add(enemyPlane);
? ? ? ? ? ? ? ? ? ? try{
? ? ? ? ? ? ? ? ? ? ? ? Thread.sleep(500);
? ? ? ? ? ? ? ? ? ? } catch (InterruptedException e){
? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? }
? ? ? ? }).start();
?
? ? }
?
?
// ? ? ? ?*在窗口上畫,內(nèi)容,paint這 個(gè)畫筆的方法在窗口初始化的時(shí)候會(huì)默認(rèn)的執(zhí)行
// ? ? ? ?@param g
?
? ? ? ? public void paint (Graphics g) {
? ? ? ? ? ? //System.out.println("繪制畫板");
? ? ? ? ? ? //兩背景
? ? ? ? ? ? BufferedImage image = (BufferedImage) this.createImage(this.getSize().width, this.getSize().height);
? ? ? ? ? ? //高效緩存的畫筆
? ? ? ? ? ? Graphics bi = image.getGraphics();
? ? ? ? ? ? //畫背景
? ? ? ? ? ? bi.drawImage(new ImageIcon("img/MAP02_01.png").getImage(),0,0,null);
? ? ? ? ? ? //畫戰(zhàn)斗機(jī)
? ? ? ? ? ? bi.drawImage(heroPlane.img, heroPlane.x,heroPlane.y, heroPlane.width,heroPlane.heigth,null);
? ? ? ? ? ? //飛機(jī)發(fā)射炮彈
? ? ? ? ? ? for (int i = 0; i < bullets.size(); i++) {
? ? ? ? ? ? ? ? System.out.println(bullets);
? ? ? ? ? ? ? ? Bullet bullet = bullets.get(i);
? ? ? ? ? ? ? ? if(bullet.y > 0)
? ? ? ? ? ? ? ? ? ? bi.drawImage(bullet.image, bullet.x,bullet.y -= bullet.speed, bullet.width,bullet.height, null);
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? bullets.remove(bullet);
? ? ? ? ? ? }
? ? ? ? ? ? //畫敵機(jī)
? ? ? ? ? ? for (int i = 0; i < enemys.size(); i++) {
? ? ? ? ? ? ? ? System.out.println(enemys);
? ? ? ? ? ? ? ? EnemyPlane ep = enemys.get(i);
? ? ? ? ? ? ? ? if(ep.y < 730 )
? ? ? ? ? ? ? ? ? ? bi.drawImage(ep.img, ep.x,ep.y += ep.speed, ep.width,ep.heigth,null);
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? enemys.remove(ep);
? ? ? ? ? ? }
?
?
? ? ? ? ? ? //生效
? ? ? ? ? ? g.drawImage(image,0,0,null);
? ? ? ? }
? ? ? ? public static void main (String[]args){
? ? ? ? ? ? GameFrame frame = new GameFrame();
? ? ? ? ? ? Player player = new Player(frame);
? ? ? ? ? ? frame.addKeyListener(player);
? ? ? ? }
? ? }

HeroPlane

package cn.tx;
import javax.swing.*;
import java.awt.*;
?
public class HeroPlane extends Thread{
? ? //英雄機(jī)在畫板上的位置
? ? int x=200, y=600;
?
? ? int width = 50, heigth = 50;
? ? //飛機(jī)的速度
? ? int speed = 10;
?
? ? Image img = new ImageIcon("img/10011.png").getImage();
?
? ? //定義方向鍵的標(biāo)志
? ? boolean up,down,left,right;
?
? ? public HeroPlane() {
? ? }
?
? ? public HeroPlane(int x, int y, int width, int heigth, Image img) {
? ? ? ? this.x = x;
? ? ? ? this.y = y;
? ? ? ? this.width = width;
? ? ? ? this.heigth = heigth;
? ? }
?
? ? @Override
? ? public void run() {
? ? ? ? while (true){
? ? ? ? ? ? if (up){
? ? ? ? ? ? ? ? y -= speed;
? ? ? ? ? ? }
? ? ? ? ? ? if (down){
? ? ? ? ? ? ? ? y += speed;
? ? ? ? ? ? }
? ? ? ? ? ? if (left){
? ? ? ? ? ? ? ? x -= speed;
? ? ? ? ? ? }
? ? ? ? ? ? if (right){
? ? ? ? ? ? ? ? x += speed;
? ? ? ? ? ? }
?
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? Thread.sleep(10);
? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}

Player

package cn.tx;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//定義一個(gè)玩家
public class Player extends KeyAdapter {
? ? GameFrame frame;
? ? HeroPlane heroPlane;
? ? public Player(GameFrame frame) {
? ? ? ?this.frame=frame;
? ? }
?
? ? public void keyPressed(KeyEvent e) {
? ? ? ? int keyCode = e.getKeyCode();
? ? ? ? //38、40、37、39
? ? ? ? switch (keyCode){
? ? ? ? ? ? case 38:
? ? ? ? ? ? ? ? frame.heroPlane.up = true;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 40:
? ? ? ? ? ? ? ? frame. heroPlane.down = true;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 37:
? ? ? ? ? ? ? ? frame. heroPlane.left = true;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 39:
? ? ? ? ? ? ? ? frame. heroPlane.right = true;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 66:
? ? ? ? ? ? ? ? addBullut();
? ? ? ? ? ? ? ? break;
? ? ? ? }
?
? ? }
?
? ? @Override
? ? public void keyReleased(KeyEvent e) {
? ? ? ? int keyCode = e.getKeyCode();
? ? ? ? //38、40、37、39
? ? ? ? switch (keyCode){
? ? ? ? ? ? case 38:
? ? ? ? ? ? ? ? frame.heroPlane.up = false;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 40:
? ? ? ? ? ? ? ? frame. heroPlane.down = false;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 37:
? ? ? ? ? ? ? ? frame. ?heroPlane.left = false;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 39:
? ? ? ? ? ? ? ? frame. heroPlane.right = false;
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ? public void addBullut(){
? ? ? ? frame.bullets.add(new Bullet( frame.heroPlane.x+5, ?frame.heroPlane.y - 20));
? ? }
}

EnemyPlane

package cn.tx;
?
import javax.swing.*;
import java.awt.*;
?
?
public class EnemyPlane extends Thread {
? ? public GameFrame gf;
? ? //子彈的坐標(biāo),大小速度
? ? public int x, y;
? ? public int width = 50;
? ? public int heigth = 50;
? ? public int speed = 2;
? ? public Image img = new ImageIcon("img/10021.png").getImage();
?
? ? public EnemyPlane(int x, int y, GameFrame gf) {
? ? ? ? super();
? ? ? ? this.x = x;
? ? ? ? this.y = y;
? ? ? ? this.gf = gf;
? ? }
?
? ? public EnemyPlane(int x, int y, int width, int heigth, GameFrame gf) {
? ? ? ? super();
? ? ? ? this.x = x;
? ? ? ? this.y = y;
? ? ? ? this.width = width;
? ? ? ? this.heigth = heigth;
? ? ? ? this.gf = gf;
? ? }
?
? ? //瑪麗飛翔的邏輯;移動(dòng)的邏輯都在這里。
? ? public void run() {
? ? ? ? while (true) {
? ? ? ? ? ? //向左走
? ? ? ? ? ? if (hit()) {
? ? ? ? ? ? ? ? System.out.println("hit................");
? ? ? ? ? ? ? ? this.speed = 0;
? ? ? ? ? ? ? ? this.img = new ImageIcon("img/300350.png").getImage();
? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? this.sleep(500);
? ? ? ? ? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? gf.enemys.remove(this);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (this.y >= 760) {
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? this.sleep(10);
? ? ? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? }
? ? }
?
?
? ? //檢測(cè)碰撞
? ? public boolean hit() {
? ? ? ? // swing在水中,人家已經(jīng)提供了
? ? ? ? Rectangle myrect = new Rectangle(this.x, this.y, this.width, this.heigth);
? ? ? ? Rectangle rect = null;
? ? ? ? for (int i = 0; 1 < gf.bullets.size(); i++) {
? ? ? ? ? ? Bullet bullet = gf.bullets.get(i);
? ? ? ? ? ? System.out.println("test hit");
? ? ? ? ? ? rect = new Rectangle(bullet.x, bullet.y - 1, bullet.width, bullet.height);
? ? ? ? ? ? //碰撞檢測(cè)
? ? ? ? ? ? if (myrect.intersects(rect)) {
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return false;
? ? }
?
? ? @Override
? ? public String toString() {
? ? ? ? return "EnemyPlane{" +
? ? ? ? ? ? ? ? "x=" + x +
? ? ? ? ? ? ? ? ", y=" + y +
? ? ? ? ? ? ? ? ", width=" + width +
? ? ? ? ? ? ? ? ", height=" + heigth +
? ? ? ? ? ? ? ? '}';
? ? }
}

Bullet

package cn.tx;
import javax.swing.*;
import java.awt.*;
?
public class Bullet {
? ? //在面板上的坐標(biāo)
? ? int x, y;
? ? int width= 50,height = 50;
?
? ? //定義飛機(jī)默認(rèn)速度
? ? int speed = 5;
?
? ? Image image = new ImageIcon("img/30022.png").getImage();
?
? ? public Bullet(int x, int y) {
? ? ? ? this.x = x;
? ? ? ? this.y = y;
? ? }
?
? ? public Bullet(int x, int y, int width, int height) {
? ? ? ? this.x = x;
? ? ? ? this.y = y;
? ? ? ? this.width = width;
? ? ? ? this.height = height;
? ? }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java?Handler同步屏障淺析講解

    Java?Handler同步屏障淺析講解

    同步屏障機(jī)制是什么?Handler發(fā)送的消息分為普通消息、屏障消息、異步消息,一旦Looper在處理消息時(shí)遇到屏障消息,那么就不再處理普通的消息,而僅僅處理異步的消息。不再使用屏障后,需要撤銷屏障,不然就再也執(zhí)行不到普通消息了
    2022-08-08
  • Java中LinkedHashSet的底層機(jī)制詳解

    Java中LinkedHashSet的底層機(jī)制詳解

    這篇文章主要介紹了Java中LinkedHashSet的底層機(jī)制解讀,   LinkedHashSet是具有可預(yù)知迭代順序的Set接口的哈希表和鏈接列表實(shí)現(xiàn),此實(shí)現(xiàn)與HashSet的不同之處在于,后者維護(hù)著一個(gè)運(yùn)行于所有條目的雙重鏈接列表,需要的朋友可以參考下
    2023-09-09
  • java貪心算法初學(xué)感悟圖解及示例分享

    java貪心算法初學(xué)感悟圖解及示例分享

    這篇文章主要為大家介紹了本人在初學(xué)java貪心算法的感悟,并通過(guò)圖解及示例代碼的方式分享給大家,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • SpringBoot返回html界面的使用示例

    SpringBoot返回html界面的使用示例

    本文主要介紹了SpringBoot返回html界面的使用示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • Spring七大組件是哪些以及作用

    Spring七大組件是哪些以及作用

    這篇文章主要介紹了Spring七大組件是哪些以及作用,幫助剛接觸spring的朋友更快理解,如果有說(shuō)的不對(duì)的地方還請(qǐng)指正,需要的朋友可以參考下
    2023-03-03
  • 詳解設(shè)計(jì)模式在Spring中的應(yīng)用(9種)

    詳解設(shè)計(jì)模式在Spring中的應(yīng)用(9種)

    這篇文章主要介紹了詳解設(shè)計(jì)模式在Spring中的應(yīng)用(9種),詳細(xì)的介紹了這9種模式在項(xiàng)目中的應(yīng)用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2019-04-04
  • Java?DirectByteBuffer堆外內(nèi)存回收詳解

    Java?DirectByteBuffer堆外內(nèi)存回收詳解

    這篇文章主要為大家詳細(xì)介紹了Java中發(fā)DirectByteBuffer堆外內(nèi)存回收,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下
    2022-10-10
  • SpringBoot@Profile注解和Spring?EL(多環(huán)境注入)

    SpringBoot@Profile注解和Spring?EL(多環(huán)境注入)

    為了方便, Spring還提供了 Profile機(jī)制, 使我們可以很方便地實(shí)現(xiàn)各個(gè)環(huán)境之間的切換,在使用DI來(lái)依賴注入的時(shí)候,能夠根據(jù)@profile標(biāo)明的環(huán)境,將注入符合當(dāng)前運(yùn)行環(huán)境的相應(yīng)的bean,本文通過(guò)示例代碼介紹SpringBoot@Profile注解和Spring?EL,需要的朋友可以參考下
    2024-02-02
  • SpringBoot項(xiàng)目部署到服務(wù)器的兩種方式

    SpringBoot項(xiàng)目部署到服務(wù)器的兩種方式

    目前,前后端分離的架構(gòu)已成主流,而使用SpringBoot構(gòu)建Web應(yīng)用是非??焖俚?項(xiàng)目發(fā)布到服務(wù)器上的時(shí)候,只需要打成一個(gè)jar包,然后通過(guò)命令 : java -jar jar包名稱即可啟動(dòng)服務(wù)了,本文介紹了SpringBoot項(xiàng)目部署到服務(wù)器的兩種方式,需要的朋友可以參考下
    2024-10-10
  • 淺談Java類的加載,鏈接及初始化

    淺談Java類的加載,鏈接及初始化

    今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著Java類的加載,鏈接及初始化展開,文中有非常詳細(xì)的解釋及代碼示例,需要的朋友可以參考下
    2021-06-06

最新評(píng)論

金塔县| 宜川县| 黄大仙区| 湟源县| 贵阳市| 晋中市| 平阴县| 称多县| 辰溪县| 孟州市| 稻城县| 新兴县| 舞钢市| 启东市| 融水| 炉霍县| 潮州市| 奉新县| 陈巴尔虎旗| 高唐县| 绥滨县| 甘洛县| 华坪县| 宁安市| 江陵县| 民丰县| 鄂伦春自治旗| 浦东新区| 蓬莱市| 慈利县| 嵩明县| 汉源县| 昆明市| 庆元县| 临朐县| 花莲市| 镇安县| 罗平县| 北票市| 汽车| 特克斯县|