Skip to content

Commit 4161ad5

Browse files
committed
Added new file Snake. Changed several methods with Segment initiation
1 parent 363f135 commit 4161ad5

File tree

5 files changed

+51
-23
lines changed

5 files changed

+51
-23
lines changed

SnakeGame.xcodeproj/project.pbxproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
A0997FE61D3CDF2500AB2AC4 /* GameViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0997FE51D3CDF2500AB2AC4 /* GameViewController.swift */; };
1515
A0997FE81D3CDFBE00AB2AC4 /* GameFieldView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0997FE71D3CDFBE00AB2AC4 /* GameFieldView.swift */; };
1616
A0997FEA1D3CDFEE00AB2AC4 /* GameModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0997FE91D3CDFEE00AB2AC4 /* GameModel.swift */; };
17+
A0AB42391D432B8700239297 /* Snake.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0AB42381D432B8700239297 /* Snake.swift */; };
1718
/* End PBXBuildFile section */
1819

1920
/* Begin PBXFileReference section */
@@ -26,6 +27,7 @@
2627
A0997FE51D3CDF2500AB2AC4 /* GameViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GameViewController.swift; sourceTree = "<group>"; };
2728
A0997FE71D3CDFBE00AB2AC4 /* GameFieldView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GameFieldView.swift; sourceTree = "<group>"; };
2829
A0997FE91D3CDFEE00AB2AC4 /* GameModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GameModel.swift; sourceTree = "<group>"; };
30+
A0AB42381D432B8700239297 /* Snake.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Snake.swift; sourceTree = "<group>"; };
2931
/* End PBXFileReference section */
3032

3133
/* Begin PBXFrameworksBuildPhase section */
@@ -62,6 +64,7 @@
6264
A0997FE51D3CDF2500AB2AC4 /* GameViewController.swift */,
6365
A0997FE71D3CDFBE00AB2AC4 /* GameFieldView.swift */,
6466
A0997FE91D3CDFEE00AB2AC4 /* GameModel.swift */,
67+
A0AB42381D432B8700239297 /* Snake.swift */,
6568
A0997FE41D3CDE2D00AB2AC4 /* SupportingFiles */,
6669
);
6770
path = SnakeGame;
@@ -151,6 +154,7 @@
151154
files = (
152155
A0997FE81D3CDFBE00AB2AC4 /* GameFieldView.swift in Sources */,
153156
A0997FEA1D3CDFEE00AB2AC4 /* GameModel.swift in Sources */,
157+
A0AB42391D432B8700239297 /* Snake.swift in Sources */,
154158
A0997FE61D3CDF2500AB2AC4 /* GameViewController.swift in Sources */,
155159
A0997FD31D3CDB5D00AB2AC4 /* AppDelegate.swift in Sources */,
156160
);
@@ -302,6 +306,7 @@
302306
A0997FE31D3CDB5D00AB2AC4 /* Release */,
303307
);
304308
defaultConfigurationIsVisible = 0;
309+
defaultConfigurationName = Release;
305310
};
306311
/* End XCConfigurationList section */
307312
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
type = "1"
4+
version = "2.0">
5+
</Bucket>

SnakeGame/GameModel.swift

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,13 @@ class GameBrain {
6060

6161
let newPoint = CGPoint(x: newX, y: newY)
6262

63-
let newSegment = GameSegment()
64-
newSegment.isEaten = false
65-
newSegment.color = UIColor.redColor()
66-
newSegment.point = newPoint
63+
let newSegment = GameSegment(point: newPoint, isEaten: false)
6764
segments.append(newSegment)
6865

6966
print(newPoint)
7067
}
7168

72-
var direction: (CGPoint)!
69+
private var direction: (CGPoint)!
7370

7471
func setDefaultPosition(defaultPosition: CGPoint, viewSize: CGSize){
7572

@@ -80,10 +77,7 @@ class GameBrain {
8077

8178
setDirection("Up")
8279

83-
let headSegment = GameSegment()
84-
headSegment.isEaten = true
85-
headSegment.color = UIColor.greenColor()
86-
headSegment.point = headPoint
80+
let headSegment = GameSegment(point: headPoint, isEaten: true)
8781
segments.append(headSegment)
8882

8983
createSegment()
@@ -116,17 +110,4 @@ class GameBrain {
116110
print("Eaten")
117111
}
118112
}
119-
}
120-
121-
class GameSegment {
122-
var isEaten: Bool!
123-
var color: UIColor!
124-
var point: CGPoint!
125-
126-
func segmentRect() -> CGRect {
127-
128-
let side: CGFloat = 10
129-
130-
return CGRect(x: point.x + side / 2, y: point.y + side / 2, width: side, height: side)
131-
}
132113
}

SnakeGame/GameViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class GameViewController: UIViewController {
3434
updateViewHead()
3535
}
3636

37-
func updateViewHead(){
37+
private func updateViewHead(){
3838
gameFieldView.renderSegments(brain.segments)
3939
}
4040
}

SnakeGame/Snake.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Snake.swift
3+
// SnakeGame
4+
//
5+
// Created by Pavel Popov on 23.07.16.
6+
// Copyright © 2016 Pavel Popov. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class GameSegment {
12+
var isEaten: Bool! {
13+
didSet{
14+
color = newColor()
15+
}
16+
}
17+
var color: UIColor!
18+
var point: CGPoint!
19+
20+
init(point: CGPoint, isEaten: Bool){
21+
22+
self.point = point
23+
self.isEaten = isEaten
24+
color = newColor()
25+
}
26+
27+
private func newColor() -> UIColor{
28+
return isEaten == true ? UIColor.greenColor() : UIColor.redColor()
29+
}
30+
31+
func segmentRect() -> CGRect {
32+
33+
let side: CGFloat = 10
34+
35+
return CGRect(x: point.x + side / 2, y: point.y + side / 2, width: side, height: side)
36+
}
37+
}

0 commit comments

Comments
 (0)