From e2da5f12a673efc393d6858cf18a7858b7359760 Mon Sep 17 00:00:00 2001 From: jaroshevskii Date: Wed, 9 Nov 2022 15:58:07 +0200 Subject: [PATCH] Update --- Package.resolved | 2 +- Sources/Snake/Field.swift | 18 +++-- Sources/Snake/Fruit.swift | 2 +- Sources/Snake/Game.swift | 144 +++++++++++++++++++++++++++-------- Sources/Snake/Point2D.swift | 4 +- Sources/Snake/Settings.swift | 22 ++++++ Sources/Snake/Snake.swift | 24 +++++- Sources/Snake/Theme.swift | 44 +++++++++++ Sources/Snake/main.swift | 3 +- 9 files changed, 217 insertions(+), 46 deletions(-) create mode 100644 Sources/Snake/Settings.swift create mode 100644 Sources/Snake/Theme.swift diff --git a/Package.resolved b/Package.resolved index 2ee218e..dc30341 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,7 +6,7 @@ "location" : "https://github.com/STREGAsGate/Raylib.git", "state" : { "branch" : "master", - "revision" : "e7cbfd48d26f85994fecc85881b4f38ca23516a3" + "revision" : "65eb7c5b699c7fe1078b13d7fa87e4871b57943e" } } ], diff --git a/Sources/Snake/Field.swift b/Sources/Snake/Field.swift index ebab30a..319bbff 100644 --- a/Sources/Snake/Field.swift +++ b/Sources/Snake/Field.swift @@ -12,24 +12,30 @@ class Field { let render: RenderTexture2D let renderDrawPosition: Point2D - init(windowWidth: Int32, windowHeight: Int32) { - squareSize = 20 + var backgroundColor: Color + var color: Color + + init( + squareSize: Int32, windowWidth: Int32, windowHeight: Int32, backgroundColor: Color, color: Color + ) { + self.squareSize = squareSize columnsCount = windowWidth / squareSize - 1 rowsCount = windowHeight / squareSize - 1 render = Raylib.loadRenderTexture(columnsCount * squareSize, rowsCount * squareSize) + self.backgroundColor = backgroundColor + self.color = color + Raylib.beginTextureMode(render) - Raylib.clearBackground( - Color(r: 26, g: 26, b: 36, a: 255) // Dark themme - ) + Raylib.clearBackground(backgroundColor) // Draw notebook cells for x in 0..= field.columnsCount { + snake.headPosition.x = 0 + } + if snake.headPosition.y < 0 { + snake.headPosition.y = field.rowsCount - 1 + } else if snake.headPosition.y >= field.rowsCount { + snake.headPosition.y = 0 + } + + if snake.headPosition == fruit.position { + snake.tail.append(snake.headPreviousPosition) + fruit.position = Point2D( + x: Int32.random(in: 0..