Mercurial > sypikslang
annotate gamelib/engine.py @ 135:8d1cf98e39b5
Reputation decays over time
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Thu, 10 May 2012 17:07:06 +0200 |
parents | e386ec5d179b |
children |
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 |
98 | 50 class GameOver(UserEvent): |
51 # The game is over | |
52 | |
53 TYPE = "GAME_OVER" | |
54 | |
55 @classmethod | |
56 def post(cls, window): | |
57 super(GameOver, cls).post(window=window) | |
58 | |
59 | |
52
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
60 class Engine(object): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
61 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
62 def __init__(self, screen): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
63 self._window_stack = [] |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
64 self._running = False |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
65 self._mouse_down = False |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
66 self._screen = screen |
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 run(self, window): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
69 clock = Clock() |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
70 self._window_stack.append(window) |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
71 self._running = True |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
72 self._mouse_down = False |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
73 while self._running: |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
74 self.process_input() |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
75 self.draw() |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
76 clock.tick(FPS) |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
77 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
78 def draw(self): |
68
7309392d9ca9
fixed widget drawing bugs
Rizmari Versfeld <rizziepit@gmail.com>
parents:
52
diff
changeset
|
79 #for view in self._window_stack: |
7309392d9ca9
fixed widget drawing bugs
Rizmari Versfeld <rizziepit@gmail.com>
parents:
52
diff
changeset
|
80 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
|
81 pygame.display.flip() |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
82 |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
83 def process_input(self): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
84 for event in pygame.event.get(): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
85 if self._mouse_down: |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
86 if event.type == MOUSEBUTTONUP: |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
87 self._mouse_down = False |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
88 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
|
89 elif event.type == MOUSEMOTION: |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
90 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
|
91 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
|
92 self._mouse_down = True |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
93 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
|
94 elif event.type == QUIT: |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
95 self._running = False |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
96 elif AddWindow.matches(event): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
97 self._window_stack.append(event.window) |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
98 elif PopWindow.matches(event): |
1d3d20bdc8b9
Move engine stuff into it's own file
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
99 self._window_stack.pop() |
98 | 100 elif GameOver.matches(event): |
101 # call game_over method on every item in the stack | |
102 for win in self._window_stack: | |
103 if hasattr(win, 'game_over'): | |
104 win.game_over() | |
105 self._window_stack.append(event.window) |