美文网首页
2021-09-09

2021-09-09

作者: 脑子 | 来源:发表于2021-09-09 17:06 被阅读0次

    MemorizeApp.swift

    import SwiftUI
    
    @main
    struct MemorizeApp: App {
        let game = EmojiMemoryGame()
        
        var body: some Scene {
            WindowGroup {
                ContentView(viewModel: game)
            }
        }
    }
    

    ContentView.swift

    import SwiftUI
    
    
    struct ContentView: View {
        let viewModel: EmojiMemoryGame
        @State var emojiCount = 4
        
        var body: some View {
            VStack {
                ScrollView {
                    LazyVGrid(columns: [GridItem(.adaptive(minimum: 65))]){
                        ForEach(viewModel.cards) { card in
                            CardView(card: card)
                                .aspectRatio(2/3, contentMode: .fit)
                        }
                    }
                }
                .foregroundColor(.red)
                Spacer()
                
            }
            .padding(.horizontal)
            
        }
    }
    
    struct CardView: View {
        let card: MemoryGame<String>.Card
        
        var body: some View {
            ZStack {
                let shape = RoundedRectangle(cornerRadius: 20)
                if card.isFaceUp {
                    shape.fill().foregroundColor(.white)
                    shape.strokeBorder(lineWidth: 3)
                    Text(card.content).font(.largeTitle)
                    
                } else {
                    shape.fill()
                }
            }
        }
    }
    
    
    
    
    
    
    
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            let game = EmojiMemoryGame()
            ContentView(viewModel: game)
                .preferredColorScheme(/*@START_MENU_TOKEN@*/.dark/*@END_MENU_TOKEN@*/)
            ContentView(viewModel: game)
                .preferredColorScheme(.light)
        }
    }
    
    
    

    MemoryGame.swift

    import Foundation
    
    struct MemoryGame<Content> {
        var cards: Array<Card>
        
        func choose(card: Card){
            
        }
        
        init(numberOfPairOfCards: Int,createCardContent:(Int) ->Content ) {
            cards = Array<Card>()
            for pairIndex in 0..<numberOfPairOfCards{
                let content: Content = createCardContent(pairIndex)
                cards.append(Card(content: content, id: pairIndex*2))
                cards.append(Card(content: content, id: pairIndex*2+1))
            }
        }
        
        struct Card: Identifiable {
            var isFaceUp: Bool = true
            var isMatched: Bool = false
            var content: Content
            var id: Int
        }
    }
    

    EmojiMemoryGame.swift

    import SwiftUI
    
    
    class EmojiMemoryGame {
        static var emojis = ["🚝","🏍","🚖","🚀","🚕","🚎","🚑","🚜","🦽","🚔","🛸","🚂","🚞","🚨"]
        
        static func makeMemoryGame() -> MemoryGame<String> {
            MemoryGame<String>(numberOfPairOfCards: 4) { pairIndex in emojis[pairIndex]}
        }
        private var model: MemoryGame<String> = makeMemoryGame()
        
        var cards: Array<MemoryGame<String>.Card> {
            return model.cards
        }
    }
    

    相关文章

      网友评论

          本文标题:2021-09-09

          本文链接:https://www.haomeiwen.com/subject/tsxwwltx.html