-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHaotian_6.24_Button & Fade.py
95 lines (65 loc) · 2.3 KB
/
Haotian_6.24_Button & Fade.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pygame
pygame.init()
win=pygame.display.set_mode((1000,800))
white=(255,255,255)
black=(0,0,0)
gray=(100,100,100)
class button():
def __init__(self, color, x,y,width,height,text=''):
self.color=color
self.x=x
self.y=y
self.width=width
self.height=height
self.text=text
#method to draw the button on the screen
def drawButton(self,win,outline=None):
if outline:
#once if there is an outline
pygame.draw.rect(win, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
pygame.draw.rect(win, self.color, (self.x,self.y,self.width,self.height),0)
if self.text !='':
font=pygame.font.SysFont('comicsans',60)
text=font.render(self.text, 1, (0,0,0))
#change the position and the size of text symoutaneously with the button
win.blit(text, (self.x+(self.width/2-text.get_width()/2), self.y+ (self.height/2-text.get_height()/2)))
def touchButton(self,mouse):
if mouse[0]>self.x and mouse[0]<self.x+self.width:
if mouse[1]>self.y and mouse[1]<self.y+self.height:
return True
return False
def fade(width, height):
fade = pygame.Surface((width, height))
fade.fill(white)
for alpha in range(0,255):
fade.set_alpha(alpha)
grayButton.drawButton(win)
win.blit(fade,(0,0))
pygame.display.update()
pygame.time.delay(3)
def homePage():
win.fill(white)
grayButton.drawButton(win)
run=True
grayButton = button(gray,375,400,250,50,'Monty Hall')
backButton = button(gray,2,2,200,100,'Back <--')
back=0
while run:
homePage()
pygame.display.update()
for event in pygame.event.get():
mouse = pygame.mouse.get_pos()
#hit the Monty Hall button
if event.type == pygame.MOUSEBUTTONDOWN:
if grayButton.touchButton(mouse):
print('clicked')
fade(1000,800)
if event.type == pygame.MOUSEMOTION:
if grayButton.touchButton(mouse):
grayButton.color=black
else:
grayButton.color=gray
if event.type == pygame.QUIT:
run=False
pygame.quit()
quit()