RhythmGame 1.4.3
Loading...
Searching...
No Matches
7. Draw notes during gameplay

Previous | Tutorial | Next: Results

Goal: Draw notes and live scores using the player's speed and cover settings, with the standard flow handling play, retry and courses.

Install docs/examples/theme-tutorial/07-gameplay/ and choose it for the gameplay screens you use in Settings > Themes. It supports 5k, 7k, their battle variants, 10k and 14k. Keep an existing result theme selected for now.

Download 07-gameplay or open the source folder.

The screen uses rectangles for notes, long notes, bar lines and mines. It also shows each player's score, combo, latest judgement and life gauge, with the chart's background animation (BGA) beside the lanes.

Gameplay.qml arranges the screen, PlayerField.qml draws one player's field, and NoteLane.qml draws one lane. GaugeBar.qml displays the active gauge.

Let the standard flow run the play

The root receives the same GameplayContext used by decide. Bind it to StandardGameplayFlow.

StandardGameplayFlow {
id: flow
gameplay: gameplayScreen.gameplay
}

The flow handles startup, Escape, retry, speed controls and results. For a course, it also continues after each stage result and opens the final course summary. Don't add StandardGameplayInput beside it or call the runner's start and finish methods yourself.

Follow the current stage and players

All gameplay themes must support courses. Bind to gameplay.chartData and gameplay.players so the display follows the stage that is currently playing.

Label {
Layout.fillWidth: true
Layout.maximumWidth: (gameplayScreen.availableWidth - playLayout.spacing) / 2
text: gameplayScreen.gameplay.chartData.title
font.pixelSize: 24
elide: Text.ElideRight
}
Label {
visible: !!gameplayScreen.gameplay.course
text: qsTr("Stage %1 of %2").arg(gameplayScreen.gameplay.stageIndex + 1)
.arg(gameplayScreen.gameplay.stageCount)
}

stageIndex starts at zero; add one for display. A single chart has index zero and a count of one. The note renderer uses the same player interface for both.

players has one entry for SP or DP and two for local battle.

Repeater {
model: gameplayScreen.gameplay.players
PlayerField {
required property int index
Layout.fillWidth: true
Layout.fillHeight: true
Layout.minimumWidth: 0
Layout.preferredWidth: 1
gameplay: gameplayScreen.gameplay
playerIndex: index
}
}

Each field receives the gameplay context and a player index. Its player binding follows gameplay.players[playerIndex] when a course changes stages.

Give the lanes half of the screen

The outer RowLayout gives its two children equal widths with uniformCellSizes: true. The player fields occupy the left half, while the right half holds BGA and leaves room for the Arena panel. Both halves share the available height.

RowLayout {
id: playLayout
Layout.fillWidth: true
Layout.fillHeight: true
uniformCellSizes: true
spacing: 16
RowLayout {
Layout.fillWidth: true
Layout.fillHeight: true
Layout.minimumWidth: 0
spacing: 16
// [players]
Repeater {
model: gameplayScreen.gameplay.players
PlayerField {
required property int index
Layout.fillWidth: true
Layout.fillHeight: true
Layout.minimumWidth: 0
Layout.preferredWidth: 1
gameplay: gameplayScreen.gameplay
playerIndex: index
}
}
// [players]
}
Item {
Layout.fillWidth: true
Layout.fillHeight: true
Layout.minimumWidth: 0
// [bga]
StandardGameplayBga {
anchors.fill: parent
gameplay: gameplayScreen.gameplay
}
// [bga]
}
}

The right half is an Item containing BGA, so it keeps its space when the player turns BGA off. The lanes stay the same width. Local battle divides the left half between two player playfields. Double play uses one playfield for all lanes.

Show loading and chart progress

gameplay.status tells you whether the stage is loading, ready, running or finished. In Arena, Ready can remain on screen while the game waits for the shared start time.

RowLayout {
Layout.fillWidth: true
Label {
text: {
switch (gameplayScreen.gameplay.status) {
case ChartRunner.Loading: return qsTr("Loading...");
case ChartRunner.Ready: return qsTr("Ready");
case ChartRunner.Running: return qsTr("Playing");
case ChartRunner.Finished: return qsTr("Finished");
}
}
}
Label {
text: qsTr("%1 BPM").arg(gameplayScreen.gameplay.players[0].bpm.toFixed(1))
}
Label {
text: qsTr("%1 / %2 s")
.arg(Math.floor(Math.max(0, gameplayScreen.gameplay.players[0].elapsed) / 1000000000))
.arg(Math.ceil(gameplayScreen.gameplay.players[0].chartLength / 1000000000))
}
Item { Layout.fillWidth: true }
}
ProgressBar {
Layout.fillWidth: true
Layout.maximumWidth: (gameplayScreen.availableWidth - playLayout.spacing) / 2
from: 0
to: Math.max(1, gameplayScreen.gameplay.players[0].chartLength)
value: Math.max(0, gameplayScreen.gameplay.players[0].elapsed)
Accessible.name: qsTr("Chart progress")
}

The first player's bpm, elapsed and chartLength supply the chart display. Divide the time values by one billion to convert nanoseconds to seconds. The progress bar can use the original units. Elapsed time can be negative before play starts, so the label clamps it to zero.

Show the background animation

StandardGameplayBga draws the current chart's BGA. Give it the gameplay context and a place in the layout.

StandardGameplayBga {
anchors.fill: parent
gameplay: gameplayScreen.gameplay
}

It handles the image layers, miss animation and course stage changes. Visibility follows the player's bgaOn setting, or the main profile's setting in battle. Override visible if needed. For a custom renderer, use gameplay.bga.layers.

Show the player's score target

StandardGameplayTarget uses the same context and player index. The example displays its current target points in a label.

StandardGameplayTarget {
id: scoreTarget
gameplay: field.gameplay
playerIndex: field.playerIndex
}
Label {
Layout.fillWidth: true
visible: scoreTarget.available
text: qsTr("Target: %1 points").arg(Math.floor(scoreTarget.points))
elide: Text.ElideRight
}

The target follows the player's profile setting: a score fraction, best score, latest score or next rank. It uses replay progress when available, or a score rate otherwise, and updates for each course stage. In battle and Arena, it follows an opponent. Check available before showing it.

Draw each player's lanes

player.columns supplies the playable column indices for the current player. They follow the default visual order, with the left scratch first and the right scratch last. Five-key modes omit unused columns. player.isScratchColumn(index) identifies scratch lanes for their colors.

Changing the visual column order does not change the input mapping.

readonly property var columns: field.player.columns

Follow the player's note speed

StandardGameplayViewport reads noteScreenTimeMillis and hiSpeedFix from the player's profile. Give it the current player, chart metadata and lane height. It calculates pixelsPerBeat for drawing and keeps the filtered note models in sync, including long notes whose bodies cross either edge of the visible area.

StandardGameplayViewport {
id: viewport
player: field.player
chartData: field.gameplay.chartData
noteHeight: lanes.height
visibleHeight: lanes.height - laneCover.height
}

noteScreenTimeMillis sets the travel time at the BPM chosen by hiSpeedFix. The helper uses the same calculation as Default.

HiSpeedFix value BPM used for note speed
Off 120
Main chartData.mainBpm
Start chartData.initialBpm
Min chartData.minBpm
Max chartData.maxBpm
Avg chartData.avgBpm

At a reference BPM of 120 and a travel time of 1000 milliseconds, two beats fit in the field. Resizing the field changes their spacing in pixels. The visible span still follows the saved travel time and BPM setting. The conversion is (noteScreenTimeMillis / 1000) * (referenceBpm / 60), which multiplies seconds by beats per second.

The calculation updates with the settings and current stage. BPM and scroll changes within the chart still affect how notes move.

The example also draws covers and lift from player.profile.vars.generalVars. Lift raises the judgement line by shortening the lanes. Lane cover draws over the top of those lanes. The helper's noteHeight is the shortened lane height, and visibleHeight subtracts the lane cover from it. Hidden cover draws over the bottom of the lanes without changing the speed calculation.

Rectangle {
id: playArea
Layout.fillWidth: true
Layout.fillHeight: true
color: "#243341"
readonly property real liftRatio: field.vars.liftOn ? field.vars.liftRatio : 0
RowLayout {
id: lanes
anchors.fill: parent
anchors.bottomMargin: Math.round(playArea.height * playArea.liftRatio)
spacing: 2
uniformCellSizes: true
Repeater {
model: field.columns
NoteLane {
required property int modelData
Layout.fillWidth: true
Layout.fillHeight: true
player: field.player
columnState: field.player.state.columnStates[modelData]
pixelsPerBeat: viewport.pixelsPerBeat
scratch: field.player.isScratchColumn(modelData)
}
}
}
// [bar-lines]
Item {
anchors.fill: lanes
clip: true
Repeater {
model: field.player.state.barLinesState
Rectangle {
required property var display
width: lanes.width
height: 1
y: lanes.height - (display.time.position - field.player.position) * viewport.pixelsPerBeat
color: "#708090"
}
}
}
// [bar-lines]
Rectangle {
id: laneCover
anchors.top: lanes.top
width: lanes.width
height: field.vars.laneCoverOn ? lanes.height * field.vars.laneCoverRatio : 0
color: "#243341"
}
Rectangle {
anchors.bottom: lanes.bottom
width: lanes.width
height: field.vars.hiddenOn ? lanes.height * field.vars.hiddenRatio : 0
color: "#243341"
}
Rectangle {
anchors.bottom: lanes.bottom
width: lanes.width
height: 2
color: "white"
}
}

For example, with a 20% lift and a 25% lane cover, the judgement line sits at 80% of the field height. The lane cover hides a quarter of that remaining area, leaving 60% of the full field for the saved note travel time.

NoteLane.qml places a note relative to player.position. As the player position approaches the note position, the note moves toward the line at the bottom. It uses the helper's pixelsPerBeat for both note positions and long note lengths. Both positions include the chart's scroll changes. Long note bodies extend from their beginning toward their end position.

The file starts with pragma ValueTypeBehavior: Addressable so it can use constants such as note.Type.LongNoteBegin. Qt requires this for the lowercase note value type.

Repeater {
model: lane.columnState
delegate: Item {
id: drawnNote
required property int noteType
required property real notePosition
required property real longNoteEndPosition
required property bool visibleNote
required property bool heldLongNote
required property bool staticLongNoteCandidate
readonly property bool longNote: drawnNote.noteType === note.Type.LongNoteBegin
readonly property real bottomPosition: drawnNote.staticLongNoteCandidate
&& drawnNote.longNoteEndPosition > lane.player.position
? lane.player.position : drawnNote.notePosition
readonly property real bodyLength: drawnNote.longNote
? (drawnNote.longNoteEndPosition - drawnNote.bottomPosition) * lane.pixelsPerBeat : 0
width: lane.width
height: 6
y: lane.height - (drawnNote.bottomPosition - lane.player.position) * lane.pixelsPerBeat - height
visible: drawnNote.visibleNote && drawnNote.noteType !== note.Type.Invisible
Rectangle {
width: drawnNote.width
height: Math.abs(drawnNote.bodyLength)
y: Math.min(0, -drawnNote.bodyLength)
visible: drawnNote.longNote
color: drawnNote.heldLongNote ? "#a8dcc9" : "#527266"
}
Rectangle {
anchors.fill: parent
color: drawnNote.noteType === note.Type.Landmine ? "#ef7777"
: lane.scratch ? "#e8bc89" : "#d9e5ee"
}
}
}

The field also draws bar lines from player.state.barLinesState. Each row's display.time.position gives its position with scroll changes applied. Use the same pixelsPerBeat as the notes so both move together. The model already filters the lines to the visible range.

Item {
anchors.fill: lanes
clip: true
Repeater {
model: field.player.state.barLinesState
Rectangle {
required property var display
width: lanes.width
height: 1
y: lanes.height - (display.time.position - field.player.position) * viewport.pixelsPerBeat
color: "#708090"
}
}
}

The lane highlight reads columnState.pressed. Autoplay and replay update that state too. Input reports physical input actions, so using it for the highlight would miss presses supplied by autoplay or replay.

Rectangle {
anchors.fill: parent
color: "#35586a"
visible: lane.columnState.pressed
}

Adjust speed and covers while playing

StandardGameplayFlow includes StandardGameplaySpeedControls. The example only needs a label to explain the controls.

Label {
Layout.fillWidth: true
text: qsTr("START + scratch: note speed. SELECT + scratch: cover. Double-press START: toggle lane cover.")
wrapMode: Text.Wrap
}

Hold START and turn the scratch to change noteScreenTimeMillis. Hold SELECT and turn the scratch to change the cover amount. Double-press START within half a second to turn lane cover on or off. The toggle adjusts the saved travel time to keep the same scrolling speed, within the allowed duration limits.

SELECT adjusts lane cover when it is on. Otherwise, it adjusts lift or hidden, whichever is on. When both lift and hidden are on, briefly press START+SELECT to switch between them. With all three off, SELECT still adjusts the saved lane-cover amount for the next time you enable it.

Each battle player changes their own profile. In double play, controls on either side use the same profile. Adjustments pause during START+SELECT and retry choice, and stop when the screen is inactive.

Set speedControlsEnabled: false on the flow to supply your own controls. The lane renderer still follows the saved settings.

Show the current gauge

player.score.gauges holds the live gauges for that player. It may contain several gauges, depending on the player's gauge mode. Course stages supply course gauges through the same property.

GaugeBar {
Layout.fillWidth: true
gauges: field.player.score.gauges
}

GaugeBar.qml shows the first gauge above its clear threshold, or the last gauge if none is above its threshold. The binding updates as the player hits or misses notes.

readonly property var activeGauge: gauges.find(gauge => gauge.gauge > gauge.threshold)
|| gauges[gauges.length - 1]
Label {
Layout.fillWidth: true
text: qsTr("%1: %2%").arg(bar.activeGauge.name).arg(bar.activeGauge.gauge.toFixed(1))
elide: Text.ElideRight
}
ProgressBar {
Layout.fillWidth: true
from: 0
to: bar.activeGauge.gaugeMax
value: bar.activeGauge.gauge
}

The bar uses gaugeMax for its range and shows the gauge's name beside its value. A theme can draw every gauge or choose another display rule using this same list.

Show the latest judgement

Each player's BmsLiveScore emits hit for gameplay events. The event's judgement identifies the outcome, or is -1 when no judgement was awarded. Listen to the score so the display also works during autoplay and replay.

Label {
id: judgementText
Layout.fillWidth: true
Layout.preferredHeight: 32
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pixelSize: 24
font.bold: true
opacity: judgementTimer.running ? 1 : 0
property int judgement: -1
readonly property var styles: ({
[Judgement.Perfect]: { text: qsTr("PGREAT"), color: "#9de6ff" },
[Judgement.Great]: { text: qsTr("GREAT"), color: "#ffe28a" },
[Judgement.Good]: { text: qsTr("GOOD"), color: "#a8dcc9" },
[Judgement.Bad]: { text: qsTr("BAD"), color: "#ef9ad0" },
[Judgement.Poor]: { text: qsTr("POOR"), color: "#ef7777" },
[Judgement.EmptyPoor]: { text: qsTr("POOR"), color: "#ef7777" },
[Judgement.MineHit]: { text: qsTr("MINE"), color: "#ef7777" }
})
text: styles[judgement]?.text || ""
color: styles[judgement]?.color || "white"
Timer {
id: judgementTimer
interval: 500
}
Connections {
target: field.player.score
enabled: field.enabled
function onHit(hit) {
if (!judgementText.styles[hit.judgement]) return;
judgementText.judgement = hit.judgement;
judgementTimer.restart();
}
}
}

The example maps judgements to text and colors, ignoring events such as "mine avoided". Each displayed judgement restarts a half-second timer. A new course stage changes the connection's target and clears the previous judgement.

Add retry controls

The Retry and Same random buttons call the flow's retry(samePattern) method. Bind their enabled state to retryAvailable so the flow decides when retry is allowed.

RowLayout {
Button {
text: qsTr("Exit")
enabled: !flow.retryChoosing
onClicked: flow.exit()
}
Button {
text: qsTr("Retry")
enabled: flow.retryAvailable
onClicked: flow.retry(false)
}
Button {
text: qsTr("Same random")
enabled: flow.retryAvailable
onClicked: flow.retry(true)
}
}

For a manual single chart, hold START+SELECT to choose a retry. Release START for the same random pattern, SELECT for a new pattern, or both to cancel. The Exit button is disabled during this choice. Standard quick retry is unavailable for courses, autoplay, replay, battle and Arena.

Include Arena standings

The screen adds StandardArenaGameplayOverlay beside the gameplay flow. Both receive the same context. The overlay shows standings and chat for Arena play and stays hidden during local play and courses. Its default position is in the upper-right corner, outside the left half used by the lanes. The player can move or resize it.

StandardArenaGameplayOverlay {
parent: gameplayScreen
gameplay: gameplayScreen.gameplay
}

Try play, retry and a course

Start with an easy chart or autoplay. Check long notes and mines, then resize the window: all lanes should remain visible. Try the speed and cover controls and confirm that the notes follow the saved settings. In manual single-chart play, try both retry buttons.

Play a course past its first result. The next stage should change both the title and notes. Closing the final course summary should return to selection. The example's combo label covers the current stage; coursePlayers provides the combo for the whole course if you want to display it.

For later customization, the flow reference describes startReady and finishReady for delaying transitions around an animation. Keep the standard score saving and navigation when adding such effects.