Advertisement
Celestial_Dodo

Simple Clicker Game

Dec 18th, 2023 (edited)
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.59 KB | Gaming | 0 0
  1. import SwiftUI
  2.  
  3. struct ContentView: View {
  4.     @State private var timer: Timer?
  5.     @State var Reload: Bool = false
  6.     @State private var Money: Int = 0
  7.     @State private var PerClick: Int = 1
  8.     @State private var PerSecond: Int = 0
  9.     @State private var DevValue1: String = ""
  10.    
  11.     var body: some View {
  12.         VStack {
  13.             DisclosureGroup("Admin") {
  14.                 Button("Reload") {
  15.                     Reload = true
  16.                     Reload = false
  17.                 }
  18.                 DisclosureGroup("Money") {
  19.                     TextField("Amount Of Money", text: $DevValue1)
  20.                    
  21.                     Button("Set") {
  22.                         Money = Int(DevValue1) ?? Money
  23.                     }
  24.                     Button("Add") {
  25.                         Money += Int(DevValue1) ?? 0
  26.                     }
  27.                     Button("Remove") {
  28.                         Money -= Int(DevValue1) ?? 0
  29.                     }
  30.                 }
  31.             }
  32.             GroupBox() {
  33.                 Button("+"+String(PerClick)+"$") {
  34.                     Money += PerClick
  35.                 }
  36.             }
  37.             Text("Hello, world!")
  38.             let Shop1 = GroupBox() {
  39.                 Button("+1 Per Click - 80$") {
  40.                     if Money >= 80 {
  41.                         Money -= 80
  42.                         PerClick += 1
  43.                     }
  44.                 }
  45.                 Text("\n")
  46.                 Button("+5 Per Click - 350$") {
  47.                     if Money >= 350 {
  48.                         Money -= 350
  49.                         PerClick += 5
  50.                     }
  51.                 }
  52.             }
  53.             let Shop2 = GroupBox() {
  54.                 Button("+1 Per Second - 170$") {
  55.                     if Money >= 170 {
  56.                         Money -= 170
  57.                         PerSecond += 1
  58.                         updateTimerInterval()
  59.                     }
  60.                 }
  61.                 Text("\n")
  62.                 Button("+5 Per Second - 800$") {
  63.                     if Money >= 800 {
  64.                         Money -= 800
  65.                         PerSecond += 5
  66.                         updateTimerInterval()
  67.                     }
  68.                 }
  69.             }
  70.             GroupBox("Stats") {
  71.                 LabeledContent("Money: ") {
  72.                     Text(String(Money))
  73.                 }
  74.                 LabeledContent("Per Click: ") {
  75.                     Text(String(PerClick))
  76.                 }
  77.                 LabeledContent("Per Second: ") {
  78.                     Text(String(PerSecond))
  79.                 }
  80.             }
  81.             GroupBox("Shop") {
  82.                 TabView(selection: .constant(1)) {
  83.                     Shop1.tabItem { Text("Per Click") }.tag(1)
  84.                     Shop2.tabItem { Text("Per Second") }.tag(2)
  85.                 }
  86.             }
  87.         }
  88.         // Start the timer when the view appears
  89.         .onAppear(perform: startTimer)
  90.         // Stop the timer when the view disappears
  91.         .onDisappear(perform: stopTimer)
  92.     }
  93.    
  94.     func startTimer() {
  95.         // Use a timer to execute a closure with a dynamic interval
  96.         timer = Timer.scheduledTimer(withTimeInterval: 1.0 / Double(PerSecond), repeats: true) { _ in
  97.             // Increase money every second
  98.             Money += 1
  99.         }
  100.     }
  101.    
  102.     func stopTimer() {
  103.         timer?.invalidate()
  104.     }
  105.    
  106.     func updateTimerInterval() {
  107.         // Stop the current timer
  108.         stopTimer()
  109.        
  110.         // Start a new timer with the updated interval
  111.         startTimer()
  112.     }
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement