Skip to content

Commit 4113cab

Browse files
committed
Added function to check the direction
1 parent 0c440f4 commit 4113cab

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

SnakeGame/GameModel.swift

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,36 @@ import UIKit
1010

1111
class GameBrain {
1212

13-
private var actions: Dictionary <String, (CGFloat, CGFloat)> = [
14-
"Up" : (-10,0),
15-
"Down" : (10,0),
16-
"Left" : (0,-10),
17-
"Right" : (0,10)
13+
private var actions: Dictionary <String, (CGPoint)> = [
14+
"Up" : CGPointMake(0, -10),
15+
"Down" : CGPointMake(0,10),
16+
"Left" : CGPointMake(-10,0),
17+
"Right" : CGPointMake(10,0)
1818
]
1919

2020
func setDirection(buttonAction: String){
2121

2222
if let action = actions[buttonAction] {
23-
direction = action
23+
24+
if (direction != nil) {
25+
if !(direction.x + action.x == 0 && direction.y + action.y == 0) {
26+
direction = action
27+
}
28+
}else{
29+
direction = action
30+
}
31+
2432
}
2533
}
2634

2735
var headPoint: CGPoint! {
2836

2937
didSet{
30-
print(headPoint)
38+
// print(headPoint)
3139
}
3240
}
33-
var direction: (v: CGFloat, h: CGFloat)!
41+
42+
var direction: (CGPoint)!
3443

3544
func setDefaultPosition(defaultPosition: CGPoint){
3645
headPoint = defaultPosition
@@ -40,8 +49,9 @@ class GameBrain {
4049

4150
func updateHead(){
4251

43-
headPoint.y += direction.v
4452

45-
headPoint.x += direction.h
53+
headPoint.x += direction.x
54+
headPoint.y += direction.y
55+
4656
}
4757
}

0 commit comments

Comments
 (0)