Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroshevskii committed Nov 9, 2022
1 parent 6ff39d8 commit e2da5f1
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"location" : "https://github.com/STREGAsGate/Raylib.git",
"state" : {
"branch" : "master",
"revision" : "e7cbfd48d26f85994fecc85881b4f38ca23516a3"
"revision" : "65eb7c5b699c7fe1078b13d7fa87e4871b57943e"
}
}
],
Expand Down
18 changes: 12 additions & 6 deletions Sources/Snake/Field.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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..<columnsCount {
for y in 0..<rowsCount {
Raylib.drawRectangle(
x * squareSize + 1, y * squareSize + 1,
squareSize - 2, squareSize - 2,
Color(r: 14, g: 14, b: 18, a: 255) // Dark themme
color
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Snake/Fruit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import Raylib

struct Fruit {
var position: Point2D
var color = Color(r: 230, g: 230, b: 236, a: 255) // Dark themme
var color: Color
}
144 changes: 113 additions & 31 deletions Sources/Snake/Game.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,118 @@
import Raylib

class Game {
// 21:9
private let windowWidth: Int32 = 840
private let windowHeight: Int32 = 360

private var field: Field
private var snake: Snake
private var fruit: Fruit

init() {
Raylib.setConfigFlags(.vsyncHint)
Raylib.initWindow(windowWidth, windowHeight, "Snake")
private var isPause = false

private var textColor: Color

init(settings: Settings) {
if settings.isVsync {
Raylib.setConfigFlags(.vsyncHint)
}
Raylib.initWindow(settings.windowWidth, settings.windowHeight, "Snake")

field = Field(windowWidth: windowWidth, windowHeight: windowHeight)
field = Field(
squareSize: 20,
windowWidth: settings.windowWidth, windowHeight: settings.windowHeight,
backgroundColor: settings.theme.fieldBackgroundColor,
color: settings.theme.fieldColor
)
snake = Snake(
headPosition: Point2D(x: field.columnsCount / 4, y: field.rowsCount / 2),
movement: .right,
speed: 15.0
speed: 15.0,
headColor: settings.theme.snakeHeadColor,
tailColor: settings.theme.snakeTailColor
)
fruit = Fruit(
position: Point2D(
x: field.columnsCount - snake.headPosition.x, y: snake.headPosition.y
)
x: field.columnsCount - 1 - snake.headPosition.x, y: snake.headPosition.y
),
color: settings.theme.fruitColor
)

textColor = settings.theme.textColor
}

deinit {
Raylib.closeWindow()
}

private func input() {
if Raylib.isKeyPressed(.letterP) {
isPause.toggle()
}

if isPause { return }

if Raylib.isKeyPressed(.up) && snake.movement.isHorizontal {
snake.movement = .up
} else if Raylib.isKeyPressed(.down) && snake.movement.isHorizontal {
snake.movement = .down
} else if Raylib.isKeyPressed(.left) && snake.movement.isVertical {
snake.movement = .left
} else if Raylib.isKeyPressed(.right) && snake.movement.isVertical {
snake.movement = .right
}
}

private func update(deltaTime: Float) {
if isPause { return }

snake.move(deltaTime: deltaTime)

if snake.headPosition.x < 0 {
snake.headPosition.x = field.columnsCount - 1
} else if snake.headPosition.x >= 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..<field.columnsCount), y: Int32.random(in: 0..<field.rowsCount)
)
}
}

private func draw() {
Raylib.beginDrawing()
Raylib.clearBackground(
Color(r: 26, g: 26, b: 36, a: 255) // Dark themme
)
Raylib.clearBackground(field.backgroundColor)

// MARK: Field

// Draw filed
Raylib.drawTexture(
field.render.texture, field.renderDrawPosition.x, field.renderDrawPosition.y, .white)
// Texture
field.render.texture,
// Position
field.renderDrawPosition.x, field.renderDrawPosition.y,
// Color
.white
)

// MARK: Snake

// Draw snake tail
for tailItem in snake.tail {
Raylib.drawRectangleRec(
Rectangle(
x: Float(field.renderDrawPosition.x + tailItem.x * field.squareSize),
y: Float(field.renderDrawPosition.y + tailItem.y * field.squareSize),
width: Float(field.squareSize),
height: Float(field.squareSize)
),
snake.tailColor
)
}

// Draw snake head
Raylib.drawRectangleRec(
Expand All @@ -63,20 +129,8 @@ class Game {
snake.headColor
)

// Draw snake tail
for snakeTailItem in snake.tail {
Raylib.drawRectangleRec(
Rectangle(
x: Float(field.renderDrawPosition.x + snakeTailItem.x * field.squareSize),
y: Float(field.renderDrawPosition.y + snakeTailItem.y * field.squareSize),
width: Float(field.squareSize),
height: Float(field.squareSize)
),
snake.tailColor
)
}
// MARK: Fruit

// Draw fruit
Raylib.drawRectangleRec(
Rectangle(
x: Float(field.renderDrawPosition.x + fruit.position.x * field.squareSize),
Expand All @@ -87,8 +141,16 @@ class Game {
fruit.color
)

Raylib.drawFPS(field.renderDrawPosition.x + 8, field.renderDrawPosition.y + 8)
// MARK: UI

Raylib.drawFPS(
// Position x
field.renderDrawPosition.x + 8,
// Postion y
field.renderDrawPosition.y + 8
)
Raylib.drawText(
// Text
"""
Field:
Columns count: \(field.columnsCount)
Expand All @@ -102,11 +164,31 @@ class Game {
X: \(fruit.position.x)
Y: \(fruit.position.y)
""",
// Position x
field.renderDrawPosition.x + 8,
// Position y
field.renderDrawPosition.y + 20 + 8 + 8,
// Font height
10,
Color(r: 230, g: 230, b: 236, a: 255) // Dark themme
// Color
textColor
)

var text = "[ Snake ]"
var fontSize: Int32 = 30
var drawPosition = Point2D(
x: (840 - Raylib.measureText(text, fontSize)) / 2,
y: (360 - fontSize) / 2
)
Raylib.drawText(text, drawPosition.x, drawPosition.y, fontSize, textColor)

text = "[ Play | Exit ]"
fontSize = 20
drawPosition = Point2D(
x: (840 - Raylib.measureText(text, fontSize)) / 2,
y: (360 - fontSize) / 2 + 128
)
Raylib.drawText(text, drawPosition.x, drawPosition.y, fontSize, textColor)

Raylib.endDrawing()
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Snake/Point2D.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
//

struct Point2D: Equatable {
let x: Int32
let y: Int32
var x: Int32
var y: Int32
}
22 changes: 22 additions & 0 deletions Sources/Snake/Settings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Snake
//

import Raylib

struct Settings {
var isVsync: Bool
var windowWidth: Int32
var windowHeight: Int32

var theme: Theme
}

extension Settings {
static let `default` = Settings(
isVsync: true,
windowWidth: 840,
windowHeight: 360,
theme: .dark
)
}
24 changes: 20 additions & 4 deletions Sources/Snake/Snake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,28 @@ struct Snake {
}
}

var headColor = Color(r: 166, g: 166, b: 191, a: 255) // Dark themme
var tail = [Point2D]()

var movement: Movement
var speed: Float

var tail = [Point2D]()
var tailColor = Color(r: 83, g: 83, b: 115, a: 255) // Dark themme
var headColor: Color
var tailColor: Color

init(headPosition: Point2D, movement: Movement, speed: Float) {
var isDeath = false

init(headPosition: Point2D, movement: Movement, speed: Float, headColor: Color, tailColor: Color)
{
self.headPreviousPosition = headPosition
self.rawHeadPosition = Vector2(x: Float(headPosition.x), y: Float(headPosition.y))
self.movement = movement
self.speed = speed
self.headColor = headColor
self.tailColor = tailColor
}

mutating func move(deltaTime: Float) {
// Move head
switch movement {
case .up:
rawHeadPosition.y -= speed * deltaTime
Expand All @@ -48,5 +54,15 @@ struct Snake {
case .right:
rawHeadPosition.x += speed * deltaTime
}

// Move tail
if headPosition != headPreviousPosition {
tail.append(headPreviousPosition)
tail.removeFirst()
}

for tailItem in tail {
if headPosition == tailItem { isDeath = true }
}
}
}
44 changes: 44 additions & 0 deletions Sources/Snake/Theme.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Snake
//

import Raylib

struct Theme {
var fieldBackgroundColor: Color
var fieldColor: Color

var snakeHeadColor: Color
var snakeTailColor: Color

var fruitColor: Color

var textColor: Color
}

extension Theme {
static let light = Theme(
fieldBackgroundColor: Color(r: 193, g: 193, b: 210, a: 255),
fieldColor: Color(r: 230, g: 230, b: 236, a: 255),
snakeHeadColor: Color(r: 83, g: 83, b: 115, a: 255), // ???
snakeTailColor: Color(r: 166, g: 166, b: 191, a: 255), // ???
fruitColor: Color(r: 14, g: 14, b: 18, a: 255),
textColor: Color(r: 14, g: 14, b: 18, a: 255)
)
static let dark = Theme(
fieldBackgroundColor: Color(r: 26, g: 26, b: 36, a: 255),
fieldColor: Color(r: 14, g: 14, b: 18, a: 255),
snakeHeadColor: Color(r: 166, g: 166, b: 191, a: 255),
snakeTailColor: Color(r: 83, g: 83, b: 115, a: 255),
fruitColor: Color(r: 230, g: 230, b: 236, a: 255),
textColor: Color(r: 230, g: 230, b: 236, a: 255)
)
static let classic = Theme(
fieldBackgroundColor: .lightGray,
fieldColor: .white,
snakeHeadColor: .darkGreen,
snakeTailColor: .green,
fruitColor: .red,
textColor: .black
)
}
3 changes: 2 additions & 1 deletion Sources/Snake/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
// Snake
//

let game = Game()
let game = Game(settings: .default)

game.run()

0 comments on commit e2da5f1

Please sign in to comment.