Amazing-Python-Scripts

Форк
0
130 строк · 3.4 Кб
1
# -*- coding: utf-8 -*-
2

3
import pygame
4
import os
5
import sys
6

7

8
class Button(object):
9

10
    def __init__(self, position, size):
11

12
        # create 3 images
13
        self._images = [
14
            pygame.Surface(size),
15
            pygame.Surface(size),
16
            pygame.Surface(size),
17
        ]
18

19
        # fill images with color - red, gree, blue
20
        self._images[0].fill((255, 226, 39))
21
        self._images[1].fill((235, 89, 110))
22
        self._images[2].fill((77, 55, 93))
23

24
        # get image size and position
25
        self._rect = pygame.Rect(position, size)
26

27
        # select first image
28
        self._index = 0
29

30
    def draw(self, screen):
31

32
        # draw selected image
33
        screen.blit(self._images[self._index], self._rect)
34

35
    def event_handler(self, event):
36

37
        # change selected color if rectange clicked
38
        if event.type == pygame.MOUSEBUTTONDOWN:  # is some button clicked
39
            if event.button == 1:  # is left button clicked
40
                if self._rect.collidepoint(event.pos):  # is mouse over button
41
                    self._index = (self._index+1) % 3  # change image
42

43
# --- main ---
44

45

46
pygame.init()
47
w, h = 800, 800
48
screen = pygame.display.set_mode((w, h))
49
GREEN = (0, 255, 0)
50
GRAY = (174, 150, 255)
51
font = pygame.font.Font('freesansbold.ttf', 22)
52

53
# =============================================================================
54
# Rendring texts23564
55
# =============================================================================
56

57
p1 = font.render("A PNG button , Click it ", True, (233, 248, 103))
58
p2 = font.render("A color img buttons", True, (254, 32, 107))
59
p0 = font.render('Go Back', True, (15, 28, 2))
60
textRectp0 = p0.get_rect()
61
textRectp1 = p1.get_rect()
62
textRectp2 = p2.get_rect()
63
textRectp0.center = (405, 225)
64
textRectp1.center = (200, 225)
65
textRectp2.center = (350, 420)
66

67
# backButton=main.backButton
68

69
module = sys.modules['__main__']
70
path, name = os.path.split(module.__file__)
71
path = os.path.join(path, 'retry_button.png')
72

73
img0 = pygame.image.load(path)
74
img0.convert()
75
rect0 = img0.get_rect()
76
rect0.x = 350
77
rect0.y = 200
78
pygame.draw.rect(img0, GREEN, rect0, 1)
79
act = False
80

81

82
# create buttons
83

84
button1 = Button((205, 435), (100, 100))
85
button2 = Button((310, 435), (100, 100))
86
button3 = Button((420, 435), (100, 100))
87

88
# mainloop
89

90

91
running = True
92
while running:
93
    for event in pygame.event.get():
94
        pos = pygame.mouse.get_pos()
95
        if event.type == pygame.QUIT:
96
            running = False
97
            print("Job Done!!")
98
            # pygame.quit()
99
            pygame.quit()
100
            sys.exit()
101

102
        # --- buttons events ---
103

104
        button1.event_handler(event)
105
        button2.event_handler(event)
106
        button3.event_handler(event)
107

108
        if event.type == pygame.MOUSEBUTTONDOWN:
109
            if rect0.collidepoint(event.pos):
110
                # Toggle the active variable.
111
                act = not act
112
            else:
113
                act = False
114
            if act:
115
                print("You Clicked PNG button")
116
                print("Gone back")
117
                running = False
118
                import main
119

120
    screen.fill(GRAY)
121
    screen.blit(img0, rect0)
122
    screen.blit(p0, textRectp0)
123
    screen.blit(p1, textRectp1)
124
    screen.blit(p2, textRectp2)
125

126
    # ---Buttons
127
    button1.draw(screen)
128
    button2.draw(screen)
129
    button3.draw(screen)
130
    pygame.display.update()
131

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.