I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished version ②)

I made a simple game with Javafx ①

"Let's find happiness game" (unfinished) ②

happy_unhappy_game2.gif

import javafx.ui.*;
import javafx.ui.canvas.*;
import java.util.Date;
import java.lang.Math;
import java.lang.System;
import timer.Sound;

public class Timer {
private attribute elapsed: Number;
public attribute seconds: Number;
public attribute minutes: Integer;
public attribute hours: Integer;
public attribute count: Number;
public attribute startTime: Number;
public attribute elapsedTime: Number;
public attribute remainingTime: Number;
public attribute running: Boolean;
public attribute alarmEnabled: Boolean;
}

attribute Timer.elapsed = bind if running
then [1..10] dur 1000 linear while running continue if running
else 0;

trigger on Timer.elapsed = value {
var now = new Date();
elapsedTime = now.getTime() / 1000 - startTime;
remainingTime = count - elapsedTime;
running = if (elapsedTime >= count) then false else running;
seconds = remainingTime % 60;
minutes = ((Integer)remainingTime / 60) % 60;
hours = ((Integer)remainingTime / 60) / 60;
hours = if (elapsedTime >= count) then 0 else hours;
minutes = if (elapsedTime >= count) then 0 else minutes;
seconds = if (elapsedTime >= count) then 0 else seconds;
}

trigger on Timer.running = value {
if ((alarmEnabled == true) and (remainingTime <= 0)) {
alarmEnabled = false;
Sound.alarm();
}
}


Frame {
var t = Timer {seconds:0, minutes:0, hours:0, count:0,
running:false, alarmEnabled:false}
title: "JavaFX alarm"
height: 350
width: 215
onClose: operation() {System.exit(0);}
content: BorderPanel {
center: Canvas {
content:
Group {
var font = new Font("Dialog", "PLAIN", 15)
var secs = bind t.seconds
var mins = bind t.minutes + secs / 60
var hrs = bind t.hours + mins / 60
content:
[Rect {
height: 400
width: 225
fill: Color {red:0.9, green:0.9, blue:0.9}
},
Circle {cx: 100, cy: 100, radius: 80, fill: white, stroke: black, strokeWidth: 1},
Group {
transform: translate(100, 100)
content: foreach (i in [1..12])
Text {
var radians = Math.toRadians(30 * i - 90)
transform: [translate((70 * Math.cos(radians)), (70 * Math.sin(radians)))]
content: "{i}"
valign: MIDDLE
halign: CENTER
}
},
Group {
transform: translate(100, 100)
var hourHand =
Line {x1: 0, y1: 0, x2: 0, y2: -35,
strokeWidth: 4, stroke: black
transform: bind rotate((hrs * 30), 0, 0)
}
var minuteHand =
Line {x1: 0, y1: 0, x2: 0, y2: -55,
strokeWidth: 2, stroke: blue,
transform: bind rotate((mins * 6), 0, 0)
}
var secondHand =
Line {x1: 0, y1: 0, x2: 0, y2: -75,
strokeWidth: 1, stroke: red,
transform: bind rotate((t.seconds * 6), 0, 0)
}
content: [hourHand, minuteHand, secondHand]
},
Circle {cx: 100, cy: 100, radius: 3, fill: black, stroke: black }]
},
}
bottom: Box {
background: white
orientation: VERTICAL
content:
[FlowPanel {
content:
[Spinner {
enabled: bind not(t.running)
min: 0
max: 23
value: bind t.hours
},
SimpleLabel {
enabled: bind not(t.running)
text: "Time"
}]
},
FlowPanel {
content:
[Spinner {
enabled: bind not(t.running)
min: 0
max: 59
value: bind t.minutes
},
SimpleLabel {
enabled: bind not(t.running)
text: "Minutes"
}]
},
FlowPanel {
content:
[Spinner {
enabled: bind not(t.running)
min: 0
max: 59
value: bind t.seconds
},
SimpleLabel {
enabled: bind not(t.running)
text: "Seconds"
}]
},
FlowPanel {
content:
[Button {
enabled: bind t.running
text: "stop"
action: bind operation() {
t.running = false;
}
},
RigidArea {
width: 20
},
Button {
enabled: bind not(t.running)
text: "start"
action: operation() {
var date = new Date();
t.count = t.hours * 3600 + t.minutes * 60 + t.seconds;
t.startTime = date.getTime() / 1000;
t.remainingTime = t.count;
if (t.count <> 0) {
t.alarmEnabled = true;
t.running = true;
}
}
}]
}]
}
}
visible: true
}

Overview and future issues

Improved the screen transition, which was the previous improvement. In addition, a status bar was added and a time limit was introduced. I intended to do it, but I didn't know how to act at the end, I spent half a day trying and error, but it was tough, so I went to the next one. I'll be back in a little while. Also, I had a hard time because I couldn't fix the status bar in a fixed position. I tried using AnchorPane and BorderPane, but for some reason it doesn't work. I would like to improve this in the future as well.

Recommended Posts

I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished version ②)
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
[Beginner] Try to make a simple RPG game with Java ①
I tried to make a group function (bulletin board) with Rails
[iOS] I tried to make a processing application like Instagram with Swift
I tried to make a Web API that connects to DB with Quarkus
I tried to make Basic authentication with Java
java I tried to break a simple block
A simple rock-paper-scissors game with JavaFX and SceneBuilder
I tried to break a block with java (1)
[Android] I tried to make a material list screen with ListView + Bottom Sheet
I tried to make a login function in Java
I tried to modernize a Java EE application with OpenShift.
[Rails] I tried to create a mini app with FullCalendar
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Let's make a simple API with EC2 + RDS + Spring boot ①
I tried to make a client of RESAS-API in Java
I tried to create a padrino development environment with Docker
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
I tried to create a simple map app in Android Studio
I want to make a button with a line break with link_to [Note]
Try to make a simple callback
[Rails] Implementation of multi-layer category function using ancestry "I tried to make a window with Bootstrap 3"
I tried to illuminate the Christmas tree in a life game
Make a slideshow tool with JavaFX
[Unity] I tried to make a native plug-in UniNWPathMonitor using NWPathMonitor
I tried to interact with Java
I tried to build a simple application using Dockder + Rails Scaffold
I tried to make an Android application with MVC now (Java)
I wanted to make JavaFX programming easier with the Spring Framework
[Java] I tried to make a maze by the digging method ♪
Make a typing game with ruby
Java beginner tried to make a simple web application using Spring Boot
I made a virtual currency arbitrage bot and tried to make money
I tried to make a talk application in Java using AI "A3RT"
Let's make a Christmas card with Processing!
I tried to get started with WebAssembly
I tried playing with BottomNavigationView a little ①
I tried to make a program that searches for the target class from the process that is overloaded with Java
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
[Java] I installed JDBC and tried to connect with servlet + MySQL. (There is a version using DAO / Bean)
I tried to implement a function equivalent to Felica Lite with HCE-F of Android
A story when I tried to make a video by linking Processing and Resolume
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
I tried to automatically generate a class to convert from a data class to a Bundle with APT
I tried to clone a web application full of bugs with Spring Boot
[Small story] I tried to make the java ArrayList a little more convenient
[Android] I want to make QA easier ... That's right! Let's make a debug menu!
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to manage struts configuration with Coggle
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to manage login information with JMX
I tried to develop a man-hour management tool
I did Java to make (a == 1 && a == 2 && a == 3) always true
I tried to develop a DUO3.0 study website.
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I tried to create a LINE clone app
Let's make a search function with Rails (ransack)
I tried to link JavaFX and Spring Framework.
I tried to develop a website to record expenses.