annotate gamelib/gamescreen.py @ 41:ad6f56bfa8b7

Cryo door, titanium leg and some interaction prototypes.
author Jeremy Thurgood <firxen@gmail.com>
date Mon, 23 Aug 2010 00:49:22 +0200
parents 9fdbfbc02a60
children 7a977f8f433a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
1 # gamescreen.py
24
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
2 # Copyright Boomslang team, 2010 (see COPYING File)
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
3 # Main menu for the game
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
4
34
e5c043aeed65 Inventory and items. And stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
5 from state import initial_state, Item
36
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
6 from hand import HandButton
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 27
diff changeset
7
26
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
8 from pygame.color import Color
29
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
9 from pygame import Rect
35
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
10 from pygame.locals import BLEND_ADD
24
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
11 from albow.screen import Screen
29
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
12 from albow.controls import Button, Label, Widget
24
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
13 from albow.layout import Column
26
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
14 from albow.palette_view import PaletteView
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
15
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
16
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
17 class InventoryView(PaletteView):
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
18
40
9fdbfbc02a60 Minor build script and inventory fixes.
Jeremy Thurgood <firxen@gmail.com>
parents: 36
diff changeset
19 sel_color = Color("yellow")
26
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
20 sel_width = 2
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
21
36
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
22 def __init__(self, state, handbutton):
26
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
23 PaletteView.__init__(self, (50, 50), 1, 6, scrolling=True)
34
e5c043aeed65 Inventory and items. And stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
24 self.state = state
26
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
25 self.selection = None
36
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
26 self.handbutton = handbutton
26
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
27
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
28 def num_items(self):
34
e5c043aeed65 Inventory and items. And stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
29 return len(self.state.inventory)
26
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
30
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
31 def draw_item(self, surface, item_no, rect):
40
9fdbfbc02a60 Minor build script and inventory fixes.
Jeremy Thurgood <firxen@gmail.com>
parents: 36
diff changeset
32 item_image = self.state.inventory[item_no].get_inventory_image()
9fdbfbc02a60 Minor build script and inventory fixes.
Jeremy Thurgood <firxen@gmail.com>
parents: 36
diff changeset
33 surface.blit(item_image, rect, None, BLEND_ADD)
26
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
34
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
35 def click_item(self, item_no, event):
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
36 self.selection = item_no
36
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
37 self.handbutton.unselect()
26
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
38
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
39 def item_is_selected(self, item_no):
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
40 return self.selection == item_no
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
41
36
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
42 def unselect(self):
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
43 self.selection = None
24
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
44
40
9fdbfbc02a60 Minor build script and inventory fixes.
Jeremy Thurgood <firxen@gmail.com>
parents: 36
diff changeset
45
29
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
46 class StateWidget(Widget):
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
47
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
48 def __init__(self, state):
35
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
49 Widget.__init__(self, Rect(0, 0, 800, 550))
29
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
50 self.state = state
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
51
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
52 def draw(self, surface):
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
53 self.state.draw(surface)
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
54
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
55
24
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
56 class GameScreen(Screen):
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
57 def __init__(self, shell):
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
58 Screen.__init__(self, shell)
29
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
59
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
60 # TODO: Randomly plonk the state here for now
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
61 self.state = initial_state()
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
62 self.state_widget = StateWidget(self.state)
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
63 self.add(self.state_widget)
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
64
24
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
65 StartButton = Button('Main Menu', action = self.main_menu)
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
66 QuitButton = Button('Quit', action = shell.quit)
26
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
67 AddItemButton = Button('Add item', action = self.add_item)
24
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
68 Title = Label('Caught! ... In SPAACE')
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
69 menu = Column([
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
70 Title,
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
71 StartButton,
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
72 QuitButton,
26
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
73 AddItemButton,
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
74 Button('Use hand', action = lambda: self.state.scenes['cryo'].things['cryo.door'].interact(None)),
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
75 Button('Use triangle', action = lambda: self.state.scenes['cryo'].things['cryo.door'].interact(self.state.items['triangle'])),
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
76 Button('Use titanium_leg', action = lambda: self.state.scenes['cryo'].things['cryo.door'].interact(self.state.items['titanium_leg'])),
24
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
77 ], align='l', spacing=20)
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
78 self.add_centered(menu)
36
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
79 self.menubutton = Button('M', action=self.main_menu)
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
80 self.menubutton.bottomleft = self.bottomleft
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
81 self.add(self.menubutton)
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
82 self.handbutton = HandButton(action=self.hand_pressed)
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
83 self.handbutton.bottomleft = self.bottomleft
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
84 self.handbutton.get_rect().move_ip(50, 0)
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
85 self.add(self.handbutton)
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
86 self.inventory = InventoryView(self.state, self.handbutton)
29
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
87
27
5c7bbbdf9296 Move inventory palette to the bottom left
Neil Muller <neil@dip.sun.ac.za>
parents: 26
diff changeset
88 self.inventory.bottomleft = self.bottomleft
36
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
89 self.inventory.get_rect().move_ip(100, 0)
26
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
90 self.add(self.inventory)
24
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
91
34
e5c043aeed65 Inventory and items. And stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
92 # Test items
e5c043aeed65 Inventory and items. And stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
93 self.state.add_inventory_item('triangle')
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
94 self.state.add_inventory_item('titanium_leg')
34
e5c043aeed65 Inventory and items. And stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
95
24
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
96 def main_menu(self):
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
97 print 'Returning to menu'
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
98 self.shell.show_screen(self.shell.menu_screen)
9d5de13e2ac3 Add a game screen. So far, the game content looks a *lot* like the main menu.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
99
26
0a68d137f509 Initial inventory palette thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 24
diff changeset
100 def add_item(self):
36
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
101 self.state.add_inventory_item("triangle")
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
102
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
103 def hand_pressed(self):
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
104 self.handbutton.toggle_selected()
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
105 self.inventory.unselect()
31a431f795e1 Add a hand button placeholder
Neil Muller <neil@dip.sun.ac.za>
parents: 35
diff changeset
106