Hi All!
Hope you're all doing well :)
Its been a few weeks longer than it should have been but work... kids.. pets...
I'd love some feedback on my attempt at Rock, Paper, Scissors..
I have jazzed it a bit by starting at 10 points and slowly decreasing the points you can get with a timer, when the timer hits 0 you loose that round and another starts - if you get it wrong you loose 5 points, if you are very quick and correct you could win 10 points!
As always feedback is very appreciated! (I know that I have just ripped off the radialgradient from the flag game :)
import SwiftUI
let possibleMoves = ["Rock", "Paper", "Scissors"]
let possibleMovesEmoji = ["πͺ¨", "π", "βοΈ"]
let winningMoves = ["Scissors", "Rock", "Paper"]
struct ContentView: View {
@State private var shouldWin = Bool.random()
@State private var move = Int.random(in:0...2)
@State private var scoreTitle = ""
@State private var score = 0
@State private var points = 10
@State private var turns = 0
@State private var showingGameOver = false
var body: some View {
ZStack {
RadialGradient(stops: [
.init(color: Color(red: 0.1, green: 0.2, blue: 0.45), location: 0.3),
.init(color: Color(red: 0.76, green: 0.15, blue: 0.26), location: 0.3)], center:
.top, startRadius: 200, endRadius: 700).ignoresSafeArea()
VStack {
Spacer()
Text("\(shouldWin ? "Win" : "Lose") ")
.font(.system(size: 100))
Text("Against")
Text("\(possibleMoves[move])")
.font(.system(size: 50))
HStack {
ForEach(0..<3) { number in
Button {
moveTapped(number)
} label : {
Text(possibleMovesEmoji[number])
.font(.system(size: 70))
}
}
}
Text(scoreTitle)
Text("Current Score : \(score)")
Text("Playing for \(points) points!")
.onReceive(timer) { _ in
if points > 0 && showingGameOver == false {
points -= 1
}
if points == 0 {
nextTurn()
}
}
Spacer()
Spacer()
}
.alert("Game Over", isPresented: $showingGameOver)
{
Button("Play Again", action: resetGame)
} message: {
Text("You scored \(score)") }
}
}
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
func moveTapped(_ number: Int)
{
if possibleMoves[move] == winningMoves[number] {
shouldWin ? correct() : incorrect() }
else {
shouldWin ? incorrect() : correct()
}
nextTurn()
}
func nextTurn() {
move = Int.random(in:0...2)
shouldWin.toggle()
turns += 1
points = 10
if turns > 10 {
showingGameOver = true
}
}
func correct()
{
scoreTitle = "Correct"
score += points
}
func incorrect()
{
scoreTitle = "Wrong"
score -= 5
if score < 0 {
score = 0
}
}
func resetGame()
{
score = 0
turns = 0
scoreTitle = ""
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}