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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
9c4bf1f15431 gui stuff
Rizmari Versfeld <rizziepit@gmail.com>
parents: 1
diff changeset
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
9c4bf1f15431 gui stuff
Rizmari Versfeld <rizziepit@gmail.com>
parents: 1
diff changeset
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
9c4bf1f15431 gui stuff
Rizmari Versfeld <rizziepit@gmail.com>
parents: 1
diff changeset
17 pygame.init()
9c4bf1f15431 gui stuff
Rizmari Versfeld <rizziepit@gmail.com>
parents: 1
diff changeset
18
9c4bf1f15431 gui stuff
Rizmari Versfeld <rizziepit@gmail.com>
parents: 1
diff changeset
19 GAME_IS_RUNNING = True
9c4bf1f15431 gui stuff
Rizmari Versfeld <rizziepit@gmail.com>
parents: 1
diff changeset
20
9c4bf1f15431 gui stuff
Rizmari Versfeld <rizziepit@gmail.com>
parents: 1
diff changeset
21 WINDOW_STACK = []
9c4bf1f15431 gui stuff
Rizmari Versfeld <rizziepit@gmail.com>
parents: 1
diff changeset
22
9c4bf1f15431 gui stuff
Rizmari Versfeld <rizziepit@gmail.com>
parents: 1
diff changeset
23 # input variables
9c4bf1f15431 gui stuff
Rizmari Versfeld <rizziepit@gmail.com>
parents: 1
diff changeset
24 MOUSE_DOWN = False
38
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
25
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
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
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
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
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
70 window = Window(screen)
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
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
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
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
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
76 WINDOW_STACK.append(window)
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
77 while GAME_IS_RUNNING:
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
78 process_input()
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
79 draw(screen)
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
80 clock.tick(FPS)
37
9c4bf1f15431 gui stuff
Rizmari Versfeld <rizziepit@gmail.com>
parents: 1
diff changeset
81
38
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
82
37
9c4bf1f15431 gui stuff
Rizmari Versfeld <rizziepit@gmail.com>
parents: 1
diff changeset
83 def draw(screen):
38
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
84 for view in WINDOW_STACK:
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
85 view.draw(screen)
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
86 pygame.display.flip()
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
87
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
88
37
9c4bf1f15431 gui stuff
Rizmari Versfeld <rizziepit@gmail.com>
parents: 1
diff changeset
89 def process_input():
38
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
90 global MOUSE_DOWN
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
91 global GAME_IS_RUNNING
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
92 for event in pygame.event.get():
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
93 if MOUSE_DOWN:
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
94 if event.type == MOUSEBUTTONUP:
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
95 MOUSE_DOWN = False
44
d35a3762edda Idiomatic
Neil Muller <drnlmuller@gmail.com>
parents: 43
diff changeset
96 WINDOW_STACK[-1].on_mouse_up(event.pos)
38
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
97 elif event.type == MOUSEMOTION:
44
d35a3762edda Idiomatic
Neil Muller <drnlmuller@gmail.com>
parents: 43
diff changeset
98 WINDOW_STACK[-1].on_mouse_move(event.pos)
38
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
99 elif not MOUSE_DOWN and event.type == MOUSEBUTTONDOWN:
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
100 MOUSE_DOWN = True
44
d35a3762edda Idiomatic
Neil Muller <drnlmuller@gmail.com>
parents: 43
diff changeset
101 WINDOW_STACK[-1].on_mouse_down(event.pos)
38
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
102 elif event.type == QUIT:
7e18a67995f6 fixed pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents: 37
diff changeset
103 GAME_IS_RUNNING = False