Skip to content

Commit 4f33cee

Browse files
committed
Added refresh button and the score
1 parent c22ccec commit 4f33cee

File tree

4 files changed

+74
-37
lines changed

4 files changed

+74
-37
lines changed

SnakeGame/Base.lproj/Main.storyboard

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15G31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15G31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="NWJ-XV-QCd">
33
<dependencies>
44
<deployment identifier="iOS"/>
55
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
@@ -15,11 +15,11 @@
1515
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
1616
</layoutGuides>
1717
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
18-
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
18+
<rect key="frame" x="0.0" y="64" width="600" height="536"/>
1919
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2020
<subviews>
2121
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="2J6-BG-JmJ" customClass="GameFieldView" customModule="SnakeGame" customModuleProvider="target">
22-
<rect key="frame" x="0.0" y="20" width="600" height="580"/>
22+
<rect key="frame" x="0.0" y="0.0" width="600" height="536"/>
2323
<color key="backgroundColor" red="0.96078431369999995" green="0.94509803920000002" blue="0.97254901959999995" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
2424
<gestureRecognizers/>
2525
<connections>
@@ -33,7 +33,7 @@
3333
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
3434
<constraints>
3535
<constraint firstAttribute="trailingMargin" secondItem="2J6-BG-JmJ" secondAttribute="trailing" constant="-20" id="IDa-sc-3JD"/>
36-
<constraint firstItem="2J6-BG-JmJ" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="topMargin" constant="20" id="Nqp-Pf-oL9"/>
36+
<constraint firstItem="2J6-BG-JmJ" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="topMargin" id="Nqp-Pf-oL9"/>
3737
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="2J6-BG-JmJ" secondAttribute="bottom" id="OWE-i7-JMs"/>
3838
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="2J6-BG-JmJ" secondAttribute="bottom" id="Oh6-IM-in2"/>
3939
<constraint firstItem="2J6-BG-JmJ" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="-20" id="bc1-gk-Vse"/>
@@ -46,6 +46,13 @@
4646
</mask>
4747
</variation>
4848
</view>
49+
<navigationItem key="navigationItem" id="5l0-jo-AZ5">
50+
<barButtonItem key="rightBarButtonItem" systemItem="refresh" id="Fbd-Qz-sEe">
51+
<connections>
52+
<action selector="refreshAction:" destination="BYZ-38-t0r" id="bg1-jj-vQj"/>
53+
</connections>
54+
</barButtonItem>
55+
</navigationItem>
4956
<connections>
5057
<outlet property="gameFieldView" destination="2J6-BG-JmJ" id="F2g-dZ-D8U"/>
5158
</connections>
@@ -72,6 +79,24 @@
7279
</connections>
7380
</swipeGestureRecognizer>
7481
</objects>
82+
<point key="canvasLocation" x="1153" y="364"/>
83+
</scene>
84+
<!--Navigation Controller-->
85+
<scene sceneID="WDu-K6-fqb">
86+
<objects>
87+
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="NWJ-XV-QCd" sceneMemberID="viewController">
88+
<toolbarItems/>
89+
<navigationBar key="navigationBar" contentMode="scaleToFill" translucent="NO" id="Rdc-Ac-meb">
90+
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
91+
<autoresizingMask key="autoresizingMask"/>
92+
</navigationBar>
93+
<nil name="viewControllers"/>
94+
<connections>
95+
<segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="vud-Dz-jiT"/>
96+
</connections>
97+
</navigationController>
98+
<placeholder placeholderIdentifier="IBFirstResponder" id="7fm-xV-GAl" userLabel="First Responder" sceneMemberID="firstResponder"/>
99+
</objects>
75100
<point key="canvasLocation" x="341" y="364"/>
76101
</scene>
77102
</scenes>

SnakeGame/GameFieldView.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class GameFieldView: UIView {
1818

1919
var delegate: DidDrawDelegate?
2020

21-
var correctSize: CGSize!
22-
2321
func renderSegments(segments: [GameSegment]){
2422

2523
for i in 0..<segments.count {
@@ -51,8 +49,6 @@ class GameFieldView: UIView {
5149
}
5250

5351
override func drawRect(rect: CGRect) {
54-
55-
correctSize = rect.size
5652

5753
delegate?.viewDidDraw()
5854
}

SnakeGame/GameModel.swift

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import UIKit
1111
let side: CGFloat = UIScreen.mainScreen().bounds.width / 30
1212

1313
protocol BrainDelegate {
14-
func snakeIsDead()
14+
func snakeIsDeadWithScore(value: Int)
15+
func updateScoreWithScore(value: Int)
1516
}
1617

1718
class GameBrain {
@@ -72,6 +73,7 @@ class GameBrain {
7273
init(viewSize: CGSize){
7374

7475
screen = viewSize
76+
score = 0
7577

7678
//prepareView
7779
borders = []
@@ -182,36 +184,40 @@ class GameBrain {
182184
return CGPoint(x: x, y: y)
183185
}
184186

187+
private var score: Int!
185188

186189
func updateHead(){
187190

188191
headPoint.x += direction.x
189192
headPoint.y += direction.y
190193

191-
for border in borders {
194+
func checkContainingInSegments(segments: [GameSegment]) {
192195

193-
if border.parameters.rect.contains(headPoint) {
194-
195-
delegate?.snakeIsDead()
196-
return
196+
for segment in segments {
197+
if segment.parameters.rect.contains(headPoint) {
198+
199+
delegate?.snakeIsDeadWithScore(score)
200+
return
201+
}
197202
}
198-
}
199-
200-
for segment in snake {
201-
if segment.parameters.rect.contains(headPoint) {
203+
204+
moveHead()
205+
206+
if food.parameters.rect.contains(headPoint) {
207+
208+
score = score + 1
202209

203-
delegate?.snakeIsDead()
204-
return
210+
delegate?.updateScoreWithScore(score)
211+
212+
snake.insert(food, atIndex: 0)
213+
214+
food = newSegment()
205215
}
216+
206217
}
207218

208-
moveHead()
219+
checkContainingInSegments(borders + snake)
209220

210-
if food.parameters.rect.contains(headPoint) {
211-
snake.insert(food, atIndex: 0)
212-
213-
food = newSegment()
214-
}
215221
}
216222

217223
private func moveHead() {

SnakeGame/GameViewController.swift

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ class GameViewController: UIViewController {
2828
default:
2929
break
3030
}
31-
31+
}
32+
@IBAction func refreshAction(sender: UIBarButtonItem) {
33+
resetBrain()
3234
}
3335

3436
private var brain: GameBrain!
@@ -39,32 +41,40 @@ class GameViewController: UIViewController {
3941
gameFieldView.delegate = self
4042
}
4143

42-
43-
4444
@objc private func movePoint() {
4545

4646
brain.updateHead()
47-
updateViewHead()
47+
gameFieldView.renderSegments(brain.segments)
4848
}
4949

50-
private func updateViewHead(){
51-
gameFieldView.renderSegments(brain.segments)
50+
private func resetBrain() {
51+
brain = GameBrain(viewSize: gameFieldView.bounds.size)
52+
brain.delegate = self
53+
gameFieldView.renderBorders(brain.borders)
54+
55+
timer = NSTimer.scheduledTimerWithTimeInterval(0.10, target: self, selector: #selector(movePoint), userInfo: nil, repeats: true)
5256
}
5357
}
5458

5559
extension GameViewController: DidDrawDelegate{
60+
5661
func viewDidDraw() {
57-
brain = GameBrain(viewSize: gameFieldView.correctSize)
58-
brain.delegate = self
59-
gameFieldView.renderBorders(brain.borders)
6062

61-
timer = NSTimer.scheduledTimerWithTimeInterval(0.50, target: self, selector: #selector(movePoint), userInfo: nil, repeats: true)
63+
resetBrain()
6264
}
6365
}
6466

6567
extension GameViewController: BrainDelegate {
66-
func snakeIsDead() {
68+
func snakeIsDeadWithScore(value: Int) {
69+
70+
navigationItem.title = "Game over! Score: \(value)"
71+
6772
timer.invalidate()
6873
timer = nil
6974
}
75+
76+
func updateScoreWithScore(value: Int) {
77+
78+
navigationItem.title = "Score: \(value)"
79+
}
7080
}

0 commit comments

Comments
 (0)