开发推广

手游任务脚本之java开发游戏脚本

开发技术动态随笔 ·

手游方面的开发要用到的工具毫无疑问要用到java了,好了不多说,直接上代码,对于开发手游辅助任务类功能兴趣的可以看下;
手游任务脚本之java开发游戏脚本

飞行器大战脚本

所有物体的父物体"GameObject",然后就是一堆物体继承于他,等到他的属性于函数。
窗口组件
工具脚本(负责做一些杂事和存放图片)
package object;

import utils.GameWindow;

import java.awt.*;

public class GameObject {

public Image image;
public int x;
public int y;
public int width;
public int height;
public double speed;
public GameWindow fram;


public Image getImage() {
    return image;
}

public void setImage(Image image) {
    this.image = image;
}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}

public int getWidth() {
    return width;
}

public void setWidth(int width) {
    this.width = width;
}

public int getHeight() {
    return height;
}

public void setHeight(int height) {
    this.height = height;
}

public double getSpeed() {
    return speed;
}

public void setSpeed(double speed) {
    this.speed = speed;
}

public GameWindow getFram() {
    return fram;
}

public void setFram(GameWindow fram) {
    this.fram = fram;
}

public GameObject(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
    this.image = image;
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.speed = speed;
    this.fram = fram;
}

public GameObject(Image image, int x, int y, double speed) {
    this.image = image;
    this.x = x;
    this.y = y;
    this.speed = speed;
}

public GameObject(int x, int y, int width, int height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
}

public GameObject(int x, int y) {
    this.x = x;
    this.y = y;
}

public GameObject() {//无参构造

}

public void paintSelf(Graphics gImage){
    gImage.drawImage(image,x,y,null);
}
public Rectangle getRec(){
    return new Rectangle(x,y,width,height);
}

}

背景

package object;

import utils.GameWindow;

import java.awt.*;

public class bgObj extends GameObject {

public bgObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
    super(image, x, y, width, height, speed, fram);
}

public bgObj(Image image, int x, int y, double speed) {
    super(image, x, y, speed);
}

public bgObj() {
    super();
}

@Override
public Image getImage() {
    return super.getImage();
}

@Override
public void paintSelf(Graphics gImage) {
    super.paintSelf(gImage);
    //y+=speed;
}

}

玩家(角色)

package object;

import utils.GameWindow;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Plane extends GameObject{

@Override
public Image getImage() {
    return super.getImage();
}

public Plane(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
    super(image, x, y, width, height, speed, fram);
    fram.addMouseMotionListener(new MouseAdapter() {
        @Override
        public void mouseMoved(MouseEvent e) {//跟随鼠标移动
            Plane.super.x=e.getX()-11;//鼠标位置减去飞机位置的一半相当于鼠标在飞机中间
            Plane.super.y=e.getY()-16;
        }
    });
}

public Plane(Image image, int x, int y, double speed) {
    super(image, x, y, speed);
}

public Plane() {
    super();
}

@Override
public void paintSelf(Graphics gImage) {
    gImage.drawImage(image,x,y,width,height,null);
    //与敌方boss碰撞死亡
    if (this.fram.bossObj!=null&&this.getRec().intersects(this.fram.bossObj.getRec())){
        this.fram.state=3;
        this.fram.repaint();
    }
}

@Override
public Rectangle getRec() {
    return super.getRec();
}

}

击打物

package object;

import utils.GameUtils;
import utils.GameWindow;

import java.awt.*;

public class shellObj extends GameObject{

@Override
public Image getImage() {
    return super.getImage();
}

public shellObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
    super(image, x, y, width, height, speed, fram);
}

public shellObj() {
    super();
}

@Override
public void paintSelf(Graphics gImage) {
    gImage.drawImage(image,x,y,width,height,null);
    y-=speed;
    if (y<0){
        this.x=100;
        this.y=-100;
        GameUtils.removeObjList.add(this);
    }
}

@Override
public Rectangle getRec() {
    return super.getRec();
}

}

击打对方飞行物

package object;

import utils.GameUtils;
import utils.GameWindow;

import java.awt.*;

public class enemyObj extends GameObject{

@Override
public Image getImage() {
    return super.getImage();
}

public enemyObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
    super(image, x, y, width, height, speed, fram);
}

public enemyObj() {
    super();
}

@Override
public void paintSelf(Graphics gImage) {
    gImage.drawImage(image,x,y,width,height,null);
    y+=speed;
    if (this.getRec().intersects(this.fram.planeObj.getRec())){
        GameWindow.state=3;
        this.fram.repaint();//重画一帧
    }
    for (shellObj item: GameUtils.shellObjList) {
        if (this.getRec().intersects(item.getRec())){
            ExplodeObj obj=new ExplodeObj(x,y,40,40);
            GameUtils.explodeObjList.add(obj);
            GameUtils.removeObjList.add(obj);
            item.setX(100);
            item.setY(-100);
            this.x=-100;
            this.y=-100;
            GameUtils.removeObjList.add(item);
            GameUtils.removeObjList.add(this);
            GameWindow.score++;
        }
    }
    if (y>600){
        this.x=-100;
        this.y=-100;
        GameUtils.removeObjList.add(this);
    }
}

@Override
public Rectangle getRec() {
    return super.getRec();
}

}

对方"高血量"分析物(可以理解为boss)

package object;

import utils.GameUtils;
import utils.GameWindow;

import java.awt.*;

public class BossObj extends GameObject{

int life=10;
@Override
public Image getImage() {
    return super.getImage();
}

public BossObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
    super(image, x, y, width, height, speed, fram);
}

public BossObj() {
    super();
}

@Override
public void paintSelf(Graphics gImage) {
    gImage.drawImage(image,x,y,width,height,null);
    if (x>485||x<-40){
        speed=-speed;
    }
    x+=speed;
    for (shellObj item: GameUtils.shellObjList) {
        if (this.getRec().intersects(item.getRec())){
            item.setX(100);
            item.setY(-100);
            GameUtils.removeObjList.add(item);
            life--;
        }
        if (life<=0){
            GameUtils.removeObjList.add(this);
            GameWindow.state=4;
            this.fram.repaint();//重画一帧
        }
    }
    //血条的白色背景
    gImage.setColor(Color.white);
    gImage.fillRect(20,40,100,10);
    //红血条的绘制
    gImage.setColor(Color.red);
    gImage.fillRect(20,40,life*100/10,10);
}

@Override
public Rectangle getRec() {
    return super.getRec();
}

}

对方发射的击打物

package object;

import utils.GameUtils;
import utils.GameWindow;

import java.awt.*;

public class BulletObj extends GameObject{

@Override
public Image getImage() {
    return super.getImage();
}

public BulletObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
    super(image, x, y, width, height, speed, fram);
}

public BulletObj() {
    super();
}

@Override
public void paintSelf(Graphics gImage) {
    gImage.drawImage(image,x,y,width,height,null);
    y+=speed;
    if (this.getRec().intersects(this.fram.planeObj.getRec())){
        this.fram.state=3;
        this.fram.repaint();
    }
    if (y>600){
        this.x=-300;
        this.y=-300;
        GameUtils.removeObjList.add(this);
    }
}

@Override
public Rectangle getRec() {
    return super.getRec();
}

}

击打对方实现的爆裂效果

package object;

import utils.GameUtils;
import utils.GameWindow;

import java.awt.*;

public class ExplodeObj extends GameObject {

//调用的帧数
int count=0;

Image explode=Toolkit.getDefaultToolkit().getImage("Images/boom.gif");
@Override
public Image getImage() {
    return super.getImage();
}

public ExplodeObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
    super(image, x, y, width, height, speed, fram);
}

public ExplodeObj(int x, int y) {
    super(x, y);
}

public ExplodeObj() {
    super();
}

public ExplodeObj(int x, int y, int width, int height) {
    super(x, y, width, height);
}

@Override
public void paintSelf(Graphics gImage) {
    if (count<16){
        super.image=explode;
        gImage.drawImage(image,x,y,width,height,null);//要可以设置大小
        count++;
    }
    //super.paintSelf(gImage);
}

@Override
public Rectangle getRec() {
    return super.getRec();
}

}

工具类

package utils;

import object.*;

import javax.tools.Tool;

import java.util.*;
import java.awt.*;
import java.util.List;

public class GameUtils {

//背景图片
public static Image bgImage = Toolkit.getDefaultToolkit().getImage("Images/backGroup.jpg");
//BOSS图片
public static Image bossImage = Toolkit.getDefaultToolkit().getImage("Images/boss.png");
//动态爆裂图片
public static Image boomImage = Toolkit.getDefaultToolkit().getImage("Images/boom.gif");
//己方飞行物爆裂图片
public static Image boom_2Image = Toolkit.getDefaultToolkit().getImage("Images/boom2.png");
//对方飞行物爆裂图片
public static Image boom_3Image = Toolkit.getDefaultToolkit().getImage("Images/boom3.png");
//己方飞行物
public static Image planeImage=Toolkit.getDefaultToolkit().getImage("Images/plane2.png");
//己方击打物
public static Image shellImage=Toolkit.getDefaultToolkit().getImage("Images/shell.png");
//对方飞行物
public static Image enemyImage=Toolkit.getDefaultToolkit().getImage("Images/enemy.png");
//对方击打物
public static Image bulletImage=Toolkit.getDefaultToolkit().getImage("Images/bullet.png");
//物体的集合
public static List<GameObject> gameObjList=new ArrayList<>();
//击打物的集合
public static List<shellObj> shellObjList=new ArrayList();
//对方飞行物集合
public static List<enemyObj> enemyObjList=new ArrayList();
//Boss击打物的集合
public static List<BulletObj> bulletObjList=new ArrayList();
//需要移除的目标的集合
public static List<GameObject> removeObjList=new ArrayList<>();
//对方飞行物爆裂的集合
public static List<ExplodeObj> explodeObjList=new ArrayList<>();

//写字
public static void DrawWord(Graphics gGrafics,String word,Color color,int size,int x,int y){
    gGrafics.setColor(color);//设置字体颜色
    gGrafics.setFont(new Font("仿宋",Font.BOLD,size));
    gGrafics.drawString(word,x,y);
}

}

窗口类

package utils;

import object.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GameWindow extends JFrame {

//0未开始,1游戏中,2暂停,3通关失败,4,通关成功
public static int state=0;
//分数
public static int score=0;
//生成多少飞机后生成boss
int num=100;
int width=600;
int height=600;
//画布重画的次数
int count=1;
//敌机生成数目
int enemy_count=0;
//开双缓存的图片
Image offImage=null;
//背景
bgObj bgOb =new bgObj(GameUtils.bgImage,0,0,2);
//玩家飞机
public Plane planeObj =new Plane(GameUtils.planeImage,100,100,30,40,0,this);
//Boss
public BossObj bossObj=null;
public void Start(){
    this.setVisible(true);
    this.setSize(width,height);//大小
    this.setLocationRelativeTo(null);//设置在屏幕中间
    this.setTitle("飞机大战之Boss决战篇");
    //关掉后清理缓存
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //添加一个监听器MouseAdapter,然后重写他的方法
    this.addMouseListener(new MouseAdapter(){
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton()==1&&state==0){
                state=1;
                repaint();
            }
        }
    });
    //添加一个键盘的监听事件
    this.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode()==' '){//按下空格就暂停,暂停就相当于不再重画画布
                switch (state){
                    case 1:
                        state=2;
                        break;
                    case 2:
                        state=1;
                        break;
                    default:
                }
            }
        }
    });
    GameUtils.gameObjList.add(bgOb);
    GameUtils.gameObjList.add(planeObj);

    while (true){
        if (state==1||state==0){
            if (state==1){
                creatGameObj();
            }
            repaint();
        }
        try {
            Thread.sleep(25);//每秒调用40次
        } catch (InterruptedException ex) {
            System.out.println("图片解析出错,错误原因:"+ex.getMessage());
        }
    }
}

@Override
public void paint(Graphics g) {
    if (offImage==null){
        offImage=createImage(width,height) ;
    }
    Graphics gGraphic=offImage.getGraphics();//使用两张图片用双缓存来画防止闪烁
    gGraphic.fillRect(0,0,width,height);
    //游戏未开始时绘图
    if (state==0){
        try {
            MediaTracker mediaTracker = new MediaTracker(new Container());
            mediaTracker.addImage(GameUtils.bgImage, 0);
            mediaTracker.addImage(GameUtils.bossImage, 1);
            mediaTracker.addImage(GameUtils.boomImage, 2);
            mediaTracker.waitForAll();
            gGraphic.drawImage(GameUtils.bgImage,0,0,null);//让图片加载完以后再显示
            gGraphic.drawImage(GameUtils.bossImage,225,100,150,150,null);//boss
            gGraphic.drawImage(GameUtils.boomImage,250,380,100,100,null);//爆炸
            GameUtils.DrawWord(gGraphic,"点击此处游戏开始",Color.blue,35,170,320);//文字标识
        } catch (InterruptedException ex) {
            System.out.println("图片解析出错,错误原因:"+ex.getMessage());
        }
    }
    //游戏开始时绘图
    else if (state==1){
        GameUtils.gameObjList.addAll(GameUtils.explodeObjList);
        //将物件集合中所有东西画出
        for (int i=0;i<GameUtils.gameObjList.size();i++){
            GameUtils.gameObjList.get(i).paintSelf(gGraphic);
        }
        //分数显示
        GameUtils.DrawWord(gGraphic,"得分: "+score+" 分",Color.green,30,30,100);//文字标识
        //将所有要移除的物件移除
        GameUtils.gameObjList.removeAll(GameUtils.removeObjList);
    }
    //暂停
    else if (state==2){
        //不做任何事就是暂停
    }
    //游戏结束
    else if (state==3){
        try {
            //让场景保持不变
            for (int i=0;i<GameUtils.gameObjList.size();i++){
                GameUtils.gameObjList.get(i).paintSelf(gGraphic);
            }
            //额外画出一些字
            MediaTracker mediaTracker = new MediaTracker(new Container());
            mediaTracker.addImage(GameUtils.boom_2Image, 0);
            mediaTracker.waitForID(0);
            gGraphic.drawImage(GameUtils.boom_2Image,planeObj.getX()-20, planeObj.getY()-15, null);//爆炸
            GameUtils.DrawWord(gGraphic,"菜",Color.red,80,270,320);//文字标识
        } catch (InterruptedException ex) {
            System.out.println("图片解析出错,错误原因:"+ex.getMessage());
        }
    }
    //游戏通关
    else if (state==4){
        try {
            //先让不该出现的东西消失
            GameUtils.gameObjList.removeAll(GameUtils.removeObjList);
            //让场景保持不变
            for (int i=0;i<GameUtils.gameObjList.size();i++){
                GameUtils.gameObjList.get(i).paintSelf(gGraphic);
            }
            //额外画出一些字
            MediaTracker mediaTracker = new MediaTracker(new Container());
            mediaTracker.addImage(GameUtils.boom_3Image, 0);
            mediaTracker.waitForID(0);
            gGraphic.drawImage(GameUtils.boom_3Image,bossObj.getX()+30, bossObj.getY(), null);//爆炸
            GameUtils.DrawWord(gGraphic,"虽然你赢了,但你还是菜",Color.red,30,140,320);//文字标识
        } catch (InterruptedException ex) {
            System.out.println("图片解析出错,错误原因:"+ex.getMessage());
        }
    }
    g.drawImage(offImage,0,0,null);
    count++;
}

void creatGameObj(){
    //我方子弹的生成
    if (count%15==0){
        GameUtils.shellObjList.add(new shellObj(GameUtils.shellImage,planeObj.getX()+8, planeObj.getY()-16,14,29,5,this));
        GameUtils.gameObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size()-1));
    }
    //敌机的生成
    if (count%15==0){
        GameUtils.enemyObjList.add(new enemyObj(GameUtils.enemyImage,(int)(Math.random()*12)*50, 0,49,36,5,this));
        GameUtils.gameObjList.add(GameUtils.enemyObjList.get(GameUtils.enemyObjList.size()-1));
        enemy_count++;
    }
    //敌方子弹的生成
    if (bossObj!=null&&count%15==0){
        GameUtils.bulletObjList.add(new BulletObj(GameUtils.bulletImage,bossObj.getX()+76, bossObj.getY()+85,14,29,5,this));
        GameUtils.gameObjList.add(GameUtils.bulletObjList.get(GameUtils.bulletObjList.size()-1));
    }
    if (enemy_count>num&&bossObj==null){
        bossObj=new BossObj(GameUtils.bossImage,250,35,155,100,5,this);
        GameUtils.gameObjList.add(bossObj);
    }
}

}

主体

import object.*;
import utils.*;
public class PlanWarMain {

public static void main(String[] args) {
    GameWindow gWin=new GameWindow();
    gWin.Start();
}

}

手游任务脚本之java开发游戏脚本