首页 >> 育儿

java飞机大战小游戏(飞机大战编程过程)

2022年04月14日 15:50:45 育儿 4916 投稿:用户投稿

多条告白如次剧本只需引入一次

一:玩耍展现

铁鸟大战小玩耍咱们都玩过,经过挪动铁鸟来打敌机,这边给大师展现一卑劣戏功效:哇哇呜因为gif只能上传5M巨细,以是就不许给大师展现操纵了,即使大师有爱好不妨本人本人做出来再玩哟。

这内里一切的铁鸟、枪弹、敌机、爆裂功效都不妨随便替代成本人想要的图片。大师不妨大肆表现。也不妨创造无敌版。

比方我不妨把敌机替代成王八:

大师不妨随便表现哦。

二:玩耍教程

1.View视图层

名目构造:

1.1创造玩耍面板类

package com.view;

import com.controller.PlaneController;

import com.eneity.EnemyPlane;

import com.eneity.FightPlane;

import com.utils.P;

import javax.swing.*;

/**

* 窗口类

*/

public class FrameWindow {

/**

* 结构本领

*/

public FrameWindow() {

launch();

}

/**

* 创造窗口界面

*/

public void launch() {

FightPlane fightPlane = new FightPlane();

PlaneController controller = new PlaneController(fightPlane);//铁鸟遏制类

GamePanel gamePanel = new GamePanel(fightPlane);

JFrame jFrame = new JFrame();

jFrame.add(gamePanel);

jFrame.setTitle(“铁鸟大战”);

jFrame.addKeyListener(controller);//给窗口树立监听事变

jFrame.setSize(P.WEIGHT, P.HEIGHT);//树立窗口巨细

jFrame.setVisible(true);//表露在窗口

jFrame.setResizable(false);//使窗口巨细恒定

jFrame.setLocationRelativeTo(null);//初始化的界面表露的场所,null为实质自符合

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击封闭之后后盾也会封闭

}

}

1.2.创造玩耍实质表露类

package com.view;

import com.eneity.CreateEnemy;

import com.eneity.EnemyPlane;

import com.eneity.FightPlane;

import com.utils.P;

import com.utils.PUtils;

import javax.swing.*;

import java.awt.*;

import java.util.LinkedList;

import java.util.List;

import java.util.Random;

/**

* 玩耍面板

*/

public class GamePanel extends JPanel {

public static long count = 0;//祛除敌机数目

public static List<EnemyPlane> EnemyPlaneArrayList = new LinkedList<EnemyPlane>();

private FightPlane fightPlane;//战役机

public ImageIcon bz2 = new

ImageIcon(PUtils.getImage(“images/dd2.png”));

//结构本领,启用这个线程

public GamePanel(FightPlane fightPlane) {

this.fightPlane = fightPlane;

new updateGame().start();//连接重画出画布

new CreateEnemy().start();//画出10架敌机

}

boolean flage = true;

@Override

public void paint(Graphics g) {

g.setColor(Color.BLACK);

g.fillRect(0, 0, P.WEIGHT, P.HEIGHT);

g.setColor(Color.red);

g.drawString(“杀龟数目:” + count, 650, 50);

//画出战役机

if (fightPlane != null) {

fightPlane.drawMe(g);

}

//画出敌机

for (int i = 0; i < EnemyPlaneArrayList.size(); i++) {

if (EnemyPlaneArrayList.get(i).flat == 1) {

int x = EnemyPlaneArrayList.get(i).x;

int y = EnemyPlaneArrayList.get(i).y;

g.drawImage(bz2.getImage(), x, y, 150, 150, null);

EnemyPlaneArrayList.get(i).flat–;

GamePanel.count++;

System.out.println(count);

if (EnemyPlaneArrayList.get(i).flat == -3) {

int c = new Random().nextInt(650) + 50;

EnemyPlaneArrayList.get(i).x = c;

EnemyPlaneArrayList.get(i).y = -150;

}

} else {

EnemyPlaneArrayList.get(i).drawMe(g);

}

}

}

//里面类,创造一个线程不停的革新界面

class updateGame extends Thread {

@Override

public void run() {

while (flage) {

repaint();

}

}

}

}

2.enetiy实业层

2.1玩耍实业笼统类

package com.eneity;

import java.awt.*;

/**

* 设置玩耍中一切东西的父类

*/

public abstract class GameObject {

public int x,y,width,height;//设置铁鸟的坐标,长,宽

public abstract void drawMe(Graphics g);//铁鸟画本人的本领

public abstract Rectangle getRect();//获得一个矩形用来检验和测定有没有和铁鸟相撞

}

2.2战机类

package com.eneity;

import com.utils.P;

import com.utils.PUtils;

import com.view.GamePanel;

import javax.swing.*;

import java.awt.*;

import java.util.LinkedList;

import java.util.List;

/**

* 战役机

*/

public class FightPlane extends GameObject {

public List<Bullet> arrayList =new LinkedList<Bullet>();

public static Image image =PUtils.getImage(“images/zdj.png”);//铁鸟图像资源

public static Image zd=PUtils.getImage(“images/zd.png”);//枪弹图片资源

public ImageIcon bz2=new

ImageIcon(PUtils.getImage(“images/dd2.png”));

public FightPlane(){

x=300;

y=300;

width=120;

height=120;

new CreateBullet().start();//创造画出枪弹的线程

}

@Override

public void drawMe(Graphics g) {

g.drawImage(image,x,y,width,height,null);

//画出枪弹;

for(int i=0;i<arrayList.size();i++){

//检验和测定枪弹能否与敌机相撞

for(int j=0;j<GamePanel.EnemyPlaneArrayList.size();j++){

if(GamePanel.EnemyPlaneArrayList.get(j).getRect().intersects(arrayList.get(i).getRect())){

/* int x=GamePanel.EnemyPlaneArrayList.get(j).x;

int y=GamePanel.EnemyPlaneArrayList.get(j).y;

GamePanel.EnemyPlaneArrayList.remove(j);

g.drawImage(bz2.getImage(),500,500,150,150,null);*/

GamePanel.EnemyPlaneArrayList.get(j).flat=1;

}

}

if(arrayList.get(i).y<20){

arrayList.remove(i);//优化枪弹类,当枪弹到达确定场所时让其消逝

}

arrayList.get(i).drawMe(g);

}

}

@Override

public Rectangle getRect() {

return new Rectangle(x,y,width,height);

}

//创造枪弹里面类

class Bullet extends GameObject{

public Bullet(){

width=60;

height=120;

}

@Override

public void drawMe(Graphics g) {//在面板上画出枪弹

g.drawImage(zd,x,y,width,height,null);

y-=3;

}

@Override

public Rectangle getRect() {

return new Rectangle(x,y,width,height);

}

}

/**

* 创造一个连接画出枪弹的线程

*/

class CreateBullet extends Thread{

@Override

public void run() {

while(true){

try {

Bullet bullet=new Bullet();

bullet.x=x+32;

bullet.y=y+20;

arrayList.add(bullet);

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

2.3敌机类

package com.eneity;

import com.utils.P;

import com.utils.PUtils;

import java.awt.*;

import java.util.LinkedList;

import java.util.List;

/**

* 敌机类

*/

public class EnemyPlane extends GameObject {

public int flat=0;//检验和测定枪弹能否与敌机相撞

public static Image dj =PUtils.getImage(“images/wugui.png”);//铁鸟图像资源

/* public static Image zd=PUtils.getImage(“images/zd.png”);//枪弹图片资源*/

public EnemyPlane(){

x=300;

y=300;

width=120;

height=120;

// new CreateBullet().start();//创造画出枪弹的线程

}

@Override

public void drawMe(Graphics g) {

g.drawImage(dj,x,y,width,height,null);

y+=3;//历次画完革新y坐标

if(y> P.HEIGHT){//俭朴资源反复运用每架铁鸟

y=-150;

}

/* //画出枪弹

for(int i=0;i<arrayList.size();i++){

arrayList.get(i).drawMe(g);

}*/

}

@Override

public Rectangle getRect() {

return new Rectangle(x,y,width,height);

}

/* //创造枪弹里面类

class Bullet extends GameObject{

public Bullet(){

width=60;

height=120;

}

@Override

public void drawMe(Graphics g) {//在面板上画出枪弹

g.drawImage(zd,x,y,width,height,null);

y-=3;

}

@Override

public Rectangle getRect() {

return null;

}

}

*//**

* 创造一个连接画出枪弹的线程

*//*

class CreateBullet extends Thread{

@Override

public void run() {

while(true){

try {

Bullet bullet=new Bullet();

bullet.x=x+32;

bullet.y=y+20;

arrayList.add(bullet);

Thread.sleep(10);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}*/

}

2.4战机连接展示类

package com.eneity;

import com.view.GamePanel;

import java.util.Random;

/**

* 源源不绝的创造敌机

*/

public class CreateEnemy extends Thread{

Random random =new Random();//创造随机数保护历次展示的敌机x坐标随机展示

@Override

public void run() {

while(true){

try {

EnemyPlane e= new EnemyPlane();

e.x=random.nextInt(650)+50;

e.y=-150;

GamePanel.EnemyPlaneArrayList.add(e);

Thread.sleep(300);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

3.controller遏制铁鸟挪动层

3.1PlaneController类

package com.controller;

import com.eneity.FightPlane;

import java.awt.*;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

/**

* 遏制铁鸟疏通

*/

public class PlaneController extends KeyAdapter {

FightPlane fightPlane;

public PlaneController(FightPlane fightPlane){

this.fightPlane=fightPlane;

}

@Override

public void keyPressed(KeyEvent e) {

diretion(e.getKeyCode());

}

//遏制铁鸟挪动

public void diretion(int code){

switch(code){

case 37://对应键盘左键

fightPlane.x-=15;

break;

case 38://对应键盘上键

fightPlane.y-=15;

break;

case 39://对应键盘右键

fightPlane.x+=15;

break;

case 40://对应键盘下键

fightPlane.y+=5;

break;

}

}

}

4.utils东西层

4.1铁鸟恒量类

package com.utils;

/**

* 用来设置运用到的恒量类

*/

public class P {

public static int HEIGHT=800,WEIGHT=800;

}

4.2加载图片类

package com.utils;

import javax.swing.*;

import java.awt.*;

import java.util.Objects;

/**

* 加载图片东西类

*/

public class PUtils {

public static Image getImage(String url){

ImageIcon icon=new ImageIcon(PUtils.class.getClassLoader().getResource(url));

return icon.getImage();

}

一:玩耍展现

铁鸟大战小玩耍咱们都玩过,经过挪动铁鸟来打敌机,这边给大师展现一卑劣戏功效:哇哇呜因为gif只能上传5M巨细,以是就不许给大师展现操纵了,即使大师有爱好不妨本人本人做出来再玩哟。

这内里一切的铁鸟、枪弹、敌机、爆裂功效都不妨随便替代成本人想要的图片。大师不妨大肆表现。也不妨创造无敌版。

比方我不妨把敌机替代成王八:

大师不妨随便表现哦。

二:玩耍教程

1.View视图层

名目构造:

1.1创造玩耍面板类

package com.view;

import com.controller.PlaneController;

import com.eneity.EnemyPlane;

import com.eneity.FightPlane;

import com.utils.P;

import javax.swing.*;

/**

* 窗口类

*/

public class FrameWindow {

/**

* 结构本领

*/

public FrameWindow() {

launch();

}

/**

* 创造窗口界面

*/

public void launch() {

FightPlane fightPlane = new FightPlane();

PlaneController controller = new PlaneController(fightPlane);//铁鸟遏制类

GamePanel gamePanel = new GamePanel(fightPlane);

JFrame jFrame = new JFrame();

jFrame.add(gamePanel);

jFrame.setTitle(“铁鸟大战”);

jFrame.addKeyListener(controller);//给窗口树立监听事变

jFrame.setSize(P.WEIGHT, P.HEIGHT);//树立窗口巨细

jFrame.setVisible(true);//表露在窗口

jFrame.setResizable(false);//使窗口巨细恒定

jFrame.setLocationRelativeTo(null);//初始化的界面表露的场所,null为实质自符合

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击封闭之后后盾也会封闭

}

}

1.2.创造玩耍实质表露类

package com.view;

import com.eneity.CreateEnemy;

import com.eneity.EnemyPlane;

import com.eneity.FightPlane;

import com.utils.P;

import com.utils.PUtils;

import javax.swing.*;

import java.awt.*;

import java.util.LinkedList;

import java.util.List;

import java.util.Random;

/**

* 玩耍面板

*/

public class GamePanel extends JPanel {

public static long count = 0;//祛除敌机数目

public static List<EnemyPlane> EnemyPlaneArrayList = new LinkedList<EnemyPlane>();

private FightPlane fightPlane;//战役机

public ImageIcon bz2 = new

ImageIcon(PUtils.getImage(“images/dd2.png”));

//结构本领,启用这个线程

public GamePanel(FightPlane fightPlane) {

this.fightPlane = fightPlane;

new updateGame().start();//连接重画出画布

new CreateEnemy().start();//画出10架敌机

}

boolean flage = true;

@Override

public void paint(Graphics g) {

g.setColor(Color.BLACK);

g.fillRect(0, 0, P.WEIGHT, P.HEIGHT);

g.setColor(Color.red);

g.drawString(“杀龟数目:” + count, 650, 50);

//画出战役机

if (fightPlane != null) {

fightPlane.drawMe(g);

}

//画出敌机

for (int i = 0; i < EnemyPlaneArrayList.size(); i++) {

if (EnemyPlaneArrayList.get(i).flat == 1) {

int x = EnemyPlaneArrayList.get(i).x;

int y = EnemyPlaneArrayList.get(i).y;

g.drawImage(bz2.getImage(), x, y, 150, 150, null);

EnemyPlaneArrayList.get(i).flat–;

GamePanel.count++;

System.out.println(count);

if (EnemyPlaneArrayList.get(i).flat == -3) {

int c = new Random().nextInt(650) + 50;

EnemyPlaneArrayList.get(i).x = c;

EnemyPlaneArrayList.get(i).y = -150;

}

} else {

EnemyPlaneArrayList.get(i).drawMe(g);

}

}

}

//里面类,创造一个线程不停的革新界面

class updateGame extends Thread {

@Override

public void run() {

while (flage) {

repaint();

}

}

}

}

2.enetiy实业层

2.1玩耍实业笼统类

package com.eneity;

import java.awt.*;

/**

* 设置玩耍中一切东西的父类

*/

public abstract class GameObject {

public int x,y,width,height;//设置铁鸟的坐标,长,宽

public abstract void drawMe(Graphics g);//铁鸟画本人的本领

public abstract Rectangle getRect();//获得一个矩形用来检验和测定有没有和铁鸟相撞

}

2.2战机类

package com.eneity;

import com.utils.P;

import com.utils.PUtils;

import com.view.GamePanel;

import javax.swing.*;

import java.awt.*;

import java.util.LinkedList;

import java.util.List;

/**

* 战役机

*/

public class FightPlane extends GameObject {

public List<Bullet> arrayList =new LinkedList<Bullet>();

public static Image image =PUtils.getImage(“images/zdj.png”);//铁鸟图像资源

public static Image zd=PUtils.getImage(“images/zd.png”);//枪弹图片资源

public ImageIcon bz2=new

ImageIcon(PUtils.getImage(“images/dd2.png”));

public FightPlane(){

x=300;

y=300;

width=120;

height=120;

new CreateBullet().start();//创造画出枪弹的线程

}

@Override

public void drawMe(Graphics g) {

g.drawImage(image,x,y,width,height,null);

//画出枪弹;

for(int i=0;i<arrayList.size();i++){

//检验和测定枪弹能否与敌机相撞

for(int j=0;j<GamePanel.EnemyPlaneArrayList.size();j++){

if(GamePanel.EnemyPlaneArrayList.get(j).getRect().intersects(arrayList.get(i).getRect())){

/* int x=GamePanel.EnemyPlaneArrayList.get(j).x;

int y=GamePanel.EnemyPlaneArrayList.get(j).y;

GamePanel.EnemyPlaneArrayList.remove(j);

g.drawImage(bz2.getImage(),500,500,150,150,null);*/

GamePanel.EnemyPlaneArrayList.get(j).flat=1;

}

}

if(arrayList.get(i).y<20){

arrayList.remove(i);//优化枪弹类,当枪弹到达确定场所时让其消逝

}

arrayList.get(i).drawMe(g);

}

}

@Override

public Rectangle getRect() {

return new Rectangle(x,y,width,height);

}

//创造枪弹里面类

class Bullet extends GameObject{

public Bullet(){

width=60;

height=120;

}

@Override

public void drawMe(Graphics g) {//在面板上画出枪弹

g.drawImage(zd,x,y,width,height,null);

y-=3;

}

@Override

public Rectangle getRect() {

return new Rectangle(x,y,width,height);

}

}

/**

* 创造一个连接画出枪弹的线程

*/

class CreateBullet extends Thread{

@Override

public void run() {

while(true){

try {

Bullet bullet=new Bullet();

bullet.x=x+32;

bullet.y=y+20;

arrayList.add(bullet);

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

2.3敌机类

package com.eneity;

import com.utils.P;

import com.utils.PUtils;

import java.awt.*;

import java.util.LinkedList;

import java.util.List;

/**

* 敌机类

*/

public class EnemyPlane extends GameObject {

public int flat=0;//检验和测定枪弹能否与敌机相撞

public static Image dj =PUtils.getImage(“images/wugui.png”);//铁鸟图像资源

/* public static Image zd=PUtils.getImage(“images/zd.png”);//枪弹图片资源*/

public EnemyPlane(){

x=300;

y=300;

width=120;

height=120;

// new CreateBullet().start();//创造画出枪弹的线程

}

@Override

public void drawMe(Graphics g) {

g.drawImage(dj,x,y,width,height,null);

y+=3;//历次画完革新y坐标

if(y> P.HEIGHT){//俭朴资源反复运用每架铁鸟

y=-150;

}

/* //画出枪弹

for(int i=0;i<arrayList.size();i++){

arrayList.get(i).drawMe(g);

}*/

}

@Override

public Rectangle getRect() {

return new Rectangle(x,y,width,height);

}

/* //创造枪弹里面类

class Bullet extends GameObject{

public Bullet(){

width=60;

height=120;

}

@Override

public void drawMe(Graphics g) {//在面板上画出枪弹

g.drawImage(zd,x,y,width,height,null);

y-=3;

}

@Override

public Rectangle getRect() {

return null;

}

}

*//**

* 创造一个连接画出枪弹的线程

*//*

class CreateBullet extends Thread{

@Override

public void run() {

while(true){

try {

Bullet bullet=new Bullet();

bullet.x=x+32;

bullet.y=y+20;

arrayList.add(bullet);

Thread.sleep(10);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}*/

}

2.4战机连接展示类

package com.eneity;

import com.view.GamePanel;

import java.util.Random;

/**

* 源源不绝的创造敌机

*/

public class CreateEnemy extends Thread{

Random random =new Random();//创造随机数保护历次展示的敌机x坐标随机展示

@Override

public void run() {

while(true){

try {

EnemyPlane e= new EnemyPlane();

e.x=random.nextInt(650)+50;

e.y=-150;

GamePanel.EnemyPlaneArrayList.add(e);

Thread.sleep(300);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

3.controller遏制铁鸟挪动层

3.1PlaneController类

package com.controller;

import com.eneity.FightPlane;

import java.awt.*;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

/**

* 遏制铁鸟疏通

*/

public class PlaneController extends KeyAdapter {

FightPlane fightPlane;

public PlaneController(FightPlane fightPlane){

this.fightPlane=fightPlane;

}

@Override

public void keyPressed(KeyEvent e) {

diretion(e.getKeyCode());

}

//遏制铁鸟挪动

public void diretion(int code){

switch(code){

case 37://对应键盘左键

fightPlane.x-=15;

break;

case 38://对应键盘上键

fightPlane.y-=15;

break;

case 39://对应键盘右键

fightPlane.x+=15;

break;

case 40://对应键盘下键

fightPlane.y+=5;

break;

}

}

}

4.utils东西层

4.1铁鸟恒量类

package com.utils;

/**

* 用来设置运用到的恒量类

*/

public class P {

public static int HEIGHT=800,WEIGHT=800;

}

4.2加载图片类

package com.utils;

import javax.swing.*;

import java.awt.*;

import java.util.Objects;

/**

* 加载图片东西?

版权声明:
本文内容由互联网用户自发贡献,该文观点仅代表作者本人,因此内容不代表本站观点、本站不对文章中的任何观点负责,内容版权归原作者所有、内容只用于提供信息阅读,无任何商业用途。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站(文章、内容、图片、音频、视频)有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至353049283@qq.com举报,一经查实,本站将立刻删除、维护您的正当权益。
tags:

关于我们

主题百科知识栏目每天分享日常生活小知识,互联为资讯,IT科技百科,家常知识科普等,旨在让大家快乐生活,开心学习,主题百科为您分享!

最火推荐

小编推荐

联系我们


Copyright 帝国主题之家 版权所有 TXT地图 | XML地图 | HTML地图 深圳市南山区海象营销策划工作室 备案号:粤ICP备2020139403号