forked from Mekire/pygame-button
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
64 lines (50 loc) · 1.61 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import sys
import random
import pygame as pg
from button import Button
os.environ["SDL_VIDEO_CENTERED"] = '1'
pg.init()
RED = (255,0,0)
BLUE = (0,0,255)
GREEN = (0,255,0)
BLACK = (0,0,0)
WHITE = (255,255,255)
ORANGE = (255,180,0)
#The button can be styled in a manner similar to CSS.
BUTTON_STYLE = {"hover_color" : BLUE,
"clicked_color" : GREEN,
"clicked_font_color" : BLACK,
"hover_font_color" : ORANGE,
"hover_sound" : pg.mixer.Sound("blipshort1.wav")}
class Control(object):
def __init__(self):
self.screen = pg.display.set_mode((500,500))
self.screen_rect = self.screen.get_rect()
self.clock = pg.time.Clock()
self.done = False
self.fps = 60.0
self.color = WHITE
message = "Change the screen color."
self.button = Button((0,0,200,50),RED, self.change_color,
text=message, **BUTTON_STYLE)
self.button.rect.center = (self.screen_rect.centerx,100)
def change_color(self):
self.color = [random.randint(0,255) for _ in range(3)]
def event_loop(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
self.button.check_event(event)
def main_loop(self):
while not self.done:
self.event_loop()
self.screen.fill(self.color)
self.button.update(self.screen)
pg.display.update()
self.clock.tick(self.fps)
if __name__ == "__main__":
run_it = Control()
run_it.main_loop()
pg.quit()
sys.exit()