File: main.qml

package info (click to toggle)
lager 0.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,916 kB
  • sloc: cpp: 10,967; javascript: 10,433; makefile: 214; python: 100; sh: 98
file content (113 lines) | stat: -rw-r--r-- 2,823 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import QtQml 2.11
import QtQuick 2.6
import QtQuick.Controls 2.4

import QtQuick.Dialogs 1.3

ApplicationWindow {
    id: window

    width: 500
    height: 500
    visible: true

    property int modelScale: width / game.width

    readonly property int initial_timer_period: 100
    readonly property int timer_decrement: 10
    readonly property int min_timer_period: 10
    readonly property int timer_change_points: 5
    readonly property int initial_snake_size: 3

    Timer {
        interval: Math.max(initial_timer_period - timer_decrement *
                            Math.floor((snake.count - initial_snake_size) / timer_change_points)
                           , min_timer_period)
        repeat: true
        running: !game.over
        onTriggered: { game.tick() }
    }

    MessageDialog {
        id: overDialog
        text: qsTr("Game Over")
        onAccepted: {
            game.reset()
        }
    }

    Connections {
        target: game
        onOverChanged: {
            if (game.over)
                overDialog.open()
        }
    }

    Connections {
        target: game
        onApplePositionChanged: {
            apple.state == "other" ? apple.state = "" : apple.state = "other"
        }
    }

    Item {
        anchors.fill: parent

        Rectangle {
            id: background
            color: "black"
            anchors.fill: parent
        }

        focus: true
        Keys.onPressed: {
            if (event.key == Qt.Key_Left) {
                game.left()
                event.accepted = true
            }
            else if (event.key == Qt.Key_Right) {
                game.right()
                event.accepted = true
            }
            else if (event.key == Qt.Key_Up) {
                game.up()
                event.accepted = true
            }
            else if (event.key == Qt.Key_Down) {
                game.down()
                event.accepted = true
            }
        }

        Rectangle {
            id: apple
            width: modelScale
            height: modelScale
            x: game.applePosition.x * modelScale
            y: game.applePosition.y * modelScale
            color: "red"
            states: [
                State {
                    name: "other"
                    changes: [
                        PropertyChanges { target: apple; color: "green" }
                    ]
                }
            ]
        }

        Repeater {
            id: snake
            model: game.snake
            property color color: "dark green"
            delegate: Rectangle {
                width: modelScale
                height: modelScale
                x: model.display.x * modelScale
                y: model.display.y * modelScale
                color: snake.color
            }
        }
    }
}