Add CaptureButton

master
Guillaume Dott 2017-09-28 13:56:32 +02:00
parent c8207afa5f
commit a5524e7fe7
1 changed files with 58 additions and 34 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python
import os, sys, pygame
import os, sys, time, pygame
class Button:
def __init__(self, size, topleft):
@ -26,25 +26,51 @@ class Button:
def press(self):
self.pressed = True
self.run()
def unpress(self):
self.pressed = False
self.run()
def run(self):
print('click')
class IncButton(Button):
def run(self):
Photoboite.i += 1
class CaptureButton(Button):
def __init__(self, size, topleft, photoboite):
super(CaptureButton, self).__init__(size, topleft)
self.photoboite = photoboite
self.elapsed = 0
self.capture = False
def draw_on(self, screen):
super(CaptureButton, self).draw_on(screen)
if self.capture:
text = str(self.elapsed + 1)
size = self.photoboite.font.size(text)
ren = self.photoboite.font.render(text, 0, (255, 255, 0))
screen.blit(ren, (400, 400))
class DecButton(Button):
def run(self):
Photoboite.i -= 1
pygame.event.set_blocked((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP))
self.capture = True
for i in range(4):
wait = True
start = time.time()
while wait:
self.elapsed = int(time.time() - start)
if self.elapsed >= 3:
wait = False
else:
photoboite.event()
photoboite.draw()
pygame.display.flip()
print('Capture')
pygame.time.wait(1000)
pygame.event.set_allowed((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP))
self.capture = False
class Photoboite:
i = 0
def __init__(self):
self.screen = self.screen()
pygame.font.init()
@ -55,8 +81,7 @@ class Photoboite:
button_size = int(self.screen.get_width() / 4)
topleft = self.screen.get_width() - button_size - 20, self.screen.get_height() / 4 - button_size / 2
self.buttons.append(IncButton(button_size, topleft))
self.buttons.append(DecButton(50, (70, 400)))
self.buttons.append(CaptureButton(button_size, topleft, self))
self.camera = pygame.Rect(20, 20, self.screen.get_width() - self.screen.get_width() / 4 - 60, self.screen.get_height() - 40)
@ -86,34 +111,33 @@ class Photoboite:
return pygame.display.set_mode()
def draw(self):
self.screen.fill(self.background)
self.screen.fill((0,0,0), rect=self.camera)
for button in self.buttons:
button.draw_on(self.screen)
def event(self):
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.unicode == 'q':
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
for button in self.buttons:
if button.is_pressed(event.pos):
button.press()
elif event.type == pygame.MOUSEBUTTONUP:
for button in self.buttons:
if button.is_pressed(event.pos):
button.unpress()
def run(self):
pressed = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.unicode == 'q':
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
for button in self.buttons:
if button.is_pressed(event.pos):
button.press()
elif event.type == pygame.MOUSEBUTTONUP:
for button in self.buttons:
if button.is_pressed(event.pos):
button.unpress()
self.event()
self.draw()
self.screen.fill(self.background)
self.screen.fill((0,0,0), rect=self.camera)
text = str(Photoboite.i)
size = self.font.size(text)
ren = self.font.render(text, 0, (255, 255, 0))
self.screen.blit(ren, (40, 40))
for button in self.buttons:
button.draw_on(self.screen)
pygame.display.flip()
photoboite = Photoboite()