-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtut35.py
More file actions
83 lines (63 loc) · 2.5 KB
/
Copy pathtut35.py
File metadata and controls
83 lines (63 loc) · 2.5 KB
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
# Chelin Tutorials Todos los Derechos Reservados
# www.chelintutorials.blogspot.com
#Como cambiar el fondo con botones
import pygame
# importo el modulo
class Cursor(pygame.Rect):
def __init__(self):
pygame.Rect.__init__(self,0,0,1,1)
def update(self):
self.left,self.top=pygame.mouse.get_pos()
class Boton(pygame.sprite.Sprite):
def __init__(self,imagen1,imagen2,x=200,y=200):
self.imagen_normal=imagen1
self.imagen_seleccion=imagen2
self.imagen_actual=self.imagen_normal
self.rect=self.imagen_actual.get_rect()
self.rect.left,self.rect.top=(x,y)
def update(self,pantalla,cursor):
if cursor.colliderect(self.rect):
self.imagen_actual=self.imagen_seleccion
else: self.imagen_actual=self.imagen_normal
pantalla.blit(self.imagen_actual,self.rect)
#funcion main
def main():
pygame.init() # inicializo el modulo
# fijo las dimensiones de la pantalla a 300,300 y creo una superficie que va ser la principal
pantalla=pygame.display.set_mode((500,400))
pygame.display.set_caption("Cambia Colores by Chelin") # Titulo de la Ventana
#creo un reloj para controlar los fps
reloj1=pygame.time.Clock()
rojo1=pygame.image.load("rojo.png")
rojo2=pygame.image.load("rojo2.png")
azul1=pygame.image.load("azul.png")
azul2=pygame.image.load("azul2.png")
boton1=Boton(rojo1,rojo2,200,100)
boton2=Boton(azul1,azul2,200,200)
cursor1=Cursor()
blanco=(255,255,255) # color blanco en RGB
rojo=(200,0,0)
azul=(0,0,200)
colordefondo=blanco
salir=False
#LOOP PRINCIPAL
while salir!=True:
#recorro todos los eventos producidos
#en realidad es una lista
for event in pygame.event.get():
if event.type==pygame.MOUSEBUTTONDOWN:
if cursor1.colliderect(boton1.rect):
colordefondo=rojo
if cursor1.colliderect(boton2.rect):
colordefondo=azul
# pygame.QUIT( cruz de la ventana)
if event.type == pygame.QUIT:
salir=True
reloj1.tick(20)#operacion para que todo corra a 20fps
pantalla.fill(colordefondo) # pinto la superficie de blanco
cursor1.update()
boton1.update(pantalla,cursor1)
boton2.update(pantalla, cursor1)
pygame.display.update() #actualizo el display
pygame.quit()
main()