Created
October 28, 2021 17:13
-
-
Save vikingosegundo/c1a36d9109ea0b11acd6a08d562fb998 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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