153 lines
3.4 KiB
QML
153 lines
3.4 KiB
QML
|
import QtQuick 2.11
|
||
|
import QtQuick.Controls 2.4
|
||
|
import "./"
|
||
|
|
||
|
Item {
|
||
|
id: calculator
|
||
|
|
||
|
property int sum: 0
|
||
|
property int nextNum: 0
|
||
|
property int lastNum: 0
|
||
|
property int actualNumCount: 0
|
||
|
property int min: 1
|
||
|
property int max: 9
|
||
|
property int tickInterval: 1000
|
||
|
property int numCount: 10
|
||
|
property bool endPageVisibility: false
|
||
|
property string endPageLabelText: "You Lose!"
|
||
|
|
||
|
signal pageOpened()
|
||
|
|
||
|
onPageOpened: {
|
||
|
calculator.start()
|
||
|
}
|
||
|
|
||
|
states: [
|
||
|
State {
|
||
|
name: "starting"
|
||
|
PropertyChanges {
|
||
|
target: calculatorStack
|
||
|
currPage: calculatorStartPageComp
|
||
|
}
|
||
|
},
|
||
|
|
||
|
State {
|
||
|
name: "running"
|
||
|
PropertyChanges {
|
||
|
target: calculatorStack
|
||
|
currPage: calculatorRunningPageComp
|
||
|
}
|
||
|
},
|
||
|
State {
|
||
|
name: "gameOver"
|
||
|
PropertyChanges {
|
||
|
target: calculatorStack
|
||
|
currPage: calculatorGameOverPageComp
|
||
|
}
|
||
|
}
|
||
|
]
|
||
|
|
||
|
StackView {
|
||
|
id: calculatorStack
|
||
|
|
||
|
property var currPage;
|
||
|
|
||
|
anchors.fill: parent
|
||
|
|
||
|
onCurrPageChanged: {
|
||
|
calculatorStack.replace(currPage)
|
||
|
}
|
||
|
onCurrentItemChanged: {
|
||
|
calculatorStack.currentItem.pageOpened()
|
||
|
}
|
||
|
|
||
|
Component {
|
||
|
id: calculatorStartPageComp
|
||
|
CalculatorStartPage {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Component {
|
||
|
id: calculatorRunningPageComp
|
||
|
CalculatorRunningPage {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Component {
|
||
|
id: calculatorGameOverPageComp
|
||
|
CalculatorGameOverPage {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
replaceExit: Transition {
|
||
|
NumberAnimation {
|
||
|
from: 1
|
||
|
to: 0
|
||
|
property: "opacity"
|
||
|
duration: 200
|
||
|
easing.type: Easing.InOutQuad
|
||
|
}
|
||
|
}
|
||
|
|
||
|
replaceEnter: Transition {
|
||
|
NumberAnimation {
|
||
|
from: 0
|
||
|
to: 1
|
||
|
property: "opacity"
|
||
|
duration: 200
|
||
|
easing.type: Easing.InOutQuad
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/*-------------------------
|
||
|
---------functions---------
|
||
|
-------------------------*/
|
||
|
|
||
|
function start() {
|
||
|
calculator.reset()
|
||
|
calculator.state = "starting"
|
||
|
}
|
||
|
|
||
|
function run() {
|
||
|
calculator.nextNumber()
|
||
|
calculator.state = "running"
|
||
|
}
|
||
|
|
||
|
function gameOver() {
|
||
|
calculator.state = "gameOver"
|
||
|
}
|
||
|
|
||
|
function reset() {
|
||
|
calculator.sum = 0
|
||
|
calculator.actualNumCount = 0
|
||
|
calculator.endPageLabelText = "You Lose!"
|
||
|
calculator.endPageVisibility = false
|
||
|
calculator.lastNum = 0
|
||
|
calculator.nextNum = 0
|
||
|
}
|
||
|
|
||
|
function nextNumber(){
|
||
|
var randNum = 0
|
||
|
var range = calculator.max - calculator.min
|
||
|
lastNum = nextNum
|
||
|
while(randNum===0 || randNum === lastNum){
|
||
|
randNum = Math.floor((Math.random()*(range+1))+min)
|
||
|
}
|
||
|
nextNum = randNum
|
||
|
calculator.sum += randNum
|
||
|
actualNumCount += 1
|
||
|
}
|
||
|
|
||
|
function checkSum(sumInputText) {
|
||
|
if (sumInputText !== "") {
|
||
|
if (calculator.sum === parseInt(sumInputText)) {
|
||
|
calculator.endPageLabelText = "You Won!"
|
||
|
}
|
||
|
calculator.endPageVisibility = true
|
||
|
}
|
||
|
}
|
||
|
}
|