Skip to content

Commit 623f8da

Browse files
committed
Fixed method with borders. Added stackView for buttons and field in storyboard
1 parent 23570ce commit 623f8da

File tree

4 files changed

+100
-79
lines changed

4 files changed

+100
-79
lines changed

SnakeGame/Base.lproj/Main.storyboard

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<dependencies>
44
<deployment identifier="iOS"/>
55
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
6-
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
76
</dependencies>
87
<scenes>
98
<!--Game View Controller-->
@@ -18,56 +17,58 @@
1817
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
1918
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2019
<subviews>
21-
<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="536"/>
23-
<color key="backgroundColor" red="0.96078431369999995" green="0.94509803920000002" blue="0.97254901959999995" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
24-
</view>
25-
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="orv-1n-LCh">
26-
<rect key="frame" x="0.0" y="556" width="600" height="44"/>
20+
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="35L-pI-qif">
21+
<rect key="frame" x="0.0" y="20" width="600" height="580"/>
2722
<subviews>
28-
<button opaque="NO" alpha="0.29999999999999999" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="cpa-cq-xhN">
29-
<rect key="frame" x="0.0" y="0.0" width="150" height="44"/>
30-
<state key="normal" title="Left"/>
31-
<connections>
32-
<action selector="buttonAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="D4u-mN-dkv"/>
33-
</connections>
34-
</button>
35-
<button opaque="NO" alpha="0.29999999999999999" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="1Si-Kd-ONi">
36-
<rect key="frame" x="150" y="0.0" width="150" height="44"/>
37-
<state key="normal" title="Up"/>
38-
<connections>
39-
<action selector="buttonAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="4ID-GC-yMb"/>
40-
</connections>
41-
</button>
42-
<button opaque="NO" alpha="0.29999999999999999" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="akL-Rj-9GF">
43-
<rect key="frame" x="300" y="0.0" width="150" height="44"/>
44-
<state key="normal" title="Down"/>
45-
<connections>
46-
<action selector="buttonAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="1tV-vF-cVr"/>
47-
</connections>
48-
</button>
49-
<button opaque="NO" alpha="0.29999999999999999" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="EbZ-y1-vZA">
50-
<rect key="frame" x="450" y="0.0" width="150" height="44"/>
51-
<state key="normal" title="Right"/>
52-
<connections>
53-
<action selector="buttonAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Dso-YT-pnR"/>
54-
</connections>
55-
</button>
23+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="2J6-BG-JmJ" customClass="GameFieldView" customModule="SnakeGame" customModuleProvider="target">
24+
<rect key="frame" x="0.0" y="0.0" width="600" height="536"/>
25+
<color key="backgroundColor" red="0.96078431369999995" green="0.94509803920000002" blue="0.97254901959999995" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
26+
</view>
27+
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="orv-1n-LCh">
28+
<rect key="frame" x="0.0" y="536" width="600" height="44"/>
29+
<subviews>
30+
<button opaque="NO" alpha="0.29999999999999999" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="cpa-cq-xhN">
31+
<rect key="frame" x="0.0" y="0.0" width="150" height="44"/>
32+
<state key="normal" title="Left"/>
33+
<connections>
34+
<action selector="buttonAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="D4u-mN-dkv"/>
35+
</connections>
36+
</button>
37+
<button opaque="NO" alpha="0.29999999999999999" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="1Si-Kd-ONi">
38+
<rect key="frame" x="150" y="0.0" width="150" height="44"/>
39+
<state key="normal" title="Up"/>
40+
<connections>
41+
<action selector="buttonAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="4ID-GC-yMb"/>
42+
</connections>
43+
</button>
44+
<button opaque="NO" alpha="0.29999999999999999" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="akL-Rj-9GF">
45+
<rect key="frame" x="300" y="0.0" width="150" height="44"/>
46+
<state key="normal" title="Down"/>
47+
<connections>
48+
<action selector="buttonAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="1tV-vF-cVr"/>
49+
</connections>
50+
</button>
51+
<button opaque="NO" alpha="0.29999999999999999" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="EbZ-y1-vZA">
52+
<rect key="frame" x="450" y="0.0" width="150" height="44"/>
53+
<state key="normal" title="Right"/>
54+
<connections>
55+
<action selector="buttonAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Dso-YT-pnR"/>
56+
</connections>
57+
</button>
58+
</subviews>
59+
<constraints>
60+
<constraint firstAttribute="height" constant="44" id="PZb-Hc-TDY"/>
61+
</constraints>
62+
</stackView>
5663
</subviews>
57-
<constraints>
58-
<constraint firstAttribute="height" constant="44" id="PZb-Hc-TDY"/>
59-
</constraints>
6064
</stackView>
6165
</subviews>
6266
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
6367
<constraints>
64-
<constraint firstItem="2J6-BG-JmJ" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="6Hd-fj-OWn"/>
65-
<constraint firstItem="2J6-BG-JmJ" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="-20" id="HsI-3E-h6C"/>
66-
<constraint firstItem="orv-1n-LCh" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="-20" id="chv-by-c9x"/>
67-
<constraint firstItem="orv-1n-LCh" firstAttribute="top" secondItem="2J6-BG-JmJ" secondAttribute="bottom" id="hGE-Og-NBW"/>
68-
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="orv-1n-LCh" secondAttribute="bottom" id="jJ4-Bh-9gO"/>
69-
<constraint firstAttribute="trailingMargin" secondItem="orv-1n-LCh" secondAttribute="trailing" constant="-20" id="pvb-Rj-d2u"/>
70-
<constraint firstAttribute="trailingMargin" secondItem="2J6-BG-JmJ" secondAttribute="trailing" constant="-20" id="z4Y-XA-U3G"/>
68+
<constraint firstAttribute="trailing" secondItem="35L-pI-qif" secondAttribute="trailing" id="cqX-rf-XVA"/>
69+
<constraint firstItem="35L-pI-qif" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="dp3-eE-5tp"/>
70+
<constraint firstItem="35L-pI-qif" firstAttribute="bottom" secondItem="wfy-db-euE" secondAttribute="top" id="jzy-J2-TIu"/>
71+
<constraint firstItem="35L-pI-qif" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="lhx-16-1TE"/>
7172
</constraints>
7273
</view>
7374
<connections>

