-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.qml
204 lines (199 loc) · 6.18 KB
/
main.qml
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1
import "game.js" as Game
Window {
visible: true
width: 300
height: 400
color: 'black'
Rectangle {
id: gameField
property int cellSize: 20
property int cols: 10
property int rows: 20
property int score
property int totalRowsKilled
property int level: totalRowsKilled / 10
width: cellSize * cols
height: cellSize * rows
color: 'black'
focus: true
state: ''
signal rowsKilled (int rows)
GameBlock {
id: gameBlock
game: Game
onGameOver: gameField.state = 'GAMEOVER'
}
Keys.onPressed: {
if (state === '') {
if (event.key === Qt.Key_Left)
gameBlock.left()
else if (event.key === Qt.Key_Right)
gameBlock.right()
else if (event.key === Qt.Key_Down) {
gameBlock.stop()
gameBlock.down()
gameBlock.restart()
}
else if (event.key === Qt.Key_Space)
gameBlock.drop()
else if (event.key === Qt.Key_Up)
gameBlock.rotate()
else if (event.key === Qt.Key_P)
pauseGame()
}
else if (state === 'PAUSED') {
if (event.key === Qt.Key_P)
resumeGame()
}
else if (state == 'GAMEOVER') {
if (event.key === Qt.Key_N)
newGame()
}
}
onRowsKilled: {
totalRowsKilled += rows
score += Game.scoreKilledRows(level, rows)
}
Column {
id: gameOverMessage
anchors.centerIn: parent
visible: false
opacity: 0.0
z: 10
Behavior on opacity { NumberAnimation { duration: 2000 } }
Text {
text: "Game Over"
font.pointSize: 32
style: Text.Outline
color: 'white'
}
Text {
text: "Press n for new game"
anchors.horizontalCenter: parent.horizontalCenter
font.pointSize: 12
style: Text.Outline
color: 'white'
}
}
Column {
id: pauseMessage
anchors.centerIn: parent
visible: false
opacity: 0.0
z: 10
Behavior on opacity { NumberAnimation { duration: 1000 } }
Text {
text: "Game Paused"
font.pointSize: 32
style: Text.Outline
color: 'white'
}
Text {
text: "Press p to continue"
anchors.horizontalCenter: parent.horizontalCenter
font.pointSize: 12
style: Text.Outline
color: 'white'
}
}
states: [
State {
name: 'PAUSED'
PropertyChanges { target: pauseMessage; visible: true; opacity: 1.0 }
},
State {
name: 'GAMEOVER'
PropertyChanges { target: gameOverMessage; visible: true; opacity: 1.0}
PropertyChanges { target: gameBlock; visible: false }
}
]
}
ColumnLayout {
spacing: 2
anchors { left: gameField.right; top: parent.top; bottom: parent.bottom; right: parent.right }
Rectangle {
color: 'black'
border { color: 'blue'; width: 2 }
radius: 10
Layout.fillWidth: true
Layout.preferredHeight: 80
Column {
anchors {fill: parent; margins: 5 }
Text {
text: 'SCORE: ' + gameField.score
anchors.horizontalCenter: parent.horizontalCenter
color: 'white'
}
Text {
text: 'LEVEL: ' + gameField.level
anchors.horizontalCenter: parent.horizontalCenter
color: 'white'
}
Text {
text: 'ROWS: ' + gameField.totalRowsKilled
anchors.horizontalCenter: parent.horizontalCenter
color: 'white'
}
Text {
text: 'SPEED: ' + gameBlock.speed
anchors.horizontalCenter: parent.horizontalCenter
color: 'white'
}
}
}
Rectangle {
color: 'black'
border { color: 'blue'; width: 2 }
radius: 10
Layout.fillWidth: true
Layout.preferredHeight: 80
Text {
id: next
text: 'NEXT'
anchors { top: parent.top; topMargin: 5; horizontalCenter: parent.horizontalCenter }
color: 'white'
}
Block {
id: nextBlock
col: 1
row: 1
anchors { centerIn: parent; verticalCenterOffset: 5 }
Connections {
target: gameBlock
onNewState: nextBlock.state = nextState
}
}
}
Rectangle {
color: 'black'
border { color: 'blue'; width: 2 }
radius: 10
Layout.fillWidth: true
Layout.fillHeight: true
Text {
text: 'HIGHSCORES'
anchors { top: parent.top; topMargin: 5; horizontalCenter: parent.horizontalCenter }
color: 'white'
}
}
}
Component.onCompleted: Game.init(gameField);
function newGame() {
Game.reset(gameField)
gameField.score = 0
gameField.totalRowsKilled = 0
gameField.state = ''
gameBlock.reset()
}
function pauseGame() {
gameBlock.stop()
gameField.state = 'PAUSED'
}
function resumeGame() {
gameBlock.restart()
gameField.state = ''
}
}