Warning, /webapps/qmlonline/qml/examples/snake.qml is written in an unsupported language. File is not indexed.

0001 import QtQuick 2.7
0002 import QtQuick.Layouts 1
0003 
0004 Rectangle {
0005     anchors.fill: parent
0006     color: "black"
0007 
0008     // 4Hz engine
0009     Timer {
0010         running: true; repeat: true; interval: 1000/4
0011         onTriggered: snake.update()
0012     }
0013 
0014     Item {
0015         id: snake
0016         z: -1
0017         property var initialBody: [Qt.point(2, 0), Qt.point(1, 0), Qt.point(0, 0)]
0018         property var body: initialBody
0019         property var direction: Qt.point(1, 0)
0020         property var command: "right"
0021 
0022         function update(where) {
0023             if(!where) {
0024                 where = command
0025             } else {
0026                 command = where
0027             }
0028 
0029             switch(where) {
0030                 case "up": {
0031                     // There is no reverse direction
0032                     if (direction.y != 1) {
0033                         direction.x = 0;
0034                         direction.y = -1;
0035                     }
0036                     break
0037                 }
0038                 case "down": {
0039                     if (direction.y != -1) {
0040                         direction.x = 0;
0041                         direction.y = 1;
0042                     }
0043                     break
0044                 }
0045                 case "right": {
0046                     if (direction.x != -1) {
0047                         direction.x = 1;
0048                         direction.y = 0;
0049                     }
0050                     break
0051                 }
0052                 case "left": {
0053                     if (direction.x != 1) {
0054                         direction.x = -1;
0055                         direction.y = 0;
0056                     }
0057                     break
0058                 }
0059             }
0060             move()
0061         }
0062 
0063         function checkCollision(array) {
0064             for(var i = 0; i < array.length; i++) {
0065                 for(var u = i + 1; u < array.length; u++) {
0066                     if(array[i].x == array[u].x && array[i].y == array[u].y) {
0067                         return true
0068                     }
0069                 }
0070             }
0071 
0072             return false
0073         }
0074 
0075         function move() {
0076             var head = snake.body[0]
0077 
0078             // Bend space to do jumps
0079             var newPoint = Qt.point(
0080                 (head.x + direction.x) % grid.columns,
0081                 (head.y + direction.y) % grid.rows
0082             )
0083             if(newPoint.x < 0) {
0084                 newPoint.x += 10
0085             }
0086             if(newPoint.y < 0) {
0087                 newPoint.y += 10
0088             }
0089 
0090             // Check if snake is eating apple
0091             if(newPoint.x != grid.food.x || newPoint.y != grid.food.y) {
0092                 snake.body.pop()
0093             } else {
0094                 grid.newFood();
0095             }
0096             snake.body.unshift(newPoint)
0097 
0098             // Check if we are hitting ourselfs
0099             if(checkCollision(snake.body)) {
0100                 snake.body = [Qt.point(2, 0), Qt.point(1, 0), Qt.point(0, 0)]
0101             }
0102 
0103             repeater.reload()
0104         }
0105 
0106     }
0107 
0108     GridLayout {
0109         id: grid
0110         rows: 15
0111         columns: 15
0112         columnSpacing: 0
0113         rowSpacing: 0
0114         anchors.fill: parent
0115 
0116         readonly property int blockWidth: width / columns
0117         readonly property int blockHeight: height / rows
0118 
0119         property var food: newFood()
0120         function newFood() {
0121             // 0.1 is to help the food to be inside grid
0122             food = Qt.point(Math.floor(Math.random() * (columns - 0.1)), Math.floor(Math.random() * (rows - 0.1)))
0123             return food
0124         }
0125 
0126         Repeater {
0127             id: repeater
0128             model: parent.rows * parent.columns
0129 
0130             Rectangle {
0131                 property var snakeBody: snake.body
0132                 property var value: {
0133                     for(var local of snake.body) {
0134                         if(index == local.x  + local.y * grid.rows // Snake body
0135                             || index == grid.food.x + grid.food.y * grid.rows) { // food
0136                             return 1
0137                         }
0138                     }
0139                     return 0
0140                 }
0141                 color: Qt.rgba(value, value, value)
0142 
0143                 Layout.fillWidth: true
0144                 Layout.fillHeight: true
0145 
0146             }
0147 
0148             // Force model update
0149             function reload() {
0150                 model = []
0151                 model = parent.rows * parent.columns
0152 
0153             }
0154         }
0155     }
0156 
0157     // User input
0158     Item {
0159         z: 100000
0160         focus: true
0161         anchors.fill: parent
0162 
0163         Keys.onPressed: {
0164             switch(event.key) {
0165                 case Qt.Key_Up: {
0166                     snake.command = "up"
0167                     break;
0168                 }
0169                 case Qt.Key_Down: {
0170                     snake.command = "down"
0171                     break;
0172                 }
0173                 case Qt.Key_Left: {
0174                     snake.command = "left"
0175                     break;
0176                 }
0177                 case Qt.Key_Right: {
0178                     snake.command = "right"
0179                     break;
0180                 }
0181             }
0182         }
0183     }
0184 }