SnakeGame/GameFieldView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ class GameFieldView: UIView {
5151
for border in borders{
5252

5353
let borderView = UIView(frame: border)
54+
5455
borderView.backgroundColor = UIColor.blackColor()
5556
self.addSubview(borderView)
5657
}
5758

5859
}
5960

60-
6161
override func drawRect(rect: CGRect) {
6262

6363
correctSize = rect.size

SnakeGame/GameModel.swift

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import UIKit
1010

1111
class GameBrain {
1212

13+
private let side: CGFloat! = 10
14+
1315
private var actions: Dictionary <String, (CGPoint)> = [
1416
"Up" : CGPointMake(0,-10),
1517
"Down" : CGPointMake(0,10),
@@ -33,13 +35,22 @@ class GameBrain {
3335

3436
private var headPoint: CGPoint! = CGPointZero
3537

36-
private var backViewSize: CGSize!
38+
private var screen: CGSize!
39+
40+
private var insets: CGPoint!{
41+
get{
42+
let horizontal = (screen.width % side) / 2
43+
44+
let vertical = (screen.height % side) / 2
45+
46+
return CGPoint(x: horizontal, y: vertical)
47+
}
48+
}
3749

3850
private var bordersDictonary: [String:CGRect]!
3951

4052
var borders: [CGRect]!{
4153
get{
42-
4354
var bordersArray: [CGRect] = []
4455

4556
for border in bordersDictonary.values {
@@ -53,48 +64,50 @@ class GameBrain {
5364

5465
private func createBorders(){
5566

56-
let screen = backViewSize
57-
58-
let borderWidth:CGFloat = 10
67+
let width = screen.width - insets.x * 2
5968

60-
let leftBorder = CGRect(x: 0, y: 0, width: borderWidth, height: screen.height)
69+
let height = screen.height - insets.y * 2
70+
71+
let leftBorder = CGRect(x: insets.x, y: insets.y, width: side, height: height)
6172

6273
bordersDictonary["Left"] = leftBorder
6374

64-
let rightBorder = CGRect(x: screen.width - borderWidth, y: 0, width: borderWidth, height: screen.height)
75+
let rightBorder = CGRect(x: screen.width - insets.x - side, y: insets.y, width: side, height: height)
6576

6677
bordersDictonary["Right"] = rightBorder
6778

68-
let horBorderWidth = screen.width - borderWidth * 2
69-
70-
let topBorder = CGRect(x: borderWidth, y: 0, width: horBorderWidth, height: borderWidth)
79+
let topBorder = CGRect(x: side + insets.x, y: insets.y, width: width, height: side)
7180

7281
bordersDictonary["Top"] = topBorder
7382

74-
let botBorder = CGRect(x: borderWidth, y: screen.height - borderWidth, width: horBorderWidth, height: borderWidth)
83+
let botBorder = CGRect(x: side + insets.x, y: screen.height - insets.y - side, width: width, height: side)
7584

76-
bordersDictonary["Bot"] = botBorder
77-
78-
print(bordersDictonary)
85+
bordersDictonary["Bot"] = botBorder
7986
}
8087

8188
var segments: [GameSegment]!
8289

8390
private func createRandomSegment(){
8491

85-
// let randomMultiplierX = arc4random_uniform(screenWidth/10 - offsetX)
86-
//
87-
// let randomMultiplierY = arc4random_uniform(screenWidth/10 - offsetY)
88-
//
89-
// let newX: CGFloat = CGFloat(randomMultiplierX * step)
90-
// let newY: CGFloat! = CGFloat(randomMultiplierY * step)
91-
//
92-
// let newPoint = CGPoint(x: newX, y: newY)
93-
//
94-
// let newSegment = GameSegment(point: newPoint, isEaten: false)
95-
// segments.append(newSegment)
96-
//
97-
// print(newPoint)
92+
let side32 = side.convertToUInt32()
93+
94+
let width32 = screen.width.convertToUInt32()
95+
96+
let height32 = screen.height.convertToUInt32()
97+
98+
let randomMultiX = arc4random_uniform(width32 / side32 - side32)
99+
100+
let randomMultiY = arc4random_uniform(height32 / side32 - side32)
101+
102+
let newX: CGFloat = CGFloat(randomMultiX) * side
103+
let newY: CGFloat! = CGFloat(randomMultiY) * side
104+
105+
let newPoint = CGPoint(x: newX, y: newY)
106+
107+
let newSegment = GameSegment(point: newPoint, isEaten: false, side: side)
108+
segments.append(newSegment)
109+
110+
print(newPoint)
98111
}
99112

100113
private var direction: (CGPoint)!
@@ -103,7 +116,7 @@ class GameBrain {
103116

104117
//prepareView
105118
bordersDictonary = [:]
106-
backViewSize = viewSize
119+
screen = viewSize
107120
createBorders()
108121

109122
//prepare segments
@@ -116,7 +129,7 @@ class GameBrain {
116129
setDirection("Up")
117130

118131
//creating the head
119-
let headSegment = GameSegment(point: headPoint, isEaten: true)
132+
let headSegment = GameSegment(point: headPoint, isEaten: true, side: side)
120133
segments.append(headSegment)
121134

122135
//create segment to eat
@@ -136,7 +149,7 @@ class GameBrain {
136149
let headSegment = segments[0]
137150
headSegment.point = headPoint
138151

139-
// checkFood()
152+
checkFood()
140153
}
141154

142155
private func checkFood(){
@@ -150,4 +163,11 @@ class GameBrain {
150163
print("Eaten")
151164
}
152165
}
166+
}
167+
168+
extension CGFloat{
169+
170+
func convertToUInt32() -> UInt32 {
171+
return UInt32(self)
172+
}
153173
}

SnakeGame/Snake.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ class GameSegment {
1616
}
1717
var color: UIColor!
1818
var point: CGPoint!
19+
var side: CGFloat!
1920

20-
init(point: CGPoint, isEaten: Bool){
21+
init(point: CGPoint, isEaten: Bool, side: CGFloat){
2122

2223
self.point = point
2324
self.isEaten = isEaten
25+
self.side = side
2426
color = newColor()
2527
}
2628

@@ -30,8 +32,6 @@ class GameSegment {
3032

3133
func segmentRect() -> CGRect {
3234

33-
let side: CGFloat = 10
34-
3535
return CGRect(x: point.x + side / 2, y: point.y + side / 2, width: side, height: side)
3636
}
3737
}

0 commit comments

Comments
 (0)