Amazing-Python-Scripts
130 строк · 3.4 Кб
1# -*- coding: utf-8 -*-
2
3import pygame
4import os
5import sys
6
7
8class Button(object):
9
10def __init__(self, position, size):
11
12# create 3 images
13self._images = [
14pygame.Surface(size),
15pygame.Surface(size),
16pygame.Surface(size),
17]
18
19# fill images with color - red, gree, blue
20self._images[0].fill((255, 226, 39))
21self._images[1].fill((235, 89, 110))
22self._images[2].fill((77, 55, 93))
23
24# get image size and position
25self._rect = pygame.Rect(position, size)
26
27# select first image
28self._index = 0
29
30def draw(self, screen):
31
32# draw selected image
33screen.blit(self._images[self._index], self._rect)
34
35def event_handler(self, event):
36
37# change selected color if rectange clicked
38if event.type == pygame.MOUSEBUTTONDOWN: # is some button clicked
39if event.button == 1: # is left button clicked
40if self._rect.collidepoint(event.pos): # is mouse over button
41self._index = (self._index+1) % 3 # change image
42
43# --- main ---
44
45
46pygame.init()
47w, h = 800, 800
48screen = pygame.display.set_mode((w, h))
49GREEN = (0, 255, 0)
50GRAY = (174, 150, 255)
51font = pygame.font.Font('freesansbold.ttf', 22)
52
53# =============================================================================
54# Rendring texts23564
55# =============================================================================
56
57p1 = font.render("A PNG button , Click it ", True, (233, 248, 103))
58p2 = font.render("A color img buttons", True, (254, 32, 107))
59p0 = font.render('Go Back', True, (15, 28, 2))
60textRectp0 = p0.get_rect()
61textRectp1 = p1.get_rect()
62textRectp2 = p2.get_rect()
63textRectp0.center = (405, 225)
64textRectp1.center = (200, 225)
65textRectp2.center = (350, 420)
66
67# backButton=main.backButton
68
69module = sys.modules['__main__']
70path, name = os.path.split(module.__file__)
71path = os.path.join(path, 'retry_button.png')
72
73img0 = pygame.image.load(path)
74img0.convert()
75rect0 = img0.get_rect()
76rect0.x = 350
77rect0.y = 200
78pygame.draw.rect(img0, GREEN, rect0, 1)
79act = False
80
81
82# create buttons
83
84button1 = Button((205, 435), (100, 100))
85button2 = Button((310, 435), (100, 100))
86button3 = Button((420, 435), (100, 100))
87
88# mainloop
89
90
91running = True
92while running:
93for event in pygame.event.get():
94pos = pygame.mouse.get_pos()
95if event.type == pygame.QUIT:
96running = False
97print("Job Done!!")
98# pygame.quit()
99pygame.quit()
100sys.exit()
101
102# --- buttons events ---
103
104button1.event_handler(event)
105button2.event_handler(event)
106button3.event_handler(event)
107
108if event.type == pygame.MOUSEBUTTONDOWN:
109if rect0.collidepoint(event.pos):
110# Toggle the active variable.
111act = not act
112else:
113act = False
114if act:
115print("You Clicked PNG button")
116print("Gone back")
117running = False
118import main
119
120screen.fill(GRAY)
121screen.blit(img0, rect0)
122screen.blit(p0, textRectp0)
123screen.blit(p1, textRectp1)
124screen.blit(p2, textRectp2)
125
126# ---Buttons
127button1.draw(screen)
128button2.draw(screen)
129button3.draw(screen)
130pygame.display.update()
131