Finished the main menu

This commit is contained in:
dvorakmortonxd 2026-10-27 19:02:33 +01:00
commit f6c2defbbe
7 changed files with 131 additions and 7 deletions

View file

@ -1,3 +1,13 @@
# retro-game <div align="center">
my "retro" game built with Java and Swing # Retro Game
## A simple retro-style game built with Java and Swing.
</div>
## credits
so basically I made a zero-asset policy for this project except for the font, <br>
which I got from [openfont.org](https://openfont.org/), the font's name is "**Jacquard 12**" :) <br>
also I took some inspiration from these pinterest pages (I will list every single one): <br>
[Retro Game Titles by "cuilong li"](https://www.pinterest.com/pin/902408844107127097/) <br>
[SHOVEL KNIGHT GameBoy by "PixelJoint"](https://www.pinterest.com/pin/902408844107127145)

View file

@ -3,7 +3,7 @@ plugins {
} }
group = "dev.dvorakmortonxd" group = "dev.dvorakmortonxd"
version = "1.0-SNAPSHOT" version = "0.0.1a"
repositories { repositories {
mavenCentral() mavenCentral()
@ -17,4 +17,10 @@ dependencies {
tasks.test { tasks.test {
useJUnitPlatform() useJUnitPlatform()
}
tasks.jar {
manifest {
attributes["Main-Class"] = "dev.dvorakmortonxd.Game"
}
} }

View file

@ -3,6 +3,8 @@ package dev.dvorakmortonxd;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.io.IOException;
import java.io.InputStream;
public class Game extends JPanel implements ActionListener { public class Game extends JPanel implements ActionListener {
@ -16,6 +18,21 @@ public class Game extends JPanel implements ActionListener {
// defines the current scene // defines the current scene
public String scene = "main_menu"; public String scene = "main_menu";
// the currently chosen option (currently used for the main menu)
public int chosenOption = 1;
// predefines the gameFont
public static Font gameFont;
// colors I'll be using
public static Color backgroundGreen = new Color(45, 73, 45);
public static Color lightGreen = new Color(100, 120, 100);
public static Color darkerGreen = new Color(25, 50, 25);
// text hover animation and if it should hover down or up
public int textHoverAmount = 0;
public boolean textHoverDown = true;
// routes to Update.java (actions) // routes to Update.java (actions)
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -35,12 +52,21 @@ public class Game extends JPanel implements ActionListener {
setPreferredSize(new Dimension(windowWidth, windowHeight)); setPreferredSize(new Dimension(windowWidth, windowHeight));
Input in = new Input(this); Input in = new Input(this);
addKeyListener(in); addKeyListener(in);
setBackground(new Color(45, 73, 45)); setBackground(backgroundGreen);
setFocusable(true); setFocusable(true);
timer.start(); timer.start();
} }
public static void main(String[] args) { public static void main(String[] args) {
// loads the game font (which is the only game asset, that's why I did not make a loadAsset class like I did in my space-sim game)
try {
InputStream inputStream = Game.class.getResourceAsStream("/assets/gameFont.ttf");
gameFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
gameFont = new Font("Arial", Font.PLAIN, 12); // fallback font
}
JFrame frame = new JFrame("My cool retro game"); JFrame frame = new JFrame("My cool retro game");
frame.setResizable(false); // NOT resizable frame.setResizable(false); // NOT resizable
frame.add(new Game()); frame.add(new Game());

View file

@ -12,14 +12,39 @@ public class Input implements KeyListener {
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) { switch (e.getKeyCode()) {
case KeyEvent.VK_ESCAPE -> System.exit(0); case KeyEvent.VK_ESCAPE -> System.exit(0);
case KeyEvent.VK_SPACE -> System.out.println("Hello world!"); case KeyEvent.VK_UP, KeyEvent.VK_W -> {
if (m.scene.equals("main_menu")) {
if (m.chosenOption > 1) {
m.chosenOption--;
} else {
m.chosenOption++;
}
}
}
case KeyEvent.VK_DOWN, KeyEvent.VK_S -> {
if (m.scene.equals("main_menu")) {
if (m.chosenOption < 2) {
m.chosenOption++;
} else {
m.chosenOption--;
}
}
}
case KeyEvent.VK_ENTER, KeyEvent.VK_SPACE -> {
if (m.scene.equals("main_menu")) {
if (m.chosenOption == 1) {
m.scene = "game entry";
} else {
System.exit(0);
}
}
}
} }
} }
// what happens when you RELEASE a key on your keyboard // what happens when you RELEASE a key on your keyboard
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) { switch (e.getKeyCode()) {
case KeyEvent.VK_SPACE -> System.out.println("Goodbye world!");
} }
} }
// I don't know what this is, or why it exists, I'll never use it but if this wasn't in the code, it'll throw an error. // I don't know what this is, or why it exists, I'll never use it but if this wasn't in the code, it'll throw an error.

View file

@ -4,9 +4,43 @@ import java.awt.*;
public class Update { public class Update {
static void performPerTick(Game m) { static void performPerTick(Game m) {
if (m.textHoverDown) {
m.textHoverAmount++;
if (m.textHoverAmount >= 30) {
m.textHoverDown = false;
}
} else {
m.textHoverAmount--;
if (m.textHoverAmount <= 0) {
m.textHoverDown = true;
}
}
m.repaint(); m.repaint();
} }
static void paint(Game m, Graphics g) { static void paint(Game m, Graphics g) {
Toolkit.getDefaultToolkit().sync(); if (m.scene.equals("main_menu")) {
g.setColor(m.lightGreen);
g.setFont(m.gameFont.deriveFont(75f));
if (m.chosenOption == 1) {
g.drawString("play game", 260, 390);
g.setColor(m.darkerGreen);
g.drawString("exit game", 265, 460);
} else if (m.chosenOption == 2) {
g.drawString("exit game", 265, 460);
g.setColor(m.darkerGreen);
g.drawString("play game", 260, 390);
} else {
m.chosenOption = 1;
}
g.drawString("by dvorakmortonxd", 135, 290 + m.textHoverAmount / 4);
g.setFont(m.gameFont.deriveFont(150f));
g.drawString("Retro", 230, 120 + m.textHoverAmount / 4);
g.drawString("Game", 235, 220 + m.textHoverAmount / 4);
g.setFont(m.gameFont.deriveFont(40f));
g.drawString("tip: use arrow keys to navigate through menu", 65, 550);
g.drawString("and enter to pick an option!", 200, 580);
Toolkit.getDefaultToolkit().sync();
}
} }
} }

Binary file not shown.

View file

@ -0,0 +1,23 @@
{
"name": "Jacquard 12",
"family": "Jacquard 12",
"description": "Jacquard is an expanded revival typeface from a Victorian needlepoint alphabet designed in Berlin by Heinrich Kuehn circa 1880. The Soft Type is a collection of typefaces designed for knitting color-work. Designing typefaces for knitting is essentially the same as creating pixel types. However, in practice, the pixel size is determined by the properties of the yarn in use. Scale is determined by the weight of the yarn as well as the number of pixels that make up the height of each letter. In this collection, the number following the typeface's name indicates the height of the capital letters for that typeface. These typefaces were designed with machine knitting in mind, but could be used for hand knitting, needlework, bedazzling, or many other textile crafts. Note: For Fair Isle knitting, you need to beware of floats and plan accordingly, as always. Each typeface has a “Regular” and “Charted” version, and some include multiple scales so you can fit type on your knits, no matter the project's size. Check out the charted version Jacquard 12 Charted . To contribute, please see github.com/scfried/soft-type-jacquard .",
"author": "Sarah Cadigan-Fried",
"website": "https://github.com/scfried/soft-type-jacquard",
"category": "Display",
"tags": [
"display",
"google"
],
"languages": [
"en",
"es"
],
"styles": [
"Regular"
],
"formats": [
"ttf"
],
"license": "SIL Open Font License 1.1"
}