var myString = "Hello world! - swift.jsrun.net"
print(myString)
var GameOver = false
var beginTimestamp : Int = 0
let GamePeriod = 20
var TimeCost = 0
func getcurrentTimeStamp() -> Int [
let now = DateC
let timelnterval : Timelnterval = now
timeIntervalsince1970
return Int(timelnterval)
func timeElapseO) (
TimerscheduledTimer(withTimelnterval: 0.5,repeats: true) ((timer) in
let timestamp = self.getCurrentTimeStamp0
let pastPeriod = timestamp - self.
beginTimestamp
let countDown = self,GamePerioc
pastperlod
self,TimeCost = pastPeriod
f countDown <= 0 elf GameOver
self GameOver
et tipsLabel = self.view.v Tag(1001)as. UILabe
tiosLabel,text=游戏结束 共消耗时间(selfmeCost)秒
linvalidateO
et tiosLabel = selfview.yi WitnTag(1002)as! UILabel
DispatchQueue.main.async
tipsLabel.text="您消耗\(pastPeriod)秒 倒时(countDown
音效 计时戏结案
@objc func buttonCheckMusicTimer( sender: UiButton)
if GameOver freturn
et tlosLaber – selfview.viewwithTag(1001)as! uTLabel
var tag = sender.tag
if tag >= 5000 (
tag- 5000
else if tag= 400,0
tao 4000
et row = Int (tag / cols)
let col = taa - cols * row
——IrOW.CO
ot
0=(TOW.CO
errorCords = reserved
if result.count >=1(
sender.backgroundColor = red
let path = Bundle,main,path(forResource; birdsound",oflype: "m4a")
let url = URL(fileURLWithPath: path!)
soundPlayer = try? AVAudioPlayer(
contentsOf; url)
soundplayer.play0
tipsLabel.text="不同找到:(A(row),\(col)),还剩(errorCords.count)A"
erro count <= 0(
GameOver = true
Label.text="任务完成 共花费时间(self.0 秒
sender, KO
let path = Bunc Resource:
error", ofType: "m4a")
let url = URL(fileURLWithPath: path!)
soundPlayer = try? AVAudioPlayer(contentsOf: url)
soundPlayer.play()
tipsLabel.text = "找错啦:(\(row),\(col))"
}
}
func displayArrayGraphButtonTips()(
let myview1 = self.view.viewWithTag(2001) as! UllmageView
let myview2 = self.view.viewWithTag(2002) as! UllmageView
myView1.backgroundColor=.darkGray
myView2.backgroundColor= .darkGray
myView1.isUserInteractionEnabled = true
myView2.isUserInteractionEnabled = true
let h = myView1.frame.size.height
let w= myView1.frame.size.width
let padding: CGFloat = 5
let margin: CGFloat = 10
/计算每个格子的高度和宽度
let grid w= (w - margin *2 - padding * CGFloat(cols - 1))/ CGFloat (cols) let grid_h = (h - margin *2 - padding CGFloat(rows - 1)) / CGFloat (rows)
for i in O..<rows {
for j in 0..<cols (
let x = margin + CGFloat(j) * (grid w + padding)
let y = margin + CGFloat(i) * (grid h + padding)
let rect = CGRect(x: x, y:y , width: grid w, height: grid h)
let tag1 = i* cols +j+ 5000
addButton(view: myView1, rect: rect, fileName: fileNamel, tag: tagl)
let fileName2 = iv2[i * cols + j]
let tag2 =i* cols + i + 4000
addButton(view: myView2, rect: rect,fileName: fileName2, tag:tag2)
let btn = UlButton(frame: rect)
btn.setlmage(img, for:.normal)
var mytag =tag
if mytag >= 5000t
mytag -= 5000
else if mytag >=4000 (
mytag -= 4000
let row = Int (mytag / cols)
let col = mytag - cols * row
$0 == (row,col)
]
if result.count >= 1 && TipsFlag
btn.backgroundColor =.blue
1)
else(
btn.backgroundColor = yellow
btn.tag = tag
btn.addTaiget(self, action: #selector(buttonCheckMusicTimer (_:)), for: .touchUpInside) view.addSubview(btn)
]
@IBAction func onTips( 【 sender : UIButton) (
TipsFlag =! TipsFlag
displayArrayGraphButtonTips0
1
@IBAction func onBegin(、 sender : UIButton) (
GameOver = false
beginTimestamp = getCurrentTimeStamp0
timeElapse(0
initGame0 Windows (CRLF)
differenceCreate(mount: 3)
displayArrayGraphButtonTips(0
let btn = UIButton(frame: rect)
btn.setImage(img, for: .normal)
var mytag = tag
if mytag >= 5000(
mytag -= 5000
else if mytag > = 4000 (
mytag -= 4000
let row = Int (mytag / cols)
let col = mytag - cols * row
let result = errorCords.filter (
$O = = (row,col)
if result.count >=1 && TipsFlag (
btn.backgroundColor =.blue
btn taa = taa Windows (CRLF)
btn.addTarget(self,action: #selector(buttonCheckMusicTimer C_:)), for:touchUpInside) view.addSubview(btn)
let result1 = cords.filter (
$O == (row,col)
if result1.count >= 1 (
btn.tag =tag
btn.addTarget(self, action: #selector (buttonCheckMusieTimer(_:)), for.touchuplnside) view.addSubview(btn)
func removeAllSubViews(view:UIView) (
if view.subviews.count> 0 (
view.subviews.forEach(($0.removeFromSuperview0))
@IBAction func onTips(_ sender :UIButton)(
TipsFlag =! TipsFlag
displayArrayGraphButtonTips0
@IBAction func onBegin(_ sender : UIButton)f
GameOver = false
beginTimestamp = getCurrentTimeStamp0
timeElapse()
initGame)
distractorCreate(mount: 10)
differenceCreate(mount: 8)
displayArrayGraphButtonTips0)
@IBAction func onNext(_ sender : UlButton)(
GameOver = true
TipsFlag = false
let myView1= self.view.viewWithTag(2001) as! UllmageView let myView2 = self.view.viewWithTag(2002) as! UIlmageView CurrentBackground += 1
if CurrentBackground > 4(
CurrentBackground = 1
14
let imaae = Ullmaae'named: "back0\(CurrentBackaround)")!
myView1.image = image
myView2.image = image
removeAllSubViews(view: myView1)
removeAllSubViews(view: myView2)
}
func differerceCreate(mount:Int){
for_in 0..<mount{
let col = Int(arc4random()) % cols
let row = Int(arc4randowm()) % rows
errorCords.append((row,col))
let ibdex = Int(arc4random()) % imag.count
let which = Int(arc4random()) % 2
if which == 0 {
iv1[cols*row + cols] = images[index]
}
else
iv2[cols*row + col] = image[index]
}
}
}
@objc func buttinCheckMusic(_sender :UIButton){
let tipsLabel = self.view.viewWith Tag(1001) as! UILabel
var tag = sender.tag
if tag >= 5000{
tag -= 5000
}
else if tag >= 4000{
tag -= 4000
}
let roow =Int(tag/cols)
let col = tag-cols*row
let result = errorCords.filter{
$0 == (row,col)
}
if result.count >= 1 {
let path =Bundle.main.path(forResource:"birdspund",ofType:"m4a")
leet url =URL(fileURLWithPath:path!)
soundplayer=try? AVAidioplyer(conentsof:url)
soundplayer.play()
tipsLayre.text = "不同找到: (\(row),\(col))"
}
else{
let path = Bundle.main.path(forResource:"error",ofType:"m4a")
let url =URL(fileURLWithpath:path!)
soundplayer=TRY? AVAidioplyer(conentsof:url)
soundplayer.play()
tipsLayre.text = "找错:(\(row),\(col))"
}
}
btn1.addTarget(self,action:#selector(buttinCheckMusic(_:)),for:.touchUplnside)
btn1.addTarget(self,action:#selector(buttinCheckMusic(_:)),for:.touchUplnside)
}