0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

2021-08-01 Godot Engine > キッチンタイマー > const, SpinBoxからの値読み取り, 書式指定付き文字列作成, Timerイベント

Last updated at Posted at 2021-08-01

動作環境

Windows 10 Pro v21H1
Godot Engine v3.3.2.stable.official

Godotの学習歴 (今日開始: 2021-08-01)

処理の概要

  • キッチンタイマー
    • 現在時刻表示
    • カウントダウン
    • 指定の秒数でカウントダウンもできる

学習内容

  • const
  • SpinBoxからの値読み取り (int型)
  • 書式指定付きの文字列作成
  • Timerイベント

準備

  • Scale=4でNode2Dを用意
    • ボタンやラベルを拡大表示したいため
  • 以下の項目を追加 (左が名前だが、任意で)
    • CurrentTime: Label
      • 右上の文字列
    • Timer : Timer
    • CountDown : Label
      • 右下の文字列
    • Button : Button
      • "reset"ボタン
    • SpinBox : SpinBox
    • Label : Label
      • "Count"という表示

image.png

実装

main.gd
extends Node2D

## A. const
## B. get integer value from SpinBox
## C. print format
## D. Timer Event

onready var l_time = $CurrentTime  # Label 
onready var t_time = $Timer        # Timer
onready var l_cdwn = $CountDown    # Label
onready var sb_num = $SpinBox      # SpinBox

const DEFAULT_COUNT_DOWN = 3610

var countdown = DEFAULT_COUNT_DOWN

func _ready():
	t_time.start(1)

func _on_Timer_timeout():
	# 1. display current time
	var timedict = OS.get_time()
	var hh = timedict.hour
	var mm = timedict.minute
	var ss = timedict.second
	l_time.text = "%02d:%02d:%02d" % [hh, mm, ss]

	# 2. display count down
	countdown -= 1;
	var leftsec = countdown % 60 
	var leftmin = countdown / 60 % 60
	var lefthour = countdown / 60 / 60 
	l_cdwn.text = "%02d:%02d:%02d" % [lefthour, leftmin, leftsec]

func _on_Button_pressed():
	countdown = int(sb_num.get_value())

関連

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?