Created
May 11, 2016 10:27
-
-
Save Tkizzy/c61951cc318e7beaf378f6c292f94883 to your computer and use it in GitHub Desktop.
ChessClock.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ui,time | |
#v = ui.load_view() | |
#v.present('sheet') | |
class clockster(object): | |
def __init__(self): | |
self.v = ui.load_view() | |
self.v.present('sheet') | |
print(self.v["playerOne"]) | |
self.startTime = time.time() | |
self.playerOne = self.v["playerOne"] | |
self.playerTwo = self.v["playerTwo"] | |
self.playerOne.totalTimeElapsed = 0 | |
self.playerTwo.totalTimeElapsed = 0 | |
self.playerOne.latestTurnElapsed = 0 | |
self.playerTwo.latestTurnElapsed = 0 | |
self.currentActivePlayer = "NOBODY" | |
self.mainLoop() | |
def togglePlayer(self,sender): | |
print(sender) | |
print(self.currentActivePlayer) | |
if self.currentActivePlayer == sender: | |
print("no change....") | |
pass | |
elif self.currentActivePlayer == "NOBODY": | |
print("this meNS THE GME HAS BEGUN") | |
self.currentActivePlayer = sender | |
self.lastTimeToggled = time.time() | |
print('game has begun with',sender.name) | |
else: | |
print('player changed') | |
#cleaning up old active plyayer | |
self.currentActivePlayer.totalTimeElapsed = self.currentActivePlayer.totalTimeElapsed+self.currentActivePlayer.latestTurnElapsed | |
#new active player | |
self.currentActivePlayer = sender | |
self.lastTimeToggled = time.time() | |
print("player toggled to",sender.name) | |
def switchClockOnForSelectedPlayer(self): | |
if self.currentActivePlayer == self.playerOne: | |
self.playerOne.latestTurnElapsed= (time.time()-self.lastTimeToggled) | |
self.playerOne.timeToDisplay = self.playerOne.totalTimeElapsed +self.playerOne.latestTurnElapsed | |
self.v["playerOneTime"].text = str(round(self.playerOne.timeToDisplay)) | |
elif self.currentActivePlayer == self.playerTwo: | |
self.playerTwo.latestTurnElapsed= (time.time()-self.lastTimeToggled) | |
self.playerTwo.timeToDisplay = self.playerTwo.totalTimeElapsed +self.playerTwo.latestTurnElapsed | |
self.v["playerTwoTime"].text = str(round(self.playerTwo.timeToDisplay)) | |
@ui.in_background | |
def mainLoop(self): | |
while True: | |
self.switchClockOnForSelectedPlayer() | |
timeElapsed = round(time.time()-self.startTime,0) | |
print(round(time.time()-self.startTime)) | |
self.v["totalTime"].text = str(timeElapsed) | |
time.sleep(1) | |
mainLoop() | |
if __name__ == "__main__": | |
a = clockster() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment