21.7 C
New York
Saturday, September 7, 2024

ios – First time coder right here… might somebody check out the snake code I’m constructing on Swift?


I am unable to get the snake circle to maneuver, however have programmed the gestures to alter the snakes route and what occurs when the snake eats the meals. Unsure the place I went mistaken, but when somebody may help me have a look, I do not assume the issue needs to be too arduous to search out…?

Thanks!

struct ContentView: View { @State non-public var startPosition: CGPoint = .zero //begin place of the swipe 
@State non-public var positions = [CGPoint(x: 0, y: 0)] // array of the snakes physique place 
@State non-public var foodPosition = CGPoint(x: 0, y: 0) // meals place
@State non-public var isStarted = true // began swipe or not
@State non-public var gameOver = false // for ending the sport

@State non-public var rating = 0 // rating

@State non-public var route = Course.down //the route the snake goes to take


var snakeSize: CGFloat = 12 //measurement of snake

let timer = Timer.publish(each: 0.08, on: .fundamental, in: .widespread) //to replace snake place after each 0.1 sec

var physique: some View {
    ZStack {
        Colour(#colorLiteral(crimson:0.1161897704, inexperienced:0.1520919502, blue:0.1818574071,alpha: 1)).ignoresSafeArea()
        
        ZStack{
            VStack{
                HStack{
                    Textual content("Rating: ")
                        .font(.headline)
                    Textual content("(rating)")
                        .font(.title2)
                        .fontWeight(.medium)
                    Spacer()
                    Button (motion:{
                        startGame()
                    } ,label: {
                        Textual content("Restart")
                            .font(.footnote)
                            .fontWeight(.medium)
                            .body(width: 65, peak: 30)
                            .background(Colour.secondary)
                            .clipShape(RoundedRectangle(cornerRadius:25))
                    })
                }
                .padding()
                .foregroundColor(.white)
                
                Spacer()
            }
            ForEach(0.. xDist {
                        route = Course.down
                    }
                    else if startPosition.y > gesture.location.y && yDist > xDist {
                        route = Course.up
                    }
                    else if startPosition.x > gesture.location.x && xDist > yDist {
                        route = Course.proper
                    }
                    else if startPosition.x < gesture.location.x && xDist > yDist {
                        route = Course.left
                    }
                }
        )
        
        //MARK: Time Frequency
        
        .onReceive(timer){ _ in
            if !gameOver {
                withAnimation(.linear(period: 0.08)){
                    changeDirection()
                }
                if positions[0] == foodPosition {
                    withAnimation(.spring()) {
                        positions.append(positions[0])
                    }
                    foodPosition = changePosition()
                    rating += 1
                }
            }
        }
    }
    
}

let minX = UIScreen.fundamental.bounds.minX
let maxX = UIScreen.fundamental.bounds.maxX
let minY = UIScreen.fundamental.bounds.minY
let maxY = UIScreen.fundamental.bounds.maxY

//Mark: -Random Positions for Snake

func changePosition() -> CGPoint{
    let rows = Int(maxX / 1.08)
    let columns = Int(maxY / 1.08)
    
    let randomX = Int.random(in: 1.. maxX && !gameOver {
        gameOver.toggle()
    }
    else if positions[0].y < minY || positions[0].y > maxY && !gameOver {
        gameOver.toggle()
    }
    
    var prev = positions[0]
    
    if route == .down{
        positions[0].y += snakeSize
    }
    else if route == .up{
        positions[0].y -= snakeSize
    }
    else if route == .proper{
        positions[0].x += snakeSize
    }
    else if route == .left{
        positions[0].x -= snakeSize
    }
    for index in 1..

I’ve gone via my code and couldn’t establish what would trigger the snake to not transfer. I am anticipating the white circle to maneuver routinely upon gameStart.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles