java小游戏制作(maxfo)

1 篇文章 0 订阅

大二暑假期间看完java紫皮书上的知识点后,陆续模拟了贪吃蛇,和飞机大战小游戏后,开始了自己的java小游戏制作。

video

maxfo

工具

编辑:idea
绘图:PS RGB-Maker

主要逻辑

通过所有管卡,收集五把通关钥匙(!不解救公主系列)

整体思路

1.构建窗体
2.开启线程(刷新窗体,双缓冲解决闪烁,后续会引进帧的概念)
2.角色生成(让角色动起来)
3.碰撞检测(与碰撞物做测试)
4.发射子弹(子弹的创建与销毁)
5.敌人出场 (敌人的创建与销毁)
6.绘制地图 地图读取(动态添加障碍物)
7.地图卷动(地图上可见物品都要卷起来)
8.丰富功能:技能(技能特效,特效类对象的创建与销毁),装备(装备拾取,装备装备),道具(消耗品,拾取与使用),商店(购买物品)
9.丰富逻辑:怪物生成条件,进入下一关卡条件,胜利条件
10.游戏优化:(多对象会造成游戏卡顿)

所用知识

  1. 窗体类
  2. 线程
  3. 面向对象
  4. 对象的创建与销毁
  5. 碰撞检测
  6. 地图卷动
  7. 数组
  8. 集合
  9. 键盘类
  10. 其他

游戏目录结构

在这里插入图片描述

背景地图绘制

底板层(草地+墙体)

RGB-maker 工具完成(不需要地图的跳过)
在这里插入图片描述

修饰层(花草+树+标记)

ps装饰底板
在这里插入图片描述

修饰层(树)

ps绘制出单一树的图层
在这里插入图片描述

最终效果

注意人物位置

在这里插入图片描述

障碍物生成

map.txt 文件(1为碰撞物,0为可移动区域)
在这里插入图片描述
文件读写(如下图所示,红色方块为碰撞物)

 //调用地图读取
{
       try {
           createSquare();
       } catch (Exception e) {
           e.printStackTrace();
       }
 }
 public void readGetMap() throws Exception {
        FileInputStream fis = new FileInputStream("src/map.txt");
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);

        for (String value = br.readLine(); value != null; value = br.readLine()) {
            lineData.add(value);
        }
        // 关闭输入流
        br.close();
        //  确定行高
        int row = lineData.size();
        int cloum = 0;
        // 读取一行 确定列数
        for (int i = 0; i < 1; i++) {
            String str = lineData.get(i);
            //用 , 分割 str 字符串,并且将分割后的字符串数组赋值给 buff。
            String[] values = str.split(",");
            cloum = values.length;
        }
        //行列清楚表明
        originData = new int[row][cloum];
        //将读到的字符转换为整数,并赋值给二维数组map
        for (int i = 0; i < lineData.size(); i++) {
            String str = lineData.get(i);
            String[] values = str.split(",");
            for (int j = 0; j < values.length; j++) {
                originData[i][j] = Integer.parseInt(values[j]);
            }
        }
    }
    //地图矩形绘制
    public void createSquare() throws Exception {
        // 读取地图
        readGetMap();
        for (int i = 0; i < originData.length; i++) {
            for (int j = 0; j < originData[0].length; j++) {
                if (originData[i][j] == 1) {
                    wallList.add(new Square(x + j * 32, y + 32 * i, 32, 32, rectangle));
                }
            }
        }
    }

地图卷动与碰撞检测

地图卷动

卷动对象:背景图,墙体,道具(商店,技能栏,工具栏,掉落物品) 血条 敌人 子弹 子弹爆炸特效
卷动逻辑:以角色为参照物,所有物品参照角色位置进行卷动,下图是禁止标记卷动失败的案例(卷动方向错误导致禁止对象位置出现了偏差)
请添加图片描述

碰撞检测

1.静态物体碰撞检测
静态碰撞检测处理移动中角色与各个静态道具的碰撞检测(如上图红色为墙体,角色与墙体之间的碰撞检测)
2.动态物体碰撞检测
主要解决移动中角色与移动中怪物的碰撞检测如何处理(怪物与角色相向而行的情况)
详细内容查看代码

对象的生成与销毁

GameObject类(核心)
游戏中所有的物体的父类(如下图)

package com.fan.games;

import java.awt.*;
import java.awt.Image;

/**
 * 游戏物体
 */
public class GameObject {
    // 坐标
    int x, y;
    //宽跟高
    int width, height;
    // 图像
    Image img;
    //游戏物体默认方向
    public Direction direction;
    // 矩形
    Rectangle rectangle;

    /**
     * 构造方法
     */
    public GameObject() {
    }

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

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

    public GameObject(int x, int y, Image img) {
        this.x = x;
        this.y = y;
        this.img = img;
    }
    //墙体
    public GameObject(int x, int y, int width, int height, Rectangle rectangle) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.rectangle = rectangle;
    }
    //掉落道具
    public GameObject(int x, int y, int width, int height,double hp,double mp, Image img,Rectangle rectangle) {
        this.x = x;
        this.y = y;
        this.img=img;
        this.width = width;
        this.height = height;
        this.rectangle = rectangle;
    }
    public GameObject(int x, int y, int width, int height, Rectangle rectangle, Direction direction) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.rectangle = rectangle;
        this.direction = direction;
    }

    //拥有默认方向的构造函数
    public GameObject(int x, int y, int width, int height, Image img, Rectangle rectangle,Direction direction) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.img = img;
        this.rectangle = rectangle;
        this.direction = direction;
}

    //物体矩形获取
    public Rectangle getRect() {
         return new Rectangle( x,  y, width, height);
    }
    //修改这个矩形物体
    public void setRect(int x , int y,int width , int height) {
        this.rectangle = new Rectangle( x, y, width, height);
    }
    /**
     * get/set
     * @return
     */
    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return width;
    }

    public Image getImg() {
        return img;
    }

    public Rectangle getRectangle() {
        return rectangle;
    }

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

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

    public void setWidth(){

    }
    public void setHeight(){

    }
    public void setImg(Image img) {
        this.img = img;
    }

    public void setRectangle(Rectangle rectangle) {
        this.rectangle = rectangle;
    }

    /**
     * 绘制
     *
     * @param g
     */
    public void drawSelf(Graphics g) {
        g.drawImage(img, x, y, null);
    }

}

主要对象的创建与销毁逻辑(核心)
静态资源:开始创建,结束销毁
动态资源
1.角色:开始时生成,血量为零时销毁,并游戏结束。
2.敌人 :开始时生成第一波敌人,通关关卡后,下一个管卡的怪物会被创建,以此类推。怪物血量为零时销毁
3.消耗品:开始时创建与野怪死亡时创建,使用后销毁。
4.子弹:
角色:创建{角色按下攻击键创建} 销毁{1.达到一定距离 2.碰撞敌人 3.碰撞墙体}
敌人:创建{根据敌人状态随机创建} 销毁{1.碰撞墙体 2.碰撞角色}
5.打击效果:碰撞时创建,短时间自动销毁
6.持续伤害效果:碰撞时创建,受击者死亡销毁,短时间内自动销毁。

游戏一览

开始界面
在这里插入图片描述
关卡2
在这里插入图片描述
死亡
在这里插入图片描述

关于游戏

游戏名称:maxfo(我个人的游戏id名称)
制作周期:40天(基础知识18天,制作22天)
背景音乐:Hawaiian Sun(light version)
备选音乐:ひやむぎ、そーめん、时々うどん
绘图工具:PS RGB-maker
剪辑工具:剪映
补充:因为时间的原因,游戏还有待优化。本篇文章只写入了写小游戏时比较重要的一些逻辑,提供了一些思路,详细看代码。
github源码下载

  • 21
    点赞
  • 102
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一缕阳光@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值