Skip to content

Commit 644a0a6

Browse files
committed
End with this game
1 parent 4ede9a5 commit 644a0a6

File tree

5 files changed

+67
-17
lines changed

5 files changed

+67
-17
lines changed

SnakeGame.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
A03D5DB61D6EEC6100F76A4A /* SnakeSegments.swift in Sources */ = {isa = PBXBuildFile; fileRef = A03D5DB51D6EEC6100F76A4A /* SnakeSegments.swift */; };
1011
A0997FD31D3CDB5D00AB2AC4 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0997FD21D3CDB5D00AB2AC4 /* AppDelegate.swift */; };
1112
A0997FD81D3CDB5D00AB2AC4 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A0997FD61D3CDB5D00AB2AC4 /* Main.storyboard */; };
1213
A0997FDA1D3CDB5D00AB2AC4 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A0997FD91D3CDB5D00AB2AC4 /* Assets.xcassets */; };
@@ -19,6 +20,7 @@
1920
/* End PBXBuildFile section */
2021

2122
/* Begin PBXFileReference section */
23+
A03D5DB51D6EEC6100F76A4A /* SnakeSegments.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SnakeSegments.swift; sourceTree = "<group>"; };
2224
A0997FCF1D3CDB5D00AB2AC4 /* SnakeGame.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SnakeGame.app; sourceTree = BUILT_PRODUCTS_DIR; };
2325
A0997FD21D3CDB5D00AB2AC4 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
2426
A0997FD71D3CDB5D00AB2AC4 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
@@ -76,6 +78,7 @@
7678
isa = PBXGroup;
7779
children = (
7880
A0997FD21D3CDB5D00AB2AC4 /* AppDelegate.swift */,
81+
A03D5DB51D6EEC6100F76A4A /* SnakeSegments.swift */,
7982
A0997FD91D3CDB5D00AB2AC4 /* Assets.xcassets */,
8083
A0997FDB1D3CDB5D00AB2AC4 /* LaunchScreen.storyboard */,
8184
A0997FDE1D3CDB5D00AB2AC4 /* Info.plist */,
@@ -159,6 +162,7 @@
159162
files = (
160163
A0997FE81D3CDFBE00AB2AC4 /* GameFieldView.swift in Sources */,
161164
A0997FEA1D3CDFEE00AB2AC4 /* GameModel.swift in Sources */,
165+
A03D5DB61D6EEC6100F76A4A /* SnakeSegments.swift in Sources */,
162166
A0AB42391D432B8700239297 /* Snake.swift in Sources */,
163167
A0997FE61D3CDF2500AB2AC4 /* GameViewController.swift in Sources */,
164168
A0997FD31D3CDB5D00AB2AC4 /* AppDelegate.swift in Sources */,

SnakeGame/GameFieldView.swift

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ class GameFieldView: UIView {
5252
}
5353
}
5454

55-
enum SegmentType{
56-
case Head
57-
case Tail
58-
case Border
59-
case Middle
60-
case Food
61-
}
6255

6356
class SegmentView: UIView {
6457

@@ -71,6 +64,8 @@ class SegmentView: UIView {
7164
}
7265
}
7366

67+
private var open: Bool = true
68+
7469
init(segment: GameSegment) {
7570
super.init(frame: segment.rect)
7671

@@ -80,21 +75,24 @@ class SegmentView: UIView {
8075

8176

8277
override func drawRect(rect: CGRect) {
83-
color().setFill()
84-
path().fill()
85-
}
86-
87-
func path() -> UIBezierPath {
78+
79+
open = !open
8880

8981
switch segment.type {
90-
case .Head, .Tail, .Middle, .Food:
91-
return UIBezierPath(ovalInRect: CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height))
82+
case .Head:
83+
SnakeSegments.drawHead(open: open, angle: segment.direction.rawValue, bounds: bounds)
84+
case .Tail:
85+
SnakeSegments.drawTail(angle: segment.direction.rawValue, bounds: bounds)
86+
case .Middle:
87+
SnakeSegments.drawMiddle(bounds: bounds)
88+
case .Food:
89+
SnakeSegments.drawFood(bounds: bounds)
9290
case .Border:
93-
let fillRect = CGRect(x: 1, y: 1, width: bounds.width-2, height: bounds.width - 2)
94-
return UIBezierPath(roundedRect: fillRect, cornerRadius: bounds.width/5)
91+
SnakeSegments.drawBorder(bounds: bounds)
9592
}
9693
}
9794

95+
9896
func color() -> UIColor {
9997
switch segment.type {
10098
case .Head:

SnakeGame/GameModel.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,36 @@ class GameBrain {
191191

192192
delegate?.updateScoreWithScore(score)
193193

194-
snake.insert(food, atIndex: 0)
194+
snake.insert(food, atIndex: snake.endIndex)
195195

196196
food = newSegment()
197197
}
198198

199199
}
200200

201+
private func directionByPoint(current: CGPoint, next: CGPoint) -> Direction {
202+
203+
var newDirection: Direction = .Up
204+
205+
if next.x - current.x > 0 {
206+
newDirection = Direction.Right
207+
}
208+
209+
if next.x - current.x < 0 {
210+
newDirection = Direction.Left
211+
}
212+
213+
if next.y - current.y > 0 {
214+
newDirection = Direction.Down
215+
}
216+
217+
if next.y - current.y < 0 {
218+
newDirection = Direction.Up
219+
}
220+
221+
return newDirection
222+
}
223+
201224
private func moveHead() {
202225

203226
guard let first = snake.first else{
@@ -208,6 +231,8 @@ class GameBrain {
208231

209232
let head = headSegment()
210233
head.type = SegmentType.Head
234+
head.direction = directionByPoint(first.rect.origin, next: headPoint)
235+
211236
snake.insert(head, atIndex: 0)
212237

213238
snake.removeLast()
@@ -217,6 +242,10 @@ class GameBrain {
217242
}
218243

219244
last.type = SegmentType.Tail
245+
246+
let tailPrevous = snake[snake.endIndex - 2]
247+
248+
last.direction = directionByPoint(last.rect.origin, next: tailPrevous.rect.origin)
220249
}
221250

222251
}

SnakeGame/GameViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class GameViewController: UIViewController {
4141

4242
private func resetBrain() {
4343

44+
navigationItem.title = "Snake"
45+
4446
brain = GameBrain(viewSize: gameFieldView.bounds.size)
4547

4648
brain.delegate = self

SnakeGame/Snake.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,27 @@
88

99
import UIKit
1010

11+
enum Direction: CGFloat {
12+
case Up = 0
13+
case Down = 180
14+
case Left = 90
15+
case Right = 270
16+
}
17+
18+
enum SegmentType{
19+
case Head
20+
case Tail
21+
case Border
22+
case Middle
23+
case Food
24+
}
25+
1126
class GameSegment {
1227

1328
private var point: CGPoint!
1429

30+
var direction: Direction!
31+
1532
var rect: CGRect {
1633
get{
1734
return CGRect(x: point.x, y: point.y, width: side, height: side)

0 commit comments

Comments
 (0)