SondreNL

main.py

Apr 1st, 2022 (edited)
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. import os.path
  2.  
  3. from kivy.core.window import Window
  4. from kivy.properties import NumericProperty
  5. from kivy.clock import Clock
  6.  
  7. from kivymd.app import MDApp
  8. from kivymd.uix.card import MDCard
  9.  
  10.  
  11. Window.size = (428, 926)
  12.  
  13.  
  14. class TimerCard(MDCard):
  15.     # set variables to be used
  16.     event = None
  17.     targettime = NumericProperty(0)
  18.     currenttime = NumericProperty(0)
  19.     progressbaropacity = NumericProperty(0)
  20.     startbuttonopacity = NumericProperty(100)
  21.  
  22.     # update the progressbar, reset when finished and add to score
  23.     def update_bar(self, dt):
  24.         if self.currenttime < self.targettime - 1:
  25.             self.currenttime += 1
  26.         else:
  27.             self.event.cancel()
  28.             self.event = None
  29.             self.progressbaropacity = 0
  30.             self.startbuttonopacity = 100
  31.             self.currenttime = 0
  32.             self.app.score += self.targettime * 1.1
  33.         print(self.currenttime)
  34.  
  35.     # start updating the progress bar, repeat the update function once every second
  36.     def start_update(self):
  37.         if self.event is None and self.app.score >= self.targettime:
  38.             self.event = Clock.schedule_interval(self.update_bar, 1)
  39.             self.startbuttonopacity = 0
  40.             self.progressbaropacity = 100
  41.             self.app.score -= self.targettime
  42.  
  43.  
  44. class ProgressBarsApp(MDApp):
  45.     # score variable
  46.     score = NumericProperty(1)
  47.  
  48.     # load the savefile to resume progress. autosave every second
  49.     def __init__(self, **kwargs):
  50.         self.cards = []
  51.         super().__init__()
  52.         Clock.schedule_once(self.load, 0)
  53.         Clock.schedule_interval(self.save, 1)
  54.  
  55.     # function for adding another card
  56.     def make_card(self):
  57.         card = TimerCard()
  58.         card.app = self
  59.         self.cards.append(card)
  60.         self.root.ids.box.add_widget(card)
  61.  
  62.     # save to file function
  63.     def save(self, _):
  64.         with open("savefile.txt", mode="w") as f:
  65.             savetext = str(self.score) + "\n"
  66.             for card in self.cards:
  67.                 savetext += str(card.currenttime) + "," + str(card.targettime) + "\n"
  68.             f.write(savetext)
  69.  
  70.     # load savefile function
  71.     def load(self, _):
  72.         if os.path.exists("savefile.txt"):
  73.             with open("savefile.txt", mode="r") as f:
  74.                 self.score = float(f.readline())
  75.                 self.cards = []
  76.                 for line in f:
  77.                     current, target = line.split(",")
  78.                     self.make_card()
  79.                     self.cards[-1].currenttime = current
  80.                     self.cards[-1].targettime = target
  81.                     if float(current) > 0:
  82.                         self.score += float((target.strip()))
  83.                         self.cards[-1].start_update()
  84.  
  85.  
  86. if __name__ == "__main__":
  87.     ProgressBarsApp().run()
  88.  
Add Comment
Please, Sign In to add comment