Advertisement
czaffik

Snake

Oct 12th, 2019
2,848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 5.74 KB | None | 0 0
  1. // main.cpp:
  2. #include <QGuiApplication>
  3. #include <QQmlApplicationEngine>
  4.  
  5. int main(int argc, char *argv[]) {
  6.     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  7.  
  8.     QGuiApplication app(argc, argv);
  9.  
  10.     QQmlApplicationEngine engine;
  11.     engine.load(QUrl(QStringLiteral("main.qml")));
  12.     if (engine.rootObjects().isEmpty())
  13.         return -1;
  14.  
  15.     return app.exec();
  16. }
  17.  
  18. // main.qml:
  19. import QtQuick 2.9
  20. import QtQuick.Window 2.2
  21. import "logic.js" as Logic
  22.  
  23. Window {
  24.     property bool stop: true
  25.     property bool first: true
  26.     property bool loose: false
  27.  
  28.     property string text: "Press Space"
  29.     property int score: 0
  30.     property int w: 400
  31.     property int h: 400
  32.     property int interval: 100
  33.  
  34.     id: mainWindow
  35.     visible: true
  36.     width: w
  37.     height: h
  38.     color: "transparent"
  39.     minimumWidth: w
  40.     maximumWidth: w
  41.     minimumHeight: h
  42.     maximumHeight: h
  43.     title: qsTr("Snake")
  44.  
  45.     Text {
  46.         text: mainWindow.text
  47.         font.family: "Helvetica"
  48.         font.pointSize: 24
  49.         color: "red"
  50.         x: mainWindow.w/2 - width/2
  51.         y: mainWindow.h/2 - height/2 - 16
  52.     }
  53.  
  54.     Text {
  55.         text: "punkty " + mainWindow.score
  56.         color: "red"
  57.     }
  58.  
  59.     Item {
  60.         focus: true
  61.         Keys.onPressed: {
  62.             if      (event.key === Qt.Key_Up)    Logic.turnUp()
  63.             else if (event.key === Qt.Key_Down)  Logic.turnDown()
  64.             else if (event.key === Qt.Key_Left)  Logic.turnLeft()
  65.             else if (event.key === Qt.Key_Right) Logic.turnRight()
  66.             else if (event.key === Qt.Key_Space) {
  67.                 if (first) {
  68.                     first = false
  69.                     stop = false
  70.                     text = ""
  71.                 }
  72.                 else if (loose) {
  73.                     text = ""
  74.                     loose = false
  75.                     stop = false
  76.                     Logic.clear()
  77.                     mainWindow.interval = 100
  78.                 }
  79.             }
  80.         }
  81.     }
  82.  
  83.     Timer {
  84.         id: timer
  85.         interval: mainWindow.interval
  86.         running: true
  87.         repeat: true
  88.         onTriggered: {
  89.             if (!mainWindow.stop) {
  90.                 if (!Logic.update()) {
  91.                     mainWindow.loose = true
  92.                     mainWindow.stop = true
  93.                     mainWindow.text = "Game Over!"
  94.                 }
  95.                 else {
  96.                     score = Logic.score
  97.                     if (score > 10) mainWindow.interval = 90
  98.                     else if (score > 40) mainWindow.interval = 80
  99.                     else if (score > 80) mainWindow.interval = 70
  100.                     else if (score > 120) mainWindow.interval = 60
  101.                 }
  102.             }
  103.         }
  104.     }
  105.  
  106.     Component.onCompleted: {
  107.         Logic.start()
  108.     }
  109. }
  110.  
  111. // Segment.qml:
  112. import QtQuick 2.0
  113.  
  114. Rectangle {
  115.     id: root
  116.     width: 20
  117.     height: 20
  118.  
  119.     x: 0
  120.     y: 0
  121.  
  122.     border.width: 2
  123.     border.color: "#111111"
  124.     //color: "#ff1111"
  125.     color: "#172208"
  126.  
  127.     function ded() {
  128.         root.destroy()
  129.     }
  130. }
  131.  
  132. // logic.js:
  133. .import QtQuick 2.0 as Quick
  134.  
  135. var snake = new Array
  136. var food = null
  137. var component = Qt.createComponent("Segment.qml")
  138.  
  139. var vx = 0
  140. var vy = -1
  141.  
  142. var score = 0
  143.  
  144. function start() {
  145.     snake.push(createSegment(mainWindow.width/2, mainWindow.height/2, "#ff1111"))
  146.     createFood()
  147. }
  148.  
  149. function createSegment(x, y, color) {
  150.     var dynamicObject = component.createObject(mainWindow)
  151.     if (dynamicObject === null) {
  152.         console.log(component.errorString())
  153.         return false
  154.     }
  155.  
  156.     dynamicObject.x = x
  157.     dynamicObject.y = y
  158.     dynamicObject.color = color
  159.     //dynamicObject.opacity = 1
  160.     return dynamicObject
  161. }
  162.  
  163. function forward() {
  164.     for (var i = snake.length-1; i > 0; i--) {
  165.         snake[i].x = snake[i-1].x
  166.         snake[i].y = snake[i-1].y
  167.     }
  168.  
  169.     snake[0].x += 20*vx
  170.     snake[0].y += 20*vy
  171. }
  172.  
  173. function eat() {
  174.     food.color = "#ff1111"
  175.     food.x = snake[snake.length-1].x
  176.     food.y = snake[snake.length-1].y
  177.     snake.push(food)
  178. }
  179.  
  180.  
  181. function turnUp() {
  182.     if (vy === 1 && snake.length > 1) return
  183.     else {
  184.         vx = 0
  185.         vy = -1
  186.     }
  187. }
  188.  
  189. function turnDown() {
  190.     if (vy === -1 && snake.length > 1) return
  191.     else {
  192.         vx = 0
  193.         vy = 1
  194.     }
  195. }
  196.  
  197. function turnRight() {
  198.     if (vx === -1 && snake.length > 1) return
  199.     else {
  200.         vx = 1
  201.         vy = 0
  202.     }
  203. }
  204.  
  205. function turnLeft() {
  206.     if (vx === 1 && snake.length > 1) return
  207.     else {
  208.         vx = -1
  209.         vy = 0
  210.     }
  211. }
  212.  
  213. function createFood() {
  214.     var repeat = true
  215.     var x
  216.     var y
  217.  
  218.     while (repeat) {
  219.         repeat = false
  220.         x = parseInt(Math.random()*mainWindow.w/20)*20
  221.         y = parseInt(Math.random()*mainWindow.h/20)*20
  222.         for (var i = 0; i < snake.length; i++) {
  223.             if (x === snake[i].x && y === snake[i].y) repeat = true
  224.         }
  225.     }
  226.  
  227.     food = createSegment(x, y, "#11ff11")
  228. }
  229.  
  230. function update() {
  231.     forward()
  232.     if (collision()) return false
  233.     if (food.x === snake[0].x && food.y === snake[0].y) {
  234.         eat()
  235.         score++
  236.         createFood()
  237.     }
  238.  
  239.     return true
  240. }
  241.  
  242. function collision() {
  243.     for (var i = 1; i < snake.length; i++) {
  244.         if (snake[0].x === snake[i].x && snake[0].y === snake[i].y) return true
  245.     }
  246.  
  247.     if (snake[0].x < 0 || snake[0].x > mainWindow.w - 20 || snake[0].y < 0 || snake[0].y > mainWindow.h - 20) {
  248.         return true
  249.     }
  250.  
  251.     return false
  252. }
  253.  
  254. function clear() {
  255.     for (var i = snake.length-1; i > 0; i--) {
  256.         snake[i].ded()
  257.         snake.pop()
  258.     }
  259.     snake[0].x = mainWindow.width/2
  260.     snake[0].y = mainWindow.height/2
  261.     vx = 0
  262.     vy = -1
  263.     score = 0
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement