Mercurial > sypikslang
annotate gamelib/engine.py @ 87:d93e1ea2bd0d
Fix method name assumption bug
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Wed, 09 May 2012 20:06:44 +0200 |
parents | 7309392d9ca9 |
children | e386ec5d179b |
rev | line source |
---|---|
52
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
2 # vim:fileencoding=utf-8 ai ts=4 sts=4 et sw=4 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
3 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
4 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
5 """Game engine stuff""" |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
6 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
7 import pygame |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
8 from pygame.locals import (MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION, QUIT, |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
9 USEREVENT) |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
10 from pygame.time import Clock |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
11 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
12 from gamelib.constants import FPS |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
13 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
14 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
15 class UserEvent(object): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
16 # Handy event wrapper |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
17 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
18 TYPE = "unknown" |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
19 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
20 @classmethod |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
21 def post(cls, **kw): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
22 event = pygame.event.Event(USEREVENT, utype=cls.TYPE, **kw) |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
23 pygame.event.post(event) |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
24 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
25 @classmethod |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
26 def matches(cls, event): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
27 return event.type == USEREVENT and event.utype == cls.TYPE |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
28 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
29 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
30 class AddWindow(UserEvent): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
31 # Add the given window to the top of the window stack |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
32 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
33 TYPE = "ADD_WINDOW" |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
34 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
35 @classmethod |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
36 def post(cls, window): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
37 super(AddWindow, cls).post(window=window) |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
38 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
39 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
40 class PopWindow(UserEvent): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
41 # Pop a window off the top of the window stack |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
42 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
43 TYPE = "POP_WINDOW" |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
44 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
45 @classmethod |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
46 def post(cls): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
47 super(PopWindow, cls).post() |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
48 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
49 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
50 class Engine(object): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
51 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
52 def __init__(self, screen): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
53 self._window_stack = [] |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
54 self._running = False |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
55 self._mouse_down = False |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
56 self._screen = screen |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
57 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
58 def run(self, window): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
59 clock = Clock() |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
60 self._window_stack.append(window) |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
61 self._running = True |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
62 self._mouse_down = False |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
63 while self._running: |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
64 self.process_input() |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
65 self.draw() |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
66 clock.tick(FPS) |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
67 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
68 def draw(self): |
68
7309392d9ca9
fixed widget drawing bugs
Rizmari Versfeld <rizziepit@gmail.com>
parents:
52
diff
changeset
|
69 #for view in self._window_stack: |
7309392d9ca9
fixed widget drawing bugs
Rizmari Versfeld <rizziepit@gmail.com>
parents:
52
diff
changeset
|
70 self._window_stack[-1].draw(self._screen) |
52
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
71 pygame.display.flip() |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
72 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
73 def process_input(self): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
74 for event in pygame.event.get(): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
75 if self._mouse_down: |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
76 if event.type == MOUSEBUTTONUP: |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
77 self._mouse_down = False |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
78 self._window_stack[-1].on_mouse_up(event.pos) |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
79 elif event.type == MOUSEMOTION: |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
80 self._window_stack[-1].on_mouse_move(event.pos) |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
81 elif not self._mouse_down and event.type == MOUSEBUTTONDOWN: |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
82 self._mouse_down = True |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
83 self._window_stack[-1].on_mouse_down(event.pos) |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
84 elif event.type == QUIT: |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
85 self._running = False |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
86 elif AddWindow.matches(event): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
87 self._window_stack.append(event.window) |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
88 elif PopWindow.matches(event): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
89 self._window_stack.pop() |