From b958325a240bee4e7333ea84c6a476092f42c7b6 Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Thu, 28 Sep 2017 16:39:22 +0200 Subject: [PATCH] Add countdown to capture photos --- photoboite.py | 60 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/photoboite.py b/photoboite.py index 7173e80..8220a16 100755 --- a/photoboite.py +++ b/photoboite.py @@ -35,41 +35,72 @@ class Button: print('click') class CaptureButton(Button): + countdown = 3 + def __init__(self, size, topleft, photoboite): super(CaptureButton, self).__init__(size, topleft) self.photoboite = photoboite self.elapsed = 0 - self.capture = False + self.capture_mode = False + self.count = 0 def draw_on(self, screen): super(CaptureButton, self).draw_on(screen) - if self.capture: - text = str(self.elapsed + 1) + if self.capture_mode: + if self.elapsed >= CaptureButton.countdown: + text = "CLIC!" + else: + text = str(CaptureButton.countdown - self.elapsed) size = self.photoboite.font.size(text) - ren = self.photoboite.font.render(text, 0, (255, 255, 0)) - screen.blit(ren, (400, 400)) + ren = self.photoboite.font.render(text, True, (0, 0, 0)) + topleft = (self.rect.topleft[0] + self.rect.width / 2 - size[0] / 2, self.rect.topleft[1] + self.rect.height / 2 - size[1] / 2) + screen.blit(ren, topleft) def run(self): pygame.event.set_blocked((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP)) - self.capture = True + self.capture_mode = True + + capture = Capture(self.count) + self.count += 1 + for i in range(4): wait = True start = time.time() while wait: self.elapsed = int(time.time() - start) - if self.elapsed >= 3: + if self.elapsed >= CaptureButton.countdown: wait = False - else: - photoboite.event() - photoboite.draw() + + self.photoboite.event() + self.photoboite.draw() pygame.display.flip() - print('Capture') + capture.take() pygame.time.wait(1000) pygame.event.set_allowed((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP)) - self.capture = False + self.capture_mode = False +class Capture: + def __init__(self, uid): + self.uid = uid + self.photos = [] + + def take(self): + self.photos.append(Photo(self.uid, len(self.photos)).take()) + + +class Photo: + def __init__(self, cid, uid): + self.cid = cid + self.uid = uid + + def take(self): + self.name = "%s-%04d-%04d" % (time.strftime('%Y%m%d-%H%M%S'), self.cid, self.uid) + print("Capture " + self.name) + + return self + class Photoboite: def __init__(self): self.screen = self.screen() @@ -85,7 +116,7 @@ class Photoboite: self.camera = pygame.Rect(20, 20, self.screen.get_width() - self.screen.get_width() / 4 - 60, self.screen.get_height() - 40) - self.font = pygame.font.Font(None, 80) + self.font = pygame.font.Font(pygame.font.match_font('calibri'), 180) def screen(self): @@ -140,5 +171,4 @@ class Photoboite: pygame.display.flip() -photoboite = Photoboite() -photoboite.run() +Photoboite().run()