Skip to content

Commit

Permalink
rocket works
Browse files Browse the repository at this point in the history
  • Loading branch information
kiltum committed Sep 30, 2018
1 parent 16b2d45 commit 3a176cd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
8 changes: 3 additions & 5 deletions pingpong/PingPong/Ball.qml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import QtMultimedia 5.8
Item {
id: mainBall
property bool run: true
property int speed: 58
property int speed: 10
property bool mute: true
property int velocityX: -speed
property int velocityY: -speed

property int size: 23
property int size: 20

property int limitX: parent.width
property int limitY: parent.height
Expand All @@ -28,7 +28,7 @@ Item {

Timer {
running: parent.run
interval: 500
interval: 50
repeat: true
onTriggered: {
parent.x = parent.x + velocityX
Expand Down Expand Up @@ -69,8 +69,6 @@ Item {
if (mainBall.y > limitY - mainBall.size/2) {
mainBall.y = limitY - mainBall.size/2
}

console.log(parent.x, parent.y)
mainBall.moved(parent.x, parent.y)
}
}
Expand Down
7 changes: 7 additions & 0 deletions pingpong/PingPong/Racket.qml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ Item {
if(racket.y < 0) {
racket.y = 0
}



racket.moved(y, size)
}
Keys.onDownPressed: {
if(racket.y < (limitY - size) ) {
racket.y = racket.y + speed
}
if((racket.y + racket.size) > limitY) {
racket.y = limitY - racket.size
}

racket.moved(y, size)
}
}
39 changes: 33 additions & 6 deletions pingpong/PingPong/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,64 @@ import QtQuick 2.9
import QtQuick.Window 2.2

Window {
id: main
visible: true
width: 640
height: 480
title: qsTr("Hello World")

property int ballX: 100
property int ballY: 100
property int ballSize: 10
property int racketY: 30
property int racketSize: 100
property int racketX: pole.width - 30
property int racketSize: 150

Rectangle {
id: pole
x: 50
y: 50
width: 500
height: 400
color: "blue"

Ball {
x: 50
y: 100
id: ball
x: ballX
y: ballY
size: ballSize
onMoved: {
// console.log(x)
ballX = x
ballY = y
checkCollide()
}
}

Racket {
x: parent.width-20
x: racketX
y: racketY
size: racketSize
onMoved: {
console.log(y)
racketY = y
racketSize = size
checkCollide()
}
}
}

function checkCollide() {
if((ballX + ballSize) >= racketX) { // no need to check if ball far left from racket
if((ballY + ballSize) >= racketY) { // ball below top corner of rocket
if((ballY + ballSize) <= racketY+racketSize) {
console.log("rocket")
ball.velocityX = -ball.speed
}
else {
console.log("HIT")
}
}
else {
console.log("hit")
}
}
}
Expand Down

0 comments on commit 3a176cd

Please sign in to comment.