Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f40903e

Browse files
committed
Sync
1 parent 8636e3f commit f40903e

File tree

20 files changed

+690
-1
lines changed

20 files changed

+690
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>ContentVersion</key>
6+
<string>1.0</string>
7+
<key>Name</key>
8+
<string>FunTime</string>
9+
<key>Pages</key>
10+
<array>
11+
<string>BouncingBall.playgroundpage</string>
12+
<string>iSpeed.playgroundpage</string>
13+
</array>
14+
<key>Version</key>
15+
<string>1.0</string>
16+
</dict>
17+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
//#-hidden-code
2+
import UIKit
3+
//#-end-hidden-code
4+
/*:
5+
Notice: Run this playground in your iPad in **landscape** mode with your device **heading to the left** and switch **on** the **orientation lock** of your device. **Switch off** the **mute** so you can hear the sound effect.
6+
7+
8+
# BouncingBall
9+
10+
A small game controlled by the position of the device.
11+
12+
* Start the game by clicking 'Run My Code'.
13+
* Change the preference by editing the variables below.
14+
* Tip: Many famous games like Asphalt are also based on the accelerometer.
15+
*/
16+
//#-hidden-code
17+
import UIKit
18+
import CoreMotion
19+
import PlaygroundSupport
20+
import AudioToolbox
21+
22+
class BouncingBall: UIViewController,UIAccelerometerDelegate {
23+
24+
var ball = UIImageView(image:#imageLiteral(resourceName: "ball.png"))
25+
//#-end-hidden-code
26+
//The percentage of the speed of the ball left after bouncing
27+
let bouncingSpeed:Double = /*#-editable-code */0.6/*#-end-editable-code*/
28+
//Update the speed of the ball according to the acceleration how many times per second
29+
let interval:Double = /*#-editable-code */60/*#-end-editable-code*/
30+
//#-hidden-code
31+
var motionManager = CMMotionManager()
32+
var speed = Speed(x:0,y:0)
33+
var noticeSound = SystemSoundID()
34+
var collideY:Bool = false
35+
var collideX:Bool = false
36+
37+
struct Speed {
38+
var x:UIAccelerationValue
39+
var y:UIAccelerationValue
40+
}
41+
42+
override func viewDidLoad() {
43+
super.viewDidLoad()
44+
self.view.backgroundColor = UIColor.white
45+
title = "BouncingBall"
46+
if(self.orientationCheck()){
47+
self.noticeSound = self.returnSoundDetail()
48+
self.startGame()
49+
}
50+
}
51+
52+
//Start the game!
53+
func startGame() {
54+
ball.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
55+
ball.center = self.view.center
56+
self.view.addSubview(ball)
57+
self.view.backgroundColor = UIColor(patternImage: #imageLiteral(resourceName: "background.jpg"))
58+
motionManager.accelerometerUpdateInterval = 1/self.interval
59+
if(motionManager.isAccelerometerAvailable)
60+
{
61+
let queue = OperationQueue.current
62+
motionManager.startAccelerometerUpdates(to: queue!, withHandler: {
63+
(accelerometerData, error) in
64+
self.speed.y += accelerometerData!.acceleration.x
65+
self.speed.x -= accelerometerData!.acceleration.y
66+
var x = self.ball.center.x + CGFloat(self.speed.x)
67+
var y = self.ball.center.y - CGFloat(self.speed.y)
68+
if (y < 60) {
69+
//Hit the roof
70+
if(!self.collideY){
71+
self.soundEffect()
72+
self.collideY = true
73+
}
74+
y = 60
75+
self.speed.y *= -self.bouncingSpeed
76+
77+
}
78+
else if (y > (self.view.bounds.size.height - 15)){
79+
//Hit the floor
80+
if(!self.collideY){
81+
self.soundEffect()
82+
self.collideY = true
83+
}
84+
y = self.view.bounds.size.height - 15
85+
self.speed.y *= -self.bouncingSpeed
86+
}
87+
else{
88+
self.collideY = false
89+
}
90+
if (x < 15) {
91+
//Hit the left wall
92+
if(!self.collideX){
93+
self.soundEffect()
94+
self.collideX = true
95+
}
96+
x = 15
97+
self.speed.x *= -self.bouncingSpeed
98+
}
99+
else if (x > (self.view.bounds.size.width - 15)) {
100+
//Hit the right wall
101+
if(!self.collideX){
102+
self.soundEffect()
103+
self.collideX = true
104+
}
105+
x = self.view.bounds.size.width - 15
106+
self.speed.x *= -self.bouncingSpeed
107+
}
108+
else {
109+
self.collideX = false
110+
}
111+
self.ball.center=CGPoint(x:x, y:y)
112+
} )
113+
}
114+
}
115+
//Check the device's orientation
116+
func orientationCheck() -> Bool {
117+
if(self.view.bounds.width < 1024){
118+
let noticeLabel = UILabel(frame: CGRect(x: 35,y :20, width: 700, height:100))
119+
noticeLabel.numberOfLines = 0
120+
noticeLabel.text = "Please switch to lanscape mode with the device heading to the left and try again."
121+
noticeLabel.textColor = UIColor.red
122+
noticeLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
123+
self.view.addSubview(noticeLabel)
124+
return false
125+
}
126+
return true
127+
}
128+
//Sound Effect!
129+
func soundEffect(){
130+
AudioServicesPlaySystemSound(self.noticeSound)
131+
//AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
132+
//iPad does not have any vibrations...
133+
}
134+
135+
//Return the sound id
136+
func returnSoundDetail() -> SystemSoundID {
137+
var soundID:SystemSoundID = 0
138+
let soundURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), "bounce" as CFString, "m4a" as CFString, nil)
139+
AudioServicesCreateSystemSoundID(soundURL!, &soundID)
140+
return soundID
141+
}
142+
143+
}
144+
145+
func startMyPlayground(_ view:UIViewController){
146+
PlaygroundPage.current.liveView = UINavigationController(rootViewController: view)
147+
}
148+
//#-end-hidden-code
149+
startMyPlayground(BouncingBall())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>ContentVersion</key>
6+
<string>1.0</string>
7+
<key>LiveViewEdgeToEdge</key>
8+
<false/>
9+
<key>LiveViewMode</key>
10+
<string>VisibleByDefault</string>
11+
<key>Name</key>
12+
<string>BouncingBall</string>
13+
<key>Version</key>
14+
<string>1.0</string>
15+
</dict>
16+
</plist>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /