Hello all,
I am new to coding and have just completed the 14 day beginner challenge. I had never written any code before starting this course, so it have been mega satisfying to complete this project.
I will attach a picture and the code below if anyone if interested to see how mine turned out. Any pointers on how to improve my approach would be greatly received.
I would like to include a box to enter a player’s name, and somewhere to enter a starting amount of credit. Plus a pop up should you win or lose. I don’t know how to do those things yet, but I will get to them. For now, this is how it turned out.
As you can see, I stole the background and deal button from the earlier challenge. I quite liked them, so I kept them.
One other thing I did, to make the randomisation work was rename the picture files so that they were all called icon instead of star, apple and coin. Then I could use the same randomisation code from the War Card Game. I’d be interested to know other ways to approach this.
Finally, here is how my code looks. I have no idea if this is good or not, but it works, so I guess that is what matters. lol.
All the best,
Matt
\\
@State var creditTotal:Int = 1000
@State var firstTumbler:String = "icon1"
@State var secondTumbler:String = "icon2"
@State var thirdTumbler:String = "icon3"
var body: some View {
ZStack {
Image("background")
.ignoresSafeArea()
VStack{
Spacer()
Text("SwiftUI Slots!")
.font(.largeTitle)
.foregroundColor(Color.orange)
.frame(width: 220, height: 50)
.background(Color.black)
.cornerRadius(/*@START_MENU_TOKEN@*/24.0/*@END_MENU_TOKEN@*/)
.frame(width: 230, height: 60)
.background(Color.red)
.cornerRadius(/*@START_MENU_TOKEN@*/54.0/*@END_MENU_TOKEN@*/)
Spacer()
HStack{
Text("Credits: " + String(creditTotal))
.font(.largeTitle)
.foregroundColor(Color.orange)
.frame(width: 220, height: 50)
.background(Color.black)
.cornerRadius(/*@START_MENU_TOKEN@*/24.0/*@END_MENU_TOKEN@*/)
.frame(width: 230, height: 60)
.background(Color.red)
.cornerRadius(/*@START_MENU_TOKEN@*/54.0/*@END_MENU_TOKEN@*/)
}
Spacer()
Spacer()
HStack(){
Spacer()
Image(firstTumbler)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 130, height: 130)
.background(Color.green.blur(radius: 2.0))
Image(secondTumbler)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 130, height: 130)
.background(Color.green.blur(radius: 2.0))
Image(thirdTumbler)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 130, height: 130)
.background(Color.green.blur(radius: 2.0))
Spacer()
}.shadow(radius: 10)
Spacer()
Button(action: {
// Random genarator.
let spinRandom1 = Int.random(in: 1...3)
let spinRandom2 = Int.random(in: 1...3)
let spinRandom3 = Int.random(in: 1...3)
// Update cards.
firstTumbler = "icon" + String(spinRandom1)
secondTumbler = "icon" + String(spinRandom2)
thirdTumbler = "icon" + String(spinRandom3)
// Update score.
if firstTumbler == secondTumbler && firstTumbler == thirdTumbler {
creditTotal += 500
}else{
creditTotal -= 100
}
}, label: {
Image("dealbutton")
.resizable()
.aspectRatio(contentMode: .fit)
.padding(/*@START_MENU_TOKEN@*/.all, 66.0/*@END_MENU_TOKEN@*/)
})
Spacer()
}
}
}
\\