2017-09-21 11:38:03 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import sys, pygame
|
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
class Button:
|
|
|
|
def __init__(self, size, topleft):
|
|
|
|
self.up_image = pygame.image.load('button-up.png').convert_alpha()
|
|
|
|
self.up_image = pygame.transform.smoothscale(self.up_image, (size, size))
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
self.down_image = pygame.image.load('button-down.png').convert_alpha()
|
|
|
|
self.down_image = pygame.transform.smoothscale(self.down_image, (size, size))
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
self.rect = self.up_image.get_rect()
|
|
|
|
self.rect.topleft = topleft
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
self.pressed = False
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def draw_on(self, screen):
|
|
|
|
if self.pressed:
|
|
|
|
screen.blit(self.down_image, self.rect)
|
|
|
|
else:
|
|
|
|
screen.blit(self.up_image, self.rect)
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def is_pressed(self, pos):
|
|
|
|
return self.rect.collidepoint(pos)
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def press(self):
|
|
|
|
self.pressed = True
|
|
|
|
self.run()
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def unpress(self):
|
|
|
|
self.pressed = False
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def run(self):
|
|
|
|
print('click')
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
class IncButton(Button):
|
|
|
|
def run(self):
|
|
|
|
Photoboite.i += 1
|
2017-09-21 14:31:45 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
class DecButton(Button):
|
|
|
|
def run(self):
|
|
|
|
Photoboite.i -= 1
|
2017-09-21 14:31:45 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
class Photoboite:
|
|
|
|
i = 0
|
2017-09-21 14:31:45 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def __init__(self):
|
|
|
|
pygame.init()
|
2017-09-21 14:31:45 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
self.size = 820, 540
|
|
|
|
infos = pygame.display.Info()
|
|
|
|
# self.size = infos.current_w, infos.current_h
|
|
|
|
self.size = 1280, 768
|
|
|
|
self.background = 255, 255, 255
|
|
|
|
self.buttons = []
|
|
|
|
|
|
|
|
self.screen = pygame.display.set_mode(self.size)
|
|
|
|
|
|
|
|
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.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)
|
|
|
|
|
|
|
|
|
|
|
|
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.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()
|
|
|
|
photoboite.run()
|