52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
import pygame
|
|
from pygame.locals import *
|
|
|
|
class Button:
|
|
def __init__(self, text, position, size):
|
|
self.text = text
|
|
self.font = pygame.font.Font(None, 36)
|
|
self.position = position
|
|
self.size = size
|
|
self.rect = pygame.Rect(position, size)
|
|
self.clicked = False
|
|
|
|
def draw(self, surface):
|
|
pygame.draw.rect(surface, (255, 255, 255), self.rect, 2)
|
|
text_surface = self.font.render(self.text, True, (255, 255, 255))
|
|
text_rect = text_surface.get_rect(center=self.rect.center)
|
|
surface.blit(text_surface, text_rect)
|
|
|
|
def is_clicked(self, pos):
|
|
if self.rect.collidepoint(pos):
|
|
self.clicked = True
|
|
return True
|
|
return False
|
|
|
|
def main():
|
|
pygame.init()
|
|
display = (800, 600)
|
|
screen = pygame.display.set_mode(display)
|
|
clock = pygame.time.Clock()
|
|
|
|
button = Button("Click Me!", (300, 250), (200, 50))
|
|
|
|
running = True
|
|
while running:
|
|
for event in pygame.event.get():
|
|
if event.type == QUIT:
|
|
running = False
|
|
elif event.type == MOUSEBUTTONDOWN:
|
|
if event.button == 1:
|
|
if button.is_clicked(event.pos):
|
|
print("Button Clicked!")
|
|
|
|
screen.fill((0, 0, 0))
|
|
button.draw(screen)
|
|
pygame.display.flip()
|
|
clock.tick(60)
|
|
|
|
pygame.quit()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|