我正在做 TicTacToe游戏。我想用进度条控制每个玩家的动作。如果游戏结束,则必须停止进度栏。如果一个玩家移动而另一个玩家不移动,则进度条必须增加或减少。这是我的代码
import UIKit
class ViewController: UIViewController {
var activePlayer = 1 // 1 = noughts, 2 = crosses
var gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]
let winningCombinations = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]
var gameActive = true
var timer = Timer()
@IBOutlet weak var btnPlayAgain: UIButton!
@IBOutlet weak var progressBar: UIProgressView!
@IBAction func btnPlayAgain(_ sender: AnyObject) {
更新
self.view.viewWithTag(1000)?.isHidden = true
if self.view.viewWithTag(1000)?.isHidden == false {
self.view.viewWithTag(1000)?.isHidden = true
}
更新
progressBar.progress = 0.5
gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]
activePlayer = 1
gameActive = true
gameOverLabel.isHidden = true
gameOverLabel.center = CGPoint(x: gameOverLabel.center.x - 500, y: gameOverLabel.center.y)
btnPlayAgain.isHidden = true
btnPlayAgain.center = CGPoint(x: btnPlayAgain.center.x - 500, y: btnPlayAgain.center.y)
var buttonToClear : UIButton
for i in 0..<9 {
buttonToClear = view.viewWithTag(i) as! UIButton
buttonToClear.setImage(nil, for: .normal)
}
}
@IBOutlet weak var button: UIButton!
@IBOutlet weak var gameOverLabel: UILabel!
@IBAction func buttonPressed(_ sender: AnyObject) {
timer = Timer.scheduledTimer(timeInterval: 0.6, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
if (gameState[sender.tag] == 0) && ( gameActive == true) {
gameState[sender.tag] = activePlayer
if activePlayer == 1 {
sender.setImage(UIImage(named: "nought.png"), for: .normal)
activePlayer = 2
} else {
sender.setImage(UIImage(named: "cross.png"), for: .normal)
activePlayer = 1
}
for combination in winningCombinations {
if (gameState[combination[0]] != 0 &&
gameState[combination[0]] == gameState[combination[1]] &&
gameState[combination[1]] == gameState[combination[2]]) {
gameActive = false
if (gameState[combination[0]] == 1)
{
gameOverLabel.text = "Noughts has won!"
} else {
gameOverLabel.text = "Crosses has won!"
}
endGame()
}
}
if gameActive == true {
gameActive = false
for buttonState in gameState {
if buttonState == 0 {
gameActive = true
}
}
if gameActive == false {
gameOverLabel.text = "It's a draw!"
endGame()
}
}
}
}
如何控制进度条是1还是0?如果1或0 gameLabel.text必须是“ Crosses or Noughts赢了!”
@objc func updateProgress() {
if activePlayer == 1 {
progressBar.progress -= 0.01
} else {
progressBar.progress += 0.01
}
}
我想在比赛结束时显示“冠军板”。我在下面添加了显示代码,没关系。但是,当我单击播放时,第一个视图中的“再次获得”按钮处于隐藏状态,而第二个游戏结束时,胜者局则不会隐藏
func endGame() {
timer.invalidate()
gameActive = false
let rect = UIView(frame: CGRect(x: 0, y: 0, width: 600, height: 500))
rect.tag = 1000
rect.backgroundColor = UIColor.white
self.view.addSubview(rect)
rect.addSubview(gameOverLabel)
rect.addSubview(btnPlayAgain)
gameOverLabel.isHidden = false
btnPlayAgain.isHidden = false
progressBar.progress = 0.5
UIView.animate(withDuration: 0.5, animations:{ () -> Void in
self.gameOverLabel.center = CGPoint(x: self.gameOverLabel.center.x + 500, y: self.gameOverLabel.center.y)
self.btnPlayAgain.center = CGPoint(x: self.btnPlayAgain.center.x + 500, y: self.btnPlayAgain.center.y)
})
}
override func viewDidLoad() {
super.viewDidLoad()
progressBar.progress = 0.5
gameOverLabel.isHidden = true
gameOverLabel.center = CGPoint(x: gameOverLabel.center.x - 500, y: gameOverLabel.center.y)
btnPlayAgain.isHidden = true
btnPlayAgain.center = CGPoint(x: btnPlayAgain.center.x - 500, y: btnPlayAgain.center.y)
}
答案 0 :(得分:0)
我想您正在创建按每次点击安排的多个计时器。你可以试试看首先,检查计时器代码,如下所示:
if self.timer.isValid == true {
self.timer.invalidate()
}
timer = Timer.scheduledTimer(timeInterval: 0.6, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
现在,在updateProgress内部,像这样进行更新。
@objc func updateProgress() {
if activePlayer == 1 {
progressBar.progress -= 0.01
} else {
progressBar.progress += 0.01
}
if progressBar.progress == 1.0 || progressBar.progress == 0.0 {
if activePlayer == 1 {
gameOverLabel.text = "Noughts has won!"
} else {
gameOverLabel.text = "Crosses has won!"
}
endGame()
}
}