Skip to content

Commit cf1473d

Browse files
committed
Now it works correct with adding the segment
1 parent eacd1b3 commit cf1473d

File tree

3 files changed

+46
-87
lines changed

3 files changed

+46
-87
lines changed

SnakeGame/GameFieldView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class GameFieldView: UIView {
3939

4040
segmentView = segmentViews[i]
4141

42-
segmentView.frame = segment.rect()
42+
segmentView.frame = segment.rect
4343
segmentView.backgroundColor = segment.color
4444
}
4545
}
@@ -50,7 +50,7 @@ class GameFieldView: UIView {
5050

5151
for border in borders{
5252

53-
let borderView = UIView(frame: border.rect())
53+
let borderView = UIView(frame: border.rect)
5454

5555
borderView.backgroundColor = border.color
5656
self.addSubview(borderView)

SnakeGame/GameModel.swift

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,34 @@ class GameBrain {
6666
}
6767
}
6868

69+
func setDefaults() {
70+
71+
//prepareView
72+
borders = []
73+
createBorders()
74+
75+
//prepare segments
76+
snake = []
77+
//main default point for the head
78+
headPoint = centerPoint()
79+
80+
//setting default direction
81+
setDirection("Up")
82+
83+
//creating the head
84+
snake.append(createHead())
85+
snake.append(createHead())
86+
snake.append(createHead())
87+
88+
//create segment to eat
89+
food = newSegment()
90+
}
91+
92+
init(viewSize: CGSize){
93+
screen = viewSize
94+
setDefaults()
95+
}
96+
6997
private func newSegment() -> SnakeSegment {
7098
return SnakeSegment(point: randomPoint(), side: side)
7199
}
@@ -144,49 +172,19 @@ class GameBrain {
144172
private func createHead() -> SnakeSegment {
145173
let headSegment = SnakeSegment(point: headPoint, side: side)
146174
headSegment.isEaten = true
147-
headSegment.isHead = true
148175
return headSegment
149176
}
150177

151-
func setDefaults() {
152-
153-
//prepareView
154-
borders = []
155-
createBorders()
156-
157-
//prepare segments
158-
snake = []
159-
//main default point for the head
160-
headPoint = centerPoint()
161-
162-
//setting default direction
163-
setDirection("Up")
164-
165-
//creating the head
166-
snake.append(createHead())
167-
snake.append(createHead())
168-
snake.append(createHead())
169-
170-
171-
//create segment to eat
172-
food = newSegment()
173-
}
174-
175-
init(viewSize: CGSize){
176-
screen = viewSize
177-
setDefaults()
178-
}
179-
180178
func updateHead(){
181179

182180
headPoint.x += direction.x
183181
headPoint.y += direction.y
184182

185183
for border in borders {
186184

187-
if border.rect().contains(headPoint) {
188-
// border.isEaten = true
185+
if border.rect.contains(headPoint) {
189186

187+
print("Dead")
190188
//add the death
191189
}
192190
}
@@ -196,22 +194,10 @@ class GameBrain {
196194

197195
private func moveHead() {
198196

199-
guard let head = snake.first else{
200-
return
201-
}
202-
203-
head.isHead = false
204-
205197
snake.insert(createHead(), atIndex: 0)
206198

207199
snake.removeLast()
208200

209-
guard let tail = snake.last else{
210-
return
211-
}
212-
213-
tail.isTail = true
214-
215201
checkFood()
216202
}
217203

@@ -220,13 +206,11 @@ class GameBrain {
220206
return
221207
}
222208

223-
let isEaten = CGRectIntersectsRect(head.rect(), food.rect())
209+
let isEaten = CGRectIntersectsRect(head.rect, food.rect)
224210

225211
if isEaten {
226212

227-
food.isHead = true
228-
head.isHead = false
229-
snake.append(food)
213+
snake.insert(food, atIndex: 0)
230214

231215
food = newSegment()
232216
}

SnakeGame/Snake.swift

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,68 +9,43 @@
99
import UIKit
1010

1111
class GameSegment {
12-
var isEaten: Bool!{
13-
didSet{
14-
isEatenAction()
15-
}
16-
}
1712

1813
var color: UIColor!
1914
var point: CGPoint!
2015
var side: CGFloat!
2116

17+
var rect: CGRect {
18+
get{
19+
return CGRect(x: point.x, y: point.y, width: side, height: side)
20+
}
21+
}
22+
2223
init(point: CGPoint, side: CGFloat){
2324

2425
self.point = point
2526
self.side = side
26-
self.isEaten = false
27-
isEatenAction()
28-
}
29-
30-
private func isEatenAction(){}
31-
32-
func rect() -> CGRect {
33-
34-
return CGRect(x: point.x, y: point.y, width: side, height: side)
3527
}
3628
}
3729

3830
class SnakeSegment: GameSegment {
39-
var isHead: Bool!{
40-
didSet{
41-
head()
42-
}
43-
}
4431

45-
var isTail: Bool!{
32+
var isEaten: Bool! {
4633
didSet{
47-
tail()
34+
color = isEaten == true ? UIColor.greenColor().colorWithAlphaComponent(0.8) : UIColor.redColor()
4835
}
4936
}
5037

51-
private func head(){
52-
color = isHead == true ? UIColor.blueColor() : UIColor.greenColor()
53-
}
54-
55-
private func tail(){
56-
color = isTail == true ? UIColor.grayColor() : UIColor.greenColor()
57-
}
58-
5938
override init(point: CGPoint, side: CGFloat) {
6039
super.init(point: point, side: side)
61-
}
62-
63-
override func isEatenAction() {
64-
color = isEaten == true ? UIColor.greenColor() : UIColor.redColor()
40+
41+
color = UIColor.redColor()
6542
}
6643
}
6744

6845
class Border: GameSegment {
6946
override init(point: CGPoint, side: CGFloat) {
7047
super.init(point: point, side: side)
71-
}
72-
73-
private override func isEatenAction() {
74-
color = isEaten == true ? UIColor.redColor() : UIColor.blackColor()
48+
49+
color = UIColor.blackColor()
7550
}
7651
}

0 commit comments

Comments
 (0)