使用Java编写一个走迷宫小游戏
使用Java编写一个走迷宫小游戏
一、游戏流程
1、开始界面
简陋的界面,背景是从《东方玉灵姬》中拾的【:)】
2、选择角色界面
点击窗口中左右两侧的按钮,按照文件存储顺序向上向下查找素材
3、加载页面
左下角的小人是会动的(为了证明这点就多贴了一张图片【:)】)
4、游戏界面
通过键盘方向控制角色行走方向,找到前往终点的正确路线
到达终点时触发胜利条件,弹出胜利窗口
如果不慎落入陷阱
触发失败条件,弹出失败窗口
以上就是游戏的全部流程
二、人员分工
人员 | 负责部分 |
---|---|
许文强 | 地图编辑器、人物行走动画、镜头移动、场景素材动态生成、地图读取 |
黄彦焜 | 地图编辑器的文件读入初始化接口、文件路径获取、文件读取、使用地图编辑器绘制地图 |
郭华凯 | 分割子图、拼贴子图、播放子图动画、补帧 |
陈杭宇 | 开始页面及角色选择界面的UI设计、失败触发类、胜利触发类、音乐 |
三、部分功能实现说明
- 角色行走动画功能的实现
代码:
public static void draw(Graphics g){
//如果角色不在移动中
if(!up&&!down&&!left&&!right){
if(towards==1){//如果角色移动的最后朝向为上
g.drawImage(walk2.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 0, 64*3, 64, 64*4, null);
}else if(towards==2){//最后移动朝向下
g.drawImage(walk2.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 0, 0, 64, 64, null);
}else if(towards==3){//最后移动朝向左
g.drawImage(walk2.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 0, 64, 64, 64*2, null);
}else if(towards==4){//最后移动朝向右
g.drawImage(walk2.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 0, 64*2, 64, 64*3, null);
}
}else{//如果角色在移动中
if(up){
//通过up1的值,来决定画哪一张图片
if(up1<5){
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 0, 64*3, 64, 64*4, null);
}else if(up1<10){
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 64, 64*3, 64*2, 64*4, null);
}else if(up1<15){
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 64*2, 64*3, 64*3, 64*4, null);
}else{
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 64*3, 64*3, 64*4, 64*4, null);
}
}else if(down){
if(down1<5){
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 0, 0, 64, 64, null);
}else if(down1<10){
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 64, 0, 64*2, 64, null);
}else if(down1<15){
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 64*2, 0, 64*3, 64, null);
}else{
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 64*3, 0, 64*4, 64, null);
}
}else if(left){
if(left1<5){
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 0, 64, 64, 64*2, null);
}else if(left1<10){
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 64, 64, 64*2, 64*2, null);
}else if(left1<15){
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 64*2, 64, 64*3, 64*2, null);
}else{
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 64*3, 64, 64*4, 64*2, null);
}
}else if(right){
if(right1<5){
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 0, 64*2, 64, 64*3, null);
}else if(right1<10){
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 64, 64*2, 64*2, 64*3, null);
}else if(right1<15){
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 64*2, 64*2, 64*3, 64*3, null);
}else{
g.drawImage(walk1.getImage(), Player.px-eleSize/2-15, Player.py-eleSize/2-25, Player.px-eleSize/2+65, Player.py-eleSize/2+55, 64*3, 64*2, 64*4, 64*3, null);
}
}
}
}
角色行走动画实现使用的素材:
使用drawImage方法来绘制图片,该方法允许只绘制图片的一部分(通过坐标选取)
g.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
/*img - 要绘制的指定图像。
*dx1 - 目标矩形第一个角的 x 坐标。
*dy1 - 目标矩形第一个角的 y 坐标。
*dx2 - 目标矩形第二个角的 x 坐标。
*dy2 - 目标矩形第二个角的 y 坐标。
*sx1 - 源矩形第一个角的 x 坐标。
*sy1 - 源矩形第一个角的 y 坐标。
*sx2 - 源矩形第二个角的 x 坐标。
*sy2 - 源矩形第二个角的 y 坐标。
*observer - 当缩放并转换了更多图像时要通知的对象。
*/
示意图:
素材来源:以上人物、背景素材来源于《东方玉灵姬》,场景图片素材来源于RPG Maker MV。