Create Button class
parent
0e88ce887d
commit
73393de3a8
116
photoboite.py
116
photoboite.py
|
@ -1,61 +1,101 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import sys, pygame
|
import sys, pygame
|
||||||
pygame.init()
|
|
||||||
|
|
||||||
infos = pygame.display.Info()
|
class Button:
|
||||||
# size = infos.current_w, infos.current_h
|
def __init__(self, size, topleft):
|
||||||
size = 1280, 768
|
self.up_image = pygame.image.load('button-up.png').convert_alpha()
|
||||||
background = 255, 255, 255
|
self.up_image = pygame.transform.smoothscale(self.up_image, (size, size))
|
||||||
|
|
||||||
screen = pygame.display.set_mode(size)
|
self.down_image = pygame.image.load('button-down.png').convert_alpha()
|
||||||
|
self.down_image = pygame.transform.smoothscale(self.down_image, (size, size))
|
||||||
|
|
||||||
screen.fill(background)
|
self.rect = self.up_image.get_rect()
|
||||||
|
self.rect.topleft = topleft
|
||||||
|
|
||||||
button_size = int(screen.get_width() / 4)
|
self.pressed = False
|
||||||
|
|
||||||
button_up = pygame.image.load('button-up.png').convert_alpha()
|
def draw_on(self, screen):
|
||||||
button_up = pygame.transform.smoothscale(button_up, (button_size, button_size))
|
if self.pressed:
|
||||||
button_down = pygame.image.load('button-down.png').convert_alpha()
|
screen.blit(self.down_image, self.rect)
|
||||||
button_down = pygame.transform.smoothscale(button_down, (button_size, button_size))
|
else:
|
||||||
button_up_rect = button_up.get_rect()
|
screen.blit(self.up_image, self.rect)
|
||||||
button_up_rect.topleft = screen.get_width() - button_size - 20, screen.get_height() / 2 - button_size / 2
|
|
||||||
|
|
||||||
button2 = pygame.Rect(screen.get_width() - 70, 100, 50, 50)
|
def is_pressed(self, pos):
|
||||||
|
return self.rect.collidepoint(pos)
|
||||||
|
|
||||||
camera = pygame.Rect(20, 20, screen.get_width() - screen.get_width() / 4 - 60, screen.get_height() - 40)
|
def press(self):
|
||||||
|
self.pressed = True
|
||||||
|
self.run()
|
||||||
|
|
||||||
font = pygame.font.Font(None, 80)
|
def unpress(self):
|
||||||
|
self.pressed = False
|
||||||
|
|
||||||
i = 0
|
def run(self):
|
||||||
pressed = False
|
print('click')
|
||||||
while True:
|
|
||||||
|
class IncButton(Button):
|
||||||
|
def run(self):
|
||||||
|
Photoboite.i += 1
|
||||||
|
|
||||||
|
class DecButton(Button):
|
||||||
|
def run(self):
|
||||||
|
Photoboite.i -= 1
|
||||||
|
|
||||||
|
class Photoboite:
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
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():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.unicode == 'q':
|
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.unicode == 'q':
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif event.type == pygame.MOUSEBUTTONDOWN:
|
elif event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
if button_up_rect.collidepoint(event.pos):
|
for button in self.buttons:
|
||||||
pressed = True
|
if button.is_pressed(event.pos):
|
||||||
|
button.press()
|
||||||
elif event.type == pygame.MOUSEBUTTONUP:
|
elif event.type == pygame.MOUSEBUTTONUP:
|
||||||
if button_up_rect.collidepoint(event.pos):
|
for button in self.buttons:
|
||||||
pressed = False
|
if button.is_pressed(event.pos):
|
||||||
i += 1
|
button.unpress()
|
||||||
if button2.collidepoint(event.pos):
|
|
||||||
i -= 1
|
|
||||||
|
|
||||||
screen.fill(background)
|
self.screen.fill(self.background)
|
||||||
|
|
||||||
screen.fill((0,0,0), rect=button2)
|
self.screen.fill((0,0,0), rect=self.camera)
|
||||||
screen.fill((0,0,0), rect=camera)
|
|
||||||
|
|
||||||
text = str(i)
|
text = str(Photoboite.i)
|
||||||
size = font.size(text)
|
size = self.font.size(text)
|
||||||
ren = font.render(text, 0, (255, 255, 0))
|
ren = self.font.render(text, 0, (255, 255, 0))
|
||||||
screen.blit(ren, (40, 40))
|
self.screen.blit(ren, (40, 40))
|
||||||
|
|
||||||
|
|
||||||
if pressed:
|
for button in self.buttons:
|
||||||
screen.blit(button_down, button_up_rect)
|
button.draw_on(self.screen)
|
||||||
else:
|
|
||||||
screen.blit(button_up, button_up_rect)
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
|
photoboite = Photoboite()
|
||||||
|
photoboite.run()
|
||||||
|
|
Loading…
Reference in New Issue