Created
February 1, 2015 22:30
-
-
Save iminurnamez/67c7e87e6d3c3ca9e39b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# Sample hueberry | |
# written in Python | |
# John Gamble 15/01/2015 | |
import sys, pygame | |
import phue | |
import time | |
from phue import Bridge | |
# setup colours | |
BLACK = (0,0,0) | |
GREY = (50,50,50,50) | |
WHITE = (255,255,255) | |
GREEN = (0,200,0) | |
LESSGREEN = (0,100,50) | |
RED = (255,0,0) | |
LESSRED = (200,0,0) | |
BLUE = (0,0,255) | |
LESSBLUE = (0,0,100) | |
#I'd put Button in its own module | |
class Button(object): | |
def __init__(self, font, msg, x, y, w, h, idle_color, active_color, callback=None, | |
args=None): | |
self.rect = pygame.Rect(x, y, w, h) | |
self.idle_color = idle_color | |
self.active_color = active_color | |
self.callback = callback | |
self.args = args if args else [] | |
self.active = False | |
self.label = font.render("{}".format(msg), True, BLACK) | |
self.label_rect = self.label.get_rect(center=self.rect.center) | |
def update(self, mouse_pos): | |
self.active = False | |
if self.rect.collidepoint(mouse_pos): | |
self.active = True | |
def get_event(self, event): | |
if event.type == pygame.MOUSEBUTTONDOWN: | |
if self.rect.collidepoint(event.pos): | |
self.callback(*self.args) | |
def draw(self, surface): | |
if self.active: | |
color = self.active_color | |
else: | |
color = self.idle_color | |
pygame.draw.rect(surface, color, self.rect) | |
surface.blit(self.label, self.label_rect) | |
#I'd put Control and AppState in a module | |
class Control(object): | |
def __init__(self, screen_size, states, start_state): | |
self.done = False | |
self.screen = pygame.display.set_mode(screen_size) | |
pygame.display.toggle_fullscreen() | |
self.clock = pygame.time.Clock() | |
self.fps = 30 | |
self.states = states | |
self.state_name = start_state | |
self.state = states[self.state_name] | |
def flip_state(self): | |
persistent = self.state.cleanup() | |
self.state_name = self.state.next | |
self.state = self.states[self.state_name] | |
self.state.startup(persistent) | |
def event_loop(self): | |
for event in pygame.event.get(): | |
self.state.get_event(event) | |
def update(self): | |
if self.state.quit: | |
self.done = True | |
elif self.state.done: | |
self.flip_state() | |
self.state.update(self.screen) | |
def run(self): | |
while not self.done: | |
self.clock.tick(self.fps) | |
self.event_loop() | |
self.update() | |
pygame.display.update() | |
class AppState(object): | |
""" | |
Class to represent a single game scene/screen/state. | |
""" | |
def __init__(self, persistent={}): | |
self.done = False | |
self.quit = False | |
self.persist = persistent | |
def startup(self, persistent): | |
""" | |
Called when the state resumes/ becomes active. | |
""" | |
self.persist = persistent | |
def cleanup(self): | |
""" | |
Called during state flipping before the next state | |
becomes active. | |
""" | |
self.done = False | |
return self.persist | |
def get_event(self, event): | |
"""Each event is passed to the state by the | |
Control instance. | |
""" | |
pass | |
def update(self, surface): | |
""" | |
Update the state of the state and draw to the | |
display surface. | |
""" | |
self.draw(surface) | |
def draw(self, surface): | |
pass | |
#I'd put each state its own module | |
class Intro(AppState): | |
""" | |
Intro State (probably not the appropriate name). | |
""" | |
def __init__(self): | |
super(Intro, self).__init__() | |
font = pygame.font.Font("freesansbold.ttf",20) | |
self.buttons = [ | |
Button(font, "1", 0, 0, 50, 50, GREEN, LESSGREEN, self.lightson, [1]), | |
Button(font, "2", 60, 0, 50, 50, GREEN, LESSGREEN, self.lightson, [2]), | |
Button(font, "3", 120, 0, 50, 50, GREEN, LESSGREEN, self.lightson, [3]), | |
Button(font, "4", 180, 0, 50, 50, GREEN, LESSGREEN, self.lightson, [4]), | |
Button(font, "5", 0, 60, 50, 50, GREEN, LESSGREEN, self.lightson, [5]), | |
Button(font, "Pair", 0, 140, 100, 100, BLUE, LESSBLUE, self.pair), | |
Button(font, "Quit", 270, 0, 50, 50, RED, LESSRED, self.quit_game) | |
] | |
def get_event(self, event): | |
if event.type == pygame.QUIT: | |
self.quit_game() | |
for button in self.buttons: | |
button.get_event(event) | |
def lightson(self, light_number): | |
b = Bridge('192.168.1.3') | |
l_status = (b.get_light(light_number, 'on')) | |
if l_status == 0: | |
b.set_light(light_number,'on', True) | |
elif l_status == 1: | |
b.set_light(light_number,'on', False) | |
else: | |
print("something went wrong") | |
print(l_status) | |
def pair(self, *args): | |
b = Bridge('192.168.1.3') | |
b.connect() | |
def quit_game(self, *args): | |
self.done = True | |
self.quit = True | |
def exit_state(self, *args): | |
self.done = True | |
def draw(self, surface): | |
surface.fill(WHITE) | |
for button in self.buttons: | |
button.draw(surface) | |
def update(self, surface): | |
mouse_pos = pygame.mouse.get_pos() | |
for button in self.buttons: | |
button.update(mouse_pos) | |
self.draw(surface) | |
class SplashScreen(AppState): | |
""" | |
A splash screen. Added to test out state flipping. | |
""" | |
def __init__(self): | |
super(SplashScreen, self).__init__() | |
self.next = "INTRO" | |
def get_event(self, event): | |
if event.type == pygame.QUIT: | |
self.quit = True | |
if event.type == pygame.MOUSEBUTTONDOWN: | |
self.done = True | |
def update(self, surface): | |
self.draw(surface) | |
def draw(self, surface): | |
surface.fill(pygame.Color("yellow")) | |
#Once the other stuff has been moved into modules | |
#this will be your main script | |
if __name__ == "__main__": | |
pygame.init() | |
states = {"SPLASH": SplashScreen(), | |
"INTRO": Intro()} | |
app = Control((320, 240), states, "SPLASH") | |
app.run() | |
pygame.quit() | |
sys.exit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment