您有多种选择,可以使用AndEngine(对我来说似乎非常未记录和随机),使用扩展自SurfaceView的“本地”Android游戏(虽然不是不可能,但这确实不会让您的生活变得轻松,特别是在处理图像和声音方面,但是这里有一个设置:
使用自定义SurfaceView和线程进行Android游戏编程(示例)),还有LibGDX。
我个人推荐LibGDX,我甚至在其中制作了一个相当简单的4人多人游戏,而且它确实不难。我建议您查看以下教程:
http://www.gamefromscratch.com/page/LibGDX-Tutorial-series.aspx
基础知识如下:
- 当您创建项目时,首先要做的是将
ApplicationAdapter
更改为
Game
,以便您可以访问
setScreen(Screen)
委派函数,以便您可以将游戏的显示和逻辑分离成屏幕。
- 您需要在屏幕中处理经过的时间,方法如下:
如何在Libgdx(android)中跟踪时间
- 您可能想制作一个菜单,当然可以使用漂亮的图片和BitmapFonts来完成,但我会指向官方wiki(
https://github.com/libgdx/libgdx/wiki)。您可以使用Scene2D,尽管我发现它稍微有些困难,所以我个人制作了由矩形组成的菜单,效果还不错:
LibGDX - Custom Click Listener?
- 我通过LibGDX处理触摸事件的“点击导向”指南:
https://dev59.com/NIHba4cB1Zd3GeqPMxyX#24511980
- 然后,就是实现游戏逻辑、计时器、数据模型和行为。
- 我解决拉伸问题的方法不是使用StretchingViewport或内置相机,而是采用以下方法:
public class Resources
{
public static Texture texture;
public static SpriteBatch batch;
public static Matrix4 normalProjection;
public static BitmapFont bitmapFont;
public static ShapeRenderer shapeRenderer;
....
}
public static void initialize()
{
int width = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();
Resources.bitmapFont = new BitmapFont();
Resources.shapeRenderer = new ShapeRenderer();
Gdx.gl.glLineWidth((width < 640 && height < 480) ? 2.5f : 6f);
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
loadTextures();
Resources.batch = new SpriteBatch();
Resources.normalProjection = new Matrix4().setToOrtho2D(0, 0, 480, 320);
Resources.batch.setProjectionMatrix(Resources.normalProjection);
Resources.shapeRenderer.setProjectionMatrix(Resources.normalProjection);
}
public class InputTransform
{
private static int appWidth = 480;
private static int appHeight = 320;
public static float getCursorToModelX(int screenX, int cursorX)
{
return (((float)cursorX) * appWidth) / ((float)screenX);
}
public static float getCursorToModelY(int screenY, int cursorY)
{
return ((float)(screenY - cursorY)) * appHeight / ((float)screenY) ;
}
}
请确保在游戏的dispose()回调中处理需要释放的资源。