Mercurial > sypikslang
annotate gamelib/main.py @ 44:d35a3762edda
Idiomatic
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Mon, 07 May 2012 16:53:18 +0200 |
parents | 2bdac178ec6f |
children | a2980cc9a060 |
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 |
0
d0de8832774b
Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff
changeset
|
14 |
1
c90a6586cd66
PEP8-ify skellington (because)
Neil Muller <drnlmuller@gmail.com>
parents:
0
diff
changeset
|
15 |
37 | 16 pygame.init() |
17 | |
18 FPS = 30 | |
19 | |
20 GAME_IS_RUNNING = True | |
21 | |
22 WINDOW_STACK = [] | |
23 | |
24 # input variables | |
25 MOUSE_DOWN = False | |
38 | 26 |
27 | |
43
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
28 class ExitGameButton(BigButton): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
29 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
30 def __init__(self): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
31 super(ExitGameButton, self).__init__(((800 - 128), 10), 'Exit') |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
32 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
33 def on_click(self): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
34 WINDOW_STACK.pop() |
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 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
37 class GameWindow(Window): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
38 """Main window for the game""" |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
39 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
40 def __init__(self, screen): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
41 super(GameWindow, self).__init__(screen) |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
42 exit = ExitGameButton() |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
43 self.add_child(exit) |
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 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
46 class StartButton(BigButton): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
47 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
48 def __init__(self, screen): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
49 super(StartButton, self).__init__(((800 - 128) / 2, 200), 'Start') |
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): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
60 super(QuitButton, self).__init__(((800 - 128) / 2, 300), 'Quit') |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
61 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
62 def on_click(self): |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
63 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
|
64 |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
65 |
0
d0de8832774b
Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff
changeset
|
66 def main(): |
38 | 67 clock = Clock() |
68 screen = pygame.display.set_mode((800, 600)) | |
69 window = Window(screen) | |
70 window.background_colour = (0, 0, 0) | |
43
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
71 button1 = StartButton(screen) |
38 | 72 window.add_child(button1) |
43
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
73 button2 = QuitButton() |
2bdac178ec6f
Play with the gui stuff a bit
Neil Muller <drnlmuller@gmail.com>
parents:
39
diff
changeset
|
74 window.add_child(button2) |
38 | 75 WINDOW_STACK.append(window) |
76 while GAME_IS_RUNNING: | |
77 process_input() | |
78 draw(screen) | |
79 clock.tick(FPS) | |
37 | 80 |
38 | 81 |
37 | 82 def draw(screen): |
38 | 83 for view in WINDOW_STACK: |
84 view.draw(screen) | |
85 pygame.display.flip() | |
86 | |
87 | |
37 | 88 def process_input(): |
38 | 89 global MOUSE_DOWN |
90 global GAME_IS_RUNNING | |
91 for event in pygame.event.get(): | |
92 if MOUSE_DOWN: | |
93 if event.type == MOUSEBUTTONUP: | |
94 MOUSE_DOWN = False | |
44 | 95 WINDOW_STACK[-1].on_mouse_up(event.pos) |
38 | 96 elif event.type == MOUSEMOTION: |
44 | 97 WINDOW_STACK[-1].on_mouse_move(event.pos) |
38 | 98 elif not MOUSE_DOWN and event.type == MOUSEBUTTONDOWN: |
99 MOUSE_DOWN = True | |
44 | 100 WINDOW_STACK[-1].on_mouse_down(event.pos) |
38 | 101 elif event.type == QUIT: |
102 GAME_IS_RUNNING = False |