那晚,我突发奇想.打了一辈子游戏,就不能自己做一个(la ji)游戏?
大型游戏的天下还是Unreal Engine,Unity,Source Engine,Frostbite Engine这些老大哥们的天下.
小型一点的有Godot,Cocos,Egret等等!
难道作为java仔在游戏界就没有出头之日了嘛?打开全世界最大的同性交友网站github,搞了半天就找出这几个!
LWJGL:据说<我的世界>就是基于这个东西
LibGDX:基于LWJGL的2D引擎,也有3D,老牌引擎,口碑不错!
JME:基于LWJGL的3D
FXGL:一个大学老师的作品,API封装的很高层,入门很是舒服,但是不跨平台!
看了看可选的就这几个,最终选择LIBGDX作为学习对象!
libGDX 是一个基于 OpenGL (ES) 的跨平台 Java 游戏开发框架,适用于 Windows、Linux、macOS、Android、浏览器和 iOS。
案例有:
国外出名的有Shattered Pixel Dungeon(破碎像素地牢)Slay the Spire(*戮尖塔),其他的我也不知道呀!
国内搜索了一圈,有一款叫《召唤与合成》的卡牌养成手游就是基于这个引擎开发的,这个游戏挺小众,但是口碑不错
无论在taptap还是应用市场,评分普遍很高。
unity使用mono和c#,那么jvm和java肯定同样有潜力开发游戏引擎,虽然性能上前者组合要优于后者,但是2D场景下应该没啥问题。
还有一些国内公司在用libgdx,并且有已经发行的游戏。例如南京颂歌的《雷电2014》《全民奥特曼》,分享时代与佳游科技合作的《天天美女切水果》
当然更多的游戏可以查看官网列表 https://libgdx.com/showcase/
依葫芦画葫芦画瓢,都算创新了,我就画葫芦!按照WIKI的教程先溜一圈!
省略下一些基本的步骤!我们用gdx-setup.jar生成了一个基础项目!为了简单,我只生成了桌面和HTML的!
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
public class MyGdxGame extends ApplicationAdapter {
private Texture dropImage;
private Texture bucketImage;
private Sound dropSound;
private Music rainMusic;
private SpriteBatch batch;
private OrthographicCamera camera;
private Rectangle bucket;
private Array<Rectangle> raindrops;
private long lastDropTime;
@Override
public void create() {
// load the images for the droplet and the bucket, 64x64 pixels each
dropImage = new Texture(Gdx.files.internal("drop.png"));
bucketImage = new Texture(Gdx.files.internal("bucket.png"));
// load the drop sound effect and the rain background "music"
dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
// start the playback of the background music immediately
rainMusic.setLooping(true);
rainMusic.play();
// create the camera and the SpriteBatch
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
batch = new SpriteBatch();
// create a Rectangle to logically represent the bucket
bucket = new Rectangle();
bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
bucket.y = 20; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
bucket.width = 64;
bucket.height = 64;
// create the raindrops array and spawn the first raindrop
raindrops = new Array<Rectangle>();
spawnRaindrop();
}
private void spawnRaindrop() {
Rectangle raindrop = new Rectangle();
raindrop.x = MathUtils.random(0, 800-64);
raindrop.y = 480;
raindrop.width = 64;
raindrop.height = 64;
raindrops.add(raindrop);
lastDropTime = TimeUtils.nanoTime();
}
@Override
public void render() {
// clear the screen with a dark blue color. The
// arguments to clear are the red, green
// blue and alpha component in the range [0,1]
// of the color to be used to clear the screen.
ScreenUtils.clear(0, 0, 0.2f, 1);
// tell the camera to update its matrices.
camera.update();
// tell the SpriteBatch to render in the
// coordinate system specified by the camera.
batch.setProjectionMatrix(camera.combined);
// begin a new batch and draw the bucket and
// all drops
batch.begin();
batch.draw(bucketImage, bucket.x, bucket.y);
for(Rectangle raindrop: raindrops) {
batch.draw(dropImage, raindrop.x, raindrop.y);
}
batch.end();
// process user input
if(Gdx.input.isTouched()) {
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
bucket.x = touchPos.x - 64 / 2;
}
if(Gdx.input.isKeyPressed(Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Keys.RIGHT)) bucket.x = 200 * Gdx.graphics.getDeltaTime();
// make sure the bucket stays within the screen bounds
if(bucket.x < 0) bucket.x = 0;
if(bucket.x > 800 - 64) bucket.x = 800 - 64;
// check if we need to create a new raindrop
if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop();
// move the raindrops, remove any that are beneath the bottom edge of
// the screen or that hit the bucket. In the latter case we play back
// a sound effect as well.
for (iterator<Rectangle> iter = raindrops.iterator(); iter.hasNext(); ) {
Rectangle raindrop = iter.next();
raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
if(raindrop.y 64 < 0) iter.remove();
if(raindrop.overlaps(bucket)) {
dropSound.play();
iter.remove();
}
}
}
@Override
public void dispose() {
// dispose of all the native resources
dropImage.dispose();
bucketImage.dispose();
dropSound.dispose();
rainMusic.dispose();
batch.dispose();
}
}
左右移动桶接住雨水!一个渣渣入门小游戏完成了!
资源类放入
接下来我们慢慢更新Libgdx!暂时告别CURD玩一玩不一样的Java!
也可以搜索微信公众号:高级面试工程师
,