-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path27.button_and_text.py
47 lines (35 loc) · 1.03 KB
/
27.button_and_text.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
import pygame,sys
from pygame.locals import*
pygame.init()
clock=pygame.time.Clock()
screen=pygame.display.set_mode((800,600))
background=(255,255,255)
black=(0,0,0)
purple=(187,51,255)
apple_green=(170,204,0)
cornflower_blue=(128,149,255)
button_color=cornflower_blue
button_position=pygame.Rect(50,50,100,50)
button_font=pygame.font.Font(pygame.font.get_default_font(), 40)
button_text=button_font.render('click',True,black)
game_running=True
while game_running:
clock.tick(60)
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
mouse_position=pygame.mouse.get_pos()
if event.type==pygame.MOUSEMOTION:
if button_position.collidepoint(mouse_position):
button_color=purple
else:
button_color=cornflower_blue
if event.type==pygame.MOUSEBUTTONDOWN:
if button_position.collidepoint(mouse_position):
if event.button==1:
button_color=apple_green
screen.fill(background)
pygame.draw.rect(screen,(button_color),(button_position))
screen.blit(button_text,(55,60))
pygame.display.update()