Guest User

Untitled

a guest
Nov 8th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.61 KB | Source Code | 0 0
  1. import SwiftUI
  2.  
  3.  
  4. struct ContentView: View {
  5.     @State private var quesionCounter = 0
  6.     @State private var showingScore = false
  7.     @State private var showingResults = false
  8.     @State private var scoreTitle = ""
  9.     @State private var score = 0
  10.    
  11.     @State private var countries = allCountries.shuffled()
  12.     @State private var correctAnswer = Int.random(in: 0...2)
  13.    
  14.     @State private var selectedFlag = -1
  15.    
  16.     static let allCountries = ["estonia", "france", "germany", "ireland", "italy", "monaco", "nigeria", "poland", "russia", "spain", "uk", "us"]
  17.    
  18.     var body: some View {
  19.         ZStack {
  20.             RadialGradient(stops: [
  21.                 .init(color: Color(red: 0.1, green: 0.2, blue: 0.45), location: 0.3),
  22.                 .init(color: Color(red: 0.76, green: 0.15, blue: 0.26), location: 0.3)],
  23.                 center: .top, startRadius: 200, endRadius: 700)
  24.             .ignoresSafeArea()
  25.            
  26.             VStack {
  27.                 Spacer()
  28.                
  29.                 Text("Guess the flag")
  30.                     .font(.largeTitle.bold())
  31.                     .foregroundColor(.white)
  32.                
  33.                 VStack(spacing: 15) {
  34.                     VStack {
  35.                         Text("Tap the flag of")
  36.                             .foregroundStyle(.secondary)
  37.                             .font(.subheadline.weight(.heavy))
  38.                        
  39.                         Text(countries[correctAnswer].capitalized)
  40.                             .font(.largeTitle.weight(.semibold))
  41.                     }
  42.                    
  43.                     ForEach(0..<3) { number in
  44.                         Button {
  45.                             flagTapped(number)
  46.                         } label: {
  47.                             Image(countries[number])
  48.                                 .renderingMode(.original)
  49.                                 .clipShape(Capsule())
  50.                                 .shadow(color: .black, radius: 5)
  51.                                 .rotation3DEffect(.degrees(selectedFlag == number ? 360 : 0), axis: (x: 0, y:1, z: 0))
  52.                                 .opacity(selectedFlag == -1 || selectedFlag == number ? 1 : 0.25)
  53.                                 .scaleEffect(selectedFlag == -1 || selectedFlag == number ? 1 : 0.75)
  54.                                 .animation(.default, value: selectedFlag)
  55.                         }
  56.                     }
  57.                 }
  58.                
  59.                 .frame(maxWidth: .infinity)
  60.                 .padding(.vertical, 20)
  61.                 .background(.regularMaterial)
  62.                 .clipShape(RoundedRectangle(cornerRadius: 20))
  63.                
  64.                 Spacer()
  65.                 Spacer()
  66.                
  67.                 Text("Score: \(score)")
  68.                     .foregroundColor(.white)
  69.                     .font(.title.bold())
  70.                 Spacer()
  71.             }
  72.             .padding()
  73.            
  74.         }
  75.         .alert(scoreTitle, isPresented: $showingScore) {
  76.             Button("Continue", action: askQuestion)
  77.         } message: {
  78.             Text("Your score is \(score)")
  79.         }
  80.         .alert(scoreTitle, isPresented: $showingResults) {
  81.             Button("Reset", action: newGame)
  82.         } message: {
  83.             Text("Great, your score is \(score)")
  84.         }
  85.     }
  86.    
  87.     func flagTapped(_ number: Int) {
  88.         selectedFlag = number
  89.         if number == correctAnswer {
  90.             scoreTitle = "Correct"
  91.             score += 1
  92.         } else{
  93.             let needsThe = ["uk", "us"]
  94.             let theirAnswer = countries[number]
  95.            
  96.             if needsThe.contains(theirAnswer) {
  97.                 scoreTitle = "Wrong! That is the flag of the \(theirAnswer)"
  98.             } else {
  99.                 scoreTitle = "Wrong! That is the flag of \(theirAnswer)"
  100.             }
  101.             if score > 0 {
  102.                 score -= 1
  103.             }
  104.         }
  105.         if quesionCounter == 8 {
  106.             showingResults = true
  107.         } else {
  108.             showingResults = true
  109.         }
  110.     }
  111.        
  112.         func askQuestion() {
  113.             countries.remove(at: correctAnswer)
  114.             countries.shuffle()
  115.             correctAnswer = Int.random(in: 0...2)
  116.             quesionCounter += 1
  117.             selectedFlag = -1
  118.         }
  119.        
  120.         func newGame(){
  121.             quesionCounter = 0
  122.             score = 0
  123.             countries = Self.allCountries
  124.             askQuestion()
  125.         }
  126.     }
  127.  
  128.     struct ContentView_Previews: PreviewProvider {
  129.         static var previews: some View {
  130.             ContentView()
  131.         }
  132.     }
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment