Mercurial > sypikslang
changeset 38:7e18a67995f6
fixed pep8 issues
author | Rizmari Versfeld <rizziepit@gmail.com> |
---|---|
date | Mon, 07 May 2012 00:13:11 +0200 |
parents | 9c4bf1f15431 |
children | d82d3e54a4ef |
files | gamelib/gui.py gamelib/gui_base.py gamelib/main.py |
diffstat | 3 files changed, 200 insertions(+), 202 deletions(-) [+] |
line wrap: on
line diff
--- a/gamelib/gui.py Sun May 06 22:05:40 2012 +0200 +++ b/gamelib/gui.py Mon May 07 00:13:11 2012 +0200 @@ -1,6 +1,7 @@ import os import pygame +from pygame import image from pygame.sprite import Sprite from gamelib import data @@ -8,24 +9,24 @@ class ImageDrawable(Drawable): - - def __init__(self, rect, image): - super(ImageDrawable, self).__init__(rect) - self.image = image - - def draw(self, surface): - surface.blit(self.image, (self.rect[0], self.rect[1])) - + + def __init__(self, rect, image): + super(ImageDrawable, self).__init__(rect) + self.image = image + + def draw(self, surface): + surface.blit(self.image, (self.rect[0], self.rect[1])) + -class MainMenuButton(TextButton): - WIDTH = 128 - HEIGHT = 64 - BACKGROUND_IMAGE_NORMAL = pygame.image.load(data.filepath('images/button_normal.png')) - BACKGROUND_IMAGE_DOWN = pygame.image.load(data.filepath('images/button_down.png')) - - def __init__(self, pos, text): - rect = (0, 0, self.WIDTH, self.HEIGHT) - drawable_normal = ImageDrawable(rect, self.BACKGROUND_IMAGE_NORMAL) - drawable_down = ImageDrawable(rect, self.BACKGROUND_IMAGE_DOWN) - super(MainMenuButton, self).__init__((pos[0],pos[1], self.WIDTH, self.HEIGHT), drawable_normal, drawable_down, text) - \ No newline at end of file +class BigButton(TextButton): + WIDTH = 128 + HEIGHT = 64 + BG_IMAGE_NORMAL = image.load(data.filepath('images/button_normal.png')) + BG_IMAGE_DOWN = image.load(data.filepath('images/button_down.png')) + + def __init__(self, pos, text): + rect1 = (0, 0, self.WIDTH, self.HEIGHT) + n = ImageDrawable(rect1, self.BG_IMAGE_NORMAL) + d = ImageDrawable(rect1, self.BG_IMAGE_DOWN) + rect2 = (pos[0], pos[1], self.WIDTH, self.HEIGHT) + super(BigButton, self).__init__(rect2, n, d, text)
--- a/gamelib/gui_base.py Sun May 06 22:05:40 2012 +0200 +++ b/gamelib/gui_base.py Mon May 07 00:13:11 2012 +0200 @@ -14,158 +14,159 @@ class Drawable(object): - - def __init__(self, rect): - if isinstance(rect, Rect): - self.rect = rect - else: - self.rect = Rect(rect[0],rect[1],rect[2],rect[3]) - - def draw(self, surface): - pass - - + + def __init__(self, rect): + if isinstance(rect, Rect): + self.rect = rect + else: + self.rect = Rect(rect[0], rect[1], rect[2], rect[3]) + + def draw(self, surface): + pass + + class Clickable(object): - - def on_mouse_down(self, pos): - pass - - def on_mouse_up(self, pos): - pass - - def on_mouse_move(self, pos): - pass - - def on_mouse_cancel(self): - pass - - def on_click(self): - pass - + + def on_mouse_down(self, pos): + pass + + def on_mouse_up(self, pos): + pass + + def on_mouse_move(self, pos): + pass + + def on_mouse_cancel(self): + pass + + def on_click(self): + pass + class ContainerView(Drawable): - - def __init__(self): - self.children = [] - - def add_child(self, child): - self.children.append(child) - - def remove_child(self, child): - self.children.remove(child) - - def get_child_by_pos(self, pos): - for child in self.children: - if child.rect.collidepoint(pos): - if isinstance(child, ContainerView): - # calculates position relative to child - return child.get_child_by_pos((pos[0] - child.rect[0], pos[1] - child.rect[1])) - else: - return child - - def draw_children(self, surface): - for child in self.children: - child.draw(surface) - - + + def __init__(self): + self.children = [] + + def add_child(self, child): + self.children.append(child) + + def remove_child(self, child): + self.children.remove(child) + + def get_child_by_pos(self, pos): + for child in self.children: + if child.rect.collidepoint(pos): + if isinstance(child, ContainerView): + # calculates position relative to child + x = pos[0] - child.rect[0] + y = pos[1] - child.rect[1] + return child.get_child_by_pos((x, y)) + else: + return child + + def draw_children(self, surface): + for child in self.children: + child.draw(surface) + + class Window(Clickable, ContainerView): - - def __init__(self, screen): - super(Window, self).__init__() - self.surface = Surface((screen.get_width(), screen.get_height())) - self.background_colour = None - self.background_image = None - self.pressed_child = None - - def on_mouse_down(self, pos): - child = self.get_child_by_pos(pos) - if isinstance(child, Clickable): - # calculates position relative to child - child.on_mouse_down((pos[0] - child.rect[0], pos[1] - child.rect[1])) - self.pressed_child = child - - def on_mouse_up(self, pos): - if self.pressed_child: - child = self.pressed_child - child.on_mouse_up((pos[0] - child.rect[0], pos[1] - child.rect[1])); - self.pressed_child = None - - def on_mouse_move(self, pos): - if self.pressed_child and self.pressed_child != self.get_child_by_pos(pos): - self.pressed_child.on_mouse_cancel() - self.pressed_child = None - - def draw(self, screen): - if self.background_colour: - self.surface.fill(self.background_colour) - if self.background_image: - self.surface.blit(self.background_image, (0,0)) - self.draw_children(self.surface) - screen.blit(self.surface, (0,0)) - + + def __init__(self, screen): + super(Window, self).__init__() + self.surface = Surface((screen.get_width(), screen.get_height())) + self.background_colour = None + self.background_image = None + self.pressed_child = None + + def on_mouse_down(self, pos): + child = self.get_child_by_pos(pos) + if isinstance(child, Clickable): + # calculates position relative to child + x = pos[0] - child.rect[0] + y = pos[1] - child.rect[1] + child.on_mouse_down((x, y)) + self.pressed_child = child + + def on_mouse_up(self, pos): + if self.pressed_child: + child = self.pressed_child + x = pos[0] - child.rect[0] + y = pos[1] - child.rect[1] + child.on_mouse_up((x, y)) + self.pressed_child = None + + def on_mouse_move(self, pos): + if self.pressed_child: + if self.pressed_child != self.get_child_by_pos(pos): + self.pressed_child.on_mouse_cancel() + self.pressed_child = None + + def draw(self, screen): + if self.background_colour: + self.surface.fill(self.background_colour) + if self.background_image: + self.surface.blit(self.background_image, (0, 0)) + self.draw_children(self.surface) + screen.blit(self.surface, (0, 0)) + class StateDrawable(Drawable): - - def __init__(self, rect): - super(StateDrawable, self).__init__(rect) - self.state = -1 - self.states = {} - self.drawables = [] - self.surface = Surface((self.rect[2], self.rect[3])) - - def draw(self, surface): - if self.state != -1 and self.drawables[self.state]: - self.drawables[self.state].draw(surface) - - def add_state(self, state_name, drawable): - self.states[state_name] = len(self.drawables) - self.drawables.append(drawable) - - def set_state(self, state_name): - self.state = self.states[state_name] - - + + def __init__(self, rect): + super(StateDrawable, self).__init__(rect) + self.state = -1 + self.states = {} + self.drawables = [] + self.surface = Surface((self.rect[2], self.rect[3])) + + def draw(self, surface): + if self.state != -1 and self.drawables[self.state]: + self.drawables[self.state].draw(surface) + + def add_state(self, state_name, drawable): + self.states[state_name] = len(self.drawables) + self.drawables.append(drawable) + + def set_state(self, state_name): + self.state = self.states[state_name] + + class Button(Clickable, StateDrawable): - - def __init__(self, rect, normal_drawable, down_drawable): - super(Button, self).__init__(rect) - self.add_state('NORMAL', normal_drawable) - self.add_state('DOWN', down_drawable) - self.set_state('NORMAL') - - def on_mouse_down(self, pos): - self.set_state('DOWN') - - def on_mouse_up(self, pos): - self.set_state('NORMAL') - self.on_click() - - def on_mouse_cancel(self): - self.set_state('NORMAL') - - + + def __init__(self, rect, normal_drawable, down_drawable): + super(Button, self).__init__(rect) + self.add_state('NORMAL', normal_drawable) + self.add_state('DOWN', down_drawable) + self.set_state('NORMAL') + + def on_mouse_down(self, pos): + self.set_state('DOWN') + + def on_mouse_up(self, pos): + self.set_state('NORMAL') + self.on_click() + + def on_mouse_cancel(self): + self.set_state('NORMAL') + + class TextButton(Button): - def __init__(self, rect, normal_drawable, down_drawable, text): - super(TextButton, self).__init__(rect, normal_drawable, down_drawable) - self.surface = Surface((rect[2],rect[3])) - self.text = text - font_large.set_bold(True) - self.text_surface = font_large.render(self.text, True, (128,128,128,128)) - shadow = font_large.render(self.text, True, (0,0,0,255)) - font_large.set_bold(False) - self.text_surface.blit(shadow, (-2, -2)) - size = font_large.size(self.text) - self.text_offset = ((rect[2]-size[0])/2, (rect[3]-size[1])/2) - - def draw(self, surface): - self.surface.fill((0,0,0,0)) - super(TextButton, self).draw(self.surface) - self.surface.blit(self.text_surface, self.text_offset) - surface.blit(self.surface, self.rect) + def __init__(self, rect, normal_drawable, down_drawable, text): + super(TextButton, self).__init__(rect, normal_drawable, down_drawable) + self.surface = Surface((rect[2], rect[3])) + self.text = text + font_large.set_bold(True) + self.text_surface = font_large.render(self.text, True, (128, 128, 128)) + shadow = font_large.render(self.text, True, (0, 0, 0)) + font_large.set_bold(False) + self.text_surface.blit(shadow, (-2, -2)) + size = font_large.size(self.text) + self.text_offset = ((rect[2] - size[0]) / 2, (rect[3] - size[1]) / 2) - - - - - \ No newline at end of file + def draw(self, surface): + self.surface.fill((0, 0, 0, 0)) + super(TextButton, self).draw(self.surface) + self.surface.blit(self.text_surface, self.text_offset) + surface.blit(self.surface, self.rect)
--- a/gamelib/main.py Sun May 06 22:05:40 2012 +0200 +++ b/gamelib/main.py Mon May 07 00:13:11 2012 +0200 @@ -27,44 +27,40 @@ # input variables MOUSE_DOWN = False - - + + def main(): - #print data.load('sample.txt').read() - clock = Clock() - screen = pygame.display.set_mode((800, 600)) - window = Window(screen) - window.background_colour = (0,0,0) - button1 = MainMenuButton(((800-128)/2, 200), 'Start') - window.add_child(button1) - WINDOW_STACK.append(window) - while GAME_IS_RUNNING: - process_input() - draw(screen) - clock.tick(FPS) + clock = Clock() + screen = pygame.display.set_mode((800, 600)) + window = Window(screen) + window.background_colour = (0, 0, 0) + button1 = BigButton(((800 - 128) / 2, 200), 'Start') + window.add_child(button1) + WINDOW_STACK.append(window) + while GAME_IS_RUNNING: + process_input() + draw(screen) + clock.tick(FPS) - + def draw(screen): - for view in WINDOW_STACK: - view.draw(screen) - pygame.display.flip() - - + for view in WINDOW_STACK: + view.draw(screen) + pygame.display.flip() + + def process_input(): - global MOUSE_DOWN - global GAME_IS_RUNNING - for event in pygame.event.get(): - if MOUSE_DOWN: - if event.type == MOUSEBUTTONUP: - MOUSE_DOWN = False - WINDOW_STACK[len(WINDOW_STACK)-1].on_mouse_up(event.pos) - elif event.type == MOUSEMOTION: - WINDOW_STACK[len(WINDOW_STACK)-1].on_mouse_move(event.pos) - elif not MOUSE_DOWN and event.type == MOUSEBUTTONDOWN: - MOUSE_DOWN = True - WINDOW_STACK[len(WINDOW_STACK)-1].on_mouse_down(event.pos) - elif event.type == QUIT: - GAME_IS_RUNNING = False - - - \ No newline at end of file + global MOUSE_DOWN + global GAME_IS_RUNNING + for event in pygame.event.get(): + if MOUSE_DOWN: + if event.type == MOUSEBUTTONUP: + MOUSE_DOWN = False + WINDOW_STACK[len(WINDOW_STACK) - 1].on_mouse_up(event.pos) + elif event.type == MOUSEMOTION: + WINDOW_STACK[len(WINDOW_STACK) - 1].on_mouse_move(event.pos) + elif not MOUSE_DOWN and event.type == MOUSEBUTTONDOWN: + MOUSE_DOWN = True + WINDOW_STACK[len(WINDOW_STACK) - 1].on_mouse_down(event.pos) + elif event.type == QUIT: + GAME_IS_RUNNING = False