Mercurial > sypikslang
annotate gamelib/main.py @ 48:a2980cc9a060
Factor out some constants
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Mon, 07 May 2012 21:23:54 +0200 |
parents | d35a3762edda |
children | ac637e84f8f8 |
rev | line source |
---|---|
0
d0de8832774b
Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff
changeset
|
1 '''Game main module. |
d0de8832774b
Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff
changeset
|
2 |
d0de8832774b
Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff
changeset
|
3 Contains the entry point used by the run_game.py script. |
d0de8832774b
Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff
changeset
|
4 |
d0de8832774b
Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff
changeset
|
5 Feel free to put all your game code here, or in other modules in this "gamelib" |
d0de8832774b
Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff
changeset
|
6 package. |
d0de8832774b
Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff
changeset
|
7 ''' |
37 | 8 import pygame |
39
d82d3e54a4ef
fixed more pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents:
38
diff
changeset
|
9 from pygame.locals import MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION, QUIT |
37 | 10 from pygame.time import Clock |
0
d0de8832774b
Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff
changeset
|
11 |
39
d82d3e54a4ef
fixed more pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents:
38
diff
changeset
|
12 from gamelib.gui_base import Window |
d82d3e54a4ef
fixed more pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents:
38
diff
changeset
|
13 from gamelib.gui import BigButton |
48
a2980cc9a060
Factor out some constants
Neil Muller <drnlmuller@gmail.com>
parents:
44
diff
changeset
|
14 from gamelib.constants import WIDTH, HEIGHT, SCREEN, FPS |
0
d0de8832774b
Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff
changeset
|
15 |
1
c90a6586cd66
PEP8-ify skellington (because)
Neil Muller <drnlmuller@gmail.com>
parents:
0
diff
changeset
|
16 |
37 | 17 pygame.init() |
18 | |
19 GAME_IS_RUNNING = True | |
20 | |
21 WINDOW_STACK = [] | |
22 | |
23 # input variables | |
24 MOUSE_DOWN = False | |
38 | 25 |
26 | |
43
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
27 class ExitGameButton(BigButton): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
28 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
29 def __init__(self): |
48
a2980cc9a060
Factor out some constants
Neil Muller <drnlmuller@gmail.com>
parents:
44
diff
changeset
|
30 super(ExitGameButton, self).__init__(((WIDTH - 128), 10), 'Exit') |
43
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
31 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
32 def on_click(self): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
33 WINDOW_STACK.pop() |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
34 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
35 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
36 class GameWindow(Window): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
37 """Main window for the game""" |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
38 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
39 def __init__(self, screen): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
40 super(GameWindow, self).__init__(screen) |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
41 exit = ExitGameButton() |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
42 self.add_child(exit) |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
43 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
44 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
45 class StartButton(BigButton): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
46 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
47 def __init__(self, screen): |
48
a2980cc9a060
Factor out some constants
Neil Muller <drnlmuller@gmail.com>
parents:
44
diff
changeset
|
48 super(StartButton, self).__init__(((WIDTH - 128) / 2, HEIGHT / 2), |
a2980cc9a060
Factor out some constants
Neil Muller <drnlmuller@gmail.com>
parents:
44
diff
changeset
|
49 'Start') |
43
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
50 self.screen = screen |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
51 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
52 def on_click(self): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
53 game_window = GameWindow(self.screen) |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
54 WINDOW_STACK.append(game_window) |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
55 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
56 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
57 class QuitButton(BigButton): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
58 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
59 def __init__(self): |
48
a2980cc9a060
Factor out some constants
Neil Muller <drnlmuller@gmail.com>
parents:
44
diff
changeset
|
60 super(QuitButton, self).__init__(((WIDTH - 128) / 2, |
a2980cc9a060
Factor out some constants
Neil Muller <drnlmuller@gmail.com>
parents:
44
diff
changeset
|
61 HEIGHT / 2 + 100), 'Quit') |
43
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
62 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
63 def on_click(self): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
64 pygame.event.post(pygame.event.Event(pygame.QUIT)) |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
65 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
66 |
0
d0de8832774b
Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff
changeset
|
67 def main(): |
38 | 68 clock = Clock() |
48
a2980cc9a060
Factor out some constants
Neil Muller <drnlmuller@gmail.com>
parents:
44
diff
changeset
|
69 screen = pygame.display.set_mode(SCREEN) |
38 | 70 window = Window(screen) |
71 window.background_colour = (0, 0, 0) | |
43
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
72 button1 = StartButton(screen) |
38 | 73 window.add_child(button1) |
43
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
74 button2 = QuitButton() |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
75 window.add_child(button2) |
38 | 76 WINDOW_STACK.append(window) |
77 while GAME_IS_RUNNING: | |
78 process_input() | |
79 draw(screen) | |
80 clock.tick(FPS) | |
37 | 81 |
38 | 82 |
37 | 83 def draw(screen): |
38 | 84 for view in WINDOW_STACK: |
85 view.draw(screen) | |
86 pygame.display.flip() | |
87 | |
88 | |
37 | 89 def process_input(): |
38 | 90 global MOUSE_DOWN |
91 global GAME_IS_RUNNING | |
92 for event in pygame.event.get(): | |
93 if MOUSE_DOWN: | |
94 if event.type == MOUSEBUTTONUP: | |
95 MOUSE_DOWN = False | |
44 | 96 WINDOW_STACK[-1].on_mouse_up(event.pos) |
38 | 97 elif event.type == MOUSEMOTION: |
44 | 98 WINDOW_STACK[-1].on_mouse_move(event.pos) |
38 | 99 elif not MOUSE_DOWN and event.type == MOUSEBUTTONDOWN: |
100 MOUSE_DOWN = True | |
44 | 101 WINDOW_STACK[-1].on_mouse_down(event.pos) |
38 | 102 elif event.type == QUIT: |
103 GAME_IS_RUNNING = False |