Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- struct FlagImage: View {
- var someImage: String
- var body: some View {
- Image(someImage)
- .renderingMode(.original)
- .clipShape(Capsule())
- .overlay(Capsule().stroke(Color.black, lineWidth: 1))
- .shadow(color: .black, radius: 2) }
- }
- struct ContentView: View {
- @State private var countries = ["Estonia", "France", "Germany", "Ireland", "Italy", "Nigeria", "Poland", "Russia", "Spain", "UK", "US"].shuffled()
- @State private var correctAnswer = Int.random(in: 0...2)
- @State private var showingScore = false
- @State private var scoreTitle = ""
- @State private var score = 0
- var body: some View {
- ZStack {
- LinearGradient(gradient: Gradient(colors: [.blue, .black]), startPoint: .top, endPoint: .bottom)
- .edgesIgnoringSafeArea(.all)
- VStack(spacing: 30) {
- VStack {
- Text("Tap the flag of")
- .foregroundColor(.white)
- Text(countries[correctAnswer])
- .foregroundColor(.white)
- .font(.largeTitle)
- .fontWeight(.black)
- }
- ForEach(0..<3) { number in
- Button(action: {
- self.flagTapped(number)
- }) {
- FlagImage(someImage: self.countries[number])
- }
- }
- Text("Your score is: \(score)")
- .font(.largeTitle)
- .foregroundColor(Color.white)
- Spacer()
- }
- }
- .alert(isPresented: $showingScore) {
- Alert(title: Text(scoreTitle), message: Text("Your score is \(score)"), dismissButton: .default(Text("Continue")) {
- self.askQuestion()
- })
- }
- }
- func flagTapped(_ number: Int) {
- if number == correctAnswer {
- scoreTitle = "Correct"
- score += 1
- } else {
- scoreTitle = "Wrong, that's the flag of \(countries[number])"
- score -= 1
- }
- showingScore = true
- }
- func askQuestion() {
- countries.shuffle()
- correctAnswer = Int.random(in: 0...2)
- }
- }
- struct ContentView_Previews: PreviewProvider {
- static var previews: some View {
- ContentView()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement