0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Diamond Hunter – Java 2D Game

0
Posted at

Introduction

There’s something incredibly satisfying about building a game without a game engine.
No Unity. No Unreal.
Just Java, Swing, and your own logic.
When I found this project — a 2D tile-based game from scratch — I knew I had to try it.
It’s simple on the surface:
move a character
collect diamonds
avoid enemies

But underneath, it teaches everything that matters:
game loops, rendering, input, and basic architecture.
This tutorial by ForeignGuyMike is a great starting point:
👉 Diamond Hunter – Java 2D Game (with source code)

🧩 0. What you’ll build

A small 2D game with:

  • Player movement (up/down/left/right)
  • Tile-based map
  • Collectible diamonds
  • Enemies
  • Simple collision system
  • Camera that follows player

🛠️ 1. Setup your environment

Install tools

Install Java JDK (17 or newer)
Install IntelliJ IDEA (recommended) or VS Code
Create a new Java Project

📁 2. Project structure

Create this folder structure:

src/
 ├── main/
 │    ├── GamePanel.java
 │    ├── KeyHandler.java
 │    ├── Main.java
 ├── entity/
 │    ├── Player.java
 ├── tile/
 │    ├── Tile.java
 │    ├── TileManager.java

🧠 3. Game loop (core engine)

Main.java

public class Main {
    public static void main(String[] args) {
        new GamePanel();
    }
}

GamePanel.java (window + loop)

import javax.swing.*;
import java.awt.*;

public class GamePanel extends JPanel implements Runnable {

    final int tileSize = 48;
    final int screenCol = 16;
    final int screenRow = 12;

    Thread gameThread;

    public GamePanel() {
        this.setPreferredSize(new Dimension(tileSize * screenCol, tileSize * screenRow));
        this.setBackground(Color.BLACK);
        this.setDoubleBuffered(true);
    }

    public void startGameThread() {
        gameThread = new Thread(this);
        gameThread.start();
    }

    @Override
    public void run() {
        while (gameThread != null) {
            update();
            repaint();
        }
    }

    public void update() {
        // update player, enemies
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // draw world
    }
}

🎮 4. Player movement

KeyHandler.java

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyHandler implements KeyListener {

    public boolean up, down, left, right;

    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();

        if (code == KeyEvent.VK_W) up = true;
        if (code == KeyEvent.VK_S) down = true;
        if (code == KeyEvent.VK_A) left = true;
        if (code == KeyEvent.VK_D) right = true;
    }

    public void keyReleased(KeyEvent e) {
        int code = e.getKeyCode();

        if (code == KeyEvent.VK_W) up = false;
        if (code == KeyEvent.VK_S) down = false;
        if (code == KeyEvent.VK_A) left = false;
        if (code == KeyEvent.VK_D) right = false;
    }

    public void keyTyped(KeyEvent e) {}
}

Player.java

public class Player {

    int x = 100;
    int y = 100;
    int speed = 4;

    KeyHandler keyH;

    public Player(KeyHandler keyH) {
        this.keyH = keyH;
    }

    public void update() {
        if (keyH.up) y -= speed;
        if (keyH.down) y += speed;
        if (keyH.left) x -= speed;
        if (keyH.right) x += speed;
    }
}

🧱 5. Tile system (map rendering)
Tile.java

import java.awt.image.BufferedImage;

public class Tile {
    BufferedImage image;
    boolean collision = false;
}

TileManager.java
Handles:

  • Loading tile images
  • Drawing map
  • Collision tiles

Concept:

int[][] map = {
    {1,1,1,1},
    {1,0,0,1},
    {1,0,0,1},
    {1,1,1,1}
};

💎 6. Add diamonds (collectibles)

Create:

entity/Diamond.java

Logic:

  • Place diamonds in map
  • When player touches → remove + score++

👾 7. Add enemies

Basic enemy:

public class Enemy {

    int x, y;
    int speed = 2;

    public void update() {
        // simple random movement or chase player
    }
}

Add collision:

If player touches enemy → reset or lose life

📷 8. Camera system

Instead of moving player across screen:

  • Keep player centered
  • Move world (tiles + objects)

🎨 9. Rendering (draw everything)

Inside paintComponent():

tileManager.draw(g);
player.draw(g);
for (Diamond d : diamonds) d.draw(g);

🔊 10. Polish

Add:

  • Sound (Java Clip API)
  • Sprite animations
  • UI (score, health)

🚀 11. Run your game

In Main:

JFrame window = new JFrame();
window.add(new GamePanel());
window.pack();
window.setVisible(true);

🧠 Important insight

The video’s game works because of:

  • Game loop (update + draw)
  • Tile-based world
  • Entity system (player, enemies, items)

These are foundational concepts used in most 2D games.

Next Step

Turn this into a full working downloadable project

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?