view gamelib/main.py @ 41:e285b1e31a08

Add can_attempt method for future flexibility
author Neil Muller <drnlmuller@gmail.com>
date Mon, 07 May 2012 13:59:50 +0200
parents d82d3e54a4ef
children 2bdac178ec6f
line wrap: on
line source

'''Game main module.

Contains the entry point used by the run_game.py script.

Feel free to put all your game code here, or in other modules in this "gamelib"
package.
'''
import pygame
from pygame.locals import MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION, QUIT
from pygame.time import Clock

from gamelib.gui_base import Window
from gamelib.gui import BigButton


pygame.init()

FPS = 30

GAME_IS_RUNNING = True

WINDOW_STACK = []

# input variables
MOUSE_DOWN = False


def main():
    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()


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