Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.qml:
- import QtQuick 2.9
- import QtQuick.Window 2.2
- import "logic.js" as Logic
- Window {
- property bool stop: true
- property bool first: true
- property bool loose: false
- property string text: "Press Space"
- property int score: 0
- property int w: 400
- property int h: 400
- property int interval: 100
- id: mainWindow
- visible: true
- width: w
- height: h
- color: "transparent"
- minimumWidth: w
- maximumWidth: w
- minimumHeight: h
- maximumHeight: h
- title: qsTr("Snake")
- Text {
- text: mainWindow.text
- font.family: "Helvetica"
- font.pointSize: 24
- color: "red"
- x: mainWindow.w/2 - width/2
- y: mainWindow.h/2 - height/2 - 16
- }
- Text {
- text: "punkty " + mainWindow.score
- color: "red"
- }
- Item {
- focus: true
- Keys.onPressed: {
- if (event.key === Qt.Key_Up) Logic.turnUp()
- else if (event.key === Qt.Key_Down) Logic.turnDown()
- else if (event.key === Qt.Key_Left) Logic.turnLeft()
- else if (event.key === Qt.Key_Right) Logic.turnRight()
- else if (event.key === Qt.Key_Space) {
- if (first) {
- first = false
- stop = false
- text = ""
- }
- else if (loose) {
- text = ""
- loose = false
- stop = false
- Logic.clear()
- mainWindow.interval = 100
- }
- }
- }
- }
- Timer {
- id: timer
- interval: mainWindow.interval
- running: true
- repeat: true
- onTriggered: {
- if (!mainWindow.stop) {
- if (!Logic.update()) {
- mainWindow.loose = true
- mainWindow.stop = true
- mainWindow.text = "Game Over!"
- }
- else {
- score = Logic.score
- if (score > 10) mainWindow.interval = 90
- else if (score > 40) mainWindow.interval = 80
- else if (score > 80) mainWindow.interval = 70
- else if (score > 120) mainWindow.interval = 60
- }
- }
- }
- }
- Component.onCompleted: {
- Logic.start()
- }
- }
- // Segment.qml:
- import QtQuick 2.0
- Rectangle {
- id: root
- width: 20
- height: 20
- x: 0
- y: 0
- border.width: 2
- border.color: "#111111"
- //color: "#ff1111"
- color: "#172208"
- function ded() {
- root.destroy()
- }
- }
- // logic.js:
- .import QtQuick 2.0 as Quick
- var snake = new Array
- var food = null
- var component = Qt.createComponent("Segment.qml")
- var vx = 0
- var vy = -1
- var score = 0
- function start() {
- snake.push(createSegment(mainWindow.width/2, mainWindow.height/2, "#ff1111"))
- createFood()
- }
- function createSegment(x, y, color) {
- var dynamicObject = component.createObject(mainWindow)
- if (dynamicObject === null) {
- console.log(component.errorString())
- return false
- }
- dynamicObject.x = x
- dynamicObject.y = y
- dynamicObject.color = color
- //dynamicObject.opacity = 1
- return dynamicObject
- }
- function forward() {
- for (var i = snake.length-1; i > 0; i--) {
- snake[i].x = snake[i-1].x
- snake[i].y = snake[i-1].y
- }
- snake[0].x += 20*vx
- snake[0].y += 20*vy
- }
- function eat() {
- food.color = "#ff1111"
- food.x = snake[snake.length-1].x
- food.y = snake[snake.length-1].y
- snake.push(food)
- }
- function turnUp() {
- if (vy === 1 && snake.length > 1) return
- else {
- vx = 0
- vy = -1
- }
- }
- function turnDown() {
- if (vy === -1 && snake.length > 1) return
- else {
- vx = 0
- vy = 1
- }
- }
- function turnRight() {
- if (vx === -1 && snake.length > 1) return
- else {
- vx = 1
- vy = 0
- }
- }
- function turnLeft() {
- if (vx === 1 && snake.length > 1) return
- else {
- vx = -1
- vy = 0
- }
- }
- function createFood() {
- var repeat = true
- var x
- var y
- while (repeat) {
- repeat = false
- x = parseInt(Math.random()*mainWindow.w/20)*20
- y = parseInt(Math.random()*mainWindow.h/20)*20
- for (var i = 0; i < snake.length; i++) {
- if (x === snake[i].x && y === snake[i].y) repeat = true
- }
- }
- food = createSegment(x, y, "#11ff11")
- }
- function update() {
- forward()
- if (collision()) return false
- if (food.x === snake[0].x && food.y === snake[0].y) {
- eat()
- score++
- createFood()
- }
- return true
- }
- function collision() {
- for (var i = 1; i < snake.length; i++) {
- if (snake[0].x === snake[i].x && snake[0].y === snake[i].y) return true
- }
- if (snake[0].x < 0 || snake[0].x > mainWindow.w - 20 || snake[0].y < 0 || snake[0].y > mainWindow.h - 20) {
- return true
- }
- return false
- }
- function clear() {
- for (var i = snake.length-1; i > 0; i--) {
- snake[i].ded()
- snake.pop()
- }
- snake[0].x = mainWindow.width/2
- snake[0].y = mainWindow.height/2
- vx = 0
- vy = -1
- score = 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement