Java来做马里奥 1 —木叶传承
继续前两天的博客内容,接着写Java中的ACT游戏实现,本来记得手里有C++版马里奥的角色和地图的,突然找不到丢那里了,凑活ps个GBA火影的图代替下……
运行效果如下:

此次重点演示了角色和地图的绘制……其实看过我以前写JAVA的RPG开发blog文章的早知道怎么弄的了……所以此次只帖代码……
Role.java:
package org.test.mario;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

import org.loon.framework.game.image.Bitmap;


/** *//**
* <p>
* Title: LoonFramework
* </p>
* <p>
* Description:角色描述及绘制用类
* </p>
* <p>
* Copyright: Copyright (c) 2008
* </p>
* <p>
* Company: LoonFramework
* </p>
*
* @author chenpeng
* @email:[email protected]
* @version 0.1
*/

public class Role ...{

private double _x;

private double _y;

private double _vx;

private double _vy;

private boolean isFlat;

private int _dir;

private int _count;

final static private Image role = new Bitmap("./role.gif").getImage();

private Map _map;

final static public int WIDTH = 40;

final static public int HEIGHT = 40;

final static private int SPEED = 6;

final static private int JUMP_SPEED = 16;

final static private int RIGHT = 0;

final static private int LEFT = 1;


public Role(double _x, double _y, Map _map) ...{
this._x = _x;
this._y = _y;
this._map = _map;
_vx = 0;
_vy = 0;
isFlat = false;
_dir = RIGHT;
_count = 0;

AnimationThread thread = new AnimationThread();
thread.start();
}


public void stop() ...{
_vx = 0;
}


public void left() ...{
_vx = -SPEED;
_dir = LEFT;
}


public void right() ...{
_vx = SPEED;
_dir = RIGHT;
}


public void jump() ...{

if (isFlat) ...{
_vy = -JUMP_SPEED;
isFlat = false;
}
}


public void update() ...{
//0.6为允许跳跃的高度限制,反值效果
_vy += 0.6;

double newX = _x + _vx;

Point tile = _map.getTileHit(this, newX, _y);

if (tile == null) ...{
_x = newX;

} else ...{

if (_vx > 0) ...{

_x = Map.tilesToPixels(tile.x) - WIDTH;

} else if (_vx < 0) ...{
_x = Map.tilesToPixels(tile.x + 1);
}
_vx = 0;
}

double newY = _y + _vy;
tile = _map.getTileHit(this, _x, newY);

if (tile == null) ...{
_y = newY;
isFlat = false;

} else ...{

if (_vy > 0) ...{
_y = Map.tilesToPixels(tile.y) - HEIGHT;
_vy = 0;
isFlat = true;

} else if (_vy < 0) ...{
_y = Map.tilesToPixels(tile.y + 1);
_vy = 0;
}
}
}


public void draw(Graphics g, int offsetX, int offsetY) ...{
g.drawImage(role, (int) _x + offsetX, (int) _y + offsetY, (int) _x
+ offsetX + WIDTH, (int) _y + offsetY + HEIGHT, _count * WIDTH,
_dir * HEIGHT, _count * WIDTH + WIDTH, _dir * HEIGHT + HEIGHT,
null);
}


public double getX() ...{
return _x;
}


public double getY() ...{
return _y;
}


private class AnimationThread extends Thread ...{

public void run() ...{

while (true) ...{

if (_count == 0) ...{
_count = 1;

} else if (_count == 1) ...{
_count = 0;
}


try ...{
Thread.sleep(300);

} catch (InterruptedException e) ...{
e.printStackTrace();
}
}
}
}

}
Map.java:
package org.test.mario;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

import org.loon.framework.game.image.Bitmap;


/** *//**
* <p>
* Title: LoonFramework
* </p>
* <p>
* Description:地图绘制及描述用类
* </p>
* <p>
* Copyright: Copyright (c) 2008
* </p>
* <p>
* Company: LoonFramework
* </p>
*
* @author chenpeng
* @email:[email protected]
* @version 0.1
*/

public class Map ...{

// 在以前的blog文章中我介绍过,游戏开发中通常以数组描述地图
// 此处1描绘为一个障碍物,0描绘为一个可通行空间
final static public int TILE_SIZE = 32;

final static public int ROW = 20;

final static public int COL = 30;

final static public int WIDTH = TILE_SIZE * COL;

final static public int HEIGHT = TILE_SIZE * ROW;

final static public double GRAVITY = 0.6;

// 地图描述

final static private int[][] map = ...{

...{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2 },

...{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1 },

...{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
运行效果如下:
此次重点演示了角色和地图的绘制……其实看过我以前写JAVA的RPG开发blog文章的早知道怎么弄的了……所以此次只帖代码……
Role.java:
Map.java: