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