Skip to content

Instantly share code, notes, and snippets.

@vikingosegundo
Created October 28, 2021 17:13
Show Gist options
  • Save vikingosegundo/c1a36d9109ea0b11acd6a08d562fb998 to your computer and use it in GitHub Desktop.
Save vikingosegundo/c1a36d9109ea0b11acd6a08d562fb998 to your computer and use it in GitHub Desktop.
struct SnakeBoard:View {
func bodyAndTarget() -> [SnakeGame.Coordinate] {
(game.target() != nil
? [ game.target()! ]
: [])
+ game.snake().body
}
@StateObject var state:ViewState
let pixelFactor:Double
var game: SnakeGame.Game { state.game }
var body:some View {
Canvas { ctx, size in
bodyAndTarget().forEach {
let color =
coordinateToPoint($0) == coordinateToPoint(game.target() != nil ? game.target()! : (-1,-1))
? Color.red : .white
let path = Path(CGRect(origin:coordinateToPoint(($0.x * Int(pixelFactor), $0.y * Int(pixelFactor))), size:.init(width:pixelFactor, height:pixelFactor)))
ctx.stroke(path, with:.color(color))
ctx.fill (path, with:.color(color))
}
}
.background(Color(white: 0.1))
.frame(width: Double(state.game.size().width) * pixelFactor, height: Double(state.game.size().height) * pixelFactor, alignment: .center)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment