Last active
October 3, 2016 19:31
-
-
Save bitcraft/f9d0bff85b58a889b48a58fa4a4fe249 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sys import exit | |
import pygame | |
from pygame.locals import * | |
SCREEN_SIZE = (1280, 960) | |
pygame.init() | |
screen = pygame.display.set_mode(SCREEN_SIZE, RESIZABLE, 32) | |
screen_centre = (SCREEN_SIZE[0] / 2, SCREEN_SIZE[1] / 2) | |
screen.fill((255, 255, 255)) | |
pygame.display.set_caption("Demo") | |
class Window(): | |
def __init__(self, screen, position, size): | |
self.screen = screen | |
self.rect = Rect(position, size) # position -> (x, y) size -> (width, height) | |
class Button(): | |
def __init__(self, parent, position, size, colour, callback_function): | |
self.parent = parent | |
self.rect = Rect(position, size) # position -> (x, y) size -> (width, height) | |
self.colour = colour | |
self.callback = callback_function | |
self.screen = parent.screen | |
self._visible = True | |
def get_event(self, event): | |
#print(event.pos) | |
if self.rect.collidepoint(event.pos): # Not sure why this isn't working | |
print("Calling callback function.") | |
self.callback()#*self.args) | |
def draw(self): | |
pygame.draw.rect(self.screen, self.colour, self.rect) | |
def test_button_event(): | |
print("Hello") | |
buttons = [] | |
def create_button(): | |
newwindow = Window(screen, (0, 0), (SCREEN_SIZE)) | |
newbutton = Button(newwindow, (0, SCREEN_SIZE[1] - 50), (100, 50), pygame.Color('Blue'), test_button_event) | |
buttons.append(newbutton) | |
newbutton.draw() | |
create_button() | |
clock = pygame.time.Clock() | |
while True: | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
pygame.quit() | |
exit(0) | |
if event.type == VIDEORESIZE: | |
SCREEN_SIZE = event.size | |
screen = pygame.display.set_mode(SCREEN_SIZE, RESIZABLE, 32) | |
print("Window resized to " + str(event.size)) | |
screen.fill((255, 255, 255)) | |
elif event.type == MOUSEBUTTONDOWN: | |
print("mouse at (%d, %d)" % event.pos) | |
for button in buttons: | |
button.get_event(event) | |
for button in buttons: | |
button.draw() | |
clock.tick(30) | |
pygame.display.update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment