Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- struct ContentView: View {
- @State private var quesionCounter = 0
- @State private var showingScore = false
- @State private var showingResults = false
- @State private var scoreTitle = ""
- @State private var score = 0
- @State private var countries = allCountries.shuffled()
- @State private var correctAnswer = Int.random(in: 0...2)
- @State private var selectedFlag = -1
- static let allCountries = ["estonia", "france", "germany", "ireland", "italy", "monaco", "nigeria", "poland", "russia", "spain", "uk", "us"]
- var body: some View {
- ZStack {
- RadialGradient(stops: [
- .init(color: Color(red: 0.1, green: 0.2, blue: 0.45), location: 0.3),
- .init(color: Color(red: 0.76, green: 0.15, blue: 0.26), location: 0.3)],
- center: .top, startRadius: 200, endRadius: 700)
- .ignoresSafeArea()
- VStack {
- Spacer()
- Text("Guess the flag")
- .font(.largeTitle.bold())
- .foregroundColor(.white)
- VStack(spacing: 15) {
- VStack {
- Text("Tap the flag of")
- .foregroundStyle(.secondary)
- .font(.subheadline.weight(.heavy))
- Text(countries[correctAnswer].capitalized)
- .font(.largeTitle.weight(.semibold))
- }
- ForEach(0..<3) { number in
- Button {
- flagTapped(number)
- } label: {
- Image(countries[number])
- .renderingMode(.original)
- .clipShape(Capsule())
- .shadow(color: .black, radius: 5)
- .rotation3DEffect(.degrees(selectedFlag == number ? 360 : 0), axis: (x: 0, y:1, z: 0))
- .opacity(selectedFlag == -1 || selectedFlag == number ? 1 : 0.25)
- .scaleEffect(selectedFlag == -1 || selectedFlag == number ? 1 : 0.75)
- .animation(.default, value: selectedFlag)
- }
- }
- }
- .frame(maxWidth: .infinity)
- .padding(.vertical, 20)
- .background(.regularMaterial)
- .clipShape(RoundedRectangle(cornerRadius: 20))
- Spacer()
- Spacer()
- Text("Score: \(score)")
- .foregroundColor(.white)
- .font(.title.bold())
- Spacer()
- }
- .padding()
- }
- .alert(scoreTitle, isPresented: $showingScore) {
- Button("Continue", action: askQuestion)
- } message: {
- Text("Your score is \(score)")
- }
- .alert(scoreTitle, isPresented: $showingResults) {
- Button("Reset", action: newGame)
- } message: {
- Text("Great, your score is \(score)")
- }
- }
- func flagTapped(_ number: Int) {
- selectedFlag = number
- if number == correctAnswer {
- scoreTitle = "Correct"
- score += 1
- } else{
- let needsThe = ["uk", "us"]
- let theirAnswer = countries[number]
- if needsThe.contains(theirAnswer) {
- scoreTitle = "Wrong! That is the flag of the \(theirAnswer)"
- } else {
- scoreTitle = "Wrong! That is the flag of \(theirAnswer)"
- }
- if score > 0 {
- score -= 1
- }
- }
- if quesionCounter == 8 {
- showingResults = true
- } else {
- showingResults = true
- }
- }
- func askQuestion() {
- countries.remove(at: correctAnswer)
- countries.shuffle()
- correctAnswer = Int.random(in: 0...2)
- quesionCounter += 1
- selectedFlag = -1
- }
- func newGame(){
- quesionCounter = 0
- score = 0
- countries = Self.allCountries
- askQuestion()
- }
- }
- struct ContentView_Previews: PreviewProvider {
- static var previews: some View {
- ContentView()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment