没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
1.
Game Development in Java
Posted on by October 31, 2020
Johan Kragt
Game development
Introduction
In my early days as a software developer I worked at a small game studio. This was
back in the days when ActionScript and Flash were still a thing.
At JCore during Corona times we’ve spent part of the JCore Fast Track looking at
game development in Unity3D and the Unreal engine. These engines work on C#
/JavaScript and C++ respectively.
Nowadays the language I’m most comfortable with is Java. A little while ago I was
wondering whether it would be possible to create a game in Java.
Of course almost all languages support some sort of drawing, so technically the
answer would be a straight yes. Modern engines and libraries offer support for
OpenGL rendering and provide a lot of tools out of the box.
Though there are 3D engines for Java I’ll be focusing on a 2D game, mostly because
the complexity of 3D and its assets makes creating anything resembling a 3D game a
daunting task.
Definitions
In this blog I will use some jargon which might need some explanation.
Texture
A texture is an image which can be loaded into the graphics card. In 3D games a
texture is applied to the surface of a 3D object to give it a certain look and feel.
In 2D games a texture is wrapped in a Sprite. A Sprite contains the bitmap
information and the location of the points defining its position and rotation point.
Sometimes behaviour of the Sprite can also be included in the definition. A sprite
can be a static image or a series of images forming an animation.
In this blog I’ll be using plain textures.
Rendering
Rendering is the process of drawing the scene onto the screen. The term is mostly
used for drawing using the graphics card.
UI
UI refers to the user interface of the game. The UI is rendered on top of the game
and contains scores, menus, buttons, etc.
The framework of choice
There are several frameworks which can be used to develop games. Most frameworks are
based on or . As far as I know all game libraries support rendering on LWJGL JOGL
graphics cards which is very important for the visual style of the game. The
graphics card is (logically) much better at rendering graphics and/by doing matrix
calculations. If you want to draw a lot of images, apply transformations and enable
lighting a CPU would be very restrictive.
While JOGL and LWJGL can be used to create 2D and 3D games, these frameworks offer
little more than a low level binding of OpenGL.
For 3D games there are some higher level engines like , which uses jMonkeyEngine
LWJGL for rendering by default but can also use JOGL, and which uses JNI to Ogre4j
connect Java to the C++ Ogre engine.
Another framework on top of LWJGL is LibGDX. LibGDX is suited for 2D and 3D game
development and includes several supporting libraries easing the creation of games.
There are other libraries like . A comprehensive list of other frameworks can Slick2D
be found . I’ve found the other frameworks that I tried more cumbersome to work here
with, both in the setup and the APIs they offer.
Because LibGDX is very easy to setup, offers just the right abstraction to retain
flexibility and can also be used for 3D game development I’ve chosen LibGDX for
this blog.
The Game
Ever had a or no connection at all?BadConnection
If you’re using Chrome you might be familiar with the game it offers to pass the
time until your internet returns.
This is the game I’ll be recreating in LibGDX. The game is extremely simple, but it
can be used to demo several of the libraries LibGDX offers.
During the blog I’ll show you almost all the code. I’ve only omitted some
unimplemented methods from interfaces in the examples. I’ll revisit existing
classes to add new features.
The full project is available on .git
Setup
LibGDX provides a tool to generate a Gradle based project: https://libgdx.
badlogicgames.com/download.html
When running it, it shows a screen to configure the game project.
After generation it will create a project with the following structure. The
generated project is based on Java 7, which might also result in some errors while
generating the project with a newer JDK installed. This wasn’t a problem for my
project.
In this blog I would like to introduce you to the framework and the supporting
libraries.
Follow along?
If you’d like to follow along, download the following zip file and place its
contents in the core/assets directory.
GameAssets.zip
I’ve used Java 11. Change in the build.gradle files in the sourceCompatibility = 11
generated project. You can probably use any Java version above 7, if you match the
sourceCompatibility.
Launching the game is done through the . Since I like the game to be DesktopLauncher
fullscreen I’ve made a minor config change to the class.
Listing 1. DesktopLauncher.java
package nl.jdriven.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import nl.jdriven.BadConnection;
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.height = 1050; (1)
config.fullscreen = true;
new LwjglApplication(new BadConnection(), config);
}
}
1 Set the height of the screen to 1050 pixels, and start the game fullscreen.
All other development in the blog will be done in the project.core
Screens
Supporting several screens
The generated class extends . To support screens, it BadConnection ApplicationAdapter
needs to extend from . In LibGDX a supports screens and will defer Game Game
rendering to the current screen. I’ve added a menu screen and a game screen. When
the game is created it should immediately enter the menu screen.
The game will be using system resources not controlled by the JVM. An example of
this is a texture currently rendered to the screen. This texture will be loaded into
the memory of the graphics card. To correctly release these resources you can’t put
your trust in the Java garbage collector. Before the game exits it will call the
method. This call must be propagated to all assets that implement the dispose
interface.Disposable
Listing 2. MenuScreen.java
package nl.jdriven.screens;
import nl.jdriven.BadConnection;
import com.badlogic.gdx.Screen;
public class MenuScreen implements Screen {
private BadConnection badConnection;
public MenuScreen(BadConnection badConnection) {
this.badConnection = badConnection;
}
// methods from interface Screen
}
剩余46页未读,继续阅读
资源评论
北极象
- 粉丝: 1w+
- 资源: 345
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功