-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpong.qml
187 lines (183 loc) · 3.97 KB
/
pong.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
import QtQuick 2.7
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.1
import QtQuick.Window 2.2
import Pong 1.0
ApplicationWindow {
id: window
visible: true
width: Screen.desktopAvailableWidth * 0.8
height: Screen.desktopAvailableHeight * 0.8
title: "Pong"
function rel_to_abs_x(x) {
var width = window.width - paddleLeft.width - paddleRight.width - ball.width;
return (x * width/100) + paddleLeft.width;
}
function rel_to_abs_y(y) {
var height = window.height - ball.height
return y * height/100
}
function abs_to_rel_x(x) {
x -= paddleLeft.width
var width = window.width - paddleLeft.width - paddleRight.width - ball.width;
return x * 100/width
}
function abs_to_rel_y(y) {
var height = window.height - ball.height
return y * 100/height
}
Rectangle {
// line in the middle
// handles input
focus: true
Keys.onPressed: {
if (event.key == Qt.Key_W) {
frame.leftPaddle.down()
event.accepted = true;
} else if (event.key == Qt.Key_S) {
frame.leftPaddle.up()
event.accepted = true;
}
if (event.key == Qt.Key_Up) {
frame.rightPaddle.down()
event.accepted = true;
} else if (event.key == Qt.Key_Down) {
frame.rightPaddle.up()
event.accepted = true;
}
}
Keys.onReleased: {
if (event.key == Qt.Key_W
|| event.key == Qt.Key_S) {
frame.leftPaddle.stop()
event.accepted = true;
}
if (event.key == Qt.Key_Up
|| event.key == Qt.Key_Down) {
frame.rightPaddle.stop()
event.accepted = true;
}
}
id: "line"
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
anchors.bottom: parent.bottom
width: 5
color: "grey"
}
Text {
id: scoreLeft
color: "grey"
anchors.topMargin: window.height/15
anchors.top: parent.top
anchors.right: parent.horizontalCenter
anchors.rightMargin: window.height/15
font.pointSize: window.height/5
font.family: "Bit5x3"
text: "0"
//text: frame.score_left
}
Text {
id: scoreRight
color: "grey"
anchors.topMargin: window.height/15
anchors.top: parent.top
anchors.left: parent.horizontalCenter
anchors.leftMargin: window.height/15
font.pointSize: window.height/5
font.family: "Bit5x3"
text: "0"
//text: frame.score_right
}
Rectangle {
id: ball
width: 40
height: width
x: window.width/2 - width/2
y: window.height/2 - width/2
radius: width/2
color: "red"
ParallelAnimation {
id: moveBall
PropertyAnimation {
target: ball
properties: "x"
to: rel_to_abs_x(frame.to_x)
duration: frame.time
}
PropertyAnimation {
target: ball
properties: "y"
to: rel_to_abs_y(frame.to_y)
duration: frame.time
}
onRunningChanged: {
if (moveBall.running) {
frame.sound()
}
if (!moveBall.running) {
frame.stoped()
frame.bounce(abs_to_rel_x(ball.x), abs_to_rel_y(ball.y))
//moveBall.start()
}
}
}
}
Rectangle {
id: paddleRight
anchors.leftMargin: 10
anchors.right: parent.right
y: right.pos*(window.height/100) - height/2
width: window.width/70
height: 2 * right.size * (window.height/100)
color: "black"
}
Rectangle {
id: paddleLeft
anchors.rightMargin: 10
anchors.left: parent.left
y: left.pos*(window.height/100) - height/2
width: window.width/70
height: 2 * left.size * (window.height/100)
color: "green"
}
Paddle {
id: right
side: -1
pos: 50
size: 10
onMove: {
paddleRight.y = pos*(window.height/100) - paddleRight.height/2
moveBall.start()
}
}
Paddle {
id: left
side: 1
pos: 50
size: 10
onMove: {
paddleLeft.y = pos*(window.height/100) - paddleLeft.height/2
moveBall.start()
}
}
Frame {
id: frame
leftPaddle: left
rightPaddle: right
height: window.height
width: window.width
onRunBall: {
//ball.x = rel_to_abs_x(50)
//ball.y = rel_to_abs_y(50)
moveBall.start()
count = count + 1
}
onResetBall: {
ball.x = window.width/2 - ball.width/2
ball.y = window.height/2 - ball.width/2
scoreRight.text = right_score()
scoreLeft.text = left_score()
}
}
}