A-Guide-to-Qt-6-Beginners-Guide-to-Qt source code learning hang man

Posted by phpQuestioner on Fri, 31 Dec 2021 03:55:37 +0100

The game is played as follows:

The host secretly writes an English word (sometimes it can also be a phrase or sentence) and does not publish it to the public. He just tells how many letters the word has, and gives the space according to the number of letters, and then let everyone guess. The guesser guesses one letter at a time. For each letter guessed correctly, the host fills in the space according to the order of the letter in the word to be guessed. If the word has two (or more) identical letters, when the letter is guessed correctly, the host will write all the letters in the order of the guessed word. In order to limit the number of word guessing, the host draws a "gallows" (see Figure 1). If there is no letter guessed by the guesser in the guessed word, it will be regarded as a missed guess. The host draws a part of the little man's body under the "gallows". When the little man is painted completely, the guesser will be "hanged", that is, the guesser will lose the game. For example, the host secretly writes the word APPLE, gives six spaces, and tells the quiz that the word has six letters. The contestant may guess the letter E first. The host tells him / her that there is no E in the word guessed, and draws a little man's head under the "gallows"; Then the contestant continues to guess the letter S, and the host tells him / her that there is an a in the word guessed, and fills in the blank in the order of the letter in the word, that is. A __; The guesser then guessed the letter D. because there was no D in the guessed word, the host drew a villain's trunk under the "gallows"; The guesser continued to guess the letter P. the host told everyone that there were two p's and filled in the blank P ­ _

main.qml

import QtQuick 2.9
import QtQuick.Controls 2.5

ApplicationWindow {
    width: 360
    height: 640
    visible: true
    title: "Hang-Man"

    StackView{
        id: contentFrame
        anchors.fill: parent
        initialItem: Qt.resolvedUrl("qrc:/Main/Load_Page.qml")
    }

    Component.onCompleted: {
        contentFrame.replace("qrc:/Main/Main_Page.qml")
    }
}

Load_Page.qml

import QtQuick 2.9
import QtQuick.Controls 2.5

Item {
    width: parent.width
    height: parent.height

    Rectangle{
        id: bgRec
        anchors.fill: parent
        color: "#2C3E50"

        BusyIndicator {
            id: busyIndicator
            anchors.centerIn: parent
        }
    }
}

import QtQuick 2.9
import QtQuick.Controls 2.5
import "qrc:/Game_Page"

Item {
    width: parent.width
    height: parent.height

    Rectangle{
        anchors.fill: parent
        color: "#2c3e50"
    }

    property var hangManCounter: 0
    property var currentWord: ""
    property var wordCount: 0
    ListModel{
        id: listModelCount
    }
    ListModel{
        id: wordModel
        ListElement{
            word: "TREE"
        }
        ListElement{
            word: "APPLE"
        }
    }
	SwipeView{
        id: swipeView
        anchors.fill: parent
        interactive: false
		//Welcome page
        Item{
            id: welcomePage
            width: 360
            height: 640

            Label{
                id: gameTitel
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.top: parent.top
                anchors.topMargin: 50
                text: "Hang-Man"
                color: "white"
                font.pointSize: 50
            }
			//Start button
            RoundButton{
                id: startGameButton
                anchors.centerIn: parent
                width: 200
                height: 200
                text: "START"
                font.bold: true
                font.pointSize: 38                
                background: Rectangle{
                    anchors.fill: parent
                    radius: 99
                    color: "#fe9000"
                    border.width: 2
                    border.color: "black"
                }
                //Start button click event
                onClicked: {
                    //Get a word randomly from wordModel
                    currentWord = wordModel.get(Math.floor(Math.random() * wordModel.count))
                    //Get the number of words
                    wordCount = currentWord.word.length
                    for(var i = 1; i <= wordCount; i++){
                        listModelCount.append({"space": "-"})
                    }
                    //Terminal output word
                    console.log(currentWord.word)
                    //Jump to the game interface
                    swipeView.setCurrentIndex(1)
                }
            }
			//Bottom two labels
            Label{
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 50
                text: "Made by BEN COEPP"
                color: "white"
                font.pointSize: 15
            }

            Label{
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 10
                text: "User Agreement other Legal Stuff"
                color: "white"
                font.pointSize: 8

                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        // Link to Legal Documents
                    }
                }
            }
        }
      Game_Page{....}
      Item{....}
    }
}

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-5kegwsci-16278979936) (/ home / XZ / picture / img/3.png)]

Game_Page game main panel

Main_Page.qml

import QtQuick 2.9
import QtQuick.Controls 2.5
import "qrc:/Game_Page"

Item {
    width: parent.width
    height: parent.height

    Rectangle{
        anchors.fill: parent
        color: "#2c3e50"
    }

    property var hangManCounter: 0
    property var currentWord: ""
    property var wordCount: 0
    ListModel{
        id: listModelCount
    }
    ListModel{
        id: wordModel
        ListElement{
            word: "TREE"
        }
        ListElement{
            word: "APPLE"
        }
    }

    SwipeView{
        id: swipeView
        anchors.fill: parent
        interactive: false
        
        Item{Id:welcomePage...}
        Game_Page
        {        
        	id: gamePage
        }        
        Item{id:endPage...}        
       }
}

Game_Page/Game_Page.qml

import QtQuick 2.9
import QtQuick.Controls 2.5

Item{
    id: gamePage
    width: 360
    height: 640

    property var recColor: "#fe9000"

    MouseArea {
        id: root
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
        width: 360
        height: 360
		//Several rectangle colors above recColor
        Rectangle {
            id: rec1
            x: 19
            y: 319
            width: 87
            height: 20
            color: recColor
            visible: false
        }

        Rectangle {
            id: rec2
            x: 52
            y: 38
            width: 20
            height: 283
            color: recColor
            visible: false
        }

        Rectangle {
            id: rec3
            x: 52
            y: 38
            width: 189
            height: 20
            color: recColor
            visible: false
        }

        Rectangle {
            id: rec4
            x: 221
            y: 39
            width: 20
            height: 54
            color: recColor
            visible: false
        }

        Rectangle {
            id: rec5
            x: 189
            y: 89
            width: 84
            height: 84
            color: "transparent"
            radius: 99
            border.width: 15
            border.color: recColor
            visible: false
        }

        Rectangle {
            id: rec6
            x: 221
            y: 168
            width: 20
            height: 91
            color: recColor
            visible: false
        }

        Rectangle {
            id: rec7
            x: 200
            y: 231
            width: 20
            height: 91
            color: recColor
            rotation: 210
            visible: false
        }

        Rectangle {
            id: rec8
            x: 242
            y: 231
            width: 20
            height: 91
            color: recColor
            rotation: 150
            visible: false
        }

        Rectangle {
            id: rec9
            x: 252
            y: 170
            width: 20
            height: 91
            color: recColor
            rotation: 130
            visible: false
        }

        Rectangle {
            id: rec10
            x: 190
            y: 170
            width: 20
            height: 91
            color: recColor
            rotation: 230
            visible: false
        }
    }

ListView{
    id: listViewLetterCount
    width: parent.width
    height: 50
    orientation: ListView.Horizontal
    anchors.bottom: parent.bottom
    anchors.bottomMargin: inputGrid.height+20
    interactive: false
    model: listModelCount
    //Draw a white underline according to the number of words
    delegate: MouseArea{
        width: 50
        height: parent.width/10

        Rectangle{
            anchors.bottom: parent.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            width: parent.width-10
            height: 4
            color: "white"
        }
    }
}

ListView{
    id: listViewWord
    width: parent.width
    height: 50
    orientation: ListView.Horizontal
    anchors.bottom: parent.bottom
    anchors.bottomMargin: inputGrid.height+40
    interactive: false
    //wordOutputModel Input_ The clicked letter in GridView in Grid
    model: ListModel{
        id: wordOutputModel
    }

    delegate: MouseArea{
        width: 50
        height: parent.width/10

        Label{
            anchors.centerIn: parent
            font.pointSize: 20
            font.bold: true
            color: "white"
            text: letter
        }
    }
}

Input_Grid{
    id: inputGrid
}

Input_Grid.qml

import QtQuick 2.9
import QtQuick.Controls 2.5

GridView {
    id: inputGrid
    width: parent.width
    height: parent.height/3
    anchors.bottom: parent.bottom
    cellHeight: 50
    cellWidth: 50
    //Letters in GridView
    model: ListModel{
        ListElement{
            letter: "A"
        }
        ListElement{
            letter: "B"
        }
        ListElement{
            letter: "C"
        }
        ListElement{
            letter: "D"
        }
        ListElement{
            letter: "E"
        }
        ListElement{
            letter: "F"
        }
        ListElement{
            letter: "G"
        }
        ListElement{
            letter: "H"
        }
        ListElement{
            letter: "I"
        }
        ListElement{
            letter: "J"
        }
        ListElement{
            letter: "K"
        }
        ListElement{
            letter: "L"
        }
        ListElement{
            letter: "M"
        }
        ListElement{
            letter: "N"
        }
        ListElement{
            letter: "O"
        }
        ListElement{
            letter: "P"
        }
        ListElement{
            letter: "Q"
        }
        ListElement{
            letter: "R"
        }
        ListElement{
            letter: "S"
        }
        ListElement{
            letter: "T"
        }
        ListElement{
            letter: "U"
        }
        ListElement{
            letter: "V"
        }
        ListElement{
            letter: "W"
        }
        ListElement{
            letter: "X"
        }
        ListElement{
            letter: "Y"
        }
        ListElement{
            letter: "Z"
        }
    }
    delegate: MouseArea{
        width: 50
        height: width
        onClicked: {
            borderRec.border.width = 5
            //Check if letter is included in the Word
            //If a letter exists in currentWord,
            if(currentWord.word.match(letter)){
                // Found Letter
                console.log("Yes Letter:" + letter + " is in the Word")
                var index = currentWord.word.indexOf(letter)
                //Print the index of latter in currentWord
                console.log(index)
                //Insert a character into the corresponding position in the wordOutputModel
                wordOutputModel.insert(index, {"letter": letter})
            }else{
                //Player did not find letter
                console.log("NO Letter:" + letter + " is in the Word")
                hangManCounter++
                //Call buildHangMan to convert the corresponding rec4 visible = true
                buildHangMan()
            }
            //Otherwise win
            winCheck()
        }

        Rectangle{
            id: borderRec
            anchors.fill: parent
            color: "transparent"
            border.width: 0
            border.color: "white"

            Label{
                anchors.centerIn: parent
                text: letter
                font.pointSize: 10
                font.bold: true
                color: "white"
            }
        }
    }
    function winCheck(){
        if(wordOutputModel.count == wordCount){
            //Player has won
            console.log("Player won")
            swipeView.setCurrentIndex(2)
            winLossLabel.text = "WON"
            wordOutputModel.clear()
            listModelCount.clear()
        }else if(hangManCounter == 10){
            //Hang-Man is complete
            console.log("Player lost")
            swipeView.setCurrentIndex(2)
            winLossLabel.text = "LOST"
            winLossLabel.color = "#d23742"
            wordOutputModel.clear()
            listModelCount.clear()
        }
    }

    function buildHangMan(){
        if(hangManCounter == 1){
            rec1.visible = true
        }else if(hangManCounter == 2){
            rec2.visible = true
        }else if(hangManCounter == 3){
            rec3.visible = true
        }else if(hangManCounter == 4){
            rec4.visible = true
        }else if(hangManCounter == 5){
            rec5.visible = true
        }else if(hangManCounter == 6){
            rec6.visible = true
        }else if(hangManCounter == 7){
            rec7.visible = true
        }else if(hangManCounter == 8){
            rec8.visible = true
        }else if(hangManCounter == 9){
            rec9.visible = true
        }else if(hangManCounter == 10){
            rec10.visible = true
        }
    }
}