#61 排球計分板App

哈士鱷
彼得潘的 Swift iOS App 開發教室
14 min readJul 31, 2022

--

說到校園青春熱門運動,不得不提到排球這項運動啦~求學過程中,高中、大學班上都盛行著排球這項運動,當然要來做款排球計分的App啦!(但我本人都沒有參加XD

排球計分App

計分標準參考 中華奧林匹克委員會-排球 之計分標準

初始登入畫面

可輸入各自隊伍名稱

登入畫面

比賽開始畫面

比賽計分畫面

.回上一步鈕

要是計分不小心按錯,可返回上一步計分一次

回上一步

.重新計分

計分直接歸零

重新計分

.離開鈕

.提醒換場

勝一局時,雙方互換場地

.比賽結束

五戰三勝,贏三局時宣布獲勝隊伍

完整程式碼:

import UIKitclass ViewController: UIViewController {@IBOutlet weak var appView: UIView!
@IBOutlet weak var enterALabel: UITextField!
@IBOutlet weak var enterBLabel: UITextField!
@IBOutlet weak var teamALabel: UILabel!
@IBOutlet weak var teamBLabel: UILabel!
@IBOutlet weak var leftButton: UIButton!
@IBOutlet weak var righrButton: UIButton!
@IBOutlet weak var leftScore: UILabel!
@IBOutlet weak var rightScore: UILabel!
@IBOutlet weak var backButton: UIButton!
@IBOutlet weak var rescoringButton: UIButton!
@IBOutlet weak var leftIntegral: UILabel!
@IBOutlet weak var rightIntegral: UILabel!
@IBOutlet weak var winnerLabel: UILabel!

var scoreLeft = 0
var scoreRight = 0
var IntegralLeft = 0
var IntegralRight = 0
var temporaryStorage = 0
var change = “”
//發球權
var ball = “”

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
leftScore.text = “0”
rightScore.text = “0”
winnerLabel.isHidden = true
}
//空白處關閉鍵盤
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}

//分數歸零
func turn0() {
scoreLeft = 0
scoreRight = 0
leftScore.text = “0”
rightScore.text = “0”
}
//換場
func changePlace() {
temporaryStorage = IntegralLeft
IntegralLeft = IntegralRight
IntegralRight = temporaryStorage
leftIntegral.text = “\(IntegralLeft)”
rightIntegral.text = “\(IntegralRight)”
change = teamALabel.text!
teamALabel.text = teamBLabel.text!
teamBLabel.text = change
}

//評斷分數
func score25() {
if IntegralLeft == 2, IntegralRight == 2 {
if scoreLeft == 15 ,scoreLeft > scoreRight {
IntegralLeft += 1
leftIntegral.text = “\(IntegralLeft)”
turn0()
} else if scoreRight == 15 ,scoreLeft < scoreRight {
IntegralRight += 1
rightIntegral.text = “\(IntegralRight)”
turn0()
}
} else {
if scoreLeft == 25 ,scoreRight <= 23 {
IntegralLeft += 1
leftIntegral.text = “\(IntegralLeft)”
turn0()
if IntegralLeft < 3 {
let changeAlert = UIAlertController(title: “交換場地”, message: “”, preferredStyle: .alert)
let changeAction = UIAlertAction(title: “確認”, style: .default) { (_) in
self.changePlace()
}
changeAlert.addAction(changeAction)
self.present(changeAlert, animated: true)
}
} else if scoreRight == 25 ,scoreLeft <= 23 {
IntegralRight += 1
rightIntegral.text = “\(IntegralRight)”
turn0()
if IntegralRight < 3 {
let changeAlert = UIAlertController(title: “交換場地”, message: “”, preferredStyle: .alert)
let changeAction = UIAlertAction(title: “確認”, style: .default) { (_) in
self.changePlace()
}
changeAlert.addAction(changeAction)
self.present(changeAlert, animated: true)
}
}
}
}

func sameScore() {
if scoreLeft >= 24 ,scoreRight >= 24 {
if scoreLeft — scoreRight == 2 {
IntegralLeft += 1
leftIntegral.text = “\(IntegralLeft)”
turn0()
if IntegralLeft < 3 {
let changeAlert = UIAlertController(title: “場地交換”, message: “”, preferredStyle: .alert)
let changeAction = UIAlertAction(title: “確認”, style: .default) { (_) in
self.changePlace()
}
changeAlert.addAction(changeAction)
self.present(changeAlert, animated: true)
}
} else if scoreLeft — scoreRight == -2 {
IntegralRight += 1
rightIntegral.text = “\(IntegralRight)”
turn0()
if IntegralRight < 3 {
let changeAlert = UIAlertController(title: “場地交換”, message: “”, preferredStyle: .alert)
let changeAction = UIAlertAction(title: “確認”, style: .default) { (_) in
self.changePlace()
}
changeAlert.addAction(changeAction)
self.present(changeAlert, animated: true)
}
}
} else {
score25()
}
if IntegralLeft == 3 {
winnerLabel.text = “\(enterALabel.text!) 獲得勝利!”
winnerLabel.isHidden = false
let winAlert = UIAlertController(title: “恭喜\(enterALabel.text!)獲勝!”, message: “比賽結束”, preferredStyle: .alert)
let winAction = UIAlertAction(title: “確認”, style: .default) {(_) in
self.righrButton.isEnabled = false
self.leftButton.isEnabled = false
self.rescoringButton.isEnabled = false
self.backButton.isEnabled = false
}
winAlert.addAction(winAction)
self.present(winAlert, animated: true)
} else if IntegralRight == 3 {
winnerLabel.text = “\(enterBLabel.text!) 獲得勝利!”
winnerLabel.isHidden = false
let winAlert = UIAlertController(title: “恭喜\(enterBLabel.text!)獲勝!”, message: “比賽結束”, preferredStyle: .alert)
let winAction = UIAlertAction(title: “確認”, style: .default) {(_) in
self.righrButton.isEnabled = false
self.leftButton.isEnabled = false
self.rescoringButton.isEnabled = false
self.backButton.isEnabled = false
}
winAlert.addAction(winAction)
self.present(winAlert, animated: true)
}
}
//左邊加分鈕
@IBAction func leftButton(_ sender: UIButton) {
scoreLeft += 1
ball = “left”
leftScore.text = “\(scoreLeft)”
sameScore()
backButton.isEnabled = true
}
//右邊加分鈕
@IBAction func rightButton(_ sender: UIButton) {
scoreRight += 1
ball = “right”
rightScore.text = “\(scoreRight)”
sameScore()
backButton.isEnabled = true
}
//回上一步
@IBAction func previous(_ sender: UIButton) {
if ball == “left” {
if scoreLeft > 0 {
scoreLeft -= 1
leftScore.text = “\(scoreLeft)”
ball = “no one”
}
} else if ball == “right” {
if scoreRight > 0 {
scoreRight -= 1
rightScore.text = “\(scoreRight)”
ball = “no one”
}
}

}
//重新開始
@IBAction func restart(_ sender: UIButton) {
let clearAlert = UIAlertController(title: “是否要重新計分?”, message: “將清除當前分數”, preferredStyle: .alert)
let clearAction = UIAlertAction(title: “確認”, style: .default) {(_) in
self.IntegralLeft = 0
self.IntegralRight = 0
self.leftIntegral.text = “0”
self.rightIntegral.text = “0”
self.turn0()
}
let clearAction2 = UIAlertAction(title: “取消”, style: .default)
clearAlert.addAction(clearAction)
clearAlert.addAction(clearAction2)
self.present(clearAlert, animated: true)
}
@IBAction func startRaceButton(_ sender: UIButton) {
appView.isHidden = true
if enterALabel.text == “” {
enterALabel.text = “Team A”
}
if enterBLabel.text == “” {
enterBLabel.text = “Team B”
}
teamALabel.text = enterALabel.text!
teamBLabel.text = enterBLabel.text!
}
//離開按鈕
@IBAction func exitButton(_ sender: UIButton) {
let exitAlert = UIAlertController(title: “是否要離開計分?”, message: “確認離開嗎?”, preferredStyle: .alert)
let exitAction = UIAlertAction(title: “確認”, style: .default) {(_) in self.appView.isHidden = false
self.IntegralLeft = 0
self.IntegralRight = 0
self.leftIntegral.text = “0”
self.rightIntegral.text = “0”
self.turn0()
self.righrButton.isEnabled = true
self.leftButton.isEnabled = true
self.rescoringButton.isEnabled = true
self.backButton.isEnabled = true
self.winnerLabel.isHidden = true
}
let exitAction2 = UIAlertAction(title: “取消”, style: .default)
exitAlert.addAction(exitAction)
exitAlert.addAction(exitAction2)
self.present(exitAlert, animated: true)
}

}

成品畫面

成果畫面

感謝大家觀看

--

--

哈士鱷
彼得潘的 Swift iOS App 開發教室

前往成為iOS工程師的超級菜鳥 從未踏入過 但我相信有努力一定會有收穫!