/home/jeky

If you take a look at the code so far, you will find out a big problem that every component in our game engine is isolated and cannot communicate with each other. In this article, we will try to introduce DI (Dependency Injection) pattern to our game engine framework. What is DI? First let us try to think about a very common scenario: you are writing class A, in which there is a method that is depending on another method in class B. What you write will be like this: Then the problem is how could we get the instance of B? We can either create a B() or inject an instance of B. The first one is considered not good enough since: You may not create an instance of B without other dependencies. B may be depending on C, D, E or something else. It is hard to test since you cannot tell the bugs are inside A or B. Although the second one is better and it is widely used in modern software architecture, it won't fit in our system. Let us take snake game for example. In this game, we hav ...
Now we have the abilities to: render images render animations control sprites This time we will try to create our first game -- SNAKE. The demo project will be split into 2 sections and this post will only contain the first one. The first thing we need to solve is how to render a snake. I will first introduce tile system here. Almost every 2D game you have played, from Super Mario to Warcraft 3, is based on tiles. The main idea of tile-based game is to split everything in game into tiles, then we can reuse them and. You may not know that the clouds and bushes are the same (just different in color) in Super Mario Bros. The developers reduced memory usage by reusing cloud tiles. Another good example is map editor in RPG Maker. Usually we will use a tileset containing a lots of different tiles to generate a map by placing them into grid. A snake game can also be a tile game. We can split the screen into several tiles and render them using different colors: red: snake head bl ...
Last time we were talking about how animation works. Basically all you need to know are how to paint and how to update. However, if you look at the demo of last article, you would find that the balls won't be removed from the list even when they fly out of game canvas. This causes a memory leak issue. So the first changes in this article is to have a method named toRemove() in Animation interface. If an animation object think itself can be removed, it will set toRemove() return true and the game context will remove it from animation list. If you are not familiar with Java 8 Stream API, it is time to learn it. It will collect all the objects of which toRemove() method return true. If you are familiar with Python, it equals to: This time we will talk about how to control the animations. We will use Sprite as the term to describe the controllable objects as in this game engine we will focusing on sprite animation (There will be an article talking about animating sprites). In Java ...
What is a game engine? It is a framework or a library. Developers can use it to create games without knowing what is behind it. You don't need to know how graphic drivers render images to screen or how to compute the speed of a dropping ball. All you need to do is to tell the engine to render images or add gravity to this ball. In this series of articles, I will show you how to write a 2D game engine in Java. I will only show the ideas and won't touch the details (such as optimization) behind. How to render images/paint in Java There are many UI libraries in Java, such as Swing, AWT, SWT or JavaFX. We will use AWT & Swing as they are easy to understand and we only need a canvas-like widget (not the Canvas.java). Here is a working example of rendering a blue rectangle: First we create a subclass of JComponent.java and override the method paintComponent() in order to have a canvas widget. We can write all the painting statements in paintComponent(). This method will be invoked once ...