Skip to main content

KorGE Tutorial - Writing 2048 game. Step 0 - Introduction

KorGE is an open source multiplatform game engine written in Kotlin. You can use it to develop your own games of various genres targeting JVM, Web, Android and iOS.

In this tutorial I will guide you through the steps of writing a 2048 game using KorGE and Kotlin.
You can check the resulting game here. The source code is on GitHub.


This tutorial will have several parts - steps. In them, we will discuss the following features of KorGE:
  • Views and graphics
  • Text and images
  • Positioning
  • Mouse and keyboard events
  • Animation
  • Native Storage
  • and more!
Well, let's get started!

Note: you need to install Java JDK and Intellij IDEA before start.

Creating new project

First, we need to create a new project for our game. There are two ways to do it:

1. Using KorGE Intellij plugin

KorGE provides an IntelliJ plugin, that allows you to create KorGE projects.
You can read this guide or watch this video to know how to install the plugin and create a new project via it.

2. Downloading/cloning project template

You can clone the template project from GitHub or download this archive with it.

Once we have a new project, we need to configure it for the game.

Game configuration

In the new project you will have the build.gradle.kts file that looks like this:
...
buildscript {
    ...
}
apply<KorgeGradlePlugin>()
korge {
   id = "com.example.example"
}
The korge block is the place where you can specify some information about the game. For example, let's write id and name of the game:
korge {
    id = "com.example.2048"
    name = "2048"
}
Note: you can read the full configuration description here.

Now let's go to the main function that is executed when the game is launched. It locates at src/commonMain/kotlin/main.kt. It should look like this:
suspend fun main() = Korge(width = 512, height = 512, bgcolor = Colors["#2b2b2b"]) {
    val minDegrees = (-16).degrees
    val maxDegrees = (+16).degrees
    val image = image(resourcesVfs["korge.png"].readBitmap()) {
        rotation = maxDegrees
        anchor(.5, .5)
        scale(.8)
        position(256, 256)
    }
    while (true) {
        image.tween(image::rotation[minDegrees], time = 1.seconds, easing = Easing.EASE_IN_OUT)
        image.tween(image::rotation[maxDegrees], time = 1.seconds, easing = Easing.EASE_IN_OUT)
    }
}
It's a template code. You can launch the project (the section below) and see what it does. The important part here is the Korge(...) call. It lets you specify the stage's size, background color and some other elements.

Let's define width, height, title and background color of our game. Also let's remove unnecessary stuff inside Korge(...) { ... }:
suspend fun main() = Korge(width = 480, height = 640, title = "2048", bgcolor = RGBA(253, 247, 240)) {
    // TODO: we will write code for our game here later
}

Note: There are several ways how you can define a color in korge. One of them is by specifying Int/Float/HexInt values for red, green, blue and alpha (optional) in RGBA. The other one is by specifying hex string for the whole color via Colors["#......"]. The another one - by using color name with Colors. prefix. So the four options below are equivalent:
- RGBA(0x00, 0x00, 0xFF, 0xFF)
- RGBA(0, 0, 255)
- Colors["#0000FF"]
- Colors.BLUE

After the previous changes let's launch our game and see that everything works correctly.

Game launch

Since KorGE supports several platforms, you can launch your game on any of them. There are official guides about game launch on Desktop (JVM), Web (JS), Desktop (Native), Android and iOS. I prefer to use JVM because it's the simplest and fastest way of testing a KorGE project.
In order to launch your game on JVM, you should write this line in terminal:
./gradlew runJvm
To simplify game launch, I suggest you use Intellij's interface for running configurations. There is a special drop-down element at the top of the Intellij's window:


You need to click on it and select "Edit Configurations...". You'll see the "Run/Debug Configurations" window. Now click on the "+" button at the top left of the window, select "Gradle" and specify your Gradle project and Gradle task runJvm like here:


After saving changes you will be able to launch your game by clicking the green triangle button at the top of Intellij.

At this moment, when launching the game, we'll see just a beige window with the specified size and title:


In the next part of the tutorial, we'll know how to use graphics and text views and how to position them. We'll add a logo, score views and a background for our game. Stay tuned!

Comments

  1. Very nice tutorial @RezMike! I like it

    ReplyDelete
  2. Replies
    1. The next part is here:
      https://blog.korge.org/2020/06/korge-tutorial-writing-2048-game-step-1.html
      Just published it.

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Some of the most effective UK casino sites permit withdrawals to cash wallets in minutes. You wants to|must also} think about processing occasions, cashout costs , account verification, weekend cashouts, and cashout charges. You can discover out more 카지노 사이트 information in our section on casino banking.

    ReplyDelete

Post a Comment

Popular posts from this blog

KorGE 1st Game Jam - 12-June - 14-June

Some updates about the KorGE organization: After a few years in development and lots of fixes and improvements, KorGE is getting mature. In April, Mikhail Reznichenko  and Nicolai Emig joined the KorGE organization. Mikhail Reznichenko, our member from Russia, has mostly worked on the technical part of KorGE and other korlibs and has given support at slack, he has done a remarkable work on the UI and other parts of the engine, helped with the intelliJ plugin and also created a 2048 clone as a sample of using KorGE. Nicolai Emig, our member from Germany, created an educational wrapper around KorGE and he is teaching it to his students, he has helped at slack, promoting the engine, and additionally he has contributed to the core with ideas and code including Sprite Animations and some samples to the core, he has also helped with organization of the Game JAM. In addition,  Tamara Gadea , joined as a sporadic contributor helping us some graphical assets, including the on

Korge/Korlibs already works with Kotlin-Native on MacOS

After some tries and several bug reports, Korge already works on Kotlin/Native. I have uploaded an  .app  file here showing the results. https://soywiz.com/content/images/demos/korge-dragonbones-demo-mac.app.7z For the icon I have used a WIP ilustration that is finishing my wife. Edit:   Final version of the image for mmo-poc . The sample is the same as the other article that worked on JS and JVM, but this time in native for macOS without additional dependencies or frameworks. It includes three dragonbones samples: one with simple skeletal animation, other imported from live2d that follows the cursor and has tons of computations, and other showing slot replacement from an animation imported from Spine. The only difference is that text in buttons is not shown, and the edges of the buttons are not antialiased. The K/N version uses the integrated software-based rasterizer of KorIM. This rasterizer is not optimized, and do not perform antialiasing yet. Though it should w